source: binary-improvements/7dtd-server-fixes/src/JSON/JSONArray.cs@ 224

Last change on this file since 224 was 187, checked in by alloc, 10 years ago

fixes

File size: 1.8 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace 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 string ToString (bool prettyPrint = false, int currentLevel = 0)
26 {
27 StringBuilder sb = new StringBuilder ("[");
28 if (prettyPrint)
29 sb.Append ('\n');
30 foreach (JSONNode n in nodes) {
31 if (prettyPrint)
32 sb.Append (new String ('\t', currentLevel + 1));
33 sb.Append (n.ToString (prettyPrint, currentLevel + 1));
34 sb.Append (",");
35 if (prettyPrint)
36 sb.Append ('\n');
37 }
38 if (sb.Length > 1)
39 sb.Remove (sb.Length - (prettyPrint ? 2 : 1), 1);
40 if (prettyPrint)
41 sb.Append (new String ('\t', currentLevel));
42 sb.Append ("]");
43 return sb.ToString ();
44 }
45
46 public static JSONArray Parse (string json, ref int offset)
47 {
48 //Log.Out ("ParseArray enter (" + offset + ")");
49 JSONArray arr = new JSONArray ();
50
51 bool nextElemAllowed = true;
52 offset++;
53 while (true) {
54 Parser.SkipWhitespace (json, ref offset);
55
56 switch (json [offset]) {
57 case ',':
58 if (!nextElemAllowed) {
59 nextElemAllowed = true;
60 offset++;
61 } else
62 throw new MalformedJSONException ("Could not parse array, found a comma without a value first");
63 break;
64 case ']':
65 offset++;
66 //Log.Out ("JSON:Parsed Array: " + arr.ToString ());
67 return arr;
68 default:
69 arr.Add (Parser.ParseInternal (json, ref offset));
70 nextElemAllowed = false;
71 break;
72 }
73 }
74 }
75
76 }
77}
78
Note: See TracBrowser for help on using the repository browser.