You are not logged in.
Pages: 1
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
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
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
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
Please check https://synopse.info/fossil/info/b47582774f
Offline
Thanks!
I checked that and it makes SIGSEGV error on line 47213 "Writer := nil; // exclusive" in FPC truck.
Offline
I've currently issues re-building my FPC+Lazarus environment - see https://github.com/newpascal/fpcupdeluxe/issues/9 - but I will fix it ASAP.
Offline
Works very well. Thanks.
Offline
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
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
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
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
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
Any comment on this @ab?
Offline
Pages: 1