-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonParser.lua
More file actions
124 lines (94 loc) · 3.96 KB
/
JsonParser.lua
File metadata and controls
124 lines (94 loc) · 3.96 KB
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
--[[
JsonParser.lua
Usage:
The function ParseJSON (jsonString) accepts a
string representation of a JSON object or array
and returns the object or array as a Lua table
with the same structure. Individual properties
can be referenced using either the dot notation
or the array notation.
Notes:
All null values in the original JSON stream will
be stored in the output table as JsonParser.NIL.
This is a necessary convention, because Lua
tables cannot store nil values and it may be
desirable to have a stored null value versus
not having the key present.
Requires:
Newtonsoft.Json
--]]
luanet.load_assembly("System");
luanet.load_assembly("Newtonsoft.Json");
luanet.load_assembly("log4net");
JsonParser = {}
JsonParser.__index = JsonParser
JsonParser.NIL = {};
JsonParser.Types = {}
JsonParser.Types["StringReader"] = luanet.import_type("System.IO.StringReader");
JsonParser.Types["JsonToken"] = luanet.import_type("Newtonsoft.Json.JsonToken");
JsonParser.Types["JsonTextReader"] = luanet.import_type("Newtonsoft.Json.JsonTextReader");
JsonParser.Types["System.Enum"] = luanet.import_type("System.Enum");
JsonParser.rootLogger = "AtlasSystems.Addons.JsonParser"
JsonParser.Log = luanet.import_type("log4net.LogManager").GetLogger(JsonParser.rootLogger);
JsonParser.DateParseHandlingStyle = "DateTime"; --Options are None, DateTime, or DateTimeOffset
function JsonParser:ParseJSON (jsonString)
local stringReader = JsonParser.Types["StringReader"](jsonString);
local reader = JsonParser.Types["JsonTextReader"](stringReader);
pcall(function()
local enumValue = JsonParser.Types["System.Enum"].Parse(reader.DateParseHandling:GetType(), JsonParser.DateParseHandlingStyle);
reader.DateParseHandling = enumValue;
end);
local outputTable = "";
if (reader:Read()) then
if (reader.TokenType == JsonParser.Types["JsonToken"].StartObject) then
outputTable = JsonParser:BuildFromJsonObject(reader);
elseif (reader.TokenType == JsonParser.Types["JsonToken"].StartArray) then
outputTable = JsonParser:BuildFromJsonArray(reader);
elseif (jsonString == nil) then
outputTable = "";
else
outputTable = jsonString;
end;
end;
return outputTable;
end;
function JsonParser:BuildFromJsonObject (reader)
local array = {};
while (reader:Read()) do
if (reader.TokenType == JsonParser.Types["JsonToken"].EndObject) then
return array;
end;
if (reader.TokenType == JsonParser.Types["JsonToken"].PropertyName) then
local propertyName = reader.Value;
if (reader:Read()) then
if (reader.TokenType == JsonParser.Types["JsonToken"].StartObject) then
array[propertyName] = JsonParser:BuildFromJsonObject(reader);
elseif (reader.TokenType == JsonParser.Types["JsonToken"].StartArray) then
array[propertyName] = JsonParser:BuildFromJsonArray(reader);
elseif (reader.Value == nil) then
array[propertyName] = JsonParser.NIL;
else
array[propertyName] = reader.Value;
end;
end;
end;
end;
return array;
end;
function JsonParser:BuildFromJsonArray (reader)
local array = {};
while (reader:Read()) do
if (reader.TokenType == JsonParser.Types["JsonToken"].EndArray) then
return array;
elseif (reader.TokenType == JsonParser.Types["JsonToken"].StartArray) then
table.insert(array, JsonParser:BuildFromJsonArray(reader));
elseif (reader.TokenType == JsonParser.Types["JsonToken"].StartObject) then
table.insert(array, JsonParser:BuildFromJsonObject(reader));
elseif (reader.Value == nil) then
table.insert(array, JsonParser.NIL);
else
table.insert(array, reader.Value);
end;
end;
return array;
end;