#1 2017-03-24 16:06:24

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

Inheritance in JSON Serialization

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

#2 2017-03-24 17:23:05

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

Re: Inheritance in JSON Serialization

Check TJSONSerializer. RegisterCustomSerializer.

See https://synopse.info/files/html/Synopse … ml#TITL_52

Offline

#3 2017-03-24 17:41:46

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

Re: Inheritance in JSON Serialization

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

#4 2017-03-25 13:23:57

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

Re: Inheritance in JSON Serialization

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

#5 2017-03-29 15:32:15

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

Re: Inheritance in JSON Serialization

Can I have your technical opinion?

Offline

Board footer

Powered by FluxBB