2 using System.Collections;
3 using System.Collections.Generic;
7 using JsonPair = System.Collections.Generic.KeyValuePair<string, System.Json.JsonValue>;
12 public abstract class JsonValue : IEnumerable
14 public static JsonValue Load (Stream stream)
17 throw new ArgumentNullException ("stream");
18 return Load (new StreamReader (stream));
21 public static JsonValue Load (TextReader textReader)
23 if (textReader == null)
24 throw new ArgumentNullException ("textReader");
25 return new JsonReader (textReader).Read ();
28 public static JsonValue Parse (string jsonString)
30 if (jsonString == null)
31 throw new ArgumentNullException ("jsonString");
32 return Load (new StringReader (jsonString));
35 public virtual int Count {
36 get { throw new InvalidOperationException (); }
39 public abstract JsonType JsonType { get; }
41 public virtual JsonValue this [int index] {
42 get { throw new InvalidOperationException (); }
43 set { throw new InvalidOperationException (); }
46 public virtual JsonValue this [string key] {
47 get { throw new InvalidOperationException (); }
48 set { throw new InvalidOperationException (); }
51 public virtual bool ContainsKey (string key)
53 throw new InvalidOperationException ();
56 public virtual void Save (Stream stream)
59 throw new ArgumentNullException ("stream");
60 Save (new StreamWriter (stream));
63 public virtual void Save (TextWriter textWriter)
65 if (textWriter == null)
66 throw new ArgumentNullException ("textWriter");
67 SaveInternal (textWriter);
70 void SaveInternal (TextWriter w)
75 bool following = false;
76 foreach (JsonPair pair in ((JsonObject) this)) {
80 w.Write (EscapeString (pair.Key));
82 pair.Value.SaveInternal (w);
90 foreach (JsonValue v in ((JsonArray) this)) {
98 case JsonType.Boolean:
99 w.Write ((bool) this ? "true" : "false");
101 case JsonType.String:
103 w.Write (EscapeString (((JsonPrimitive) this).GetFormattedString ()));
107 w.Write (((JsonPrimitive) this).GetFormattedString ());
112 public override string ToString ()
114 StringWriter sw = new StringWriter ();
116 return sw.ToString ();
119 IEnumerator IEnumerable.GetEnumerator ()
121 throw new InvalidOperationException ();
124 internal string EscapeString (string src)
129 for (int i = 0; i < src.Length; i++)
130 if (src [i] == '"' || src [i] == '\\') {
131 var sb = new StringBuilder ();
133 sb.Append (src, 0, i - 1);
134 return DoEscapeString (sb, src, i);
139 string DoEscapeString (StringBuilder sb, string src, int cur)
141 for (int i = cur; i < src.Length; i++)
142 if (src [i] == '"' || src [i] == '\\') {
144 sb.Append (src [i++]);
145 return DoEscapeString (sb, src, i);
147 sb.Append (src, cur, src.Length - cur);
148 return sb.ToString ();
153 public static implicit operator JsonValue (bool value)
155 return new JsonPrimitive (value);
158 public static implicit operator JsonValue (byte value)
160 return new JsonPrimitive (value);
163 public static implicit operator JsonValue (char value)
165 return new JsonPrimitive (value);
168 public static implicit operator JsonValue (decimal value)
170 return new JsonPrimitive (value);
173 public static implicit operator JsonValue (double value)
175 return new JsonPrimitive (value);
178 public static implicit operator JsonValue (float value)
180 return new JsonPrimitive (value);
183 public static implicit operator JsonValue (int value)
185 return new JsonPrimitive (value);
188 public static implicit operator JsonValue (long value)
190 return new JsonPrimitive (value);
193 public static implicit operator JsonValue (sbyte value)
195 return new JsonPrimitive (value);
198 public static implicit operator JsonValue (short value)
200 return new JsonPrimitive (value);
203 public static implicit operator JsonValue (string value)
205 return new JsonPrimitive (value);
208 public static implicit operator JsonValue (uint value)
210 return new JsonPrimitive (value);
213 public static implicit operator JsonValue (ulong value)
215 return new JsonPrimitive (value);
218 public static implicit operator JsonValue (ushort value)
220 return new JsonPrimitive (value);
223 public static implicit operator JsonValue (DateTime value)
225 return new JsonPrimitive (value);
228 public static implicit operator JsonValue (DateTimeOffset value)
230 return new JsonPrimitive (value);
233 public static implicit operator JsonValue (Guid value)
235 return new JsonPrimitive (value);
238 public static implicit operator JsonValue (TimeSpan value)
240 return new JsonPrimitive (value);
243 public static implicit operator JsonValue (Uri value)
245 return new JsonPrimitive (value);
250 public static implicit operator bool (JsonValue value)
253 throw new ArgumentNullException ("value");
254 return (bool) ((JsonPrimitive) value).Value;
257 public static implicit operator byte (JsonValue value)
260 throw new ArgumentNullException ("value");
261 return (byte) ((JsonPrimitive) value).Value;
264 public static implicit operator char (JsonValue value)
267 throw new ArgumentNullException ("value");
268 return (char) ((JsonPrimitive) value).Value;
271 public static implicit operator decimal (JsonValue value)
274 throw new ArgumentNullException ("value");
275 return (decimal) ((JsonPrimitive) value).Value;
278 public static implicit operator double (JsonValue value)
281 throw new ArgumentNullException ("value");
282 return (double) ((JsonPrimitive) value).Value;
285 public static implicit operator float (JsonValue value)
288 throw new ArgumentNullException ("value");
289 return (float) ((JsonPrimitive) value).Value;
292 public static implicit operator int (JsonValue value)
295 throw new ArgumentNullException ("value");
296 return (int) ((JsonPrimitive) value).Value;
299 public static implicit operator long (JsonValue value)
302 throw new ArgumentNullException ("value");
303 return (long) ((JsonPrimitive) value).Value;
306 public static implicit operator sbyte (JsonValue value)
309 throw new ArgumentNullException ("value");
310 return (sbyte) ((JsonPrimitive) value).Value;
313 public static implicit operator short (JsonValue value)
316 throw new ArgumentNullException ("value");
317 return (short) ((JsonPrimitive) value).Value;
320 public static implicit operator string (JsonValue value)
324 return (string) ((JsonPrimitive) value).Value;
327 public static implicit operator uint (JsonValue value)
330 throw new ArgumentNullException ("value");
331 return (uint) ((JsonPrimitive) value).Value;
334 public static implicit operator ulong (JsonValue value)
337 throw new ArgumentNullException ("value");
338 return (ulong) ((JsonPrimitive) value).Value;
341 public static implicit operator ushort (JsonValue value)
344 throw new ArgumentNullException ("value");
345 return (ushort) ((JsonPrimitive) value).Value;
348 public static implicit operator DateTime (JsonValue value)
351 throw new ArgumentNullException ("value");
352 return (DateTime) ((JsonPrimitive) value).Value;
355 public static implicit operator DateTimeOffset (JsonValue value)
358 throw new ArgumentNullException ("value");
359 return (DateTimeOffset) ((JsonPrimitive) value).Value;
362 public static implicit operator TimeSpan (JsonValue value)
365 throw new ArgumentNullException ("value");
366 return (TimeSpan) ((JsonPrimitive) value).Value;
369 public static implicit operator Guid (JsonValue value)
372 throw new ArgumentNullException ("value");
373 return (Guid) ((JsonPrimitive) value).Value;
376 public static implicit operator Uri (JsonValue value)
379 throw new ArgumentNullException ("value");
380 return (Uri) ((JsonPrimitive) value).Value;