forked from Bullrich/Unity-Json-Wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JsonNetParser.cs
executable file
·159 lines (138 loc) · 5.23 KB
/
JsonNetParser.cs
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Newtonsoft.Json;
// ReSharper disable SuggestBaseTypeForParameter
namespace Blue.Json
{
public class JsonNetParser : IJsonParser
{
private EndOfStreamException IncompleteJsonException =>
new EndOfStreamException("Json shouldn't have finished here!");
public string Serialize(IDictionary<string, object> obj, bool prettyPrint = false)
{
return Serialize((object) obj, prettyPrint);
}
public string Serialize(object obj, bool prettyPrint = false)
{
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
Formatting formatting = prettyPrint ? Formatting.Indented : Formatting.None;
using (var writer = new JsonTextWriter(sw) {Formatting = formatting})
SerializeValue(writer, obj);
return sb.ToString();
}
public JsonDictionary Parse(string json)
{
return Parse<JsonDictionary>(json);
}
public T Parse<T>(string json)
{
return (T) ReadJson(json);
}
#region Serialization
private void SerializeValue(JsonTextWriter writer, object value)
{
if (value is IDictionary)
SerializeDictionary(writer, (IDictionary) value);
else if (value is IList)
SerializeList(writer, (IList) value);
else if (value == null)
writer.WriteNull();
else
writer.WriteValue(value);
}
private void SerializeDictionary(JsonTextWriter writer, IDictionary dict)
{
writer.WriteStartObject();
foreach (DictionaryEntry kv in dict)
{
writer.WritePropertyName((string) kv.Key);
SerializeValue(writer, kv.Value);
}
writer.WriteEndObject();
}
private void SerializeList(JsonTextWriter writer, IList list)
{
writer.WriteStartArray();
foreach (var element in list)
SerializeValue(writer, element);
writer.WriteEndArray();
}
#endregion
#region Deserialization
private object ReadJson(string json)
{
using (var reader = new JsonTextReader(new StringReader(json)))
while (reader.Read())
{
var tokenType = reader.TokenType;
switch (tokenType)
{
case JsonToken.StartObject:
return ParseObject(reader);
case JsonToken.StartArray:
return ParseArray(reader);
default:
return reader.Value;
}
}
throw IncompleteJsonException;
}
private JsonDictionary ParseObject(JsonReader reader)
{
JsonDictionary parsedDictionary = new JsonDictionary();
while (reader.Read())
{
var tokenType = reader.TokenType;
// Object ended, return closed object
if (tokenType == JsonToken.EndObject)
return parsedDictionary;
if (tokenType != JsonToken.PropertyName)
throw new TypeLoadException("Should be a property!");
var key = reader.Value as string;
// We have the key, move to the value
if (!reader.Read())
throw IncompleteJsonException;
parsedDictionary.Add(key, ParseValue(reader));
}
// Shouldn't get here
throw IncompleteJsonException;
}
private object ParseValue(JsonReader reader)
{
var tokenType = reader.TokenType;
switch (tokenType)
{
case JsonToken.StartObject:
return ParseObject(reader);
case JsonToken.StartArray:
return ParseArray(reader);
case JsonToken.String:
case JsonToken.Float:
case JsonToken.Integer:
case JsonToken.Boolean:
case JsonToken.Null:
return reader.Value;
case JsonToken.Comment:
throw new FormatException("Comments are not supported in this parser!");
default:
throw new FormatException(string.Format("Token type {0} is not supported", tokenType));
}
}
private JsonList ParseArray(JsonReader reader)
{
var parsedArray = new JsonList();
while (reader.Read())
{
if (reader.TokenType == JsonToken.EndArray)
return parsedArray;
parsedArray.Add(ParseValue(reader));
}
throw IncompleteJsonException;
}
#endregion
}
}