Report a bug
If you spot a problem with this page, click here to create a GitHub issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.

mir.deser

High level deserialization API

enum TableKind: int;
compiletime
scopeRuntime
immutableRuntime
struct DeserializationParams(TableKind tableKind, bool annotated = false);
IonDescribedValue data;
const DeserializationParams!tableKind withData(IonDescribedValue data);
IonAnnotations annotations;
const DeserializationParams!tableKind withoutAnnotations();
const(char[])[] runtimeSymbolTable;
const(uint)[] compiletimeIndex;
template deserializeValue(string[] symbolTable)
Deserialize aggregate value using compile time symbol table
Examples:
import mir.ion.symbol_table;
import mir.ion.value;
import mir.ion.exception;
import mir.small_array;
import mir.small_string;

static struct Book
{
    string title;
    bool wouldRecommend;
    string description;
    uint numberOfNovellas;
    double price;
    float weight;
    string[] tags;
}

static immutable symbolTable = ["title", "wouldRecommend", "description", "numberOfNovellas", "price", "weight", "tags"];
static immutable binaryData = cast(immutable ubyte[]) "\xde\xc9\x8a\x8e\x92A Hero of Our Time\x8b\x11\x8c\x0f\x8d!\x05\x8eS\xc2\x03\x1f\x8fH@\x1b\x85\x1e\xb8Q\xeb\x85\x90\xbe\x9b\x87russian\x85novel\x8c19th century";

auto data = IonValue(binaryData).describe;

Book book;

auto params = DeserializationParams!(TableKind.compiletime)(data); 
if (auto exception = deserializeValue!(IonSystemSymbolTable_v1 ~ symbolTable)(params, book))
    throw exception;

assert(book.description.length == 0);
assert(book.numberOfNovellas == 5);
assert(book.price == 7.99);
assert(book.tags.length == 3);
assert(book.tags[0] == "russian");
assert(book.tags[1] == "novel");
assert(book.tags[2] == "19th century");
assert(book.title == "A Hero of Our Time");
assert(book.weight == 6.88f);
assert(book.wouldRecommend);
Examples:
import mir.deser.json;
import std.uuid;

static struct S
{
    @serdeScoped
    @serdeProxy!string
    UUID id;
}
assert(`{"id":"8AB3060E-2cba-4f23-b74c-b52db3bdfb46"}`.deserializeJson!S.id
            == UUID("8AB3060E-2cba-4f23-b74c-b52db3bdfb46"));
Examples:
Mir types
import mir.bignum.integer;
import mir.date;
import mir.deser.json: deserializeJson;
assert(`"2021-04-24"`.deserializeJson!Date == Date(2021, 4, 24));
assert(`123`.deserializeJson!(BigInt!2) == BigInt!2(123));
Examples:
Mir types
static struct S
{
    @serdeIgnoreIn
    bool set;
    @serdeScoped
    @property auto a(scope int[] a) @safe
    {
        static immutable d = [1, 2, 3];
        set = a == d;
    }
}
import mir.deser.json: deserializeJson;
auto s = `{"a":[1, 2, 3]}`.deserializeJson!S;
assert(s.set);
Examples:
enum Kind { request, cancel }

@serdeRealOrderedIn
static struct S
{
    Kind kind;

    @serdeIgnoreInIfAggregate!((ref a) => a.kind == Kind.cancel)
    @serdeIgnoreOutIfAggregate!((ref a) => a.kind == Kind.cancel)
    int number;
}

import mir.deser.json: deserializeJson;
import mir.ser.json: serializeJson;
assert(`{"number":3, "kind":"cancel"}`.deserializeJson!S.kind == Kind.cancel);
assert(`{"number":3, "kind":"cancel"}`.deserializeJson!S.number == 0);
assert(`{"number":3, "kind":"request"}`.deserializeJson!S.number == 3);
assert(`{"kind":"request","number":3}`.deserializeJson!S.number == 3);
assert(S(Kind.cancel, 4).serializeJson == `{"kind":"cancel"}`);
assert(S(Kind.request, 4).serializeJson == `{"kind":"request","number":4}`);
Examples:
enum Kind { request, cancel }

@serdeRealOrderedIn
static struct S
{
    Kind kind;

    @serdeIgnoreIfAggregate!((ref a) => a.kind == Kind.cancel)
    int number;
}

import mir.deser.json: deserializeJson;
import mir.ser.json: serializeJson;
assert(`{"kind":"cancel"}`.deserializeJson!S.kind == Kind.cancel);
assert(`{"kind":"cancel","number":3}`.deserializeJson!S.number == 0); // ignores number
assert(`{"kind":"request","number":3}`.deserializeJson!S.number == 3);
assert(S(Kind.cancel, 4).serializeJson == `{"kind":"cancel"}`);
assert(S(Kind.request, 4).serializeJson == `{"kind":"request","number":4}`);
IonException deserializeValue(T, TableKind tableKind, bool annotated)(DeserializationParams!(tableKind, annotated) params, ref T value)
if (!isFirstOrderSerdeType!T);
Deserialize aggregate value
Parameters:
DeserializationParams!(tableKind, annotated) params DeserializationParams
T value value to deserialize
Returns:
IonException
alias deserializeValue = .deserializeValue_;