Function serializeToJsonString
Serializes the given value
to JSON.
The following types of values are supported:
Json
- Used as-is
null
) $(DD Converted to
Json.Type.null_
bool
) $(DD Converted to
Json.Type.bool_
float
,
double
) $(DD Converted to
Json.Type.float_
short
,
ushort
,
int
,
uint
,
long
,
ulong
) $(DD Converted to
Json.Type.int_
BigInt
) $(DD Converted to
Json.Type.bigInt
string
) $(DD Converted to
Json.Type.string
T[]
) $(DD Converted to
Json.Type.array
T[string]
) $(DD Converted to
Json.Type.object
struct
) $(DD Converted to
Json.Type.object
class
) $(DD Converted to
Json.Type.object
or
Json.Type.null_
All entries of an array or an associative array, as well as all R/W properties and all public fields of a struct/class are recursively serialized using the same rules.
Fields ending with an underscore will have the last underscore stripped in the serialized output. This makes it possible to use fields with D keywords as their name by simply appending an underscore.
The following methods can be used to customize the serialization of structs/classes:
Json toJson() const;
static T fromJson(Json src);
string toString() const;
static T fromString(string src);
The methods will have to be defined in pairs. The first pair that is implemented by
the type will be used for serialization (i.e. toJson
overrides
toString
).
Prototype
string serializeToJsonString(T)(
T value
);
See Also
deserializeJson
,
vibe.data.serialization
Example
struct Foo {
int number;
string str;
}
Foo f;
f .number = 12;
f .str = "hello";
string json = serializeToJsonString(f);
assert(json == `{"number":12,"str":"hello"}`);
Json jsonval = serializeToJson(f);
assert(jsonval .type == Json .Type .object);
assert(jsonval["number"] == Json(12));
assert(jsonval["str"] == Json("hello"));
Authors
Sönke Ludwig
Copyright
© 2012-2015 RejectedSoftware e.K.
License
Subject to the terms of the MIT license, as written in the included LICENSE.txt file.