[154] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Text;
|
---|
| 4 |
|
---|
| 5 | namespace AllocsFixes.JSON
|
---|
| 6 | {
|
---|
| 7 | public class JSONObject : JSONNode
|
---|
| 8 | {
|
---|
| 9 | private Dictionary<string, JSONNode> nodes = new Dictionary<string, JSONNode> ();
|
---|
| 10 |
|
---|
[187] | 11 | public JSONNode this [string name] {
|
---|
| 12 | get { return nodes [name]; }
|
---|
| 13 | set { nodes [name] = value; }
|
---|
| 14 | }
|
---|
| 15 |
|
---|
| 16 | public int Count {
|
---|
| 17 | get { return nodes.Count; }
|
---|
| 18 | }
|
---|
| 19 |
|
---|
| 20 | public List<string> Keys {
|
---|
| 21 | get { return new List<string> (nodes.Keys); }
|
---|
| 22 | }
|
---|
| 23 |
|
---|
| 24 | public bool ContainsKey (string name)
|
---|
| 25 | {
|
---|
| 26 | return nodes.ContainsKey (name);
|
---|
| 27 | }
|
---|
| 28 |
|
---|
[154] | 29 | public void Add (string name, JSONNode node)
|
---|
| 30 | {
|
---|
| 31 | nodes.Add (name, node);
|
---|
| 32 | }
|
---|
| 33 |
|
---|
[309] | 34 | public override void ToString (StringBuilder stringBuilder, bool prettyPrint = false, int currentLevel = 0)
|
---|
[154] | 35 | {
|
---|
[309] | 36 | stringBuilder.Append ("{");
|
---|
[187] | 37 | if (prettyPrint)
|
---|
[309] | 38 | stringBuilder.Append ('\n');
|
---|
[154] | 39 | foreach (KeyValuePair<string, JSONNode> kvp in nodes) {
|
---|
[187] | 40 | if (prettyPrint)
|
---|
[309] | 41 | stringBuilder.Append (new String ('\t', currentLevel + 1));
|
---|
| 42 | stringBuilder.Append (String.Format ("\"{0}\":", kvp.Key));
|
---|
[187] | 43 | if (prettyPrint)
|
---|
[309] | 44 | stringBuilder.Append (" ");
|
---|
| 45 | kvp.Value.ToString (stringBuilder, prettyPrint, currentLevel + 1);
|
---|
| 46 | stringBuilder.Append (",");
|
---|
[187] | 47 | if (prettyPrint)
|
---|
[309] | 48 | stringBuilder.Append ('\n');
|
---|
[154] | 49 | }
|
---|
[309] | 50 | if (stringBuilder.Length > 1)
|
---|
| 51 | stringBuilder.Remove (stringBuilder.Length - (prettyPrint ? 2 : 1), 1);
|
---|
[187] | 52 | if (prettyPrint)
|
---|
[309] | 53 | stringBuilder.Append (new String ('\t', currentLevel));
|
---|
| 54 | stringBuilder.Append ("}");
|
---|
[154] | 55 | }
|
---|
| 56 |
|
---|
[187] | 57 | public static JSONObject Parse (string json, ref int offset)
|
---|
| 58 | {
|
---|
| 59 | //Log.Out ("ParseObject enter (" + offset + ")");
|
---|
| 60 | JSONObject obj = new JSONObject ();
|
---|
| 61 |
|
---|
| 62 | bool nextElemAllowed = true;
|
---|
| 63 | offset++;
|
---|
| 64 | while (true) {
|
---|
| 65 | Parser.SkipWhitespace (json, ref offset);
|
---|
| 66 | switch (json [offset]) {
|
---|
| 67 | case '"':
|
---|
| 68 | if (nextElemAllowed) {
|
---|
| 69 | JSONString key = JSONString.Parse (json, ref offset);
|
---|
| 70 | Parser.SkipWhitespace (json, ref offset);
|
---|
| 71 | if (json [offset] != ':') {
|
---|
| 72 | throw new MalformedJSONException ("Could not parse object, missing colon (\":\") after key");
|
---|
| 73 | }
|
---|
| 74 | offset++;
|
---|
| 75 | JSONNode val = Parser.ParseInternal (json, ref offset);
|
---|
| 76 | obj.Add (key.GetString (), val);
|
---|
| 77 | nextElemAllowed = false;
|
---|
| 78 | } else {
|
---|
| 79 | throw new MalformedJSONException ("Could not parse object, found new key without a separating comma");
|
---|
| 80 | }
|
---|
| 81 | break;
|
---|
| 82 | case ',':
|
---|
| 83 | if (!nextElemAllowed) {
|
---|
| 84 | nextElemAllowed = true;
|
---|
| 85 | offset++;
|
---|
| 86 | } else
|
---|
| 87 | throw new MalformedJSONException ("Could not parse object, found a comma without a key/value pair first");
|
---|
| 88 | break;
|
---|
| 89 | case '}':
|
---|
| 90 | offset++;
|
---|
| 91 | //Log.Out ("JSON:Parsed Object: " + obj.ToString ());
|
---|
| 92 | return obj;
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 | }
|
---|
| 96 |
|
---|
[154] | 97 | }
|
---|
| 98 | }
|
---|
| 99 |
|
---|