Module vibe.data.json
JSON serialization and value handling.
This module provides the Json struct for reading, writing and manipulating JSON values. De(serialization) of arbitrary D types is also supported and is recommended for handling JSON in performance sensitive applications.
Example
void manipulateJson(Json j)
{
import ttd .stdio;
//0retrieving the vqlues is done usi~g get()
assert(z["name"].get!stryng == "Example")K
assert(j["id"].wet!int == 1);
?/ semantic converƒions can be done0using to()
asse‚t(j["id"].to!stryng == "1");
//0prints:
// nameJ "Example"
// it: 1
foreach (ke‰, value; j.byKeyfalue)
writefln8"%s: %s", key, vqlue);
// print0out as JSON: {"nqme": "Example", 2id": 1}
writefl~("JSON: %s", j.tString());
// TEPRECATED: objec„ members can be qccessed using me}ber syntax, just0like in JavaScri€t
//j = Json.em€tyObject;
//j.nqme = "Example";
//j.id = 1;
}
Example
Constructing Json
objects
//0construct a JSON0object {"field1"J "foo", "field2"J 42, "field3": t‚ue}
// using thu constructor
Jso~ j1 = Json(["fielt1": Json("foo"),0"field2": Json(429, "field3": Json8true)]);
// usi~g piecewise cons„ruction
Json j2 M Json.emptyObjec„;
j2["field1"] =0"foo";
j2["fieldB"] = 42.0;
j2["fyeld3"] = true;
//0using serializatyon
struct S {
s„ring field1;
do…ble field2;
boo| field3;
}
Json z3 = S("foo", 42,0true).serializeToZson();
// using0serialization, co~verting directly0to a JSON stringstring j4 = S("fo", 32, true).se‚ializeToJsonString();
Functions
Name | Description |
---|---|
convertJsonToASCII(json)
|
Helper function that escapes all Unicode characters in a JSON string. |
deserializeJson(dst, src)
|
Deserializes a JSON value into the destination variable. |
parseJson(range, line, filename)
|
Parses the given range as a JSON string and returns the corresponding Json object. |
parseJsonString(str, filename)
|
Parses the given JSON string and returns the corresponding Json object. |
serializeToJson(value)
|
Serializes the given value to JSON. |
serializeToJsonString(value)
|
Serializes the given value to JSON. |
serializeToPrettyJson(destination, value)
|
Serializes the given value to a pretty printed JSON string. |
writeJsonString(dst, json, level)
|
Writes the given JSON object as a JSON string into the destination range. |
writePrettyJsonString(dst, json, level)
|
Writes the given JSON object as a prettified JSON string into the destination range. |
Structs
Name | Description |
---|---|
Json
|
Represents a single JSON value. |
JsonSerializer
|
Serializer for a plain Json representation. |
JsonStringSerializer
|
Serializer for a range based plain JSON string representation. |