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.ion.conv

Conversion utilities.
template serde(T)
Serialize value to binary ion data and deserialize it back to requested type. Uses GC allocated string tables.
Examples:
import mir.algebraic_alias.json: JsonAlgebraic;
static struct S
{
    double a;
    string s;
}
auto s = S(12.34, "str");
assert(s.serde!JsonAlgebraic.serde!S == s);
T serde(V)(auto ref const V value, SerdeTarget serdeTarget = SerdeTarget.ion);
pure @trusted immutable(ubyte)[] json2ion(scope const(char)[] text);
Converts JSON Value Stream to binary Ion data.
Examples:
static immutable ubyte[] data = [0xe0, 0x01, 0x00, 0xea, 0xe9, 0x81, 0x83, 0xd6, 0x87, 0xb4, 0x81, 0x61, 0x81, 0x62, 0xd6, 0x8a, 0x21, 0x01, 0x8b, 0x21, 0x02];
assert(`{"a":1,"b":2}`.json2ion == data);
pure @safe string ion2json(scope const(ubyte)[] data);
Converts Ion Value Stream data to JSON text.
The function performs data.IonValueStream.serializeJson.
Examples:
static immutable ubyte[] data = [0xe0, 0x01, 0x00, 0xea, 0xe9, 0x81, 0x83, 0xd6, 0x87, 0xb4, 0x81, 0x61, 0x81, 0x62, 0xd6, 0x8a, 0x21, 0x01, 0x8b, 0x21, 0x02];
assert(data.ion2json == `{"a":1,"b":2}`);
// static assert(data.ion2json == `{"a":1,"b":2}`);
pure @safe string ion2jsonPretty(scope const(ubyte)[] data);
Converts Ion Value Stream data to JSON text
The function performs data.IonValueStream.serializeJsonPretty.
Examples:
static immutable ubyte[] data = [0xe0, 0x01, 0x00, 0xea, 0xe9, 0x81, 0x83, 0xd6, 0x87, 0xb4, 0x81, 0x61, 0x81, 0x62, 0xd6, 0x8a, 0x21, 0x01, 0x8b, 0x21, 0x02];
assert(data.ion2jsonPretty == "{\n\t\"a\": 1,\n\t\"b\": 2\n}");
// static assert(data.ion2jsonPretty == "{\n\t\"a\": 1,\n\t\"b\": 2\n}");
pure @trusted immutable(ubyte)[] text2ion(scope const(char)[] text);
Convert an Ion Text value to a Ion Value Stream.
Parameters:
const(char)[] text The text to convert
Returns:
An array containing the Ion Text value as an Ion Value Stream.
Examples:
static immutable ubyte[] data = [0xe0, 0x01, 0x00, 0xea, 0xe9, 0x81, 0x83, 0xd6, 0x87, 0xb4, 0x81, 0x61, 0x81, 0x62, 0xd6, 0x8a, 0x21, 0x01, 0x8b, 0x21, 0x02];
assert(`{"a":1,"b":2}`.text2ion == data);
static assert(`{"a":1,"b":2}`.text2ion == data);
enum s = `{a:2.232323e2, b:2.1,}`.text2ion;
pure @nogc @trusted void text2ion(Appender)(scope const(char)[] text, ref Appender appender);
Convert an Ion Text value to a Ion Value Stream.
This function is the @nogc version of text2ion.
Parameters:
const(char)[] text The text to convert
Appender appender A buffer that will receive the Ion binary data
Examples:
import mir.appender : scopedBuffer;
static immutable data = [0xe0, 0x01, 0x00, 0xea, 0xe9, 0x81, 0x83, 0xd6, 0x87, 0xb4, 0x81, 0x61, 0x81, 0x62, 0xd6, 0x8a, 0x21, 0x01, 0x8b, 0x21, 0x02];
auto buf = scopedBuffer!ubyte;
text2ion(`{"a":1,"b":2}`, buf);
assert(buf.data == data);
pure @safe string ion2text(scope const(ubyte)[] data);
Converts Ion Value Stream data to text.
The function performs data.IonValueStream.serializeText.
Examples:
static immutable ubyte[] data = [0xe0, 0x01, 0x00, 0xea, 0xe9, 0x81, 0x83, 0xd6, 0x87, 0xb4, 0x81, 0x61, 0x81, 0x62, 0xd6, 0x8a, 0x21, 0x01, 0x8b, 0x21, 0x02];
assert(data.ion2text == `{a:1,b:2}`);
// static assert(data.ion2text == `{a:1,b:2}`);
Examples:
static immutable ubyte[] data = [0xe0, 0x01, 0x00, 0xea, 0xea, 0x81, 0x83, 0xde, 0x86, 0x87, 0xb4, 0x83, 0x55, 0x53, 0x44, 0xe6, 0x81, 0x8a, 0x53, 0xc1, 0x04, 0xd2];
assert(data.ion2text == `USD::123.4`);
// static assert(data.ion2text == `USD::123.4`);
pure @safe string ion2textPretty(scope const(ubyte)[] data);
Converts Ion Value Stream data to text
The function performs data.IonValueStream.serializeTextPretty.
Examples:
static immutable ubyte[] data = [0xe0, 0x01, 0x00, 0xea, 0xe9, 0x81, 0x83, 0xd6, 0x87, 0xb4, 0x81, 0x61, 0x81, 0x62, 0xd6, 0x8a, 0x21, 0x01, 0x8b, 0x21, 0x02];
assert(data.ion2textPretty == "{\n\ta: 1,\n\tb: 2\n}");
// static assert(data.ion2textPretty == "{\n\ta: 1,\n\tb: 2\n}");