You are not logged in.
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 ? Many thanks !
https://gist.github.com/anonymous/333f077797aecdc0ee65253e8ad819ba
Last edited by ComingNine (2016-12-19 13:32:36)
Offline
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
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
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
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.
> 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 ?
Offline