std.json API improvement - Request for code review

Brian Schott brian-schott at cox.net
Sun Sep 12 19:53:54 PDT 2010


Everything is a JSONValue. JSONValue has a union inside it that actually
holds the data. I just wrote a short program that reads your example
file. (This could be a bit more efficient if I stored obj2, but I think
it's enough to communicate the idea.)

import std.stdio;
import std.json;
import std.file;

void main(string[] args)
{
	auto jsonString = readText("../ml.json");
	JSONValue json = parseJSON(jsonString);
	writeln(json["obj1"]["obj2"]["val1"].integer);
	writeln(json["obj1"]["obj2"]["val2"].str);
	foreach(value; json["obj1"]["val3"].array)
		writeln(value.integer);
}

Output:

1
a string
1
2
3
4

Your question did remind me to document the union members so that the
HTML documentation will show how to access the actual data. I've
uploaded the new version of the file. The link is the same.

On 09/12/2010 05:19 PM, sybrandy wrote:
> Everything I've seen looks good to me, though I haven't tried to execute
> it.  The fact that I can directly manipulate a JSONValue looks good to
> me.  However, here's a question: if I have the following JSON document
> and parse it using parseJSON, will obj1 and obj2 both be JSONValues?  My
> current project deals with this type of situation on a regular basis, so
> I'm curious about how easy this will be to access the data.
> 
> Casey
> 
> {
>     "obj1":
>     {
>         "obj2":
>         {
>             "val1": 1,
>             "val2": "a string"
>         },
>         "val3": [ 1, 2, 3, 4]
>     }
> }



More information about the Digitalmars-d mailing list