Changeset 351 for binary-improvements/7dtd-server-fixes/src/JSON
- Timestamp:
- Jan 19, 2019, 6:12:21 PM (6 years ago)
- Location:
- binary-improvements/7dtd-server-fixes/src/JSON
- Files:
-
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements/7dtd-server-fixes/src/JSON/JSONArray.cs
r325 r351 6 6 private readonly List<JSONNode> nodes = new List<JSONNode> (); 7 7 8 public JSONNode this [int index] {9 get { return nodes [ index]; }10 set { nodes [ index] = value; }8 public JSONNode this [int _index] { 9 get { return nodes [_index]; } 10 set { nodes [_index] = value; } 11 11 } 12 12 … … 15 15 } 16 16 17 public void Add (JSONNode node) {18 nodes.Add ( node);17 public void Add (JSONNode _node) { 18 nodes.Add (_node); 19 19 } 20 20 21 public override void ToString (StringBuilder stringBuilder, bool prettyPrint = false, intcurrentLevel = 0) {22 stringBuilder.Append ("[");23 if ( prettyPrint) {24 stringBuilder.Append ('\n');21 public override void ToString (StringBuilder _stringBuilder, bool _prettyPrint = false, int _currentLevel = 0) { 22 _stringBuilder.Append ("["); 23 if (_prettyPrint) { 24 _stringBuilder.Append ('\n'); 25 25 } 26 26 27 27 foreach (JSONNode n in nodes) { 28 if ( prettyPrint) {29 stringBuilder.Append (new string ('\t',currentLevel + 1));28 if (_prettyPrint) { 29 _stringBuilder.Append (new string ('\t', _currentLevel + 1)); 30 30 } 31 31 32 n.ToString ( stringBuilder, prettyPrint,currentLevel + 1);33 stringBuilder.Append (",");34 if ( prettyPrint) {35 stringBuilder.Append ('\n');32 n.ToString (_stringBuilder, _prettyPrint, _currentLevel + 1); 33 _stringBuilder.Append (","); 34 if (_prettyPrint) { 35 _stringBuilder.Append ('\n'); 36 36 } 37 37 } 38 38 39 39 if (nodes.Count > 0) { 40 stringBuilder.Remove (stringBuilder.Length - (prettyPrint ? 2 : 1), 1);40 _stringBuilder.Remove (_stringBuilder.Length - (_prettyPrint ? 2 : 1), 1); 41 41 } 42 42 43 if ( prettyPrint) {44 stringBuilder.Append (new string ('\t',currentLevel));43 if (_prettyPrint) { 44 _stringBuilder.Append (new string ('\t', _currentLevel)); 45 45 } 46 46 47 stringBuilder.Append ("]");47 _stringBuilder.Append ("]"); 48 48 } 49 49 50 public static JSONArray Parse (string json, ref intoffset) {50 public static JSONArray Parse (string _json, ref int _offset) { 51 51 //Log.Out ("ParseArray enter (" + offset + ")"); 52 52 JSONArray arr = new JSONArray (); 53 53 54 54 bool nextElemAllowed = true; 55 offset++;55 _offset++; 56 56 while (true) { 57 Parser.SkipWhitespace ( json, refoffset);57 Parser.SkipWhitespace (_json, ref _offset); 58 58 59 switch ( json [offset]) {59 switch (_json [_offset]) { 60 60 case ',': 61 61 if (!nextElemAllowed) { 62 62 nextElemAllowed = true; 63 offset++;63 _offset++; 64 64 } else { 65 65 throw new MalformedJSONException ( … … 69 69 break; 70 70 case ']': 71 offset++;71 _offset++; 72 72 73 73 //Log.Out ("JSON:Parsed Array: " + arr.ToString ()); 74 74 return arr; 75 75 default: 76 arr.Add (Parser.ParseInternal ( json, refoffset));76 arr.Add (Parser.ParseInternal (_json, ref _offset)); 77 77 nextElemAllowed = false; 78 78 break; -
binary-improvements/7dtd-server-fixes/src/JSON/JSONBoolean.cs
r325 r351 5 5 private readonly bool value; 6 6 7 public JSONBoolean (bool value) {8 this.value =value;7 public JSONBoolean (bool _value) { 8 value = _value; 9 9 } 10 10 … … 13 13 } 14 14 15 public override void ToString (StringBuilder stringBuilder, bool prettyPrint = false, intcurrentLevel = 0) {16 stringBuilder.Append (value ? "true" : "false");15 public override void ToString (StringBuilder _stringBuilder, bool _prettyPrint = false, int _currentLevel = 0) { 16 _stringBuilder.Append (value ? "true" : "false"); 17 17 } 18 18 19 public static JSONBoolean Parse (string json, ref intoffset) {19 public static JSONBoolean Parse (string _json, ref int _offset) { 20 20 //Log.Out ("ParseBool enter (" + offset + ")"); 21 21 22 if ( json.Substring (offset, 4).Equals ("true")) {22 if (_json.Substring (_offset, 4).Equals ("true")) { 23 23 //Log.Out ("JSON:Parsed Bool: true"); 24 offset += 4;24 _offset += 4; 25 25 return new JSONBoolean (true); 26 26 } 27 27 28 if ( json.Substring (offset, 5).Equals ("false")) {28 if (_json.Substring (_offset, 5).Equals ("false")) { 29 29 //Log.Out ("JSON:Parsed Bool: false"); 30 offset += 5;30 _offset += 5; 31 31 return new JSONBoolean (false); 32 32 } -
binary-improvements/7dtd-server-fixes/src/JSON/JSONNode.cs
r325 r351 3 3 namespace AllocsFixes.JSON { 4 4 public abstract class JSONNode { 5 public abstract void ToString (StringBuilder stringBuilder, bool prettyPrint = false, intcurrentLevel = 0);5 public abstract void ToString (StringBuilder _stringBuilder, bool _prettyPrint = false, int _currentLevel = 0); 6 6 7 7 public override string ToString () { -
binary-improvements/7dtd-server-fixes/src/JSON/JSONNull.cs
r326 r351 3 3 namespace AllocsFixes.JSON { 4 4 public class JSONNull : JSONValue { 5 public override void ToString (StringBuilder stringBuilder, bool prettyPrint = false, intcurrentLevel = 0) {6 stringBuilder.Append ("null");5 public override void ToString (StringBuilder _stringBuilder, bool _prettyPrint = false, int _currentLevel = 0) { 6 _stringBuilder.Append ("null"); 7 7 } 8 8 9 public static JSONNull Parse (string json, ref intoffset) {9 public static JSONNull Parse (string _json, ref int _offset) { 10 10 //Log.Out ("ParseNull enter (" + offset + ")"); 11 11 12 if (! json.Substring (offset, 4).Equals ("null")) {12 if (!_json.Substring (_offset, 4).Equals ("null")) { 13 13 throw new MalformedJSONException ("No valid null value found"); 14 14 } 15 15 16 16 //Log.Out ("JSON:Parsed Null"); 17 offset += 4;17 _offset += 4; 18 18 return new JSONNull (); 19 19 } -
binary-improvements/7dtd-server-fixes/src/JSON/JSONNumber.cs
r325 r351 6 6 private readonly double value; 7 7 8 public JSONNumber (double value) {9 this.value =value;8 public JSONNumber (double _value) { 9 value = _value; 10 10 } 11 11 … … 18 18 } 19 19 20 public override void ToString (StringBuilder stringBuilder, bool prettyPrint = false, intcurrentLevel = 0) {21 stringBuilder.Append (value.ToCultureInvariantString ());20 public override void ToString (StringBuilder _stringBuilder, bool _prettyPrint = false, int _currentLevel = 0) { 21 _stringBuilder.Append (value.ToCultureInvariantString ()); 22 22 } 23 23 24 public static JSONNumber Parse (string json, ref intoffset) {24 public static JSONNumber Parse (string _json, ref int _offset) { 25 25 //Log.Out ("ParseNumber enter (" + offset + ")"); 26 26 StringBuilder sbNum = new StringBuilder (); … … 28 28 bool hasDec = false; 29 29 bool hasExp = false; 30 while ( offset <json.Length) {31 if ( json [offset] >= '0' && json [offset] <= '9') {30 while (_offset < _json.Length) { 31 if (_json [_offset] >= '0' && _json [_offset] <= '9') { 32 32 if (hasExp) { 33 sbExp.Append ( json [offset]);33 sbExp.Append (_json [_offset]); 34 34 } else { 35 sbNum.Append ( json [offset]);35 sbNum.Append (_json [_offset]); 36 36 } 37 } else if ( json [offset] == '.') {37 } else if (_json [_offset] == '.') { 38 38 if (hasExp) { 39 39 throw new MalformedJSONException ("Decimal separator in exponent"); … … 50 50 sbNum.Append ('.'); 51 51 hasDec = true; 52 } else if ( json [offset] == '-') {52 } else if (_json [_offset] == '-') { 53 53 if (hasExp) { 54 54 if (sbExp.Length > 0) { … … 56 56 } 57 57 58 sbExp.Append ( json [offset]);58 sbExp.Append (_json [_offset]); 59 59 } else { 60 60 if (sbNum.Length > 0) { … … 62 62 } 63 63 64 sbNum.Append ( json [offset]);64 sbNum.Append (_json [_offset]); 65 65 } 66 } else if ( json [offset] == 'e' || json [offset] == 'E') {66 } else if (_json [_offset] == 'e' || _json [_offset] == 'E') { 67 67 if (hasExp) { 68 68 throw new MalformedJSONException ("Multiple exponential markers in number found"); … … 75 75 sbExp = new StringBuilder (); 76 76 hasExp = true; 77 } else if ( json [offset] == '+') {77 } else if (_json [_offset] == '+') { 78 78 if (hasExp) { 79 79 if (sbExp.Length > 0) { … … 81 81 } 82 82 83 sbExp.Append ( json [offset]);83 sbExp.Append (_json [_offset]); 84 84 } else { 85 85 throw new MalformedJSONException ("Positive sign in mantissa found"); … … 104 104 } 105 105 106 offset++;106 _offset++; 107 107 } 108 108 -
binary-improvements/7dtd-server-fixes/src/JSON/JSONObject.cs
r326 r351 6 6 private readonly Dictionary<string, JSONNode> nodes = new Dictionary<string, JSONNode> (); 7 7 8 public JSONNode this [string name] {9 get { return nodes [ name]; }10 set { nodes [ name] = value; }8 public JSONNode this [string _name] { 9 get { return nodes [_name]; } 10 set { nodes [_name] = value; } 11 11 } 12 12 … … 19 19 } 20 20 21 public bool ContainsKey (string name) {22 return nodes.ContainsKey ( name);21 public bool ContainsKey (string _name) { 22 return nodes.ContainsKey (_name); 23 23 } 24 24 25 public void Add (string name, JSONNodenode) {26 nodes.Add ( name,node);25 public void Add (string _name, JSONNode _node) { 26 nodes.Add (_name, _node); 27 27 } 28 28 29 public override void ToString (StringBuilder stringBuilder, bool prettyPrint = false, intcurrentLevel = 0) {30 stringBuilder.Append ("{");31 if ( prettyPrint) {32 stringBuilder.Append ('\n');29 public override void ToString (StringBuilder _stringBuilder, bool _prettyPrint = false, int _currentLevel = 0) { 30 _stringBuilder.Append ("{"); 31 if (_prettyPrint) { 32 _stringBuilder.Append ('\n'); 33 33 } 34 34 35 35 foreach (KeyValuePair<string, JSONNode> kvp in nodes) { 36 if ( prettyPrint) {37 stringBuilder.Append (new string ('\t',currentLevel + 1));36 if (_prettyPrint) { 37 _stringBuilder.Append (new string ('\t', _currentLevel + 1)); 38 38 } 39 39 40 stringBuilder.Append (string.Format ("\"{0}\":", kvp.Key));41 if ( prettyPrint) {42 stringBuilder.Append (" ");40 _stringBuilder.Append (string.Format ("\"{0}\":", kvp.Key)); 41 if (_prettyPrint) { 42 _stringBuilder.Append (" "); 43 43 } 44 44 45 kvp.Value.ToString ( stringBuilder, prettyPrint,currentLevel + 1);46 stringBuilder.Append (",");47 if ( prettyPrint) {48 stringBuilder.Append ('\n');45 kvp.Value.ToString (_stringBuilder, _prettyPrint, _currentLevel + 1); 46 _stringBuilder.Append (","); 47 if (_prettyPrint) { 48 _stringBuilder.Append ('\n'); 49 49 } 50 50 } 51 51 52 52 if (nodes.Count > 0) { 53 stringBuilder.Remove (stringBuilder.Length - (prettyPrint ? 2 : 1), 1);53 _stringBuilder.Remove (_stringBuilder.Length - (_prettyPrint ? 2 : 1), 1); 54 54 } 55 55 56 if ( prettyPrint) {57 stringBuilder.Append (new string ('\t',currentLevel));56 if (_prettyPrint) { 57 _stringBuilder.Append (new string ('\t', _currentLevel)); 58 58 } 59 59 60 stringBuilder.Append ("}");60 _stringBuilder.Append ("}"); 61 61 } 62 62 63 public static JSONObject Parse (string json, ref intoffset) {63 public static JSONObject Parse (string _json, ref int _offset) { 64 64 //Log.Out ("ParseObject enter (" + offset + ")"); 65 65 JSONObject obj = new JSONObject (); 66 66 67 67 bool nextElemAllowed = true; 68 offset++;68 _offset++; 69 69 while (true) { 70 Parser.SkipWhitespace ( json, refoffset);71 switch ( json [offset]) {70 Parser.SkipWhitespace (_json, ref _offset); 71 switch (_json [_offset]) { 72 72 case '"': 73 73 if (nextElemAllowed) { 74 JSONString key = JSONString.Parse ( json, refoffset);75 Parser.SkipWhitespace ( json, refoffset);76 if ( json [offset] != ':') {74 JSONString key = JSONString.Parse (_json, ref _offset); 75 Parser.SkipWhitespace (_json, ref _offset); 76 if (_json [_offset] != ':') { 77 77 throw new MalformedJSONException ( 78 78 "Could not parse object, missing colon (\":\") after key"); 79 79 } 80 80 81 offset++;82 JSONNode val = Parser.ParseInternal ( json, refoffset);81 _offset++; 82 JSONNode val = Parser.ParseInternal (_json, ref _offset); 83 83 obj.Add (key.GetString (), val); 84 84 nextElemAllowed = false; … … 92 92 if (!nextElemAllowed) { 93 93 nextElemAllowed = true; 94 offset++;94 _offset++; 95 95 } else { 96 96 throw new MalformedJSONException ( … … 100 100 break; 101 101 case '}': 102 offset++;102 _offset++; 103 103 104 104 //Log.Out ("JSON:Parsed Object: " + obj.ToString ()); -
binary-improvements/7dtd-server-fixes/src/JSON/JSONString.cs
r325 r351 5 5 private readonly string value; 6 6 7 public JSONString (string value) {8 this.value =value;7 public JSONString (string _value) { 8 value = _value; 9 9 } 10 10 … … 13 13 } 14 14 15 public override void ToString (StringBuilder stringBuilder, bool prettyPrint = false, intcurrentLevel = 0) {15 public override void ToString (StringBuilder _stringBuilder, bool _prettyPrint = false, int _currentLevel = 0) { 16 16 if (value == null || value.Length == 0) { 17 stringBuilder.Append ("\"\"");17 _stringBuilder.Append ("\"\""); 18 18 return; 19 19 } … … 21 21 int len = value.Length; 22 22 23 stringBuilder.EnsureCapacity (stringBuilder.Length + 2 * len);23 _stringBuilder.EnsureCapacity (_stringBuilder.Length + 2 * len); 24 24 25 stringBuilder.Append ('"');25 _stringBuilder.Append ('"'); 26 26 27 27 foreach (char c in value) { … … 31 31 32 32 // case '/': 33 stringBuilder.Append ('\\');34 stringBuilder.Append (c);33 _stringBuilder.Append ('\\'); 34 _stringBuilder.Append (c); 35 35 break; 36 36 case '\b': 37 stringBuilder.Append ("\\b");37 _stringBuilder.Append ("\\b"); 38 38 break; 39 39 case '\t': 40 stringBuilder.Append ("\\t");40 _stringBuilder.Append ("\\t"); 41 41 break; 42 42 case '\n': 43 stringBuilder.Append ("\\n");43 _stringBuilder.Append ("\\n"); 44 44 break; 45 45 case '\f': 46 stringBuilder.Append ("\\f");46 _stringBuilder.Append ("\\f"); 47 47 break; 48 48 case '\r': 49 stringBuilder.Append ("\\r");49 _stringBuilder.Append ("\\r"); 50 50 break; 51 51 default: 52 52 if (c < ' ') { 53 stringBuilder.Append ("\\u");54 stringBuilder.Append (((int) c).ToString ("X4"));53 _stringBuilder.Append ("\\u"); 54 _stringBuilder.Append (((int) c).ToString ("X4")); 55 55 } else { 56 stringBuilder.Append (c);56 _stringBuilder.Append (c); 57 57 } 58 58 … … 61 61 } 62 62 63 stringBuilder.Append ('"');63 _stringBuilder.Append ('"'); 64 64 } 65 65 66 public static JSONString Parse (string json, ref intoffset) {66 public static JSONString Parse (string _json, ref int _offset) { 67 67 //Log.Out ("ParseString enter (" + offset + ")"); 68 68 StringBuilder sb = new StringBuilder (); 69 offset++;70 while ( offset <json.Length) {71 switch ( json [offset]) {69 _offset++; 70 while (_offset < _json.Length) { 71 switch (_json [_offset]) { 72 72 case '\\': 73 offset++;74 switch ( json [offset]) {73 _offset++; 74 switch (_json [_offset]) { 75 75 case '\\': 76 76 case '"': 77 77 case '/': 78 sb.Append ( json [offset]);78 sb.Append (_json [_offset]); 79 79 break; 80 80 case 'b': … … 94 94 break; 95 95 default: 96 sb.Append ( json [offset]);96 sb.Append (_json [_offset]); 97 97 break; 98 98 } 99 99 100 offset++;100 _offset++; 101 101 break; 102 102 case '"': 103 offset++;103 _offset++; 104 104 105 105 //Log.Out ("JSON:Parsed String: " + sb.ToString ()); 106 106 return new JSONString (sb.ToString ()); 107 107 default: 108 sb.Append ( json [offset]);109 offset++;108 sb.Append (_json [_offset]); 109 _offset++; 110 110 break; 111 111 } -
binary-improvements/7dtd-server-fixes/src/JSON/MalformedJSONException.cs
r325 r351 7 7 } 8 8 9 public MalformedJSONException (string message) : base (message) {9 public MalformedJSONException (string _message) : base (_message) { 10 10 } 11 11 12 public MalformedJSONException (string message, Exception inner) : base (message,inner) {12 public MalformedJSONException (string _message, Exception _inner) : base (_message, _inner) { 13 13 } 14 14 15 protected MalformedJSONException (SerializationInfo info, StreamingContext context) : base (info,context) {15 protected MalformedJSONException (SerializationInfo _info, StreamingContext _context) : base (_info, _context) { 16 16 } 17 17 } -
binary-improvements/7dtd-server-fixes/src/JSON/Parser.cs
r325 r351 1 1 namespace AllocsFixes.JSON { 2 2 public class Parser { 3 public static JSONNode Parse (string json) {3 public static JSONNode Parse (string _json) { 4 4 int offset = 0; 5 return ParseInternal ( json, ref offset);5 return ParseInternal (_json, ref offset); 6 6 } 7 7 8 public static JSONNode ParseInternal (string json, ref intoffset) {9 SkipWhitespace ( json, refoffset);8 public static JSONNode ParseInternal (string _json, ref int _offset) { 9 SkipWhitespace (_json, ref _offset); 10 10 11 11 //Log.Out ("ParseInternal (" + offset + "): Decide on: '" + json [offset] + "'"); 12 switch ( json [offset]) {12 switch (_json [_offset]) { 13 13 case '[': 14 return JSONArray.Parse ( json, refoffset);14 return JSONArray.Parse (_json, ref _offset); 15 15 case '{': 16 return JSONObject.Parse ( json, refoffset);16 return JSONObject.Parse (_json, ref _offset); 17 17 case '"': 18 return JSONString.Parse ( json, refoffset);18 return JSONString.Parse (_json, ref _offset); 19 19 case 't': 20 20 case 'f': 21 return JSONBoolean.Parse ( json, refoffset);21 return JSONBoolean.Parse (_json, ref _offset); 22 22 case 'n': 23 return JSONNull.Parse ( json, refoffset);23 return JSONNull.Parse (_json, ref _offset); 24 24 default: 25 return JSONNumber.Parse ( json, refoffset);25 return JSONNumber.Parse (_json, ref _offset); 26 26 } 27 27 } 28 28 29 public static void SkipWhitespace (string json, ref intoffset) {29 public static void SkipWhitespace (string _json, ref int _offset) { 30 30 //Log.Out ("SkipWhitespace (" + offset + "): '" + json [offset] + "'"); 31 while ( offset <json.Length) {32 switch ( json [offset]) {31 while (_offset < _json.Length) { 32 switch (_json [_offset]) { 33 33 case ' ': 34 34 case '\t': 35 35 case '\r': 36 36 case '\n': 37 offset++;37 _offset++; 38 38 break; 39 39 default:
Note:
See TracChangeset
for help on using the changeset viewer.