source: binary-improvements2/7dtd-server-fixes/src/JSON/JSONObject.cs@ 391

Last change on this file since 391 was 391, checked in by alloc, 2 years ago

Major refactoring/cleanup

File size: 2.8 KB
Line 
1using System.Collections.Generic;
2using System.Text;
3
4namespace AllocsFixes.JSON {
5 public class JsonObject : JsonNode {
6 private readonly Dictionary<string, JsonNode> nodes = new Dictionary<string, JsonNode> ();
7
8 public JsonNode this [string _name] {
9 get => nodes [_name];
10 set => nodes [_name] = value;
11 }
12
13 public int Count => nodes.Count;
14
15 public List<string> Keys => new List<string> (nodes.Keys);
16
17 public bool ContainsKey (string _name) {
18 return nodes.ContainsKey (_name);
19 }
20
21 public bool TryGetValue (string _name, out JsonNode _node) {
22 return nodes.TryGetValue (_name, out _node);
23 }
24
25 public void Add (string _name, JsonNode _node) {
26 nodes.Add (_name, _node);
27 }
28
29 public override void ToString (StringBuilder _stringBuilder, bool _prettyPrint = false, int _currentLevel = 0) {
30 _stringBuilder.Append ("{");
31 if (_prettyPrint) {
32 _stringBuilder.Append ('\n');
33 }
34
35 foreach ((string key, JsonNode value) in nodes) {
36 if (_prettyPrint) {
37 _stringBuilder.Append (new string ('\t', _currentLevel + 1));
38 }
39
40 _stringBuilder.Append ($"\"{key}\":");
41 if (_prettyPrint) {
42 _stringBuilder.Append (" ");
43 }
44
45 value.ToString (_stringBuilder, _prettyPrint, _currentLevel + 1);
46 _stringBuilder.Append (",");
47 if (_prettyPrint) {
48 _stringBuilder.Append ('\n');
49 }
50 }
51
52 if (nodes.Count > 0) {
53 _stringBuilder.Remove (_stringBuilder.Length - (_prettyPrint ? 2 : 1), 1);
54 }
55
56 if (_prettyPrint) {
57 _stringBuilder.Append (new string ('\t', _currentLevel));
58 }
59
60 _stringBuilder.Append ("}");
61 }
62
63 public static JsonObject Parse (string _json, ref int _offset) {
64 //Log.Out ("ParseObject enter (" + offset + ")");
65 JsonObject obj = new JsonObject ();
66
67 bool nextElemAllowed = true;
68 _offset++;
69 while (true) {
70 Parser.SkipWhitespace (_json, ref _offset);
71 switch (_json [_offset]) {
72 case '"':
73 if (nextElemAllowed) {
74 JsonString key = JsonString.Parse (_json, ref _offset);
75 Parser.SkipWhitespace (_json, ref _offset);
76 if (_json [_offset] != ':') {
77 throw new MalformedJsonException (
78 "Could not parse object, missing colon (\":\") after key");
79 }
80
81 _offset++;
82 JsonNode val = Parser.ParseInternal (_json, ref _offset);
83 obj.Add (key.GetString (), val);
84 nextElemAllowed = false;
85 } else {
86 throw new MalformedJsonException (
87 "Could not parse object, found new key without a separating comma");
88 }
89
90 break;
91 case ',':
92 if (!nextElemAllowed) {
93 nextElemAllowed = true;
94 _offset++;
95 } else {
96 throw new MalformedJsonException (
97 "Could not parse object, found a comma without a key/value pair first");
98 }
99
100 break;
101 case '}':
102 _offset++;
103
104 //Log.Out ("JSON:Parsed Object: " + obj.ToString ());
105 return obj;
106 }
107 }
108 }
109 }
110}
Note: See TracBrowser for help on using the repository browser.