The OpenD Programming Language

1 /++
2 $(H4 High level JSON deserialization API)
3 
4 Macros:
5 IONREF = $(REF_ALTTEXT $(TT $2), $2, mir, ion, $1)$(NBSP)
6 +/
7 module mir.deser.json;
8 
9 public import mir.serde;
10 
11 private enum dip1000 = __traits(compiles, ()@nogc { throw new Exception(""); });
12 
13 /++
14 Deserialize JSON string to a type trying to do perform less memort allocations.
15 +/
16 template deserializeJson(T)
17 {
18     T deserializeJson()(scope const(char)[] text)
19     {
20         version (LDC) pragma(inline, true);
21         T value;
22         deserializeJson(value, text);
23         return value;
24     }
25 
26     void deserializeJson(scope ref T value, scope const(char)[] text)
27     {
28         version (LDC) pragma(inline, true);
29         import mir.ion.internal.stage3;
30         import mir.deser.ion: deserializeIon;
31         import mir.ion.exception: ionException, ionErrorMsg, IonParserMirException;
32 
33         auto callback(IonErrorInfo error, scope const ubyte[] data)
34         {
35             enum nogc = __traits(compiles, (scope ref T value, const(ubyte)[] data)@nogc { deserializeIon!T(value, data); });
36             if (error.code)
37             {
38                 static if (!nogc || dip1000)
39                 {
40                     throw new IonParserMirException(error.code.ionErrorMsg, error.location);
41                 }
42                 else
43                 {
44                     throw error.code.ionException;
45                 }
46             }
47             deserializeIon!T(value, data);
48         }
49 
50         mir_json2ion(text, &callback);
51     }
52 }
53 
54 deprecated ("Use deserializeJson instead")
55 alias deserializeDynamicJson = deserializeJson;