View source code Display the source code in std/json.d from which this page was generated on github. Improve this page Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone. Page wiki View or edit the community-maintained wiki page associated with this page.

Module std.json

JavaScript Object Notation

Synopsis

//parse a file or string of json into a usable structure
string s = "{ \"language\": \"D\", \"rating\": 3.14, \"code\": \"42\" }";
JSONValue j = parseJSON(s);
writeln("Language: ", j["language"].str(),
        " Rating: ", j["rating"].floating()
);

// j and j["language"] return JSONValue,
// j["language"].str returns a string

//check a type
long x;
if (j["code"].type() == JSON_TYPE.INTEGER)
{
    x = j["code"].integer;
}
else
{
    x = to!int(j["code"].str);
}

// create a json struct
JSONValue jj = [ "language": "D" ];
// rating doesnt exist yet, so use .object to assign
jj.object["rating"] = JSONValue(3.14);
// create an array to assign to list
jj.object["list"] = JSONValue( ["a", "b", "c"] );
// list already exists, so .object optional
jj["list"].array ~= JSONValue("D");

s = j.toString();
writeln(s);

References

http://json.org/

Functions

Name Description
parseJSON Parses a serialized string and returns a tree of JSON values. Throws a std.json.JSONException if the depth exceeds the max depth.
parseJSON Parses a serialized string and returns a tree of JSON values. Throws a std.json.JSONException if the depth exceeds the max depth.
toJSON Takes a tree of JSON values and returns the serialized string.

Classes

Name Description
JSONException Exception thrown on JSON errors

Structs

Name Description
JSONValue JSON value node

Enums

Name Description
JSONFloatLiteral String literals used to represent special float values within JSON strings.
JSONOptions Flags that control how json is encoded and parsed.
JSON_TYPE JSON type enumeration

Authors

Jeremie Pelletier, David Herberth

License

Boost License 1.0.

Comments