[154] | 1 | using System.Collections.Generic;
|
---|
| 2 | using System.Text;
|
---|
| 3 |
|
---|
[325] | 4 | namespace AllocsFixes.JSON {
|
---|
| 5 | public class JSONArray : JSONNode {
|
---|
| 6 | private readonly List<JSONNode> nodes = new List<JSONNode> ();
|
---|
[154] | 7 |
|
---|
[351] | 8 | public JSONNode this [int _index] {
|
---|
| 9 | get { return nodes [_index]; }
|
---|
| 10 | set { nodes [_index] = value; }
|
---|
[187] | 11 | }
|
---|
| 12 |
|
---|
| 13 | public int Count {
|
---|
| 14 | get { return nodes.Count; }
|
---|
| 15 | }
|
---|
| 16 |
|
---|
[351] | 17 | public void Add (JSONNode _node) {
|
---|
| 18 | nodes.Add (_node);
|
---|
[154] | 19 | }
|
---|
| 20 |
|
---|
[351] | 21 | public override void ToString (StringBuilder _stringBuilder, bool _prettyPrint = false, int _currentLevel = 0) {
|
---|
| 22 | _stringBuilder.Append ("[");
|
---|
| 23 | if (_prettyPrint) {
|
---|
| 24 | _stringBuilder.Append ('\n');
|
---|
[325] | 25 | }
|
---|
| 26 |
|
---|
[154] | 27 | foreach (JSONNode n in nodes) {
|
---|
[351] | 28 | if (_prettyPrint) {
|
---|
| 29 | _stringBuilder.Append (new string ('\t', _currentLevel + 1));
|
---|
[325] | 30 | }
|
---|
| 31 |
|
---|
[351] | 32 | n.ToString (_stringBuilder, _prettyPrint, _currentLevel + 1);
|
---|
| 33 | _stringBuilder.Append (",");
|
---|
| 34 | if (_prettyPrint) {
|
---|
| 35 | _stringBuilder.Append ('\n');
|
---|
[325] | 36 | }
|
---|
[154] | 37 | }
|
---|
[325] | 38 |
|
---|
| 39 | if (nodes.Count > 0) {
|
---|
[351] | 40 | _stringBuilder.Remove (_stringBuilder.Length - (_prettyPrint ? 2 : 1), 1);
|
---|
[325] | 41 | }
|
---|
| 42 |
|
---|
[351] | 43 | if (_prettyPrint) {
|
---|
| 44 | _stringBuilder.Append (new string ('\t', _currentLevel));
|
---|
[325] | 45 | }
|
---|
| 46 |
|
---|
[351] | 47 | _stringBuilder.Append ("]");
|
---|
[154] | 48 | }
|
---|
| 49 |
|
---|
[351] | 50 | public static JSONArray Parse (string _json, ref int _offset) {
|
---|
[187] | 51 | //Log.Out ("ParseArray enter (" + offset + ")");
|
---|
| 52 | JSONArray arr = new JSONArray ();
|
---|
| 53 |
|
---|
| 54 | bool nextElemAllowed = true;
|
---|
[351] | 55 | _offset++;
|
---|
[187] | 56 | while (true) {
|
---|
[351] | 57 | Parser.SkipWhitespace (_json, ref _offset);
|
---|
[187] | 58 |
|
---|
[351] | 59 | switch (_json [_offset]) {
|
---|
[187] | 60 | case ',':
|
---|
| 61 | if (!nextElemAllowed) {
|
---|
| 62 | nextElemAllowed = true;
|
---|
[351] | 63 | _offset++;
|
---|
[325] | 64 | } else {
|
---|
| 65 | throw new MalformedJSONException (
|
---|
| 66 | "Could not parse array, found a comma without a value first");
|
---|
| 67 | }
|
---|
| 68 |
|
---|
[187] | 69 | break;
|
---|
| 70 | case ']':
|
---|
[351] | 71 | _offset++;
|
---|
[325] | 72 |
|
---|
[187] | 73 | //Log.Out ("JSON:Parsed Array: " + arr.ToString ());
|
---|
| 74 | return arr;
|
---|
| 75 | default:
|
---|
[351] | 76 | arr.Add (Parser.ParseInternal (_json, ref _offset));
|
---|
[187] | 77 | nextElemAllowed = false;
|
---|
| 78 | break;
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
[154] | 82 | }
|
---|
[325] | 83 | }
|
---|