Changeset 325 for binary-improvements/7dtd-server-fixes/src/JSON
- Timestamp:
- Sep 4, 2018, 1:00:48 PM (6 years ago)
- Location:
- binary-improvements/7dtd-server-fixes/src/JSON
- Files:
-
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements/7dtd-server-fixes/src/JSON/JSONArray.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 JSONArray : JSONNode 8 { 9 private List<JSONNode> nodes = new List<JSONNode> (); 4 namespace AllocsFixes.JSON { 5 public class JSONArray : JSONNode { 6 private readonly List<JSONNode> nodes = new List<JSONNode> (); 10 7 11 8 public JSONNode this [int index] { … … 18 15 } 19 16 20 public void Add (JSONNode node) 21 { 17 public void Add (JSONNode node) { 22 18 nodes.Add (node); 23 19 } 24 20 25 public override void ToString (StringBuilder stringBuilder, bool prettyPrint = false, int currentLevel = 0) 26 { 21 public override void ToString (StringBuilder stringBuilder, bool prettyPrint = false, int currentLevel = 0) { 27 22 stringBuilder.Append ("["); 28 if (prettyPrint) 23 if (prettyPrint) { 29 24 stringBuilder.Append ('\n'); 25 } 26 30 27 foreach (JSONNode n in nodes) { 31 if (prettyPrint) 32 stringBuilder.Append (new String ('\t', currentLevel + 1)); 28 if (prettyPrint) { 29 stringBuilder.Append (new string ('\t', currentLevel + 1)); 30 } 31 33 32 n.ToString (stringBuilder, prettyPrint, currentLevel + 1); 34 33 stringBuilder.Append (","); 35 if (prettyPrint) 34 if (prettyPrint) { 36 35 stringBuilder.Append ('\n'); 36 } 37 37 } 38 if (nodes.Count > 0) 38 39 if (nodes.Count > 0) { 39 40 stringBuilder.Remove (stringBuilder.Length - (prettyPrint ? 2 : 1), 1); 40 if (prettyPrint) 41 stringBuilder.Append (new String ('\t', currentLevel)); 41 } 42 43 if (prettyPrint) { 44 stringBuilder.Append (new string ('\t', currentLevel)); 45 } 46 42 47 stringBuilder.Append ("]"); 43 48 } 44 49 45 public static JSONArray Parse (string json, ref int offset) 46 { 50 public static JSONArray Parse (string json, ref int offset) { 47 51 //Log.Out ("ParseArray enter (" + offset + ")"); 48 52 JSONArray arr = new JSONArray (); … … 58 62 nextElemAllowed = true; 59 63 offset++; 60 } else 61 throw new MalformedJSONException ("Could not parse array, found a comma without a value first"); 64 } else { 65 throw new MalformedJSONException ( 66 "Could not parse array, found a comma without a value first"); 67 } 68 62 69 break; 63 70 case ']': 64 71 offset++; 72 65 73 //Log.Out ("JSON:Parsed Array: " + arr.ToString ()); 66 74 return arr; … … 72 80 } 73 81 } 74 75 82 } 76 83 } 77 -
binary-improvements/7dtd-server-fixes/src/JSON/JSONBoolean.cs
r309 r325 1 using System;2 1 using System.Text; 3 2 4 namespace AllocsFixes.JSON 5 { 6 public class JSONBoolean : JSONValue 7 { 8 private bool value; 3 namespace AllocsFixes.JSON { 4 public class JSONBoolean : JSONValue { 5 private readonly bool value; 9 6 10 public JSONBoolean (bool value) 11 { 7 public JSONBoolean (bool value) { 12 8 this.value = value; 13 9 } 14 10 15 public bool GetBool () 16 { 11 public bool GetBool () { 17 12 return value; 18 13 } 19 14 20 public override void ToString (StringBuilder stringBuilder, bool prettyPrint = false, int currentLevel = 0) 21 { 15 public override void ToString (StringBuilder stringBuilder, bool prettyPrint = false, int currentLevel = 0) { 22 16 stringBuilder.Append (value ? "true" : "false"); 23 17 } 24 18 25 public static JSONBoolean Parse (string json, ref int offset) 26 { 19 public static JSONBoolean Parse (string json, ref int offset) { 27 20 //Log.Out ("ParseBool enter (" + offset + ")"); 28 21 … … 31 24 offset += 4; 32 25 return new JSONBoolean (true); 33 } else if (json.Substring (offset, 5).Equals ("false")) { 26 } 27 28 if (json.Substring (offset, 5).Equals ("false")) { 34 29 //Log.Out ("JSON:Parsed Bool: false"); 35 30 offset += 5; 36 31 return new JSONBoolean (false); 37 } else {38 throw new MalformedJSONException ("No valid boolean found");39 32 } 33 34 throw new MalformedJSONException ("No valid boolean found"); 40 35 } 41 42 36 } 43 37 } 44 -
binary-improvements/7dtd-server-fixes/src/JSON/JSONNode.cs
r309 r325 1 using System;2 1 using System.Text; 3 2 4 namespace AllocsFixes.JSON 5 { 6 public abstract class JSONNode 7 { 8 public abstract void ToString(StringBuilder stringBuilder, bool prettyPrint = false, int currentLevel = 0); 3 namespace AllocsFixes.JSON { 4 public abstract class JSONNode { 5 public abstract void ToString (StringBuilder stringBuilder, bool prettyPrint = false, int currentLevel = 0); 9 6 10 7 public override string ToString () { -
binary-improvements/7dtd-server-fixes/src/JSON/JSONNull.cs
r309 r325 1 using System;2 1 using System.Text; 3 2 4 namespace AllocsFixes.JSON 5 { 6 public class JSONNull : JSONValue 7 { 8 public JSONNull () 9 { 10 } 11 12 public override void ToString (StringBuilder stringBuilder, bool prettyPrint = false, int currentLevel = 0) 13 { 3 namespace AllocsFixes.JSON { 4 public class JSONNull : JSONValue { 5 public override void ToString (StringBuilder stringBuilder, bool prettyPrint = false, int currentLevel = 0) { 14 6 stringBuilder.Append ("null"); 15 7 } 16 8 17 public static JSONNull Parse (string json, ref int offset) 18 { 9 public static JSONNull Parse (string json, ref int offset) { 19 10 //Log.Out ("ParseNull enter (" + offset + ")"); 20 11 … … 23 14 offset += 4; 24 15 return new JSONNull (); 25 } else {26 throw new MalformedJSONException ("No valid null value found");27 16 } 17 18 throw new MalformedJSONException ("No valid null value found"); 28 19 } 29 30 20 } 31 21 } 32 -
binary-improvements/7dtd-server-fixes/src/JSON/JSONNumber.cs
r324 r325 2 2 using System.Text; 3 3 4 namespace AllocsFixes.JSON 5 { 6 public class JSONNumber : JSONValue 7 { 8 private double value; 4 namespace AllocsFixes.JSON { 5 public class JSONNumber : JSONValue { 6 private readonly double value; 9 7 10 public JSONNumber (double value) 11 { 8 public JSONNumber (double value) { 12 9 this.value = value; 13 10 } 14 11 15 public double GetDouble () 16 { 12 public double GetDouble () { 17 13 return value; 18 14 } 19 15 20 public int GetInt () 21 { 22 return (int)Math.Round(value); 16 public int GetInt () { 17 return (int) Math.Round (value); 23 18 } 24 19 25 public override void ToString (StringBuilder stringBuilder, bool prettyPrint = false, int currentLevel = 0) 26 { 20 public override void ToString (StringBuilder stringBuilder, bool prettyPrint = false, int currentLevel = 0) { 27 21 stringBuilder.Append (value.ToCultureInvariantString ()); 28 22 } 29 23 30 public static JSONNumber Parse (string json, ref int offset) 31 { 24 public static JSONNumber Parse (string json, ref int offset) { 32 25 //Log.Out ("ParseNumber enter (" + offset + ")"); 33 26 StringBuilder sbNum = new StringBuilder (); … … 37 30 while (offset < json.Length) { 38 31 if (json [offset] >= '0' && json [offset] <= '9') { 39 if (hasExp) 32 if (hasExp) { 40 33 sbExp.Append (json [offset]); 41 else34 } else { 42 35 sbNum.Append (json [offset]); 36 } 43 37 } else if (json [offset] == '.') { 44 38 if (hasExp) { 45 39 throw new MalformedJSONException ("Decimal separator in exponent"); 40 } 41 42 if (hasDec) { 43 throw new MalformedJSONException ("Multiple decimal separators in number found"); 44 } 45 46 if (sbNum.Length == 0) { 47 throw new MalformedJSONException ("No leading digits before decimal separator found"); 48 } 49 50 sbNum.Append ('.'); 51 hasDec = true; 52 } else if (json [offset] == '-') { 53 if (hasExp) { 54 if (sbExp.Length > 0) { 55 throw new MalformedJSONException ("Negative sign in exponent after digits"); 56 } 57 58 sbExp.Append (json [offset]); 46 59 } else { 47 if (hasDec) 48 throw new MalformedJSONException ("Multiple decimal separators in number found"); 49 else if (sbNum.Length == 0) { 50 throw new MalformedJSONException ("No leading digits before decimal separator found"); 51 } else { 52 sbNum.Append ('.'); 53 hasDec = true; 60 if (sbNum.Length > 0) { 61 throw new MalformedJSONException ("Negative sign in mantissa after digits"); 54 62 } 55 } 56 } else if (json [offset] == '-') { 57 if (hasExp) { 58 if (sbExp.Length > 0) 59 throw new MalformedJSONException ("Negative sign in exponent after digits"); 60 else 61 sbExp.Append (json [offset]); 62 } else { 63 if (sbNum.Length > 0) 64 throw new MalformedJSONException ("Negative sign in mantissa after digits"); 65 else 66 sbNum.Append (json [offset]); 63 64 sbNum.Append (json [offset]); 67 65 } 68 66 } else if (json [offset] == 'e' || json [offset] == 'E') { 69 if (hasExp) 67 if (hasExp) { 70 68 throw new MalformedJSONException ("Multiple exponential markers in number found"); 71 else if (sbNum.Length == 0) { 69 } 70 71 if (sbNum.Length == 0) { 72 72 throw new MalformedJSONException ("No leading digits before exponential marker found"); 73 } else {74 sbExp = new StringBuilder ();75 hasExp = true;76 73 } 74 75 sbExp = new StringBuilder (); 76 hasExp = true; 77 77 } else if (json [offset] == '+') { 78 78 if (hasExp) { 79 if (sbExp.Length > 0) 79 if (sbExp.Length > 0) { 80 80 throw new MalformedJSONException ("Positive sign in exponent after digits"); 81 else 82 sbExp.Append (json [offset]); 81 } 82 83 sbExp.Append (json [offset]); 83 84 } else { 84 85 throw new MalformedJSONException ("Positive sign in mantissa found"); … … 86 87 } else { 87 88 double number; 88 if (!StringParsers.TryParseDouble (sbNum.ToString (), out number)) {89 throw new MalformedJSONException ("Mantissa is not a valid decimal (\"" + sbNum .ToString ()+ "\")");89 if (!StringParsers.TryParseDouble (sbNum.ToString (), out number)) { 90 throw new MalformedJSONException ("Mantissa is not a valid decimal (\"" + sbNum + "\")"); 90 91 } 91 92 … … 93 94 int exp; 94 95 if (!int.TryParse (sbExp.ToString (), out exp)) { 95 throw new MalformedJSONException ("Exponent is not a valid integer (\"" + sbExp .ToString ()+ "\")");96 throw new MalformedJSONException ("Exponent is not a valid integer (\"" + sbExp + "\")"); 96 97 } 97 98 … … 102 103 return new JSONNumber (number); 103 104 } 105 104 106 offset++; 105 107 } 108 106 109 throw new MalformedJSONException ("End of JSON reached before parsing number finished"); 107 110 } 108 109 111 } 110 112 } 111 -
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 -
binary-improvements/7dtd-server-fixes/src/JSON/JSONString.cs
r309 r325 1 using System;2 1 using System.Text; 3 2 4 namespace AllocsFixes.JSON 5 { 6 public class JSONString : JSONValue 7 { 8 private string value; 3 namespace AllocsFixes.JSON { 4 public class JSONString : JSONValue { 5 private readonly string value; 9 6 10 public JSONString (string value) 11 { 7 public JSONString (string value) { 12 8 this.value = value; 13 9 } 14 10 15 public string GetString () 16 { 11 public string GetString () { 17 12 return value; 18 13 } 19 14 20 public override void ToString (StringBuilder stringBuilder, bool prettyPrint = false, int currentLevel = 0) 21 { 15 public override void ToString (StringBuilder stringBuilder, bool prettyPrint = false, int currentLevel = 0) { 22 16 if (value == null || value.Length == 0) { 23 17 stringBuilder.Append ("\"\""); … … 27 21 int len = value.Length; 28 22 29 stringBuilder.EnsureCapacity (stringBuilder.Length + 2*len); 30 String t; 23 stringBuilder.EnsureCapacity (stringBuilder.Length + 2 * len); 31 24 32 25 stringBuilder.Append ('"'); … … 36 29 case '\\': 37 30 case '"': 31 38 32 // case '/': 39 33 stringBuilder.Append ('\\'); … … 58 52 if (c < ' ') { 59 53 stringBuilder.Append ("\\u"); 60 stringBuilder.Append (((int) c).ToString ("X4"));54 stringBuilder.Append (((int) c).ToString ("X4")); 61 55 } else { 62 56 stringBuilder.Append (c); 63 57 } 58 64 59 break; 65 60 } … … 69 64 } 70 65 71 public static JSONString Parse (string json, ref int offset) 72 { 66 public static JSONString Parse (string json, ref int offset) { 73 67 //Log.Out ("ParseString enter (" + offset + ")"); 74 68 StringBuilder sb = new StringBuilder (); … … 103 97 break; 104 98 } 99 105 100 offset++; 106 101 break; 107 102 case '"': 108 103 offset++; 104 109 105 //Log.Out ("JSON:Parsed String: " + sb.ToString ()); 110 106 return new JSONString (sb.ToString ()); … … 115 111 } 116 112 } 113 117 114 throw new MalformedJSONException ("End of JSON reached before parsing string finished"); 118 115 } 119 120 116 } 121 117 } 122 -
binary-improvements/7dtd-server-fixes/src/JSON/JSONValue.cs
r279 r325 1 using System; 2 3 namespace AllocsFixes.JSON 4 { 1 namespace AllocsFixes.JSON { 5 2 public abstract class JSONValue : JSONNode { 6 3 } 7 4 } 8 -
binary-improvements/7dtd-server-fixes/src/JSON/MalformedJSONException.cs
r188 r325 2 2 using System.Runtime.Serialization; 3 3 4 namespace AllocsFixes.JSON 5 { 6 public class MalformedJSONException : ApplicationException 7 { 8 public MalformedJSONException () 9 { 4 namespace AllocsFixes.JSON { 5 public class MalformedJSONException : ApplicationException { 6 public MalformedJSONException () { 10 7 } 11 8 12 public MalformedJSONException (string message) : base(message) 13 { 9 public MalformedJSONException (string message) : base (message) { 14 10 } 15 11 16 public MalformedJSONException (string message, System.Exception inner) : base(message, inner) 17 { 12 public MalformedJSONException (string message, Exception inner) : base (message, inner) { 18 13 } 19 20 protected MalformedJSONException (SerializationInfo info, StreamingContext context) : base(info, context) 21 { 14 15 protected MalformedJSONException (SerializationInfo info, StreamingContext context) : base (info, context) { 22 16 } 23 17 } 24 18 } 25 -
binary-improvements/7dtd-server-fixes/src/JSON/Parser.cs
r188 r325 1 using System; 2 using System.Text; 3 4 namespace AllocsFixes.JSON 5 { 6 public class Parser 7 { 8 9 public static JSONNode Parse (string json) 10 { 1 namespace AllocsFixes.JSON { 2 public class Parser { 3 public static JSONNode Parse (string json) { 11 4 int offset = 0; 12 5 return ParseInternal (json, ref offset); 13 6 } 14 7 15 public static JSONNode ParseInternal (string json, ref int offset) 16 { 8 public static JSONNode ParseInternal (string json, ref int offset) { 17 9 SkipWhitespace (json, ref offset); 10 18 11 //Log.Out ("ParseInternal (" + offset + "): Decide on: '" + json [offset] + "'"); 19 12 switch (json [offset]) { … … 34 27 } 35 28 36 public static void SkipWhitespace (string json, ref int offset) 37 { 29 public static void SkipWhitespace (string json, ref int offset) { 38 30 //Log.Out ("SkipWhitespace (" + offset + "): '" + json [offset] + "'"); 39 31 while (offset < json.Length) { … … 49 41 } 50 42 } 43 51 44 throw new MalformedJSONException ("End of JSON reached before parsing finished"); 52 45 } 53 54 55 46 } 56 47 } 57
Note:
See TracChangeset
for help on using the changeset viewer.