Tango JSON Parser: Accessing the members [Phobos Alternative]

Alan Knowles alan at akbkhome.com
Sun Aug 17 16:34:08 PDT 2008


I needed a JSON encoder/decoder the other day as well, since I'm Phobos 
based, rather than Tango based. and am not that keen on introducing 
Templates into my code (as they tend to make the code extremely 
difficult to read for me)

You can grabe the source here..

http://www.akbkhome.com/svn/D_Stuff/json.d

Caveats  (although if someone wants to fix and send me it I add it)
- Unicode support is incompatible with JSON (it just uses \xHH rather 
than /uHHHH
- No Complex Number support (only ulong/uint are really supported
- It should support streams for encoding/decoding... but does not... 
(should be quite trivial to add though)

As the examples show, the code is designed for object serialization and 
storage.  and making that code as simple and easy to write/maintain.


Examples:

f.write(jSomething(obj).encode());

JsonValue jSomething(something t) {
	  auto x = jsonO(); // make a json object
	  x.add('fred', t.fred);  // for all the properties do this...
                                  //(that are of normal types...)
	  x.add('blogs', t.blogs);
	
	  x.add('someobj', t.someobj.toJSON());
                                               // an property that's an
                                               // object for example...
	
	  auto y = jsonA(); // create a new array
	  foreach(a;t.b) {
		  y.add(a);
	  }
	  x.add('b', y); // add the array.
	  return x;
   }

and decoding...

JsonValue x = jsonDecode(mystring);

     myobj fromJSON(JsonValue x) {
	  auto t = new myobj(); // create the return obj.

           // copy the serialize code, and replace add with get!!!!
	  x.get('fred', t.fred);
	  x.get('blogs', t.blogs);
	
	
	  t.someobj = x.getO('someobj');
                             // object getting a little difffernt.
	
	  auto a = x.getA('b');
	  for (int i=0; i < a.length(); i++ ){
			t.b ~= a.getS(i);
                        //// explicit type get an array object value....
		}
	
   }







Benji Smith wrote:
> I've been trying out the new tango JSON parser, and the functionality 
> for actually parsing the string is very easy to invoke. But I can't 
> figure out, for the life of me, how to read the individual children of 
> the resultant object hierarchy.
> 
> Here's what I've got:
> 
> module mymodule;
> 
> import tango.text.json.Json;
> 
> void main() {
> 
>   char[] json = `{"t": true}`;
> 
>   auto parser = new Json!(char);
> 
>   // "Error: identifier 'JsonValue' is not defined"
>   JsonValue* jsonValue = p.parse(json);
> 
>   // "Error: identifier 'JsonObject' is not defined"
>   JsonObject jsonObject = p.parse(json).toObject();
> 
>   // "Error: identifier 'Composite' is not defined"
>   Composite composite = p.parse(json).toObject();
> 
> }
> 
> I'm sure I'm missing something from my 'import' declarations, but I've 
> tried adding "tango.text.json.JsonValue" or 
> "tango.text.json.Json.JsonValue" or "tango.text.json.Json.Composite" and 
> the compiler has not been happy with any of those (each of which is a 
> struct or an alias defined within the Json class).
> 
> Has anyone else tinkered with the JSON stuff yet? Any ideas?
> 
> Thanks!
> 
> --benji


More information about the Digitalmars-d-learn mailing list