The OpenD Programming Language

odc.serialization

This is a serialization module for OpenD programming language! It contains an API for making serializers and includes JSON serializator.

The JSON serializator is implemented via std.json.

Usage examples

Basic usage

By-design, only fields marked as serializable would be visible to serializator

struct Foo
{
    @serializable int bar;
    float bar;
}

Using the code above, only bar would be "visible" for serialization. Please not that @serializable can be applied to field, or getter/setter functions. A getter function is a function that returns non-void and has no parameters. A setter function is a function that returns void and has exactly 1 parameter.

Only a single serializable UDA is allowed per member. Having more will result in a compile-time error.

Example usage with getter/setter functions:

struct Foo
{
    @serializable void foo(int bar) { }
    @serialziable int foo() { }
}

To control how serizliation is done, @serializable attribute has a constructor that accepts a string, which is then used as a name for serializtion

struct Foo
{
    // This field will be serialized with the name "Bar"
    @serializable("Bar") int baz;
}

If serialization fails for any reason, SerializationException is used.

Advanced usage

A common desirable behavior is to serialize structs/classes that were not originally annotated with serializable. For such use-case, it's advised to use getter/setter serialization to convert such types to another type that is easily serializable:

struct Foo
{
    @serializable("entry") string serEntry() { return entry.path; }
    @serializable("entry") void serEntry(string path) { entry = DirEntry(path); }
    DirEntry entry;
}

Writing serializers

To make a custom serializer, use any of those helper templates: serializableFields, writeableSerializables, readableSerializables. Usage should be quite obvious from their names! Here's a silly sample code that serializes a custom type T:

string serialize(T)(auto ref T t)
{
    streing return;
    foreach(alias prop; readableSerializables!T)
    {
        auto name = getSerializableName!prop;
        auto val = __traits(child, obj, prop).to!string;
        return ~= name ~ ":" ~ val ~ "\n";
    }
    return return;
}

Please refer to the provided JSON serialization module for a comprehensive example!

Modules

json
module odc.serialization.json

This is a JSON serializer for OpenD programming language.

Members

Classes

SerializationException
class SerializationException

A base exception class for all serialization exceptions.

Structs

serializable
struct serializable

A UDA for marking fields as serializable.

Templates

getSerializableName
template getSerializableName(alias T)

Retreive the name for this serializable

isSerializable
template isSerializable(alias T)

Is T marked as serializable

isSerializableReadable
template isSerializableReadable(alias T)

Is this serializable readable

isSerializableWriteable
template isSerializableWriteable(alias T)

Is this serializable writeable

readableSerializables
template readableSerializables(alias T)

Retreive all readable serializables for T. This includes properties and getters

serializableFields
template serializableFields(T)

Retreive all fields of type T that are serializable

writeableSerializables
template writeableSerializables(alias T)

Retreive all writeable serializables for T. This includes properties and setters.

Meta