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

Last change on this file since 345 was 325, checked in by alloc, 6 years ago

Code style cleanup (mostly whitespace changes, enforcing braces, using cleanup)

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