File: JsonValue.cs

package info (click to toggle)
mono 4.6.2.7+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (497 lines) | stat: -rw-r--r-- 13,569 bytes parent folder | download
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Json;
using System.Text;

using JsonPair = System.Collections.Generic.KeyValuePair<string, System.Json.JsonValue>;


namespace System.Json
{
	public abstract class JsonValue : IEnumerable
	{
		public static JsonValue Load (Stream stream)
		{
			if (stream == null)
				throw new ArgumentNullException ("stream");
			return Load (new StreamReader (stream, true));
		}

		public static JsonValue Load (TextReader textReader)
		{
			if (textReader == null)
				throw new ArgumentNullException ("textReader");

			var ret = new JavaScriptReader (textReader, true).Read ();

			return ToJsonValue (ret);
		}

		static IEnumerable<KeyValuePair<string,JsonValue>> ToJsonPairEnumerable (IEnumerable<KeyValuePair<string,object>> kvpc)
		{
			foreach (var kvp in kvpc)
				yield return new KeyValuePair<string,JsonValue> (kvp.Key, ToJsonValue (kvp.Value));
		}

		static IEnumerable<JsonValue> ToJsonValueEnumerable (IEnumerable<object> arr)
		{
			foreach (var obj in arr)
				yield return ToJsonValue (obj);
		}

		static JsonValue ToJsonValue (object ret)
		{
			if (ret == null)
				return null;
			var kvpc = ret as IEnumerable<KeyValuePair<string,object>>;
			if (kvpc != null)
				return new JsonObject (ToJsonPairEnumerable (kvpc));
			var arr = ret as IEnumerable<object>;
			if (arr != null)
				return new JsonArray (ToJsonValueEnumerable (arr));

			if (ret is bool)
				return new JsonPrimitive ((bool) ret);
			if (ret is byte)
				return new JsonPrimitive ((byte) ret);
			if (ret is char)
				return new JsonPrimitive ((char) ret);
			if (ret is decimal)
				return new JsonPrimitive ((decimal) ret);
			if (ret is double)
				return new JsonPrimitive ((double) ret);
			if (ret is float)
				return new JsonPrimitive ((float) ret);
			if (ret is int)
				return new JsonPrimitive ((int) ret);
			if (ret is long)
				return new JsonPrimitive ((long) ret);
			if (ret is sbyte)
				return new JsonPrimitive ((sbyte) ret);
			if (ret is short)
				return new JsonPrimitive ((short) ret);
			if (ret is string)
				return new JsonPrimitive ((string) ret);
			if (ret is uint)
				return new JsonPrimitive ((uint) ret);
			if (ret is ulong)
				return new JsonPrimitive ((ulong) ret);
			if (ret is ushort)
				return new JsonPrimitive ((ushort) ret);
			if (ret is DateTime)
				return new JsonPrimitive ((DateTime) ret);
			if (ret is DateTimeOffset)
				return new JsonPrimitive ((DateTimeOffset) ret);
			if (ret is Guid)
				return new JsonPrimitive ((Guid) ret);
			if (ret is TimeSpan)
				return new JsonPrimitive ((TimeSpan) ret);
			if (ret is Uri)
				return new JsonPrimitive ((Uri) ret);
			throw new NotSupportedException (String.Format ("Unexpected parser return type: {0}", ret.GetType ()));
		}

		public static JsonValue Parse (string jsonString)
		{
			if (jsonString == null)
				throw new ArgumentNullException ("jsonString");
			return Load (new StringReader (jsonString));
		}

		public virtual int Count {
			get { throw new InvalidOperationException (); }
		}

		public abstract JsonType JsonType { get; }

		public virtual JsonValue this [int index] {
			get { throw new InvalidOperationException (); }
			set { throw new InvalidOperationException (); }
		}

		public virtual JsonValue this [string key] {
			get { throw new InvalidOperationException (); }
			set { throw new InvalidOperationException (); }
		}

		public virtual bool ContainsKey (string key)
		{
			throw new InvalidOperationException ();
		}

		public virtual void Save (Stream stream)
		{
			if (stream == null)
				throw new ArgumentNullException ("stream");
			Save (new StreamWriter (stream));
		}

		public virtual void Save (TextWriter textWriter)
		{
			if (textWriter == null)
				throw new ArgumentNullException ("textWriter");
			SaveInternal (textWriter);
		}
		
