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)) {
170 case JsonType.Boolean:
171 w.Write ((bool) this ? "true" : "false");
173 case JsonType.String:
175 w.Write (EscapeString (((JsonPrimitive) this).GetFormattedString ()));
179 w.Write (((JsonPrimitive) this).GetFormattedString ());
184 public override string ToString ()
186 StringWriter sw = new StringWriter ();
188 return sw.ToString ();
191 IEnumerator IEnumerable.GetEnumerator ()
193 throw new InvalidOperationException ();
196 internal string EscapeString (string src)
201 for (int i = 0; i < src.Length; i++)
202 if (src [i] == '"' || src [i] == '\\') {
203 var sb = new StringBuilder ();
205 sb.Append (src, 0, i);
206 return DoEscapeString (sb, src, i);
211 string DoEscapeString (StringBuilder sb, string src, int cur)
214 for (int i = cur; i < src.Length; i++)
215 if (src [i] == '"' || src [i] == '\\') {
216 sb.Append (src, start, i - start);
218 sb.Append (src [i++]);
221 sb.Append (src, start, src.Length - start);
222 return sb.ToString ();
227 public static implicit operator JsonValue (bool value)
229 return new JsonPrimitive (value);
232 public static implicit operator JsonValue (byte value)
234 return new JsonPrimitive (value);
237 public static implicit operator JsonValue (char value)
239 return new JsonPrimitive (value);
242 public static implicit operator JsonValue (decimal value)
244 return new JsonPrimitive (value);
247 public static implicit operator JsonValue (double value)
249 return new JsonPrimitive (value);
252 public static implicit operator JsonValue (float value)
254 return new JsonPrimitive (value);
257 public static implicit operator JsonValue (int value)
259 return new JsonPrimitive (value);
262 public static implicit operator JsonValue (long value)
264 return new JsonPrimitive (value);
267 public static implicit operator JsonValue (sbyte value)
269 return new JsonPrimitive (value);
272 public static implicit operator JsonValue (short value)
274 return new JsonPrimitive (value);
277 public static implicit operator JsonValue (string value)
279 return new JsonPrimitive (value);
282 public static implicit operator JsonValue (uint value)
284 return new JsonPrimitive (value);
287 public static implicit operator JsonValue (ulong value)
289 return new JsonPrimitive (value);
292 public static implicit operator JsonValue (ushort value)
294 return new JsonPrimitive (value);
297 public static implicit operator JsonValue (DateTime value)
299 return new JsonPrimitive (value);
302 public static implicit operator JsonValue (DateTimeOffset value)
304 return new JsonPrimitive (value);
307 public static implicit operator JsonValue (Guid value)
309 return new JsonPrimitive (value);
312 public static implicit operator JsonValue (TimeSpan value)
314 return new JsonPrimitive (value);
317 public static implicit operator JsonValue (Uri value)
319 return new JsonPrimitive (value);
324 public static implicit operator bool (JsonValue value)
327 throw new ArgumentNullException ("value");
328 return Convert.ToBoolean (((JsonPrimitive) value).Value);
331 public static implicit operator byte (JsonValue value)
334 throw new ArgumentNullException ("value");
335 return Convert.ToByte (((JsonPrimitive) value).Value);
338 public static implicit operator char (JsonValue value)
341 throw new ArgumentNullException ("value");
342 return Convert.ToChar (((JsonPrimitive) value).Value);
345 public static implicit operator decimal (JsonValue value)
348 throw new ArgumentNullException ("value");
349 return Convert.ToDecimal (((JsonPrimitive) value).Value);
352 public static implicit operator double (JsonValue value)
355 throw new ArgumentNullException ("value");
356 return Convert.ToDouble (((JsonPrimitive) value).Value);
359 public static implicit operator float (JsonValue value)
362 throw new ArgumentNullException ("value");
363 return Convert.ToSingle (((JsonPrimitive) value).Value);
366 public static implicit operator int (JsonValue value)
369 throw new ArgumentNullException ("value");
370 return Convert.ToInt32 (((JsonPrimitive) value).Value);
373 public static implicit operator long (JsonValue value)
376 throw new ArgumentNullException ("value");
377 return Convert.ToInt64 (((JsonPrimitive) value).Value);
380 public static implicit operator sbyte (JsonValue value)
383 throw new ArgumentNullException ("value");
384 return Convert.ToSByte (((JsonPrimitive) value).Value);
387 public static implicit operator short (JsonValue value)
390 throw new ArgumentNullException ("value");
391 return Convert.ToInt16 (((JsonPrimitive) value).Value);
394 public static implicit operator string (JsonValue value)
398 return (string) ((JsonPrimitive) value).Value;
401 public static implicit operator uint (JsonValue value)
404 throw new ArgumentNullException ("value");
405 return Convert.ToUInt16 (((JsonPrimitive) value).Value);
408 public static implicit operator ulong (JsonValue value)
411 throw new ArgumentNullException ("value");
412 return Convert.ToUInt64(((JsonPrimitive) value).Value);
415 public static implicit operator ushort (JsonValue value)
418 throw new ArgumentNullException ("value");
419 return Convert.ToUInt16 (((JsonPrimitive) value).Value);
422 public static implicit operator DateTime (JsonValue value)
425 throw new ArgumentNullException ("value");
426 return (DateTime) ((JsonPrimitive) value).Value;
429 public static implicit operator DateTimeOffset (JsonValue value)
432 throw new ArgumentNullException ("value");
433 return (DateTimeOffset) ((JsonPrimitive) value).Value;
436 public static implicit operator TimeSpan (JsonValue value)
439 throw new ArgumentNullException ("value");
440 return (TimeSpan) ((JsonPrimitive) value).Value;
443 public static implicit operator Guid (JsonValue value)
446 throw new ArgumentNullException ("value");
447 return (Guid) ((JsonPrimitive) value).Value;
450 public static implicit operator Uri (JsonValue value)
453 throw new ArgumentNullException ("value");
454 return (Uri) ((JsonPrimitive) value).Value;