corrade-vassal – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | vero | 1 | using System; |
2 | using System.Collections; |
||
3 | using System.Collections.Generic; |
||
4 | using System.Collections.Specialized; |
||
5 | using System.IO; |
||
6 | using LitJson; |
||
7 | |||
8 | namespace OpenMetaverse.StructuredData |
||
9 | { |
||
10 | public static partial class OSDParser |
||
11 | { |
||
12 | public static OSD DeserializeJson(Stream json) |
||
13 | { |
||
14 | using (StreamReader streamReader = new StreamReader(json)) |
||
15 | { |
||
16 | JsonReader reader = new JsonReader(streamReader); |
||
17 | return DeserializeJson(JsonMapper.ToObject(reader)); |
||
18 | } |
||
19 | } |
||
20 | |||
21 | public static OSD DeserializeJson(string json) |
||
22 | { |
||
23 | return DeserializeJson(JsonMapper.ToObject(json)); |
||
24 | } |
||
25 | |||
26 | public static OSD DeserializeJson(JsonData json) |
||
27 | { |
||
28 | if (json == null) return new OSD(); |
||
29 | |||
30 | switch (json.GetJsonType()) |
||
31 | { |
||
32 | case JsonType.Boolean: |
||
33 | return OSD.FromBoolean((bool)json); |
||
34 | case JsonType.Int: |
||
35 | return OSD.FromInteger((int)json); |
||
36 | case JsonType.Long: |
||
37 | return OSD.FromLong((long)json); |
||
38 | case JsonType.Double: |
||
39 | return OSD.FromReal((double)json); |
||
40 | case JsonType.String: |
||
41 | string str = (string)json; |
||
42 | if (String.IsNullOrEmpty(str)) |
||
43 | return new OSD(); |
||
44 | else |
||
45 | return OSD.FromString(str); |
||
46 | case JsonType.Array: |
||
47 | OSDArray array = new OSDArray(json.Count); |
||
48 | for (int i = 0; i < json.Count; i++) |
||
49 | array.Add(DeserializeJson(json[i])); |
||
50 | return array; |
||
51 | case JsonType.Object: |
||
52 | OSDMap map = new OSDMap(json.Count); |
||
53 | IDictionaryEnumerator e = ((IOrderedDictionary)json).GetEnumerator(); |
||
54 | while (e.MoveNext()) |
||
55 | map.Add((string)e.Key, DeserializeJson((JsonData)e.Value)); |
||
56 | return map; |
||
57 | case JsonType.None: |
||
58 | default: |
||
59 | return new OSD(); |
||
60 | } |
||
61 | } |
||
62 | |||
63 | public static string SerializeJsonString(OSD osd) |
||
64 | { |
||
65 | return SerializeJson(osd, false).ToJson(); |
||
66 | } |
||
67 | |||
68 | public static string SerializeJsonString(OSD osd, bool preserveDefaults) |
||
69 | { |
||
70 | return SerializeJson(osd, preserveDefaults).ToJson(); |
||
71 | } |
||
72 | |||
73 | public static void SerializeJsonString(OSD osd, bool preserveDefaults, ref JsonWriter writer) |
||
74 | { |
||
75 | SerializeJson(osd, preserveDefaults).ToJson(writer); |
||
76 | } |
||
77 | |||
78 | public static JsonData SerializeJson(OSD osd, bool preserveDefaults) |
||
79 | { |
||
80 | switch (osd.Type) |
||
81 | { |
||
82 | case OSDType.Boolean: |
||
83 | return new JsonData(osd.AsBoolean()); |
||
84 | case OSDType.Integer: |
||
85 | return new JsonData(osd.AsInteger()); |
||
86 | case OSDType.Real: |
||
87 | return new JsonData(osd.AsReal()); |
||
88 | case OSDType.String: |
||
89 | case OSDType.Date: |
||
90 | case OSDType.URI: |
||
91 | case OSDType.UUID: |
||
92 | return new JsonData(osd.AsString()); |
||
93 | case OSDType.Binary: |
||
94 | byte[] binary = osd.AsBinary(); |
||
95 | JsonData jsonbinarray = new JsonData(); |
||
96 | jsonbinarray.SetJsonType(JsonType.Array); |
||
97 | for (int i = 0; i < binary.Length; i++) |
||
98 | jsonbinarray.Add(new JsonData(binary[i])); |
||
99 | return jsonbinarray; |
||
100 | case OSDType.Array: |
||
101 | JsonData jsonarray = new JsonData(); |
||
102 | jsonarray.SetJsonType(JsonType.Array); |
||
103 | OSDArray array = (OSDArray)osd; |
||
104 | for (int i = 0; i < array.Count; i++) |
||
105 | jsonarray.Add(SerializeJson(array[i], preserveDefaults)); |
||
106 | return jsonarray; |
||
107 | case OSDType.Map: |
||
108 | JsonData jsonmap = new JsonData(); |
||
109 | jsonmap.SetJsonType(JsonType.Object); |
||
110 | OSDMap map = (OSDMap)osd; |
||
111 | foreach (KeyValuePair<string, OSD> kvp in map) |
||
112 | { |
||
113 | JsonData data; |
||
114 | |||
115 | if (preserveDefaults) |
||
116 | data = SerializeJson(kvp.Value, preserveDefaults); |
||
117 | else |
||
118 | data = SerializeJsonNoDefaults(kvp.Value); |
||
119 | |||
120 | if (data != null) |
||
121 | jsonmap[kvp.Key] = data; |
||
122 | } |
||
123 | return jsonmap; |
||
124 | case OSDType.Unknown: |
||
125 | default: |
||
126 | return new JsonData(null); |
||
127 | } |
||
128 | } |
||
129 | |||
130 | private static JsonData SerializeJsonNoDefaults(OSD osd) |
||
131 | { |
||
132 | switch (osd.Type) |
||
133 | { |
||
134 | case OSDType.Boolean: |
||
135 | bool b = osd.AsBoolean(); |
||
136 | if (!b) |
||
137 | return null; |
||
138 | |||
139 | return new JsonData(b); |
||
140 | case OSDType.Integer: |
||
141 | int v = osd.AsInteger(); |
||
142 | if (v == 0) |
||
143 | return null; |
||
144 | |||
145 | return new JsonData(v); |
||
146 | case OSDType.Real: |
||
147 | double d = osd.AsReal(); |
||
148 | if (d == 0.0d) |
||
149 | return null; |
||
150 | |||
151 | return new JsonData(d); |
||
152 | case OSDType.String: |
||
153 | case OSDType.Date: |
||
154 | case OSDType.URI: |
||
155 | string str = osd.AsString(); |
||
156 | if (String.IsNullOrEmpty(str)) |
||
157 | return null; |
||
158 | |||
159 | return new JsonData(str); |
||
160 | case OSDType.UUID: |
||
161 | UUID uuid = osd.AsUUID(); |
||
162 | if (uuid == UUID.Zero) |
||
163 | return null; |
||
164 | |||
165 | return new JsonData(uuid.ToString()); |
||
166 | case OSDType.Binary: |
||
167 | byte[] binary = osd.AsBinary(); |
||
168 | if (binary == Utils.EmptyBytes) |
||
169 | return null; |
||
170 | |||
171 | JsonData jsonbinarray = new JsonData(); |
||
172 | jsonbinarray.SetJsonType(JsonType.Array); |
||
173 | for (int i = 0; i < binary.Length; i++) |
||
174 | jsonbinarray.Add(new JsonData(binary[i])); |
||
175 | return jsonbinarray; |
||
176 | case OSDType.Array: |
||
177 | JsonData jsonarray = new JsonData(); |
||
178 | jsonarray.SetJsonType(JsonType.Array); |
||
179 | OSDArray array = (OSDArray)osd; |
||
180 | for (int i = 0; i < array.Count; i++) |
||
181 | jsonarray.Add(SerializeJson(array[i], false)); |
||
182 | return jsonarray; |
||
183 | case OSDType.Map: |
||
184 | JsonData jsonmap = new JsonData(); |
||
185 | jsonmap.SetJsonType(JsonType.Object); |
||
186 | OSDMap map = (OSDMap)osd; |
||
187 | foreach (KeyValuePair<string, OSD> kvp in map) |
||
188 | { |
||
189 | JsonData data = SerializeJsonNoDefaults(kvp.Value); |
||
190 | if (data != null) |
||
191 | jsonmap[kvp.Key] = data; |
||
192 | } |
||
193 | return jsonmap; |
||
194 | case OSDType.Unknown: |
||
195 | default: |
||
196 | return null; |
||
197 | } |
||
198 | } |
||
199 | } |
||
200 | } |