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

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

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

File size: 1.9 KB
RevLine 
[154]1using System.Collections.Generic;
2using System.Text;
3
[325]4namespace AllocsFixes.JSON {
5 public class JSONArray : JSONNode {
6 private readonly List<JSONNode> nodes = new List<JSONNode> ();
[154]7
[187]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
[325]17 public void Add (JSONNode node) {
[187]18 nodes.Add (node);
[154]19 }
20
[325]21 public override void ToString (StringBuilder stringBuilder, bool prettyPrint = false, int currentLevel = 0) {
[309]22 stringBuilder.Append ("[");
[325]23 if (prettyPrint) {
[309]24 stringBuilder.Append ('\n');
[325]25 }
26
[154]27 foreach (JSONNode n in nodes) {
[325]28 if (prettyPrint) {
29 stringBuilder.Append (new string ('\t', currentLevel + 1));
30 }
31
[309]32 n.ToString (stringBuilder, prettyPrint, currentLevel + 1);
33 stringBuilder.Append (",");
[325]34 if (prettyPrint) {
[309]35 stringBuilder.Append ('\n');
[325]36 }
[154]37 }
[325]38
39 if (nodes.Count > 0) {
[309]40 stringBuilder.Remove (stringBuilder.Length - (prettyPrint ? 2 : 1), 1);
[325]41 }
42
43 if (prettyPrint) {
44 stringBuilder.Append (new string ('\t', currentLevel));
45 }
46
[309]47 stringBuilder.Append ("]");
[154]48 }
49
[325]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;
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++;
[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 ']':
71 offset++;
[325]72
[187]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 }
[154]82 }
[325]83}
Note: See TracBrowser for help on using the repository browser.