Changeset 391 for binary-improvements2/7dtd-server-fixes/src/JSON
- Timestamp:
- Aug 7, 2022, 3:02:24 PM (2 years ago)
- Location:
- binary-improvements2/7dtd-server-fixes/src/JSON
- Files:
-
- 11 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements2/7dtd-server-fixes/src/JSON/JSONArray.cs
r351 r391 3 3 4 4 namespace AllocsFixes.JSON { 5 public class J SONArray : JSONNode {6 private readonly List<J SONNode> nodes = new List<JSONNode> ();5 public class JsonArray : JsonNode { 6 private readonly List<JsonNode> nodes = new List<JsonNode> (); 7 7 8 public J SONNode this [int _index] {9 get { return nodes [_index]; }10 set { nodes [_index] = value; }8 public JsonNode this [int _index] { 9 get => nodes [_index]; 10 set => nodes [_index] = value; 11 11 } 12 12 13 public int Count { 14 get { return nodes.Count; } 15 } 13 public int Count => nodes.Count; 16 14 17 public void Add (J SONNode _node) {15 public void Add (JsonNode _node) { 18 16 nodes.Add (_node); 19 17 } … … 25 23 } 26 24 27 foreach (J SONNode n in nodes) {25 foreach (JsonNode n in nodes) { 28 26 if (_prettyPrint) { 29 27 _stringBuilder.Append (new string ('\t', _currentLevel + 1)); … … 48 46 } 49 47 50 public static J SONArray Parse (string _json, ref int _offset) {48 public static JsonArray Parse (string _json, ref int _offset) { 51 49 //Log.Out ("ParseArray enter (" + offset + ")"); 52 J SONArray arr = new JSONArray ();50 JsonArray arr = new JsonArray (); 53 51 54 52 bool nextElemAllowed = true; … … 63 61 _offset++; 64 62 } else { 65 throw new MalformedJ SONException (63 throw new MalformedJsonException ( 66 64 "Could not parse array, found a comma without a value first"); 67 65 } -
binary-improvements2/7dtd-server-fixes/src/JSON/JSONBoolean.cs
r389 r391 2 2 3 3 namespace AllocsFixes.JSON { 4 public class J SONBoolean : JSONValue {4 public class JsonBoolean : JsonValue { 5 5 private readonly bool value; 6 6 7 public J SONBoolean (bool _value) {7 public JsonBoolean (bool _value) { 8 8 value = _value; 9 9 } … … 17 17 } 18 18 19 public static J SONBoolean Parse (string _json, ref int _offset) {19 public static JsonBoolean Parse (string _json, ref int _offset) { 20 20 //Log.Out ("ParseBool enter (" + offset + ")"); 21 21 … … 23 23 //Log.Out ("JSON:Parsed Bool: true"); 24 24 _offset += 4; 25 return new J SONBoolean (true);25 return new JsonBoolean (true); 26 26 } 27 27 … … 29 29 //Log.Out ("JSON:Parsed Bool: false"); 30 30 _offset += 5; 31 return new J SONBoolean (false);31 return new JsonBoolean (false); 32 32 } 33 33 34 throw new MalformedJ SONException ("No valid boolean found");34 throw new MalformedJsonException ("No valid boolean found"); 35 35 } 36 36 -
binary-improvements2/7dtd-server-fixes/src/JSON/JSONNode.cs
r351 r391 2 2 3 3 namespace AllocsFixes.JSON { 4 public abstract class J SONNode {4 public abstract class JsonNode { 5 5 public abstract void ToString (StringBuilder _stringBuilder, bool _prettyPrint = false, int _currentLevel = 0); 6 6 -
binary-improvements2/7dtd-server-fixes/src/JSON/JSONNull.cs
r389 r391 3 3 4 4 namespace AllocsFixes.JSON { 5 public class J SONNull : JSONValue {5 public class JsonNull : JsonValue { 6 6 public override void ToString (StringBuilder _stringBuilder, bool _prettyPrint = false, int _currentLevel = 0) { 7 7 _stringBuilder.Append ("null"); 8 8 } 9 9 10 public static J SONNull Parse (string _json, ref int _offset) {10 public static JsonNull Parse (string _json, ref int _offset) { 11 11 //Log.Out ("ParseNull enter (" + offset + ")"); 12 12 13 13 if (!_json.Substring (_offset, 4).Equals ("null")) { 14 throw new MalformedJ SONException ("No valid null value found");14 throw new MalformedJsonException ("No valid null value found"); 15 15 } 16 16 17 17 //Log.Out ("JSON:Parsed Null"); 18 18 _offset += 4; 19 return new J SONNull ();19 return new JsonNull (); 20 20 } 21 21 -
binary-improvements2/7dtd-server-fixes/src/JSON/JSONNumber.cs
r389 r391 3 3 4 4 namespace AllocsFixes.JSON { 5 public class J SONNumber : JSONValue {5 public class JsonNumber : JsonValue { 6 6 private readonly double value; 7 7 8 public J SONNumber (double _value) {8 public JsonNumber (double _value) { 9 9 value = _value; 10 10 } … … 22 22 } 23 23 24 public static J SONNumber Parse (string _json, ref int _offset) {24 public static JsonNumber Parse (string _json, ref int _offset) { 25 25 //Log.Out ("ParseNumber enter (" + offset + ")"); 26 26 StringBuilder sbNum = new StringBuilder (); … … 37 37 } else if (_json [_offset] == '.') { 38 38 if (hasExp) { 39 throw new MalformedJ SONException ("Decimal separator in exponent");39 throw new MalformedJsonException ("Decimal separator in exponent"); 40 40 } 41 41 42 42 if (hasDec) { 43 throw new MalformedJ SONException ("Multiple decimal separators in number found");43 throw new MalformedJsonException ("Multiple decimal separators in number found"); 44 44 } 45 45 46 46 if (sbNum.Length == 0) { 47 throw new MalformedJ SONException ("No leading digits before decimal separator found");47 throw new MalformedJsonException ("No leading digits before decimal separator found"); 48 48 } 49 49 … … 53 53 if (hasExp) { 54 54 if (sbExp.Length > 0) { 55 throw new MalformedJ SONException ("Negative sign in exponent after digits");55 throw new MalformedJsonException ("Negative sign in exponent after digits"); 56 56 } 57 57 … … 59 59 } else { 60 60 if (sbNum.Length > 0) { 61 throw new MalformedJ SONException ("Negative sign in mantissa after digits");61 throw new MalformedJsonException ("Negative sign in mantissa after digits"); 62 62 } 63 63 … … 66 66 } else if (_json [_offset] == 'e' || _json [_offset] == 'E') { 67 67 if (hasExp) { 68 throw new MalformedJ SONException ("Multiple exponential markers in number found");68 throw new MalformedJsonException ("Multiple exponential markers in number found"); 69 69 } 70 70 71 71 if (sbNum.Length == 0) { 72 throw new MalformedJ SONException ("No leading digits before exponential marker found");72 throw new MalformedJsonException ("No leading digits before exponential marker found"); 73 73 } 74 74 … … 78 78 if (hasExp) { 79 79 if (sbExp.Length > 0) { 80 throw new MalformedJ SONException ("Positive sign in exponent after digits");80 throw new MalformedJsonException ("Positive sign in exponent after digits"); 81 81 } 82 82 83 83 sbExp.Append (_json [_offset]); 84 84 } else { 85 throw new MalformedJ SONException ("Positive sign in mantissa found");85 throw new MalformedJsonException ("Positive sign in mantissa found"); 86 86 } 87 87 } else { 88 double number; 89 if (!StringParsers.TryParseDouble (sbNum.ToString (), out number)) { 90 throw new MalformedJSONException ("Mantissa is not a valid decimal (\"" + sbNum + "\")"); 88 if (!StringParsers.TryParseDouble (sbNum.ToString (), out double number)) { 89 throw new MalformedJsonException ("Mantissa is not a valid decimal (\"" + sbNum + "\")"); 91 90 } 92 91 93 92 if (hasExp) { 94 int exp; 95 if (!int.TryParse (sbExp.ToString (), out exp)) { 96 throw new MalformedJSONException ("Exponent is not a valid integer (\"" + sbExp + "\")"); 93 if (!int.TryParse (sbExp.ToString (), out int exp)) { 94 throw new MalformedJsonException ("Exponent is not a valid integer (\"" + sbExp + "\")"); 97 95 } 98 96 99 number = number *Math.Pow (10, exp);97 number *= Math.Pow (10, exp); 100 98 } 101 99 102 100 //Log.Out ("JSON:Parsed Number: " + number.ToString ()); 103 return new J SONNumber (number);101 return new JsonNumber (number); 104 102 } 105 103 … … 107 105 } 108 106 109 throw new MalformedJ SONException ("End of JSON reached before parsing number finished");107 throw new MalformedJsonException ("End of JSON reached before parsing number finished"); 110 108 } 111 109 -
binary-improvements2/7dtd-server-fixes/src/JSON/JSONObject.cs
r389 r391 3 3 4 4 namespace AllocsFixes.JSON { 5 public class J SONObject : JSONNode {6 private readonly Dictionary<string, J SONNode> nodes = new Dictionary<string, JSONNode> ();5 public class JsonObject : JsonNode { 6 private readonly Dictionary<string, JsonNode> nodes = new Dictionary<string, JsonNode> (); 7 7 8 public J SONNode this [string _name] {8 public JsonNode this [string _name] { 9 9 get => nodes [_name]; 10 10 set => nodes [_name] = value; … … 19 19 } 20 20 21 public bool TryGetValue (string _name, out J SONNode _node) {21 public bool TryGetValue (string _name, out JsonNode _node) { 22 22 return nodes.TryGetValue (_name, out _node); 23 23 } 24 24 25 public void Add (string _name, J SONNode _node) {25 public void Add (string _name, JsonNode _node) { 26 26 nodes.Add (_name, _node); 27 27 } … … 33 33 } 34 34 35 foreach ( KeyValuePair<string, JSONNode> kvpin nodes) {35 foreach ((string key, JsonNode value) in nodes) { 36 36 if (_prettyPrint) { 37 37 _stringBuilder.Append (new string ('\t', _currentLevel + 1)); 38 38 } 39 39 40 _stringBuilder.Append ( string.Format ("\"{0}\":", kvp.Key));40 _stringBuilder.Append ($"\"{key}\":"); 41 41 if (_prettyPrint) { 42 42 _stringBuilder.Append (" "); 43 43 } 44 44 45 kvp.Value.ToString (_stringBuilder, _prettyPrint, _currentLevel + 1);45 value.ToString (_stringBuilder, _prettyPrint, _currentLevel + 1); 46 46 _stringBuilder.Append (","); 47 47 if (_prettyPrint) { … … 61 61 } 62 62 63 public static J SONObject Parse (string _json, ref int _offset) {63 public static JsonObject Parse (string _json, ref int _offset) { 64 64 //Log.Out ("ParseObject enter (" + offset + ")"); 65 J SONObject obj = new JSONObject ();65 JsonObject obj = new JsonObject (); 66 66 67 67 bool nextElemAllowed = true; … … 72 72 case '"': 73 73 if (nextElemAllowed) { 74 J SONString key = JSONString.Parse (_json, ref _offset);74 JsonString key = JsonString.Parse (_json, ref _offset); 75 75 Parser.SkipWhitespace (_json, ref _offset); 76 76 if (_json [_offset] != ':') { 77 throw new MalformedJ SONException (77 throw new MalformedJsonException ( 78 78 "Could not parse object, missing colon (\":\") after key"); 79 79 } 80 80 81 81 _offset++; 82 J SONNode val = Parser.ParseInternal (_json, ref _offset);82 JsonNode val = Parser.ParseInternal (_json, ref _offset); 83 83 obj.Add (key.GetString (), val); 84 84 nextElemAllowed = false; 85 85 } else { 86 throw new MalformedJ SONException (86 throw new MalformedJsonException ( 87 87 "Could not parse object, found new key without a separating comma"); 88 88 } … … 94 94 _offset++; 95 95 } else { 96 throw new MalformedJ SONException (96 throw new MalformedJsonException ( 97 97 "Could not parse object, found a comma without a key/value pair first"); 98 98 } -
binary-improvements2/7dtd-server-fixes/src/JSON/JSONString.cs
r389 r391 2 2 3 3 namespace AllocsFixes.JSON { 4 public class J SONString : JSONValue {4 public class JsonString : JsonValue { 5 5 private readonly string value; 6 6 7 public J SONString (string _value) {7 public JsonString (string _value) { 8 8 value = _value; 9 9 } … … 14 14 15 15 public override void ToString (StringBuilder _stringBuilder, bool _prettyPrint = false, int _currentLevel = 0) { 16 if ( value == null || value.Length == 0) {16 if (string.IsNullOrEmpty (value)) { 17 17 _stringBuilder.Append ("\"\""); 18 18 return; … … 64 64 } 65 65 66 public static J SONString Parse (string _json, ref int _offset) {66 public static JsonString Parse (string _json, ref int _offset) { 67 67 //Log.Out ("ParseString enter (" + offset + ")"); 68 68 StringBuilder sb = new StringBuilder (); … … 104 104 105 105 //Log.Out ("JSON:Parsed String: " + sb.ToString ()); 106 return new J SONString (sb.ToString ());106 return new JsonString (sb.ToString ()); 107 107 default: 108 108 sb.Append (_json [_offset]); … … 112 112 } 113 113 114 throw new MalformedJ SONException ("End of JSON reached before parsing string finished");114 throw new MalformedJsonException ("End of JSON reached before parsing string finished"); 115 115 } 116 116 -
binary-improvements2/7dtd-server-fixes/src/JSON/JSONValue.cs
r389 r391 1 1 namespace AllocsFixes.JSON { 2 public abstract class J SONValue : JSONNode {2 public abstract class JsonValue : JsonNode { 3 3 public abstract string AsString { get; } 4 4 public abstract int AsInt { get; } -
binary-improvements2/7dtd-server-fixes/src/JSON/JsonManualBuilder.cs
r354 r391 9 9 NonEmpty = 1, 10 10 Object = 2, 11 Array = 4 ,11 Array = 4 12 12 } 13 13 … … 20 20 private int currentLevelNumber; 21 21 22 private ELevelInfo CurrentLevelInfo { 23 get { return (ELevelInfo) (currentLevelType & levelBitsMask); } 24 } 25 26 private bool CurrentLevelIsNonEmpty { 27 get { return (CurrentLevelInfo & ELevelInfo.NonEmpty) == ELevelInfo.NonEmpty; } 28 } 29 30 private bool CurrentLevelIsArray { 31 get { return (CurrentLevelInfo & ELevelInfo.Array) != ELevelInfo.Array; } 32 } 33 34 private bool CurrentLevelIsObject { 35 get { return (CurrentLevelInfo & ELevelInfo.Object) != ELevelInfo.Object; } 36 } 37 22 private ELevelInfo CurrentLevelInfo => (ELevelInfo) (currentLevelType & levelBitsMask); 23 24 private bool CurrentLevelIsNonEmpty => (CurrentLevelInfo & ELevelInfo.NonEmpty) == ELevelInfo.NonEmpty; 25 26 private bool CurrentLevelIsArray => (CurrentLevelInfo & ELevelInfo.Array) != ELevelInfo.Array; 27 28 private bool CurrentLevelIsObject => (CurrentLevelInfo & ELevelInfo.Object) != ELevelInfo.Object; 29 38 30 public JsonManualBuilder (bool _prettyPrint) { 39 31 prettyPrint = _prettyPrint; … … 54 46 } 55 47 56 currentLevelType = currentLevelType |(long) ELevelInfo.NonEmpty;48 currentLevelType |= (long) ELevelInfo.NonEmpty; 57 49 } 58 50 … … 210 202 211 203 currentLevelNumber--; 212 currentLevelType = currentLevelType >>levelTypeBits;204 currentLevelType >>= levelTypeBits; 213 205 214 206 if (prettyPrint) { -
binary-improvements2/7dtd-server-fixes/src/JSON/MalformedJSONException.cs
r351 r391 3 3 4 4 namespace AllocsFixes.JSON { 5 public class MalformedJ SONException : ApplicationException {6 public MalformedJ SONException () {5 public class MalformedJsonException : ApplicationException { 6 public MalformedJsonException () { 7 7 } 8 8 9 public MalformedJ SONException (string _message) : base (_message) {9 public MalformedJsonException (string _message) : base (_message) { 10 10 } 11 11 12 public MalformedJ SONException (string _message, Exception _inner) : base (_message, _inner) {12 public MalformedJsonException (string _message, Exception _inner) : base (_message, _inner) { 13 13 } 14 14 15 protected MalformedJ SONException (SerializationInfo _info, StreamingContext _context) : base (_info, _context) {15 protected MalformedJsonException (SerializationInfo _info, StreamingContext _context) : base (_info, _context) { 16 16 } 17 17 } -
binary-improvements2/7dtd-server-fixes/src/JSON/Parser.cs
r351 r391 1 1 namespace AllocsFixes.JSON { 2 public class Parser {3 public static J SONNode Parse (string _json) {2 public static class Parser { 3 public static JsonNode Parse (string _json) { 4 4 int offset = 0; 5 5 return ParseInternal (_json, ref offset); 6 6 } 7 7 8 public static J SONNode ParseInternal (string _json, ref int _offset) {8 public static JsonNode ParseInternal (string _json, ref int _offset) { 9 9 SkipWhitespace (_json, ref _offset); 10 10 … … 12 12 switch (_json [_offset]) { 13 13 case '[': 14 return J SONArray.Parse (_json, ref _offset);14 return JsonArray.Parse (_json, ref _offset); 15 15 case '{': 16 return J SONObject.Parse (_json, ref _offset);16 return JsonObject.Parse (_json, ref _offset); 17 17 case '"': 18 return J SONString.Parse (_json, ref _offset);18 return JsonString.Parse (_json, ref _offset); 19 19 case 't': 20 20 case 'f': 21 return J SONBoolean.Parse (_json, ref _offset);21 return JsonBoolean.Parse (_json, ref _offset); 22 22 case 'n': 23 return J SONNull.Parse (_json, ref _offset);23 return JsonNull.Parse (_json, ref _offset); 24 24 default: 25 return J SONNumber.Parse (_json, ref _offset);25 return JsonNumber.Parse (_json, ref _offset); 26 26 } 27 27 } … … 42 42 } 43 43 44 throw new MalformedJ SONException ("End of JSON reached before parsing finished");44 throw new MalformedJsonException ("End of JSON reached before parsing finished"); 45 45 } 46 46 }
Note:
See TracChangeset
for help on using the changeset viewer.