Interhaptics SDK for Unity 1.6
Loading...
Searching...
No Matches
SimpleJSON.cs
Go to the documentation of this file.
1
2/* * * * *
3 * A simple JSON Parser / builder
4 * ------------------------------
5 *
6 * It mainly has been written as a simple JSON parser. It can build a JSON string
7 * from the node-tree, or generate a node tree from any valid JSON string.
8 *
9 * Written by Bunny83
10 * 2012-06-09
11 *
12 * Changelog now external. See Changelog.txt
13 *
14 * The MIT License (MIT)
15 *
16 * Copyright (c) 2012-2019 Markus Göbel (Bunny83)
17 *
18 * Permission is hereby granted, free of charge, to any person obtaining a copy
19 * of this software and associated documentation files (the "Software"), to deal
20 * in the Software without restriction, including without limitation the rights
21 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
22 * copies of the Software, and to permit persons to whom the Software is
23 * furnished to do so, subject to the following conditions:
24 *
25 * The above copyright notice and this permission notice shall be included in all
26 * copies or substantial portions of the Software.
27 *
28 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
31 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
32 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
33 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34 * SOFTWARE.
35 *
36 * * * * */
37using System;
38using System.Collections;
39using System.Collections.Generic;
40using System.Globalization;
41using System.Linq;
42using System.Text;
43
44namespace SimpleJSON
45{
46 public enum JSONNodeType
47 {
48 Array = 1,
49 Object = 2,
50 String = 3,
51 Number = 4,
52 NullValue = 5,
53 Boolean = 6,
54 None = 7,
55 Custom = 0xFF,
56 }
57 public enum JSONTextMode
58 {
59 Compact,
60 Indent
61 }
62
63 public abstract partial class JSONNode
64 {
65 #region Enumerators
66 public struct Enumerator
67 {
68 private enum Type { None, Array, Object }
69 private Type type;
70 private Dictionary<string, JSONNode>.Enumerator m_Object;
71 private List<JSONNode>.Enumerator m_Array;
72 public bool IsValid { get { return type != Type.None; } }
73 public Enumerator(List<JSONNode>.Enumerator aArrayEnum)
74 {
75 type = Type.Array;
76 m_Object = default(Dictionary<string, JSONNode>.Enumerator);
77 m_Array = aArrayEnum;
78 }
79 public Enumerator(Dictionary<string, JSONNode>.Enumerator aDictEnum)
80 {
81 type = Type.Object;
82 m_Object = aDictEnum;
83 m_Array = default(List<JSONNode>.Enumerator);
84 }
85 public KeyValuePair<string, JSONNode> Current
86 {
87 get
88 {
89 if (type == Type.Array)
90 return new KeyValuePair<string, JSONNode>(string.Empty, m_Array.Current);
91 else if (type == Type.Object)
92 return m_Object.Current;
93 return new KeyValuePair<string, JSONNode>(string.Empty, null);
94 }
95 }
96 public bool MoveNext()
97 {
98 if (type == Type.Array)
99 return m_Array.MoveNext();
100 else if (type == Type.Object)
101 return m_Object.MoveNext();
102 return false;
103 }
104 }
105 public struct ValueEnumerator
106 {
107 private Enumerator m_Enumerator;
108 public ValueEnumerator(List<JSONNode>.Enumerator aArrayEnum) : this(new Enumerator(aArrayEnum)) { }
109 public ValueEnumerator(Dictionary<string, JSONNode>.Enumerator aDictEnum) : this(new Enumerator(aDictEnum)) { }
110 public ValueEnumerator(Enumerator aEnumerator) { m_Enumerator = aEnumerator; }
111 public JSONNode Current { get { return m_Enumerator.Current.Value; } }
112 public bool MoveNext() { return m_Enumerator.MoveNext(); }
113 public ValueEnumerator GetEnumerator() { return this; }
114 }
115 public struct KeyEnumerator
116 {
117 private Enumerator m_Enumerator;
118 public KeyEnumerator(List<JSONNode>.Enumerator aArrayEnum) : this(new Enumerator(aArrayEnum)) { }
119 public KeyEnumerator(Dictionary<string, JSONNode>.Enumerator aDictEnum) : this(new Enumerator(aDictEnum)) { }
120 public KeyEnumerator(Enumerator aEnumerator) { m_Enumerator = aEnumerator; }
121 public string Current { get { return m_Enumerator.Current.Key; } }
122 public bool MoveNext() { return m_Enumerator.MoveNext(); }
123 public KeyEnumerator GetEnumerator() { return this; }
124 }
125
126 public class LinqEnumerator : IEnumerator<KeyValuePair<string, JSONNode>>, IEnumerable<KeyValuePair<string, JSONNode>>
127 {
128 private JSONNode m_Node;
129 private Enumerator m_Enumerator;
130 internal LinqEnumerator(JSONNode aNode)
131 {
132 m_Node = aNode;
133 if (m_Node != null)
134 m_Enumerator = m_Node.GetEnumerator();
135 }
136 public KeyValuePair<string, JSONNode> Current { get { return m_Enumerator.Current; } }
137 object IEnumerator.Current { get { return m_Enumerator.Current; } }
138 public bool MoveNext() { return m_Enumerator.MoveNext(); }
139
140 public void Dispose()
141 {
142 m_Node = null;
143 m_Enumerator = new Enumerator();
144 }
145
146 public IEnumerator<KeyValuePair<string, JSONNode>> GetEnumerator()
147 {
148 return new LinqEnumerator(m_Node);
149 }
150
151 public void Reset()
152 {
153 if (m_Node != null)
154 m_Enumerator = m_Node.GetEnumerator();
155 }
156
157 IEnumerator IEnumerable.GetEnumerator()
158 {
159 return new LinqEnumerator(m_Node);
160 }
161 }
162
163 #endregion Enumerators
164
165 #region common interface
166
167 public static bool forceASCII = false; // Use Unicode by default
168 public static bool longAsString = false; // lazy creator creates a JSONString instead of JSONNumber
169 public static bool allowLineComments = true; // allow "//"-style comments at the end of a line
170
171 public abstract JSONNodeType Tag { get; }
172
173 public virtual JSONNode this[int aIndex] { get { return null; } set { } }
174
175 public virtual JSONNode this[string aKey] { get { return null; } set { } }
176
177 public virtual string Value { get { return ""; } set { } }
178
179 public virtual int Count { get { return 0; } }
180
181 public virtual bool IsNumber { get { return false; } }
182 public virtual bool IsString { get { return false; } }
183 public virtual bool IsBoolean { get { return false; } }
184 public virtual bool IsNull { get { return false; } }
185 public virtual bool IsArray { get { return false; } }
186 public virtual bool IsObject { get { return false; } }
187
188 public virtual bool Inline { get { return false; } set { } }
189
190 public virtual void Add(string aKey, JSONNode aItem)
191 {
192 }
193 public virtual void Add(JSONNode aItem)
194 {
195 Add("", aItem);
196 }
197
198 public virtual JSONNode Remove(string aKey)
199 {
200 return null;
201 }
202
203 public virtual JSONNode Remove(int aIndex)
204 {
205 return null;
206 }
207
208 public virtual JSONNode Remove(JSONNode aNode)
209 {
210 return aNode;
211 }
212
213 public virtual JSONNode Clone()
214 {
215 return null;
216 }
217
218 public virtual IEnumerable<JSONNode> Children
219 {
220 get
221 {
222 yield break;
223 }
224 }
225
226 public IEnumerable<JSONNode> DeepChildren
227 {
228 get
229 {
230 foreach (var C in Children)
231 foreach (var D in C.DeepChildren)
232 yield return D;
233 }
234 }
235
236 public virtual bool HasKey(string aKey)
237 {
238 return false;
239 }
240
241 public virtual JSONNode GetValueOrDefault(string aKey, JSONNode aDefault)
242 {
243 return aDefault;
244 }
245
246 public override string ToString()
247 {
248 StringBuilder sb = new StringBuilder();
249 WriteToStringBuilder(sb, 0, 0, JSONTextMode.Compact);
250 return sb.ToString();
251 }
252
253 public virtual string ToString(int aIndent)
254 {
255 StringBuilder sb = new StringBuilder();
256 WriteToStringBuilder(sb, 0, aIndent, JSONTextMode.Indent);
257 return sb.ToString();
258 }
259 internal abstract void WriteToStringBuilder(StringBuilder aSB, int aIndent, int aIndentInc, JSONTextMode aMode);
260
261 public abstract Enumerator GetEnumerator();
262 public IEnumerable<KeyValuePair<string, JSONNode>> Linq { get { return new LinqEnumerator(this); } }
263 public KeyEnumerator Keys { get { return new KeyEnumerator(GetEnumerator()); } }
264 public ValueEnumerator Values { get { return new ValueEnumerator(GetEnumerator()); } }
265
266 #endregion common interface
267
268 #region typecasting properties
269
270
271 public virtual double AsDouble
272 {
273 get
274 {
275 double v = 0.0;
276 if (double.TryParse(Value, NumberStyles.Float, CultureInfo.InvariantCulture, out v))
277 return v;
278 return 0.0;
279 }
280 set
281 {
282 Value = value.ToString(CultureInfo.InvariantCulture);
283 }
284 }
285
286 public virtual int AsInt
287 {
288 get { return (int)AsDouble; }
289 set { AsDouble = value; }
290 }
291
292 public virtual float AsFloat
293 {
294 get { return (float)AsDouble; }
295 set { AsDouble = value; }
296 }
297
298 public virtual bool AsBool
299 {
300 get
301 {
302 bool v = false;
303 if (bool.TryParse(Value, out v))
304 return v;
305 return !string.IsNullOrEmpty(Value);
306 }
307 set
308 {
309 Value = (value) ? "true" : "false";
310 }
311 }
312
313 public virtual long AsLong
314 {
315 get
316 {
317 long val = 0;
318 if (long.TryParse(Value, out val))
319 return val;
320 return 0L;
321 }
322 set
323 {
324 Value = value.ToString();
325 }
326 }
327
328 public virtual JSONArray AsArray
329 {
330 get
331 {
332 return this as JSONArray;
333 }
334 }
335
336 public virtual JSONObject AsObject
337 {
338 get
339 {
340 return this as JSONObject;
341 }
342 }
343
344
345 #endregion typecasting properties
346
347 #region operators
348
349 public static implicit operator JSONNode(string s)
350 {
351 return new JSONString(s);
352 }
353 public static implicit operator string(JSONNode d)
354 {
355 return (d == null) ? null : d.Value;
356 }
357
358 public static implicit operator JSONNode(double n)
359 {
360 return new JSONNumber(n);
361 }
362 public static implicit operator double(JSONNode d)
363 {
364 return (d == null) ? 0 : d.AsDouble;
365 }
366
367 public static implicit operator JSONNode(float n)
368 {
369 return new JSONNumber(n);
370 }
371 public static implicit operator float(JSONNode d)
372 {
373 return (d == null) ? 0 : d.AsFloat;
374 }
375
376 public static implicit operator JSONNode(int n)
377 {
378 return new JSONNumber(n);
379 }
380 public static implicit operator int(JSONNode d)
381 {
382 return (d == null) ? 0 : d.AsInt;
383 }
384
385 public static implicit operator JSONNode(long n)
386 {
387 if (longAsString)
388 return new JSONString(n.ToString());
389 return new JSONNumber(n);
390 }
391 public static implicit operator long(JSONNode d)
392 {
393 return (d == null) ? 0L : d.AsLong;
394 }
395
396 public static implicit operator JSONNode(bool b)
397 {
398 return new JSONBool(b);
399 }
400 public static implicit operator bool(JSONNode d)
401 {
402 return (d == null) ? false : d.AsBool;
403 }
404
405 public static implicit operator JSONNode(KeyValuePair<string, JSONNode> aKeyValue)
406 {
407 return aKeyValue.Value;
408 }
409
410 public static bool operator ==(JSONNode a, object b)
411 {
412 if (ReferenceEquals(a, b))
413 return true;
414 bool aIsNull = a is JSONNull || ReferenceEquals(a, null) || a is JSONLazyCreator;
415 bool bIsNull = b is JSONNull || ReferenceEquals(b, null) || b is JSONLazyCreator;
416 if (aIsNull && bIsNull)
417 return true;
418 return !aIsNull && a.Equals(b);
419 }
420
421 public static bool operator !=(JSONNode a, object b)
422 {
423 return !(a == b);
424 }
425
426 public override bool Equals(object obj)
427 {
428 return ReferenceEquals(this, obj);
429 }
430
431 public override int GetHashCode()
432 {
433 return base.GetHashCode();
434 }
435
436 #endregion operators
437
438 [ThreadStatic]
439 private static StringBuilder m_EscapeBuilder;
440 internal static StringBuilder EscapeBuilder
441 {
442 get
443 {
444 if (m_EscapeBuilder == null)
445 m_EscapeBuilder = new StringBuilder();
446 return m_EscapeBuilder;
447 }
448 }
449 internal static string Escape(string aText)
450 {
451 var sb = EscapeBuilder;
452 sb.Length = 0;
453 if (sb.Capacity < aText.Length + aText.Length / 10)
454 sb.Capacity = aText.Length + aText.Length / 10;
455 foreach (char c in aText)
456 {
457 switch (c)
458 {
459 case '\\':
460 sb.Append("\\\\");
461 break;
462 case '\"':
463 sb.Append("\\\"");
464 break;
465 case '\n':
466 sb.Append("\\n");
467 break;
468 case '\r':
469 sb.Append("\\r");
470 break;
471 case '\t':
472 sb.Append("\\t");
473 break;
474 case '\b':
475 sb.Append("\\b");
476 break;
477 case '\f':
478 sb.Append("\\f");
479 break;
480 default:
481 if (c < ' ' || (forceASCII && c > 127))
482 {
483 ushort val = c;
484 sb.Append("\\u").Append(val.ToString("X4"));
485 }
486 else
487 sb.Append(c);
488 break;
489 }
490 }
491 string result = sb.ToString();
492 sb.Length = 0;
493 return result;
494 }
495
496 private static JSONNode ParseElement(string token, bool quoted)
497 {
498 if (quoted)
499 return token;
500 string tmp = token.ToLower();
501 if (tmp == "false" || tmp == "true")
502 return tmp == "true";
503 if (tmp == "null")
504 return JSONNull.CreateOrGet();
505 double val;
506 if (double.TryParse(token, NumberStyles.Float, CultureInfo.InvariantCulture, out val))
507 return val;
508 else
509 return token;
510 }
511
512 public static JSONNode Parse(string aJSON)
513 {
514 Stack<JSONNode> stack = new Stack<JSONNode>();
515 JSONNode ctx = null;
516 int i = 0;
517 StringBuilder Token = new StringBuilder();
518 string TokenName = "";
519 bool QuoteMode = false;
520 bool TokenIsQuoted = false;
521 while (i < aJSON.Length)
522 {
523 switch (aJSON[i])
524 {
525 case '{':
526 if (QuoteMode)
527 {
528 Token.Append(aJSON[i]);
529 break;
530 }
531 stack.Push(new JSONObject());
532 if (ctx != null)
533 {
534 ctx.Add(TokenName, stack.Peek());
535 }
536 TokenName = "";
537 Token.Length = 0;
538 ctx = stack.Peek();
539 break;
540
541 case '[':
542 if (QuoteMode)
543 {
544 Token.Append(aJSON[i]);
545 break;
546 }
547
548 stack.Push(new JSONArray());
549 if (ctx != null)
550 {
551 ctx.Add(TokenName, stack.Peek());
552 }
553 TokenName = "";
554 Token.Length = 0;
555 ctx = stack.Peek();
556 break;
557
558 case '}':
559 case ']':
560 if (QuoteMode)
561 {
562
563 Token.Append(aJSON[i]);
564 break;
565 }
566 if (stack.Count == 0)
567 throw new Exception("JSON Parse: Too many closing brackets");
568
569 stack.Pop();
570 if (Token.Length > 0 || TokenIsQuoted)
571 ctx.Add(TokenName, ParseElement(Token.ToString(), TokenIsQuoted));
572 TokenIsQuoted = false;
573 TokenName = "";
574 Token.Length = 0;
575 if (stack.Count > 0)
576 ctx = stack.Peek();
577 break;
578
579 case ':':
580 if (QuoteMode)
581 {
582 Token.Append(aJSON[i]);
583 break;
584 }
585 TokenName = Token.ToString();
586 Token.Length = 0;
587 TokenIsQuoted = false;
588 break;
589
590 case '"':
591 QuoteMode ^= true;
592 TokenIsQuoted |= QuoteMode;
593 break;
594
595 case ',':
596 if (QuoteMode)
597 {
598 Token.Append(aJSON[i]);
599 break;
600 }
601 if (Token.Length > 0 || TokenIsQuoted)
602 ctx.Add(TokenName, ParseElement(Token.ToString(), TokenIsQuoted));
603 TokenIsQuoted = false;
604 TokenName = "";
605 Token.Length = 0;
606 TokenIsQuoted = false;
607 break;
608
609 case '\r':
610 case '\n':
611 break;
612
613 case ' ':
614 case '\t':
615 if (QuoteMode)
616 Token.Append(aJSON[i]);
617 break;
618
619 case '\\':
620 ++i;
621 if (QuoteMode)
622 {
623 char C = aJSON[i];
624 switch (C)
625 {
626 case 't':
627 Token.Append('\t');
628 break;
629 case 'r':
630 Token.Append('\r');
631 break;
632 case 'n':
633 Token.Append('\n');
634 break;
635 case 'b':
636 Token.Append('\b');
637 break;
638 case 'f':
639 Token.Append('\f');
640 break;
641 case 'u':
642 {
643 string s = aJSON.Substring(i + 1, 4);
644 Token.Append((char)int.Parse(
645 s,
646 System.Globalization.NumberStyles.AllowHexSpecifier));
647 i += 4;
648 break;
649 }
650 default:
651 Token.Append(C);
652 break;
653 }
654 }
655 break;
656 case '/':
657 if (allowLineComments && !QuoteMode && i + 1 < aJSON.Length && aJSON[i + 1] == '/')
658 {
659 while (++i < aJSON.Length && aJSON[i] != '\n' && aJSON[i] != '\r') ;
660 break;
661 }
662 Token.Append(aJSON[i]);
663 break;
664 case '\uFEFF': // remove / ignore BOM (Byte Order Mark)
665 break;
666
667 default:
668 Token.Append(aJSON[i]);
669 break;
670 }
671 ++i;
672 }
673 if (QuoteMode)
674 {
675 throw new Exception("JSON Parse: Quotation marks seems to be messed up.");
676 }
677 if (ctx == null)
678 return ParseElement(Token.ToString(), TokenIsQuoted);
679 return ctx;
680 }
681
682 }
683 // End of JSONNode
684
685 public partial class JSONArray : JSONNode
686 {
687 private List<JSONNode> m_List = new List<JSONNode>();
688 private bool inline = false;
689 public override bool Inline
690 {
691 get { return inline; }
692 set { inline = value; }
693 }
694
695 public override JSONNodeType Tag { get { return JSONNodeType.Array; } }
696 public override bool IsArray { get { return true; } }
697 public override Enumerator GetEnumerator() { return new Enumerator(m_List.GetEnumerator()); }
698
699 public override JSONNode this[int aIndex]
700 {
701 get
702 {
703 if (aIndex < 0 || aIndex >= m_List.Count)
704 return new JSONLazyCreator(this);
705 return m_List[aIndex];
706 }
707 set
708 {
709 if (value == null)
710 value = JSONNull.CreateOrGet();
711 if (aIndex < 0 || aIndex >= m_List.Count)
712 m_List.Add(value);
713 else
714 m_List[aIndex] = value;
715 }
716 }
717
718 public override JSONNode this[string aKey]
719 {
720 get { return new JSONLazyCreator(this); }
721 set
722 {
723 if (value == null)
724 value = JSONNull.CreateOrGet();
725 m_List.Add(value);
726 }
727 }
728
729 public override int Count
730 {
731 get { return m_List.Count; }
732 }
733
734 public override void Add(string aKey, JSONNode aItem)
735 {
736 if (aItem == null)
737 aItem = JSONNull.CreateOrGet();
738 m_List.Add(aItem);
739 }
740
741 public override JSONNode Remove(int aIndex)
742 {
743 if (aIndex < 0 || aIndex >= m_List.Count)
744 return null;
745 JSONNode tmp = m_List[aIndex];
746 m_List.RemoveAt(aIndex);
747 return tmp;
748 }
749
750 public override JSONNode Remove(JSONNode aNode)
751 {
752 m_List.Remove(aNode);
753 return aNode;
754 }
755
756 public override JSONNode Clone()
757 {
758 var node = new JSONArray();
759 node.m_List.Capacity = m_List.Capacity;
760 foreach(var n in m_List)
761 {
762 if (n != null)
763 node.Add(n.Clone());
764 else
765 node.Add(null);
766 }
767 return node;
768 }
769
770 public override IEnumerable<JSONNode> Children
771 {
772 get
773 {
774 foreach (JSONNode N in m_List)
775 yield return N;
776 }
777 }
778
779
780 internal override void WriteToStringBuilder(StringBuilder aSB, int aIndent, int aIndentInc, JSONTextMode aMode)
781 {
782 aSB.Append('[');
783 int count = m_List.Count;
784 if (inline)
785 aMode = JSONTextMode.Compact;
786 for (int i = 0; i < count; i++)
787 {
788 if (i > 0)
789 aSB.Append(',');
790 if (aMode == JSONTextMode.Indent)
791 aSB.AppendLine();
792
793 if (aMode == JSONTextMode.Indent)
794 aSB.Append(' ', aIndent + aIndentInc);
795 m_List[i].WriteToStringBuilder(aSB, aIndent + aIndentInc, aIndentInc, aMode);
796 }
797 if (aMode == JSONTextMode.Indent)
798 aSB.AppendLine().Append(' ', aIndent);
799 aSB.Append(']');
800 }
801 }
802 // End of JSONArray
803
804 public partial class JSONObject : JSONNode
805 {
806 private Dictionary<string, JSONNode> m_Dict = new Dictionary<string, JSONNode>();
807
808 private bool inline = false;
809 public override bool Inline
810 {
811 get { return inline; }
812 set { inline = value; }
813 }
814
815 public override JSONNodeType Tag { get { return JSONNodeType.Object; } }
816 public override bool IsObject { get { return true; } }
817
818 public override Enumerator GetEnumerator() { return new Enumerator(m_Dict.GetEnumerator()); }
819
820
821 public override JSONNode this[string aKey]
822 {
823 get
824 {
825 if (m_Dict.ContainsKey(aKey))
826 return m_Dict[aKey];
827 else
828 return new JSONLazyCreator(this, aKey);
829 }
830 set
831 {
832 if (value == null)
833 value = JSONNull.CreateOrGet();
834 if (m_Dict.ContainsKey(aKey))
835 m_Dict[aKey] = value;
836 else
837 m_Dict.Add(aKey, value);
838 }
839 }
840
841 public override JSONNode this[int aIndex]
842 {
843 get
844 {
845 if (aIndex < 0 || aIndex >= m_Dict.Count)
846 return null;
847 return m_Dict.ElementAt(aIndex).Value;
848 }
849 set
850 {
851 if (value == null)
852 value = JSONNull.CreateOrGet();
853 if (aIndex < 0 || aIndex >= m_Dict.Count)
854 return;
855 string key = m_Dict.ElementAt(aIndex).Key;
856 m_Dict[key] = value;
857 }
858 }
859
860 public override int Count
861 {
862 get { return m_Dict.Count; }
863 }
864
865 public override void Add(string aKey, JSONNode aItem)
866 {
867 if (aItem == null)
868 aItem = JSONNull.CreateOrGet();
869
870 if (aKey != null)
871 {
872 if (m_Dict.ContainsKey(aKey))
873 m_Dict[aKey] = aItem;
874 else
875 m_Dict.Add(aKey, aItem);
876 }
877 else
878 m_Dict.Add(Guid.NewGuid().ToString(), aItem);
879 }
880
881 public override JSONNode Remove(string aKey)
882 {
883 if (!m_Dict.ContainsKey(aKey))
884 return null;
885 JSONNode tmp = m_Dict[aKey];
886 m_Dict.Remove(aKey);
887 return tmp;
888 }
889
890 public override JSONNode Remove(int aIndex)
891 {
892 if (aIndex < 0 || aIndex >= m_Dict.Count)
893 return null;
894 var item = m_Dict.ElementAt(aIndex);
895 m_Dict.Remove(item.Key);
896 return item.Value;
897 }
898
899 public override JSONNode Remove(JSONNode aNode)
900 {
901 try
902 {
903 var item = m_Dict.Where(k => k.Value == aNode).First();
904 m_Dict.Remove(item.Key);
905 return aNode;
906 }
907 catch
908 {
909 return null;
910 }
911 }
912
913 public override JSONNode Clone()
914 {
915 var node = new JSONObject();
916 foreach (var n in m_Dict)
917 {
918 node.Add(n.Key, n.Value.Clone());
919 }
920 return node;
921 }
922
923 public override bool HasKey(string aKey)
924 {
925 return m_Dict.ContainsKey(aKey);
926 }
927
928 public override JSONNode GetValueOrDefault(string aKey, JSONNode aDefault)
929 {
930 JSONNode res;
931 if (m_Dict.TryGetValue(aKey, out res))
932 return res;
933 return aDefault;
934 }
935
936 public override IEnumerable<JSONNode> Children
937 {
938 get
939 {
940 foreach (KeyValuePair<string, JSONNode> N in m_Dict)
941 yield return N.Value;
942 }
943 }
944
945 internal override void WriteToStringBuilder(StringBuilder aSB, int aIndent, int aIndentInc, JSONTextMode aMode)
946 {
947 aSB.Append('{');
948 bool first = true;
949 if (inline)
950 aMode = JSONTextMode.Compact;
951 foreach (var k in m_Dict)
952 {
953 if (!first)
954 aSB.Append(',');
955 first = false;
956 if (aMode == JSONTextMode.Indent)
957 aSB.AppendLine();
958 if (aMode == JSONTextMode.Indent)
959 aSB.Append(' ', aIndent + aIndentInc);
960 aSB.Append('\"').Append(Escape(k.Key)).Append('\"');
961 if (aMode == JSONTextMode.Compact)
962 aSB.Append(':');
963 else
964 aSB.Append(" : ");
965 k.Value.WriteToStringBuilder(aSB, aIndent + aIndentInc, aIndentInc, aMode);
966 }
967 if (aMode == JSONTextMode.Indent)
968 aSB.AppendLine().Append(' ', aIndent);
969 aSB.Append('}');
970 }
971
972 }
973 // End of JSONObject
974
975 public partial class JSONString : JSONNode
976 {
977 private string m_Data;
978
979 public override JSONNodeType Tag { get { return JSONNodeType.String; } }
980 public override bool IsString { get { return true; } }
981
982 public override Enumerator GetEnumerator() { return new Enumerator(); }
983
984
985 public override string Value
986 {
987 get { return m_Data; }
988 set
989 {
990 m_Data = value;
991 }
992 }
993
994 public JSONString(string aData)
995 {
996 m_Data = aData;
997 }
998 public override JSONNode Clone()
999 {
1000 return new JSONString(m_Data);
1001 }
1002
1003 internal override void WriteToStringBuilder(StringBuilder aSB, int aIndent, int aIndentInc, JSONTextMode aMode)
1004 {
1005 aSB.Append('\"').Append(Escape(m_Data)).Append('\"');
1006 }
1007 public override bool Equals(object obj)
1008 {
1009 if (base.Equals(obj))
1010 return true;
1011 string s = obj as string;
1012 if (s != null)
1013 return m_Data == s;
1014 JSONString s2 = obj as JSONString;
1015 if (s2 != null)
1016 return m_Data == s2.m_Data;
1017 return false;
1018 }
1019 public override int GetHashCode()
1020 {
1021 return m_Data.GetHashCode();
1022 }
1023 }
1024 // End of JSONString
1025
1026 public partial class JSONNumber : JSONNode
1027 {
1028 private double m_Data;
1029
1030 public override JSONNodeType Tag { get { return JSONNodeType.Number; } }
1031 public override bool IsNumber { get { return true; } }
1032 public override Enumerator GetEnumerator() { return new Enumerator(); }
1033
1034 public override string Value
1035 {
1036 get { return m_Data.ToString(CultureInfo.InvariantCulture); }
1037 set
1038 {
1039 double v;
1040 if (double.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out v))
1041 m_Data = v;
1042 }
1043 }
1044
1045 public override double AsDouble
1046 {
1047 get { return m_Data; }
1048 set { m_Data = value; }
1049 }
1050 public override long AsLong
1051 {
1052 get { return (long)m_Data; }
1053 set { m_Data = value; }
1054 }
1055
1056 public JSONNumber(double aData)
1057 {
1058 m_Data = aData;
1059 }
1060
1061 public JSONNumber(string aData)
1062 {
1063 Value = aData;
1064 }
1065
1066 public override JSONNode Clone()
1067 {
1068 return new JSONNumber(m_Data);
1069 }
1070
1071 internal override void WriteToStringBuilder(StringBuilder aSB, int aIndent, int aIndentInc, JSONTextMode aMode)
1072 {
1073 aSB.Append(Value);
1074 }
1075 private static bool IsNumeric(object value)
1076 {
1077 return value is int || value is uint
1078 || value is float || value is double
1079 || value is decimal
1080 || value is long || value is ulong
1081 || value is short || value is ushort
1082 || value is sbyte || value is byte;
1083 }
1084 public override bool Equals(object obj)
1085 {
1086 if (obj == null)
1087 return false;
1088 if (base.Equals(obj))
1089 return true;
1090 JSONNumber s2 = obj as JSONNumber;
1091 if (s2 != null)
1092 return m_Data == s2.m_Data;
1093 if (IsNumeric(obj))
1094 return Convert.ToDouble(obj) == m_Data;
1095 return false;
1096 }
1097 public override int GetHashCode()
1098 {
1099 return m_Data.GetHashCode();
1100 }
1101 }
1102 // End of JSONNumber
1103
1104 public partial class JSONBool : JSONNode
1105 {
1106 private bool m_Data;
1107
1108 public override JSONNodeType Tag { get { return JSONNodeType.Boolean; } }
1109 public override bool IsBoolean { get { return true; } }
1110 public override Enumerator GetEnumerator() { return new Enumerator(); }
1111
1112 public override string Value
1113 {
1114 get { return m_Data.ToString(); }
1115 set
1116 {
1117 bool v;
1118 if (bool.TryParse(value, out v))
1119 m_Data = v;
1120 }
1121 }
1122 public override bool AsBool
1123 {
1124 get { return m_Data; }
1125 set { m_Data = value; }
1126 }
1127
1128 public JSONBool(bool aData)
1129 {
1130 m_Data = aData;
1131 }
1132
1133 public JSONBool(string aData)
1134 {
1135 Value = aData;
1136 }
1137
1138 public override JSONNode Clone()
1139 {
1140 return new JSONBool(m_Data);
1141 }
1142
1143 internal override void WriteToStringBuilder(StringBuilder aSB, int aIndent, int aIndentInc, JSONTextMode aMode)
1144 {
1145 aSB.Append((m_Data) ? "true" : "false");
1146 }
1147 public override bool Equals(object obj)
1148 {
1149 if (obj == null)
1150 return false;
1151 if (obj is bool)
1152 return m_Data == (bool)obj;
1153 return false;
1154 }
1155 public override int GetHashCode()
1156 {
1157 return m_Data.GetHashCode();
1158 }
1159 }
1160 // End of JSONBool
1161
1162 public partial class JSONNull : JSONNode
1163 {
1164 static JSONNull m_StaticInstance = new JSONNull();
1165 public static bool reuseSameInstance = true;
1166 public static JSONNull CreateOrGet()
1167 {
1169 return m_StaticInstance;
1170 return new JSONNull();
1171 }
1172 private JSONNull() { }
1173
1174 public override JSONNodeType Tag { get { return JSONNodeType.NullValue; } }
1175 public override bool IsNull { get { return true; } }
1176 public override Enumerator GetEnumerator() { return new Enumerator(); }
1177
1178 public override string Value
1179 {
1180 get { return "null"; }
1181 set { }
1182 }
1183 public override bool AsBool
1184 {
1185 get { return false; }
1186 set { }
1187 }
1188
1189 public override JSONNode Clone()
1190 {
1191 return CreateOrGet();
1192 }
1193
1194 public override bool Equals(object obj)
1195 {
1196 if (object.ReferenceEquals(this, obj))
1197 return true;
1198 return (obj is JSONNull);
1199 }
1200 public override int GetHashCode()
1201 {
1202 return 0;
1203 }
1204
1205 internal override void WriteToStringBuilder(StringBuilder aSB, int aIndent, int aIndentInc, JSONTextMode aMode)
1206 {
1207 aSB.Append("null");
1208 }
1209 }
1210 // End of JSONNull
1211
1212 internal partial class JSONLazyCreator : JSONNode
1213 {
1214 private JSONNode m_Node = null;
1215 private string m_Key = null;
1216 public override JSONNodeType Tag { get { return JSONNodeType.None; } }
1217 public override Enumerator GetEnumerator() { return new Enumerator(); }
1218
1219 public JSONLazyCreator(JSONNode aNode)
1220 {
1221 m_Node = aNode;
1222 m_Key = null;
1223 }
1224
1225 public JSONLazyCreator(JSONNode aNode, string aKey)
1226 {
1227 m_Node = aNode;
1228 m_Key = aKey;
1229 }
1230
1231 private T Set<T>(T aVal) where T : JSONNode
1232 {
1233 if (m_Key == null)
1234 m_Node.Add(aVal);
1235 else
1236 m_Node.Add(m_Key, aVal);
1237 m_Node = null; // Be GC friendly.
1238 return aVal;
1239 }
1240
1241 public override JSONNode this[int aIndex]
1242 {
1243 get { return new JSONLazyCreator(this); }
1244 set { Set(new JSONArray()).Add(value); }
1245 }
1246
1247 public override JSONNode this[string aKey]
1248 {
1249 get { return new JSONLazyCreator(this, aKey); }
1250 set { Set(new JSONObject()).Add(aKey, value); }
1251 }
1252
1253 public override void Add(JSONNode aItem)
1254 {
1255 Set(new JSONArray()).Add(aItem);
1256 }
1257
1258 public override void Add(string aKey, JSONNode aItem)
1259 {
1260 Set(new JSONObject()).Add(aKey, aItem);
1261 }
1262
1263 public static bool operator ==(JSONLazyCreator a, object b)
1264 {
1265 if (b == null)
1266 return true;
1267 return System.Object.ReferenceEquals(a, b);
1268 }
1269
1270 public static bool operator !=(JSONLazyCreator a, object b)
1271 {
1272 return !(a == b);
1273 }
1274
1275 public override bool Equals(object obj)
1276 {
1277 if (obj == null)
1278 return true;
1279 return System.Object.ReferenceEquals(this, obj);
1280 }
1281
1282 public override int GetHashCode()
1283 {
1284 return 0;
1285 }
1286
1287 public override int AsInt
1288 {
1289 get { Set(new JSONNumber(0)); return 0; }
1290 set { Set(new JSONNumber(value)); }
1291 }
1292
1293 public override float AsFloat
1294 {
1295 get { Set(new JSONNumber(0.0f)); return 0.0f; }
1296 set { Set(new JSONNumber(value)); }
1297 }
1298
1299 public override double AsDouble
1300 {
1301 get { Set(new JSONNumber(0.0)); return 0.0; }
1302 set { Set(new JSONNumber(value)); }
1303 }
1304
1305 public override long AsLong
1306 {
1307 get
1308 {
1309 if (longAsString)
1310 Set(new JSONString("0"));
1311 else
1312 Set(new JSONNumber(0.0));
1313 return 0L;
1314 }
1315 set
1316 {
1317 if (longAsString)
1318 Set(new JSONString(value.ToString()));
1319 else
1320 Set(new JSONNumber(value));
1321 }
1322 }
1323
1324 public override bool AsBool
1325 {
1326 get { Set(new JSONBool(false)); return false; }
1327 set { Set(new JSONBool(value)); }
1328 }
1329
1330 public override JSONArray AsArray
1331 {
1332 get { return Set(new JSONArray()); }
1333 }
1334
1335 public override JSONObject AsObject
1336 {
1337 get { return Set(new JSONObject()); }
1338 }
1339 internal override void WriteToStringBuilder(StringBuilder aSB, int aIndent, int aIndentInc, JSONTextMode aMode)
1340 {
1341 aSB.Append("null");
1342 }
1343 }
1344 // End of JSONLazyCreator
1345
1346 public static class JSON
1347 {
1348 public static JSONNode Parse(string aJSON)
1349 {
1350 return JSONNode.Parse(aJSON);
1351 }
1352 }
1353}
override int Count
override IEnumerable< JSONNode > Children
override JSONNode Clone()
override JSONNode Remove(JSONNode aNode)
override bool IsArray
override Enumerator GetEnumerator()
override void Add(string aKey, JSONNode aItem)
override bool Inline
override JSONNodeType Tag
override JSONNode Remove(int aIndex)
override JSONNode Clone()
override bool AsBool
JSONBool(string aData)
override JSONNodeType Tag
override int GetHashCode()
override bool Equals(object obj)
JSONBool(bool aData)
override string Value
override Enumerator GetEnumerator()
override bool IsBoolean
IEnumerator< KeyValuePair< string, JSONNode > > GetEnumerator()
KeyValuePair< string, JSONNode > Current
virtual bool IsBoolean
virtual int AsInt
virtual string Value
ValueEnumerator Values
static bool allowLineComments
JSONNodeType Tag
virtual JSONNode Remove(JSONNode aNode)
virtual bool IsObject
virtual int Count
virtual bool Inline
virtual JSONArray AsArray
virtual JSONNode Remove(string aKey)
static bool operator==(JSONNode a, object b)
virtual IEnumerable< JSONNode > Children
override string ToString()
override bool Equals(object obj)
virtual bool IsString
virtual bool AsBool
virtual bool HasKey(string aKey)
virtual float AsFloat
static bool operator!=(JSONNode a, object b)
KeyEnumerator Keys
override int GetHashCode()
virtual bool IsNull
IEnumerable< JSONNode > DeepChildren
Enumerator GetEnumerator()
virtual void Add(JSONNode aItem)
virtual JSONNode GetValueOrDefault(string aKey, JSONNode aDefault)
static bool longAsString
virtual string ToString(int aIndent)
virtual void Add(string aKey, JSONNode aItem)
virtual bool IsArray
virtual JSONNode Clone()
virtual double AsDouble
virtual long AsLong
virtual JSONNode Remove(int aIndex)
virtual JSONObject AsObject
virtual bool IsNumber
static JSONNode Parse(string aJSON)
static bool forceASCII
override Enumerator GetEnumerator()
override JSONNodeType Tag
override string Value
override bool IsNull
override bool Equals(object obj)
static bool reuseSameInstance
static JSONNull CreateOrGet()
override bool AsBool
override int GetHashCode()
override JSONNode Clone()
JSONNumber(double aData)
override long AsLong
override JSONNodeType Tag
override int GetHashCode()
override Enumerator GetEnumerator()
override double AsDouble
override bool Equals(object obj)
override bool IsNumber
override string Value
JSONNumber(string aData)
override JSONNode Clone()
override JSONNode Clone()
override JSONNode Remove(string aKey)
override JSONNode Remove(int aIndex)
override JSONNodeType Tag
override JSONNode Remove(JSONNode aNode)
override Enumerator GetEnumerator()
override bool HasKey(string aKey)
override bool IsObject
override IEnumerable< JSONNode > Children
override void Add(string aKey, JSONNode aItem)
override bool Inline
override JSONNode GetValueOrDefault(string aKey, JSONNode aDefault)
JSONString(string aData)
override Enumerator GetEnumerator()
override JSONNode Clone()
override bool IsString
override bool Equals(object obj)
override string Value
override JSONNodeType Tag
override int GetHashCode()
Enumerator(Dictionary< string, JSONNode >.Enumerator aDictEnum)
Definition SimpleJSON.cs:79
KeyValuePair< string, JSONNode > Current
Definition SimpleJSON.cs:86
Enumerator(List< JSONNode >.Enumerator aArrayEnum)
Definition SimpleJSON.cs:73
KeyEnumerator(List< JSONNode >.Enumerator aArrayEnum)
KeyEnumerator(Dictionary< string, JSONNode >.Enumerator aDictEnum)
KeyEnumerator(Enumerator aEnumerator)
ValueEnumerator(Dictionary< string, JSONNode >.Enumerator aDictEnum)
ValueEnumerator(List< JSONNode >.Enumerator aArrayEnum)
ValueEnumerator(Enumerator aEnumerator)