-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.d
139 lines (117 loc) · 3.03 KB
/
json.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
module api.json;
import lumars, api, std.json, std.array;
void registerJsonApi(LuaState* lua)
{
lua.registerAndDocument!(
"parse", parse,
"toString", toString
)("sh.json");
}
private:
LuaValue parse(LuaState* lua, string str)
{
auto json = parseJSON(str);
return fromAny(lua, json);
}
LuaTable fromObject(LuaState* lua, JSONValue json)
{
auto ret = LuaTable.makeNew(lua);
foreach(k, v; json.object)
ret.set(k, fromAny(lua, v));
return ret;
}
LuaTable fromArray(LuaState* lua, JSONValue json)
{
auto ret = LuaTable.makeNew(lua);
foreach(i, v; json.array)
ret.set(i+1, fromAny(lua, v));
return ret;
}
LuaValue fromValue(JSONValue json)
{
switch(json.type) with(JSONType)
{
case null_: return LuaValue(LuaNil());
case string: return LuaValue(json.str);
case integer: return LuaValue(json.integer);
case uinteger: return LuaValue(json.uinteger);
case float_: return LuaValue(json.floating);
case true_: return LuaValue(true);
case false_: return LuaValue(false);
default: assert(false);
}
}
LuaValue fromAny(LuaState* lua, JSONValue json)
{
if(json.type == JSONType.array)
return LuaValue(fromArray(lua, json));
else if(json.type == JSONType.object)
return LuaValue(fromObject(lua, json));
else
return fromValue(json);
}
string toString(LuaValue v)
{
import std.exception : assumeUnique;
Appender!(char[]) ret;
toString(ret, v);
return ret.data.assumeUnique;
}
void toString(ref Appender!(char[]) ret, LuaValue v)
{
import std.conv : to;
final switch(v.kind) with(LuaValue.Kind)
{
case nil: ret.put("null"); break;
case number: ret.put(v.numberValue.to!string); break;
case text: ret.put('"'~v.textValue~'"'); break;
case boolean: ret.put(v.booleanValue.to!string); break;
case table: toString(ret, v.tableValue); break;
case func:
case userData:
break;
case textWeak:
case tableWeak:
case funcWeak:
assert(false);
}
}
void toString(ref Appender!(char[]) ret, LuaTable v)
{
bool isArray;
bool isObject;
v.pairs!((i,v) { if(i.isNumber) isArray = true; else isObject = true; });
if(isArray && isObject)
throw new Exception("Table has numeric and non-numeric keys, cannot convert into JSON.");
if(!isArray && !isObject)
{
ret.put("null");
return;
}
bool isFirst = true;
if(isArray)
{
ret.put('[');
v.pairs!((i, v) {
if(!isFirst)
ret.put(", ");
toString(ret, v);
isFirst = false;
});
ret.put(']');
}
else
{
ret.put('{');
v.pairs!(string, LuaValue, (i, v) {
if(!isFirst)
ret.put(", ");
ret.put("\"");
ret.put(i);
ret.put("\": ");
toString(ret, v);
isFirst = false;
});
ret.put('}');
}
}