You are not logged in.
Pages: 1
Hi,
Is it OK that I inherit classes that I use with JSONToObject? I guess yes it is. but a simple problem is here, ObjectToJSONFile saves published properties in reverse order, from child to parent so If Parent has Prop1 and Prop2 and child add Prop3, ObjectToJSONFile save Prop3 and Prop2 and the Prop1.
I checked TTextWriterWriteObjectOption but it seems not has an option for this.
A sample for test:
program project1;
uses
Classes,
SysUtils,
FileUtil,
SynCommons,
mORMot;
type
TParentItem = class;
TParentItems = array of TParentItem;
{ TItem }
TItem = class(TSynAutoCreateFields)
private
FID: integer;
published
property ID: integer read FID write FID;
end;
TParentItem = class(TItem)
private
FChilds: TParentItems;
published
property Childs: TParentItems read FChilds write FChilds;
end;
procedure TestIt;
var
it: TParentItem;
s: RawUTF8;
begin
TJSONSerializer.RegisterObjArrayForJSON([TypeInfo(TParentItems), TParentItem]);
s := StringFromFile('test.json');
it := TParentItem.Create;
ObjectLoadJSON(it, s);
ObjectToJSONFile(it, 'output.json');
it.Free;
end;
begin
TestIt;
end.
Input JSON:
{
"ID": 1,
"Childs": [{
"ID": 11,
"Childs": [{
"ID": 111,
"Childs": [{}, {}
]
}, {
"ID": 112,
"Childs": [{}, {}
]
}
]
}, {
"ID": 12,
"Childs": [{
"ID": 121,
"Childs": [{}, {}
]
}, {
"ID": 122,
"Childs": [{}, {}
]
}
]
}
]
}
And output is:
{
"Childs":
[
{
"Childs":
[
{
"Childs":
[
{
"Childs": [],
"ID": 0
},
{
"Childs": [],
"ID": 0
}
],
"ID": 111
},
{
"Childs":
[
{
"Childs": [],
"ID": 0
},
{
"Childs": [],
"ID": 0
}
],
"ID": 112
}
],
"ID": 11
},
{
"Childs":
[
{
"Childs":
[
{
"Childs": [],
"ID": 0
},
{
"Childs": [],
"ID": 0
}
],
"ID": 121
},
{
"Childs":
[
{
"Childs": [],
"ID": 0
},
{
"Childs": [],
"ID": 0
}
],
"ID": 122
}
],
"ID": 12
}
],
"ID": 1
}
As you can see it will write "Childs" first and then "ID".
Is there a way to make it sort in order or I should make a custom serializer?
Offline
Check TJSONSerializer. RegisterCustomSerializer.
Offline
So I need a custom one. For now automated serialization works very fine on most of the classes and I wished I can change the order so it does not make me to write all of the classes by hand.
I should say your automated serialization works very well and it is the best I found in Pascal language, these options I'm talking about make it more configurable to use it easily out of an ORM too.
I can change the source if you think it is right though.
Offline
I checked the code and it seems InternalClassPropInfo give the prop list in revers order, but I cant clearly understand how you do that but I checked FPC implementation like this and it write the list in order.
uses typinfo,Rtti;
c: TRttiContext;
RttiType: TRttiType;
PropList: {$ifdef fpc}specialize{$endif} TArray<TRttiProperty>;
i: Integer;
begin
c := TRttiContext.Create;
RttiType := c.GetType(TParentItem.ClassInfo);
PropList:=RttiType.GetProperties;
WriteLn(Length(PropList));
for i:=0 to Length(PropList)-1 do
begin
WriteLn(PropList[i].Name);// will print ID,Childs but InternalClassPropInfo give Childs first
end;
c.Free;
What do you think?
Offline
Can I have your technical opinion?
Offline
Pages: 1