The OpenD Programming Language

odc.serialization.json

This is a JSON serializer for OpenD programming language.

JSON Serializer relies on new to create arrays and objects, so it's incompatible with @nogc and betterC code. Limited compatibility might be provided if no arrays / objects are used in serializable object

## Usage examples To use this serializer, annotate any field you want serialized with serializable. JSON serializator will attempt to automatically convert primitives to corresponding JSON tupes, such as any of the number types to JSON Number, string to JSON String, bool to JSON true/false.

struct Foo
{
    @serializable int bar;
    @serializable string baz;
}

string test = serializeToJSONString(Foo());

By default, serializer will skip serializing fields that are null. If you want to ensure that a ceratin field exists in the JSON object, use jsonRequired attribute:

struct Foo
{
    @serializable object bar;
    @serializable @jsonRequired object baz;
}

assert(serializeToJSONString(Foo()) == "{\"baz\": null}")

Marking a field with jsonRequired will also result in an error if a required field was missing when deserializing:

struct Foo
{
    @serializeable @jsonRequired object bar;
}

deserializeJSONFromString!Foo("{}"); // Error

Members

Functions

deserializeJSON
T deserializeJSON(JSONValue root)

Deserializes a JSONValue to T

deserializeJSONFromString
T deserializeJSONFromString(string json)

Deserialize a JSON string into T

serializeJSON
JSONValue serializeJSON(T obj)

Serializes an object to a JSONValue. To make this work, use serializable UDA on any fields that you want to be serializable. Automatically maps marked fields to corresponding JSON types. Any field not marked with serializable is not serialized.

serializeToJSONString
string serializeToJSONString(T obj, bool pretty)

Serialize T into a JSON string

Structs

jsonRequired
struct jsonRequired

A UDA to mark a JSON field as required for deserialization

Meta