#1 2016-12-19 13:30:49

ComingNine
Member
Registered: 2010-07-29
Posts: 294

Certain property makes the generated json invalid to parse back.

Dear ab, in the sample code below which tries to serialize a plain object and then de-serialize, it seems that certain property ("Arr" as in the sample) makes the generated json invalid (see the value of "Arr", which should be the same as the value of "ArrByRef") to parse back (see the difference of "ArrByRef" in the above and below block, which should be the same. Apparently, JsonToObject detects invalid json content and aborts parsing). If that "Arr" property is removed from the published section, the generated json is valid and can be parsed back. Could you help to comment whether this is a bug or by design ? yikes Many thanks !

https://gist.github.com/anonymous/333f077797aecdc0ee65253e8ad819ba

Last edited by ComingNine (2016-12-19 13:32:36)

Offline

#2 2016-12-19 14:16:41

mpv
Member
From: Ukraine
Registered: 2012-03-24
Posts: 1,540
Website

Re: Certain property makes the generated json invalid to parse back.

In your code:

function TPOJO.GetArr: TIntegerDynArray;
begin
  Result := Copy(FArr);
end;

Are you sure what doing a copy of an array inside the property getter is a good idea? Why do so?

Offline

#3 2016-12-19 14:46:13

ComingNine
Member
Registered: 2010-07-29
Posts: 294

Re: Certain property makes the generated json invalid to parse back.

mpv wrote:

In your code:

function TPOJO.GetArr: TIntegerDynArray;
begin
  Result := Copy(FArr);
end;

Are you sure what doing a copy of an array inside the property getter is a good idea? Why do so?

Providing a copy of a primitive array serves me very well when the original primitive array is held by a object ("manual-Box" since there is no auto-Box) and needed by other objects who go on to modify the array according to their own needs.

Last edited by ComingNine (2016-12-19 14:46:45)

Offline

#4 2016-12-19 17:36:08

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

Re: Certain property makes the generated json invalid to parse back.

IMHO it is up to the consumers (other objets) to make their own private copy of the original dynamic array.
Using such a getter is slower when you access the array items, and also probably misleading the serializer expectations.

Offline

#5 2016-12-20 08:47:49

ComingNine
Member
Registered: 2010-07-29
Posts: 294

Re: Certain property makes the generated json invalid to parse back.

Many thanks !

> IMHO it is up to the consumers (other objets) to make their own private copy of the original dynamic array.
In principle you are right.

Still, it does not hurt anyone to be defensive, when the overhead of copying the primitive array is negligible. big_smile

> Using such a getter is slower when you access the array items,

Iteration will not be done on the property but always on a local var instead. "ArrByRef" is provided in case "consumer" is reading only.

> , and also probably misleading the serializer expectations.

Apparently the serializer is "definitely" misled. The value of "Arr" is "[]", which should be the same as the value of "ArrByRef" "[0]" or "[0,1,..]". Therefore, could you help to comment whether it is technically possible for mORMot to serialize "Arr" property correctly here ? big_smile

Offline

Board footer

Powered by FluxBB