		void SaveInternal (TextWriter w)
		{
			switch (JsonType) {
			case JsonType.Object:
				w.Write ('{');
				bool following = false;
				foreach (JsonPair pair in ((JsonObject) this)) {
					if (following)
						w.Write (", ");
					w.Write ('\"');
					w.Write (EscapeString (pair.Key));
					w.Write ("\": ");
					if (pair.Value == null)
						w.Write ("null");
					else
						pair.Value.SaveInternal (w);
					following = true;
				}
				w.Write ('}');
				break;
			case JsonType.Array:
				w.Write ('[');
				following = false;
				foreach (JsonValue v in ((JsonArray) this)) {
					if (following)
						w.Write (", ");
					if (v != null) 
						v.SaveInternal (w);
					else
						w.Write ("null");
					following = true;
				}
				w.Write (']');
				break;
			case JsonType.Boolean:
				w.Write ((bool) this ? "true" : "false");
				break;
			case JsonType.String:
				w.Write ('"');
				w.Write (EscapeString (((JsonPrimitive) this).GetFormattedString ()));
				w.Write ('"');
				break;
			default:
				w.Write (((JsonPrimitive) this).GetFormattedString ());
				break;
			}
		}

		public override string ToString ()
		{
			StringWriter sw = new StringWriter ();
			Save (sw);
			return sw.ToString ();
		}

		IEnumerator IEnumerable.GetEnumerator ()
		{
			throw new InvalidOperationException ();
		}

		// Characters which have to be escaped:
		// - Required by JSON Spec: Control characters, '"' and '\\'
		// - Broken surrogates to make sure the JSON string is valid Unicode
		//   (and can be encoded as UTF8)
		// - JSON does not require U+2028 and U+2029 to be escaped, but
		//   JavaScript does require this:
		//   http://stackoverflow.com/questions/2965293/javascript-parse-error-on-u2028-unicode-character/9168133#9168133
		// - '/' also does not have to be escaped, but escaping it when
		//   preceeded by a '<' avoids problems with JSON in HTML <script> tags
		bool NeedEscape (string src, int i) {
			char c = src [i];
			return c < 32 || c == '"' || c == '\\'
				// Broken lead surrogate
				|| (c >= '\uD800' && c <= '\uDBFF' &&
					(i == src.Length - 1 || src [i + 1] < '\uDC00' || src [i + 1] > '\uDFFF'))
				// Broken tail surrogate
				|| (c >= '\uDC00' && c <= '\uDFFF' &&
					(i == 0 || src [i - 1] < '\uD800' || src [i - 1] > '\uDBFF'))
				// To produce valid JavaScript
				|| c == '\u2028' || c == '\u2029'
				// Escape "</" for <script> tags
				|| (c == '/' && i > 0 && src [i - 1] == '<');
		}
		
		internal string EscapeString (string src)
		{
			if (src == null)
				return null;

			for (int i = 0; i < src.Length; i++)
				if (NeedEscape (src, i)) {
					var sb = new StringBuilder ();
					if (i > 0)
						sb.Append (src, 0, i);
					return DoEscapeString (sb, src, i);
				}
			return src;
		}

		string DoEscapeString (StringBuilder sb, string src, int cur)
		{
			int start = cur;
			for (int i = cur; i < src.Length; i++)
				if (NeedEscape (src, i)) {
					sb.Append (src, start, i - start);
					switch (src [i]) {
					case '\b': sb.Append ("\\b"); break;
					case '\f': sb.Append ("\\f"); break;
					case '\n': sb.Append ("\\n"); break;
					case '\r': sb.Append ("\\r"); break;
					case '\t': sb.Append ("\\t"); break;
					case '\"': sb.Append ("\\\""); break;
					case '\\': sb.Append ("\\\\"); break;
					case '/': sb.Append ("\\/"); break;
					default:
						sb.Append ("\\u");
						sb.Append (((int) src [i]).ToString ("x04"));
						break;
					}
					start = i + 1;
				}
			sb.Append (src, start, src.Length - start);
			return sb.ToString ();
		}

		// CLI -> JsonValue

		public static implicit operator JsonValue (bool value)
		{
			return new JsonPrimitive (value);
		}

		public static implicit operator JsonValue (byte value)
		{
			return new JsonPrimitive (value);
		}

		public static implicit operator JsonValue (char value)
		{
			return new JsonPrimitive (value);
		}

		public static implicit operator JsonValue (decimal value)
		{
			return new JsonPrimitive (value);
		}

		public static implicit operator JsonValue (double value)
		{
			return new JsonPrimitive (value);
		}

		public static implicit operator JsonValue (float value)
		{
			return new JsonPrimitive (value);
		}

		public static implicit operator JsonValue (int value)
		{
			return new JsonPrimitive (value);
		}

		public static implicit operator JsonValue (long value)
		{
			return new JsonPrimitive (value);
		}

		public static implicit operator JsonValue (sbyte value)
		{
			return new JsonPrimitive (value);
		}

		public static implicit operator JsonValue (short value)
		{
			return new JsonPrimitive (value);
		}

		public static implicit operator JsonValue (string value)
		{
			return new JsonPrimitive (value);
		}

		public static implicit operator JsonValue (uint value)
		{
			return new JsonPrimitive (value);
		}

		public static implicit operator JsonValue (ulong value)
		{
			return new JsonPrimitive (value);
		}

		public static implicit operator JsonValue (ushort value)
		{
			return new JsonPrimitive (value);
		}

