2 using System.Collections;
3 using System.Collections.Generic;
6 using System.Runtime.Serialization.Json;
9 using JsonPair = System.Collections.Generic.KeyValuePair<string, System.Json.JsonValue>;
14 public abstract class JsonValue : IEnumerable
16 public static JsonValue Load (Stream stream)
19 throw new ArgumentNullException ("stream");
20 return Load (new StreamReader (stream, true));
23 public static JsonValue Load (TextReader textReader)
25 if (textReader == null)
26 throw new ArgumentNullException ("textReader");
28 var ret = new JavaScriptReader (textReader, true).Read ();
30 return ToJsonValue (ret);
33 static IEnumerable<KeyValuePair<string,JsonValue>> ToJsonPairEnumerable (IEnumerable<KeyValuePair<string,object>> kvpc)
35 foreach (var kvp in kvpc)
36 yield return new KeyValuePair<string,JsonValue> (kvp.Key, ToJsonValue (kvp.Value));
39 static IEnumerable<JsonValue> ToJsonValueEnumerable (IEnumerable<object> arr)
41 foreach (var obj in arr)
42 yield return ToJsonValue (obj);
45 static JsonValue ToJsonValue (object ret)
49 var kvpc = ret as IEnumerable<KeyValuePair<string,object>>;
51 return new JsonObject (ToJsonPairEnumerable (kvpc));
52 var arr = ret as IEnumerable<object>;
54 return new JsonArray (ToJsonValueEnumerable (arr));
57 return new JsonPrimitive ((bool) ret);
59 return new JsonPrimitive ((byte) ret);
61 return new JsonPrimitive ((char) ret);
63 return new JsonPrimitive ((decimal) ret);
65 return new JsonPrimitive ((double) ret);
67 return new JsonPrimitive ((float) ret);
69 return new JsonPrimitive ((int) ret);
71 return new JsonPrimitive ((long) ret);
73 return new JsonPrimitive ((sbyte) ret);
75 return new JsonPrimitive ((short) ret);
77 return new JsonPrimitive ((string) ret);
79 return new JsonPrimitive ((uint) ret);
81 return new JsonPrimitive ((ulong) ret);
83 return new JsonPrimitive ((ushort) ret);
85 return new JsonPrimitive ((DateTime) ret);
86 if (ret is DateTimeOffset)
87 return new JsonPrimitive ((DateTimeOffset) ret);
89 return new JsonPrimitive ((Guid) ret);
91 return new JsonPrimitive ((TimeSpan) ret);
93 return new JsonPrimitive ((Uri) ret);
94 throw new NotSupportedException (String.Format ("Unexpected parser return type: {0}", ret.GetType ()));
97 public static JsonValue Parse (string jsonString)
99 if (jsonString == null)
100 throw new ArgumentNullException ("jsonString");
101 return Load (new StringReader (jsonString));
104 public virtual int Count {
105 get { throw new InvalidOperationException (); }
108 public abstract JsonType JsonType { get; }
110 public virtual JsonValue this [int index] {
111 get { throw new InvalidOperationException (); }
112 set { throw new InvalidOperationException (); }
115 public virtual JsonValue this [string key] {
116 get { throw new InvalidOperationException (); }
117 set { throw new InvalidOperationException (); }
120 public virtual bool ContainsKey (string key)
122 throw new InvalidOperationException ();
125 public virtual void Save (Stream stream)
128 throw new ArgumentNullException ("stream");
129 Save (new StreamWriter (stream));
132 public virtual void Save (TextWriter textWriter)
134 if (textWriter == null)
135 throw new ArgumentNullException ("textWriter");
136 SaveInternal (textWriter);
139 void SaveInternal (TextWriter w)
142 case JsonType.Object:
144 bool following = false;
145 foreach (JsonPair pair in ((JsonObject) this)) {
149 w.Write (EscapeString (pair.Key));
151 if (pair.Value == null)
154 pair.Value.SaveInternal (w);
162 foreach (JsonValue v in ((JsonArray) this)) {
173 case JsonType.Boolean:
174 w.Write ((bool) this ? "true" : "false");
176 case JsonType.String:
178 w.Write (EscapeString (((JsonPrimitive) this).GetFormattedString ()));
182 w.Write (((JsonPrimitive) this).GetFormattedString ());
187 public override string ToString ()
189 StringWriter sw = new StringWriter ();
191 return sw.ToString ();
194 IEnumerator IEnumerable.GetEnumerator ()
196 throw new InvalidOperationException ();
199 internal string EscapeString (string src)
204 for (int i = 0; i < src.Length; i++)
205 if (src [i] == '"' || src [i] == '\\') {
206 var sb = new StringBuilder ();
208 sb.Append (src, 0, i);
209 return DoEscapeString (sb, src, i);
214 string DoEscapeString (StringBuilder sb, string src, int cur)
217 for (int i = cur; i < src.Length; i++)
218 if (src [i] == '"' || src [i] == '\\') {
219 sb.Append (src, start, i - start);
221 sb.Append (src [i++]);
224 sb.Append (src, start, src.Length - start);
225 return sb.ToString ();
230 public static implicit operator JsonValue (bool value)
232 return new JsonPrimitive (value);
235 public static implicit operator JsonValue (byte value)
237 return new JsonPrimitive (value);
240 public static implicit operator JsonValue (char value)
242 return new JsonPrimitive (value);
245 public static implicit operator JsonValue (decimal value)
247 return new JsonPrimitive (value);
250 public static implicit operator JsonValue (double value)
252 return new JsonPrimitive (value);
255 public static implicit operator JsonValue (float value)
257 return new JsonPrimitive (value);
260 public static implicit operator JsonValue (int value)
262 return new JsonPrimitive (value);
265 public static implicit operator JsonValue (long value)
267 return new JsonPrimitive (value);
270 public static implicit operator JsonValue (sbyte value)
272 return new JsonPrimitive (value);
275 public static implicit operator JsonValue (short value)
277 return new JsonPrimitive (value);
280 public static implicit operator JsonValue (string value)
282 return new JsonPrimitive (value);
285 public static implicit operator JsonValue (uint value)
287 return new JsonPrimitive (value);
290 public static implicit operator JsonValue (ulong value)
292 return new JsonPrimitive (value);
295 public static implicit operator JsonValue (ushort value)
297 return new JsonPrimitive (value);
300 public static implicit operator JsonValue (DateTime value)
302 return new JsonPrimitive (value);
305 public static implicit operator JsonValue (DateTimeOffset value)
307 return new JsonPrimitive (value);
310 public static implicit operator JsonValue (Guid value)
312 return new JsonPrimitive (value);
315 public static implicit operator JsonValue (TimeSpan value)
317 return new JsonPrimitive (value);
320 public static implicit operator JsonValue (Uri value)
322 return new JsonPrimitive (value);
327 public static implicit operator bool (JsonValue value)
330 throw new ArgumentNullException ("value");
331 return Convert.ToBoolean (((JsonPrimitive) value).Value);
334 public static implicit operator byte (JsonValue value)
337 throw new ArgumentNullException ("value");
338 return Convert.ToByte (((JsonPrimitive) value).Value);
341 public static implicit operator char (JsonValue value)
344 throw new ArgumentNullException ("value");
345 return Convert.ToChar (((JsonPrimitive) value).Value);
348 public static implicit operator decimal (JsonValue value)
351 throw new ArgumentNullException ("value");
352 return Convert.ToDecimal (((JsonPrimitive) value).Value);
355 public static implicit operator double (JsonValue value)
358 throw new ArgumentNullException ("value");
359 return Convert.ToDouble (((JsonPrimitive) value).Value);
362 public static implicit operator float (JsonValue value)
365 throw new ArgumentNullException ("value");
366 return Convert.ToSingle (((JsonPrimitive) value).Value);
369 public static implicit operator int (JsonValue value)
372 throw new ArgumentNullException ("value");
373 return Convert.ToInt32 (((JsonPrimitive) value).Value);
376 public static implicit operator long (JsonValue value)
379 throw new ArgumentNullException ("value");
380 return Convert.ToInt64 (((JsonPrimitive) value).Value);
383 public static implicit operator sbyte (JsonValue value)
386 throw new ArgumentNullException ("value");
387 return Convert.ToSByte (((JsonPrimitive) value).Value);
390 public static implicit operator short (JsonValue value)
393 throw new ArgumentNullException ("value");
394 return Convert.ToInt16 (((JsonPrimitive) value).Value);
397 public static implicit operator string (JsonValue value)
401 return (string) ((JsonPrimitive) value).Value;
404 public static implicit operator uint (JsonValue value)
407 throw new ArgumentNullException ("value");
408 return Convert.ToUInt16 (((JsonPrimitive) value).Value);
411 public static implicit operator ulong (JsonValue value)
414 throw new ArgumentNullException ("value");
415 return Convert.ToUInt64(((JsonPrimitive) value).Value);
418 public static implicit operator ushort (JsonValue value)
421 throw new ArgumentNullException ("value");
422 return Convert.ToUInt16 (((JsonPrimitive) value).Value);
425 public static implicit operator DateTime (JsonValue value)
428 throw new ArgumentNullException ("value");
429 return (DateTime) ((JsonPrimitive) value).Value;
432 public static implicit operator DateTimeOffset (JsonValue value)
435 throw new ArgumentNullException ("value");
436 return (DateTimeOffset) ((JsonPrimitive) value).Value;
439 public static implicit operator TimeSpan (JsonValue value)
442 throw new ArgumentNullException ("value");
443 return (TimeSpan) ((JsonPrimitive) value).Value;
446 public static implicit operator Guid (JsonValue value)
449 throw new ArgumentNullException ("value");
450 return (Guid) ((JsonPrimitive) value).Value;
453 public static implicit operator Uri (JsonValue value)
456 throw new ArgumentNullException ("value");
457 return (Uri) ((JsonPrimitive) value).Value;