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