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

Last change on this file since 427 was 351, checked in by alloc, 6 years ago

Fixed game version compatibility of GamePrefs
Code style cleanup (mostly argument names)

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.