[fpc-pascal] JSON - RTTI streaming.

michael.vancanneyt at wisa.be michael.vancanneyt at wisa.be
Tue May 17 17:05:01 CEST 2011



On Tue, 17 May 2011, Lee Jenkins wrote:

> On 1/5/2011 6:12 PM, Michael Van Canneyt wrote:
>> Hello,
>> 
>> For those of you that need JSON support:
>> 
>> I have committed support for streaming published properties (properties for
>> which RTTI is generated) from objects to JSON and vice versa, in a unit 
>> fpjsonrtti.
>> 
>> Not the full streaming as implemented in the classes system is supported:
>> - No DefineProperties.
>> - No methods.
>> Although it should not be difficult to add support for this.
>> 
>> In contrast, it can stream simple collections and stringlists (with ojects
>> attached) in various ways.
>> There are various events to influence the streaming process.
>> This should be more than enough to stream objects to and from a 
>> web-browser.
>> 
>> I have committed an example and a testsuite.
>> All this is in packages/fcl-json.
>> 
>> Despite the fact that the testsuite runs OK , I'd like to ask those for 
>> whom it
>> might be useful to test the code and report any errors you find to me.
>> Suggestions for improvements are also welcome.
>> 
>
> Cool stuff, Michael.
>
> One thought.  What do you think about a base abstract "TObjectStreamer" or 
> similar object that provides methods that all such helpers could inherit 
> from? Like:
>
> TObejctStreamer = class(TObject)
> Public
>  function    ToString: string; virtual; abstract;
>  function    ToStream(AStream: TStream): integer; virtual; abstract;
>  function    FromString(const AString: string): TObject; virtual; abstract;
>  function    FromStream(AStream: TStream): TObject; virtual; abstract;
> end;

We have this in part, the abstract object streamers in the classes unit.
I thought about implementing it like that, but then there is a problem:
defineproperties is hard to do, because it expects a seekable stream, and
that's just not possible with JSON.

> No sooner will you have started using a json object streamer in your project 
> when the need for a xml or yaml format is required!  This could allow to swap 
> out different implementations and promote decoupling.

This could be implemented, yes. But I don't have the immediate need, so
don't expect me to jump on this. I'm open for patches, though :-)

> BTW, do you ever sleep?

Like a baby, once I scratched some of the FPC itches I have ;-)

Michael.



More information about the fpc-pascal mailing list