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.

Public Imports

vibe.data.serialization
public import vibe.data.serialization;
Undocumented in source.

Members

Functions

convertJsonToASCII
string convertJsonToASCII(string json)

Helper function that escapes all Unicode characters in a JSON string.

deserializeJson
void deserializeJson(T dst, Json src)
T deserializeJson(Json src)
T deserializeJson(R input)

Deserializes a JSON value into the destination variable.

parseJson
Json parseJson(R range, int* line, string filename)

Parses the given range as a JSON string and returns the corresponding Json object.

parseJsonString
Json parseJsonString(string str, string filename)

Parses the given JSON string and returns the corresponding Json object.

serializeToJson
Json serializeToJson(T value)
void serializeToJson(R destination, T value)
serializeToJsonString
string serializeToJsonString(T value)

Serializes the given value to JSON.

serializeToPrettyJson
void serializeToPrettyJson(R destination, T value)
string serializeToPrettyJson(T value)

Serializes the given value to a pretty printed JSON string.

writeJsonString
void writeJsonString(R dst, Json json, size_t level)

Writes the given JSON object as a JSON string into the destination range.

writePrettyJsonString
void writePrettyJsonString(R dst, Json json, int level)

Writes the given JSON object as a prettified JSON string into the destination range.

Imports

JSONException (from std.json)
public import std.json : JSONException;
Undocumented in source.

Structs

Json
struct Json

Represents a single JSON value.

JsonSerializer
struct JsonSerializer

Serializer for a plain Json representation.

JsonStringSerializer
struct JsonStringSerializer(R, bool pretty = false)

Serializer for a range based plain JSON string representation.

Templates

isJsonSerializable
template isJsonSerializable(T)

private

Examples

void manipulateJson(Json j)
{
	import std.stdio;

	// retrieving the values is done using get()
	assert(j["name"].get!string == "Example");
	assert(j["id"].get!int == 1);

	// semantic conversions can be done using to()
	assert(j["id"].to!string == "1");

	// prints:
	// name: "Example"
	// id: 1
	foreach (key, value; j.byKeyValue)
		writefln("%s: %s", key, value);

	// print out as JSON: {"name": "Example", "id": 1}
	writefln("JSON: %s", j.toString());

	// DEPRECATED: object members can be accessed using member syntax, just like in JavaScript
	//j = Json.emptyObject;
	//j.name = "Example";
	//j.id = 1;
}

Constructing Json objects

// construct a JSON object {"field1": "foo", "field2": 42, "field3": true}

// using the constructor
Json j1 = Json(["field1": Json("foo"), "field2": Json(42), "field3": Json(true)]);

// using piecewise construction
Json j2 = Json.emptyObject;
j2["field1"] = "foo";
j2["field2"] = 42.0;
j2["field3"] = true;

// using serialization
struct S {
	string field1;
	double field2;
	bool field3;
}
Json j3 = S("foo", 42, true).serializeToJson();

// using serialization, converting directly to a JSON string
string j4 = S("foo", 32, true).serializeToJsonString();

Meta

License

Subject to the terms of the MIT license, as written in the included LICENSE.txt file.

Authors

Sönke Ludwig