Page 1 of 1

JSON serializer / deserializer

Posted: Tue Feb 14, 2012 1:34 am
by Dr Danyo
Hi folks,

I'm looking to integrate to a couple of web api's, and I was wondering if anyone has got a JSON serialize / deserializer handy that they don't mind making available to the forum.

What approaches are people using to handle JSON in JADE ( importing .net assembly, or writing their own? )

A implementation of JADEXMLDocument -> JADEJSONDocument might be the way to go.

- Dr Danyo.

Re: JSON serializer / deserializer

Posted: Tue Feb 14, 2012 7:49 am
by murray
A implementation of JADEXMLDocument -> JADEJSONDocument might be the way to go.
I agree with this - it would provide a full implementation that would work equivalent to the Jade XML classes.

Currently I have only had to serialise outbound data as JSON and have implemented several methods called asJsonString() on the classes where they are required.
These are limited and include String, StringArray and JadeDynamicObject, which I use to build up some JSON structured data.

examples:

Code: Select all

JadeDynamicObject::asJsonString() : String; /* Return the contents formatted as a JSON string, e.g. { "custno":1234, "name":"Joe Bloggs", isActive:true } Recursion is possible into arrays and other JadeDynamicObjects (so it is possible for endless self-recusion). */ vars i : Integer; ptype : Type; pval : Any; pname : String; sval : String; delim : String; result : String; begin foreach i in 1 to propertyCount do ptype := getPropertyTypeByIndex( i ); pname := getPropertyName( i ).asJsonString; pval := getPropertyValueByIndex( i ); if pval.isKindOf( Object ) and pval = null then if pval.isKindOf( StringArray) then sval := '[]'; else sval := "null"; endif; else if ptype = Integer then sval := pval.Integer.String; elseif ptype = String then sval := pval.String.asJsonString; elseif ptype = Decimal then sval := pval.Decimal.String; elseif ptype = Boolean then sval := pval.Boolean.String; elseif ptype = Character then sval := pval.Character.String.asJsonString; elseif ptype = Integer64 then sval := pval.Integer64.String; elseif ptype = Real then sval := pval.Real.String; elseif ptype = StringArray then sval := pval.StringArray.asJsonString(); elseif ptype = HugeStringArray then sval := pval.HugeStringArray.asJsonString(); elseif ptype = JadeDynamicObject then sval := pval.JadeDynamicObject.asJsonString(); elseif ptype = JadeDynamicObjectArray then sval := pval.JadeDynamicObjectArray.asJsonString(); else // all other data types not implemented yet - add when required endif; endif; result := result & delim & pname & ":" & sval; delim := ', '; endforeach; return '{' & result & '}'; end;

Code: Select all

String::asJsonString() : String; /* Return the string formatted as per JSON specification, including the enclosing double-quotes. Input of abc"123\xyz will be output as "abc\"123\\xyz" (note the addition of enclosing quotes). Standard escaping will be included (e.g. Cr, Lf, Tab, "), but unprintable characters that would require expression as Unicode hex values ("\uHHHH") will NOT be included. */ vars c : Character; i : Integer; s : String; begin foreach i in 1 to self.length do c := self[i]; if c.isOneOf( '"', '\', '/') then s := s & '\' & c; elseif c = Cr then s := s & '\r'; elseif c = Lf then s := s & '\n'; elseif c = Tab then s := s & '\t'; elseif c = Control_BS then s := s & '\b'; elseif c = Control_FF then s := s & '\f'; elseif c >= ' ' and c <= '~' then s := s & c; endif; endforeach; return '"' & s & '"'; end;

Re: JSON serializer / deserializer

Posted: Tue Feb 14, 2012 2:28 pm
by murray
By the way, for anyone who needs to know, the JSON spec is covered in RFC 4627 http://www.ietf.org/rfc/rfc4627.txt.

It is much simpler than the W3C XML specification :-) .