		public static implicit operator JsonValue (DateTime value)
		{
			return new JsonPrimitive (value);
		}

		public static implicit operator JsonValue (DateTimeOffset value)
		{
			return new JsonPrimitive (value);
		}

		public static implicit operator JsonValue (Guid value)
		{
			return new JsonPrimitive (value);
		}

		public static implicit operator JsonValue (TimeSpan value)
		{
			return new JsonPrimitive (value);
		}

		public static implicit operator JsonValue (Uri value)
		{
			return new JsonPrimitive (value);
		}

		// JsonValue -> CLI

		public static implicit operator bool (JsonValue value)
		{
			if (value == null)
				throw new ArgumentNullException ("value");
			return Convert.ToBoolean (((JsonPrimitive) value).Value, NumberFormatInfo.InvariantInfo);
		}

		public static implicit operator byte (JsonValue value)
		{
			if (value == null)
				throw new ArgumentNullException ("value");
			return Convert.ToByte (((JsonPrimitive) value).Value, NumberFormatInfo.InvariantInfo);
		}

		public static implicit operator char (JsonValue value)
		{
			if (value == null)
				throw new ArgumentNullException ("value");
			return Convert.ToChar (((JsonPrimitive) value).Value, NumberFormatInfo.InvariantInfo);
		}

		public static implicit operator decimal (JsonValue value)
		{
			if (value == null)
				throw new ArgumentNullException ("value");
			return Convert.ToDecimal (((JsonPrimitive) value).Value, NumberFormatInfo.InvariantInfo);
		}

		public static implicit operator double (JsonValue value)
		{
			if (value == null)
				throw new ArgumentNullException ("value");
			return Convert.ToDouble (((JsonPrimitive) value).Value, NumberFormatInfo.InvariantInfo);
		}

		public static implicit operator float (JsonValue value)
		{
			if (value == null)
				throw new ArgumentNullException ("value");
			return Convert.ToSingle (((JsonPrimitive) value).Value, NumberFormatInfo.InvariantInfo);
		}

		public static implicit operator int (JsonValue value)
		{
			if (value == null)
				throw new ArgumentNullException ("value");
			return Convert.ToInt32 (((JsonPrimitive) value).Value, NumberFormatInfo.InvariantInfo);
		}

		public static implicit operator long (JsonValue value)
		{
			if (value == null)
				throw new ArgumentNullException ("value");
			return Convert.ToInt64 (((JsonPrimitive) value).Value, NumberFormatInfo.InvariantInfo);
		}

		public static implicit operator sbyte (JsonValue value)
		{
			if (value == null)
				throw new ArgumentNullException ("value");
			return Convert.ToSByte (((JsonPrimitive) value).Value, NumberFormatInfo.InvariantInfo);
		}

		public static implicit operator short (JsonValue value)
		{
			if (value == null)
				throw new ArgumentNullException ("value");
			return Convert.ToInt16 (((JsonPrimitive) value).Value, NumberFormatInfo.InvariantInfo);
		}

		public static implicit operator string (JsonValue value)
		{
			if (value == null)
				return null;
			return (string) ((JsonPrimitive) value).Value;
		}

		public static implicit operator uint (JsonValue value)
		{
			if (value == null)
				throw new ArgumentNullException ("value");
			return Convert.ToUInt32 (((JsonPrimitive) value).Value, NumberFormatInfo.InvariantInfo);
		}

		public static implicit operator ulong (JsonValue value)
		{
			if (value == null)
				throw new ArgumentNullException ("value");
			return Convert.ToUInt64(((JsonPrimitive) value).Value, NumberFormatInfo.InvariantInfo);
		}

		public static implicit operator ushort (JsonValue value)
		{
			if (value == null)
				throw new ArgumentNullException ("value");
			return Convert.ToUInt16 (((JsonPrimitive) value).Value, NumberFormatInfo.InvariantInfo);
		}

		public static implicit operator DateTime (JsonValue value)
		{
			if (value == null)
				throw new ArgumentNullException ("value");
			return (DateTime) ((JsonPrimitive) value).Value;
		}

		public static implicit operator DateTimeOffset (JsonValue value)
		{
			if (value == null)
				throw new ArgumentNullException ("value");
			return (DateTimeOffset) ((JsonPrimitive) value).Value;
		}

		public static implicit operator TimeSpan (JsonValue value)
		{
			if (value == null)
				throw new ArgumentNullException ("value");
			return (TimeSpan) ((JsonPrimitive) value).Value;
		}

		public static implicit operator Guid (JsonValue value)
		{
			if (value == null)
				throw new ArgumentNullException ("value");
			return (Guid) ((JsonPrimitive) value).Value;
		}

		public static implicit operator Uri (JsonValue value)
		{
			if (value == null)
				throw new ArgumentNullException ("value");
			return (Uri) ((JsonPrimitive) value).Value;
		}
	}
}