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