#1 2017-03-25 00:31:29

mohsenti
Member
Registered: 2015-04-11
Posts: 72

JSON Serialization with custom naming

Hi,

In my journey in reading JSON with mORMot, as @ab said I went to a road that I make custom serializer for my classes that need special care.
In many of these 3rd party JSONs there is only one problem; they are using identifier names like "Type", "Class" or "To" and I obviously I cant use them as property name so I saw my self making many custom reader an writer for only one property name and I think it does not seem wise so I made some changes in JSONToObject so it takes care of these "CustomNaming" and here is my changes and it works well so I can read JSON like this:

{
	"ID": 1,
	"Type": "Item",
	"Class":"Book"
}

like this:

ObjectLoadJSONWithCustomNames(it, s,nil,[],['Type','Type_','Class','Class_']);   

I changed the function code like this:

...
    PropName := GetJSONPropName(From,@PropNameLen);  // get property name
    //CustomNaming
    if Length(CustomNames) mod 2 =0 then
    begin
      cni:=0;
      while cni<=(Length(CustomNames) div 2) do
      begin
        if UTF8ToString(CustomNames[cni])=UTF8ToString(PropName) then
        begin
          PropName:=CustomNames[cni+1];
          PropNameLen:=Length(PropName);
        end;
        inc(cni,2);
      end;
    end;

    if (From=nil) or (PropName=nil) then    
... 

So I wanted to ask you @ab, is it a good practice to do the job like this?

PS, Here is the link to the patch of my changes:
https://ufile.io/c2983

Last edited by mohsenti (2017-03-25 00:33:41)

Offline

#2 2017-03-25 10:16:55

mohsenti
Member
Registered: 2015-04-11
Posts: 72

Re: JSON Serialization with custom naming

So I work more with it and find out my implementation was silly so I made a new one so you can now register custom names for a class.

TJSONSerializer.RegisterCustomNames(TItem, ['Type', 'Type_', 'Class', 'Class_']);

Please tell me what you think.
Patch:
https://ufile.io/722201

Offline

#3 2017-03-25 11:50:10

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,661
Website

Re: JSON Serialization with custom naming

Your code is interresting, but it is very un-optimized: it allocates memory everywhere, since its generates hidden conversion between PUTF8Char and RawUTF8.

I'll find out another way of implementing it.

Offline

#4 2017-03-25 12:31:09

mohsenti
Member
Registered: 2015-04-11
Posts: 72

Re: JSON Serialization with custom naming

Great.
Yes I know, its a test and honestly I still trying to understand the flow of your codes. they are clean but very complicated and optimized.

Offline

#5 2017-03-27 10:11:18

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,661
Website

Re: JSON Serialization with custom naming

Offline

#6 2017-03-27 11:20:12

mohsenti
Member
Registered: 2015-04-11
Posts: 72

Re: JSON Serialization with custom naming

Thanks!

I checked that and it makes SIGSEGV error on line 47213 "Writer := nil; // exclusive" in FPC truck.

Offline

#7 2017-03-27 14:26:33

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,661
Website

Re: JSON Serialization with custom naming

I've currently issues re-building my FPC+Lazarus environment - see https://github.com/newpascal/fpcupdeluxe/issues/9  - but I will fix it ASAP.
smile

Offline

#8 2017-03-27 15:58:01

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,661
Website

Re: JSON Serialization with custom naming

Should be OK now.

Offline

#9 2017-03-29 15:30:26

mohsenti
Member
Registered: 2015-04-11
Posts: 72

Re: JSON Serialization with custom naming

Works very well. Thanks.

Offline

#10 2017-12-28 11:10:00

mohsenti
Member
Registered: 2015-04-11
Posts: 72

Re: JSON Serialization with custom naming

This ability seems broken in last Lazarus and mORMot with 64bit fpc.
I checked the code and somehow pointer of the specific field will change in the process so in JSONCustomParsersFieldProp it cant find the correct property.
@ab do you have a test case to check?

Offline

#11 2017-12-28 16:21:10

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,661
Website

Re: JSON Serialization with custom naming

The feature seems to be removed in today's mORMot trunk, in fact.

Offline

#12 2017-12-30 06:24:32

mohsenti
Member
Registered: 2015-04-11
Posts: 72

Re: JSON Serialization with custom naming

So is there any replacement for such feature?
Making a custom reader for just renaming seems overwork.
Unfrotuanalty I cant find the remove commit.

Last edited by mohsenti (2017-12-30 06:31:09)

Offline

#13 2017-12-30 09:49:29

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,661
Website

Re: JSON Serialization with custom naming

Define a DTO record if you expect custom naming.
It is the most convenient for maintainability.

Offline

#14 2017-12-30 15:25:05

mohsenti
Member
Registered: 2015-04-11
Posts: 72

Re: JSON Serialization with custom naming

Can you example more? I use classes and I cant go with records if you mean that.
Also, can I ask why you removed that? When I use mORMot with servers that made in another language there is a high chance of using Pascal keywords like Class, Type, To, From and ...

Offline

#15 2017-12-30 15:43:27

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,661
Website

Re: JSON Serialization with custom naming

I honestly don't remember why it was removed.
Probably because it was breaking something, and that we didn't use it.

With records, you can customize the whole serialization using text-based definition.
You could even create several dedicated record types, with diverse field names for serialization.

Offline

#16 2017-12-30 16:04:45

mohsenti
Member
Registered: 2015-04-11
Posts: 72

Re: JSON Serialization with custom naming

Thanks.
Do you allow me to correct it if I can? Becuase the code is still there but not working and I have projects dependent on it and it still seems a good feature in my view.
And also I don't get your explanation in the replacement with DTO because in my usage I have complicated classes with inheritance and my need is very simple, just rename trouble maker names.

Offline

#17 2018-01-13 13:54:54

mohsenti
Member
Registered: 2015-04-11
Posts: 72

Re: JSON Serialization with custom naming

Any comment on this @ab?

Offline

Board footer

Powered by FluxBB