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