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

Last change on this file since 310 was 309, checked in by alloc, 7 years ago

Fixes 14_16_21

File size: 1.8 KB
RevLine 
[154]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
[187]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
[154]20 public void Add (JSONNode node)
21 {
[187]22 nodes.Add (node);
[154]23 }
24
[309]25 public override void ToString (StringBuilder stringBuilder, bool prettyPrint = false, int currentLevel = 0)
[154]26 {
[309]27 stringBuilder.Append ("[");
[187]28 if (prettyPrint)
[309]29 stringBuilder.Append ('\n');
[154]30 foreach (JSONNode n in nodes) {
[187]31 if (prettyPrint)
[309]32 stringBuilder.Append (new String ('\t', currentLevel + 1));
33 n.ToString (stringBuilder, prettyPrint, currentLevel + 1);
34 stringBuilder.Append (",");
[187]35 if (prettyPrint)
[309]36 stringBuilder.Append ('\n');
[154]37 }
[309]38 if (stringBuilder.Length > 1)
39 stringBuilder.Remove (stringBuilder.Length - (prettyPrint ? 2 : 1), 1);
[187]40 if (prettyPrint)
[309]41 stringBuilder.Append (new String ('\t', currentLevel));
42 stringBuilder.Append ("]");
[154]43 }
44
[187]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
[154]75 }
76}
77
Note: See TracBrowser for help on using the repository browser.