- Timestamp:
- Sep 4, 2018, 1:00:48 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements/7dtd-server-fixes/src/JSON/JSONObject.cs
r315 r325 1 using System;2 1 using System.Collections.Generic; 3 2 using System.Text; 4 3 5 namespace AllocsFixes.JSON 6 { 7 public class JSONObject : JSONNode 8 { 9 private Dictionary<string, JSONNode> nodes = new Dictionary<string, JSONNode> (); 4 namespace AllocsFixes.JSON { 5 public class JSONObject : JSONNode { 6 private readonly Dictionary<string, JSONNode> nodes = new Dictionary<string, JSONNode> (); 10 7 11 8 public JSONNode this [string name] { … … 22 19 } 23 20 24 public bool ContainsKey (string name) 25 { 21 public bool ContainsKey (string name) { 26 22 return nodes.ContainsKey (name); 27 23 } 28 24 29 public void Add (string name, JSONNode node) 30 { 25 public void Add (string name, JSONNode node) { 31 26 nodes.Add (name, node); 32 27 } 33 28 34 public override void ToString (StringBuilder stringBuilder, bool prettyPrint = false, int currentLevel = 0) 35 { 29 public override void ToString (StringBuilder stringBuilder, bool prettyPrint = false, int currentLevel = 0) { 36 30 stringBuilder.Append ("{"); 37 if (prettyPrint) 31 if (prettyPrint) { 38 32 stringBuilder.Append ('\n'); 33 } 34 39 35 foreach (KeyValuePair<string, JSONNode> kvp in nodes) { 40 if (prettyPrint) 41 stringBuilder.Append (new String ('\t', currentLevel + 1)); 42 stringBuilder.Append (String.Format ("\"{0}\":", kvp.Key)); 43 if (prettyPrint) 36 if (prettyPrint) { 37 stringBuilder.Append (new string ('\t', currentLevel + 1)); 38 } 39 40 stringBuilder.Append (string.Format ("\"{0}\":", kvp.Key)); 41 if (prettyPrint) { 44 42 stringBuilder.Append (" "); 43 } 44 45 45 kvp.Value.ToString (stringBuilder, prettyPrint, currentLevel + 1); 46 46 stringBuilder.Append (","); 47 if (prettyPrint) 47 if (prettyPrint) { 48 48 stringBuilder.Append ('\n'); 49 } 49 50 } 50 if (nodes.Count > 0) 51 52 if (nodes.Count > 0) { 51 53 stringBuilder.Remove (stringBuilder.Length - (prettyPrint ? 2 : 1), 1); 52 if (prettyPrint) 53 stringBuilder.Append (new String ('\t', currentLevel)); 54 } 55 56 if (prettyPrint) { 57 stringBuilder.Append (new string ('\t', currentLevel)); 58 } 59 54 60 stringBuilder.Append ("}"); 55 61 } 56 62 57 public static JSONObject Parse (string json, ref int offset) 58 { 63 public static JSONObject Parse (string json, ref int offset) { 59 64 //Log.Out ("ParseObject enter (" + offset + ")"); 60 65 JSONObject obj = new JSONObject (); … … 70 75 Parser.SkipWhitespace (json, ref offset); 71 76 if (json [offset] != ':') { 72 throw new MalformedJSONException ("Could not parse object, missing colon (\":\") after key"); 77 throw new MalformedJSONException ( 78 "Could not parse object, missing colon (\":\") after key"); 73 79 } 80 74 81 offset++; 75 82 JSONNode val = Parser.ParseInternal (json, ref offset); … … 77 84 nextElemAllowed = false; 78 85 } else { 79 throw new MalformedJSONException ("Could not parse object, found new key without a separating comma"); 86 throw new MalformedJSONException ( 87 "Could not parse object, found new key without a separating comma"); 80 88 } 89 81 90 break; 82 91 case ',': … … 84 93 nextElemAllowed = true; 85 94 offset++; 86 } else 87 throw new MalformedJSONException ("Could not parse object, found a comma without a key/value pair first"); 95 } else { 96 throw new MalformedJSONException ( 97 "Could not parse object, found a comma without a key/value pair first"); 98 } 99 88 100 break; 89 101 case '}': 90 102 offset++; 103 91 104 //Log.Out ("JSON:Parsed Object: " + obj.ToString ()); 92 105 return obj; … … 94 107 } 95 108 } 96 97 109 } 98 110 } 99
Note:
See TracChangeset
for help on using the changeset viewer.