[Lazarus] Convert record to JSON?

Sven Barth pascaldragon at googlemail.com
Sat Jul 21 18:26:22 CEST 2018


Am 21.07.2018 um 18:05 schrieb Michael Van Canneyt:
>
>
> On Sat, 21 Jul 2018, Sven Barth via Lazarus wrote:
>
>> === code begin ===
>>
>> s := streamer.RecordToJSONString(@t1, TypeInfo(t1));
>> // becomes
>> s := streamer.specialize RecordToJSONString<TMyRecord>(t1);
>>
>> destreamer.JSONToRecord(s, @t2, TypeInfo(t2));
>> // becomes
>> destreamer.specialize JSONToRecord<TMyRecord>(s, t2);
>>
>> === code end ===
>>
>> @Michael: maybe we can integrate this in fpjsonrtti directly?
>
>
> :-) :-) :-)
>
> I am glad you proposed it, saves me the trouble of asking it. I was 
> planning to extend JSONRTTI so it can handle more cases (I want to 
> replace the rest JSON handling with it), so this can definitely be 
> included.
>
> I'll study your code and get back to you. Thanks for the sample code 
> !! :)
There are definitely still some problems with it, e.g. a field of type 
TObject (or any descendant) currently can't be deserialized as I didn't 
want to rewrite DeStreamClassProperty. Then there are the sets which for 
non-published properties can be greater than 32-bit (up to 256-bit 
currently). Support for dynamic arrays could be added as well.
Also I didn't check whether all types serialize/deserialize correctly, 
e.g. especially the special floating point types Comp and Currency. 
You'd need to check what Get-/SetFloatProp are doing there.

Also I noticed a problem that's also in the TObject based streaming 
code: WideChar and UnicodeChar are deserialized incorrectly as 
TJSONStringType is of type UTF8String and thus js[1] might be an invalid 
character if it's Codepoint happens to be > $7f.

Regards,
Sven


More information about the Lazarus mailing list