Changeset 187 for binary-improvements/7dtd-server-fixes/src/JSON
- Timestamp:
- Sep 10, 2014, 8:09:28 PM (10 years ago)
- Location:
- binary-improvements/7dtd-server-fixes/src/JSON
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements/7dtd-server-fixes/src/JSON/JSONArray.cs
r154 r187 9 9 private List<JSONNode> nodes = new List<JSONNode> (); 10 10 11 public JSONNode this [int index] { 12 get { return nodes [index]; } 13 set { nodes [index] = value; } 14 } 15 16 public int Count { 17 get { return nodes.Count; } 18 } 19 11 20 public void Add (JSONNode node) 12 21 { 13 nodes.Add (node);22 nodes.Add (node); 14 23 } 15 24 16 public override string ToString ( )25 public override string ToString (bool prettyPrint = false, int currentLevel = 0) 17 26 { 18 27 StringBuilder sb = new StringBuilder ("["); 28 if (prettyPrint) 29 sb.Append ('\n'); 19 30 foreach (JSONNode n in nodes) { 20 sb.Append (n.ToString ()); 31 if (prettyPrint) 32 sb.Append (new String ('\t', currentLevel + 1)); 33 sb.Append (n.ToString (prettyPrint, currentLevel + 1)); 21 34 sb.Append (","); 35 if (prettyPrint) 36 sb.Append ('\n'); 22 37 } 23 38 if (sb.Length > 1) 24 sb.Remove (sb.Length - 1, 1); 39 sb.Remove (sb.Length - (prettyPrint ? 2 : 1), 1); 40 if (prettyPrint) 41 sb.Append (new String ('\t', currentLevel)); 25 42 sb.Append ("]"); 26 43 return sb.ToString (); 44 } 45 46 public static JSONArray Parse (string json, ref int offset) 47 { 48 //Log.Out ("ParseArray enter (" + offset + ")"); 49 JSONArray arr = new JSONArray (); 50 51 bool nextElemAllowed = true; 52 offset++; 53 while (true) { 54 Parser.SkipWhitespace (json, ref offset); 55 56 switch (json [offset]) { 57 case ',': 58 if (!nextElemAllowed) { 59 nextElemAllowed = true; 60 offset++; 61 } else 62 throw new MalformedJSONException ("Could not parse array, found a comma without a value first"); 63 break; 64 case ']': 65 offset++; 66 //Log.Out ("JSON:Parsed Array: " + arr.ToString ()); 67 return arr; 68 default: 69 arr.Add (Parser.ParseInternal (json, ref offset)); 70 nextElemAllowed = false; 71 break; 72 } 73 } 27 74 } 28 75 -
binary-improvements/7dtd-server-fixes/src/JSON/JSONBoolean.cs
r154 r187 12 12 } 13 13 14 public override string ToString () 14 public bool GetBool () 15 { 16 return value; 17 } 18 19 public override string ToString (bool prettyPrint = false, int currentLevel = 0) 15 20 { 16 21 return value.ToString (System.Globalization.CultureInfo.InvariantCulture).ToLower (); 22 } 23 24 public static JSONBoolean Parse (string json, ref int offset) 25 { 26 //Log.Out ("ParseBool enter (" + offset + ")"); 27 28 if (json.Substring (offset, 4).Equals ("true")) { 29 //Log.Out ("JSON:Parsed Bool: true"); 30 offset += 4; 31 return new JSONBoolean (true); 32 } else if (json.Substring (offset, 5).Equals ("false")) { 33 //Log.Out ("JSON:Parsed Bool: false"); 34 offset += 5; 35 return new JSONBoolean (false); 36 } else { 37 throw new MalformedJSONException ("No valid boolean found"); 38 } 17 39 } 18 40 -
binary-improvements/7dtd-server-fixes/src/JSON/JSONNode.cs
r160 r187 5 5 public abstract class JSONNode 6 6 { 7 public abstract override string ToString();7 public abstract string ToString(bool prettyPrint = false, int currentLevel = 0); 8 8 } 9 9 } -
binary-improvements/7dtd-server-fixes/src/JSON/JSONNumber.cs
r154 r187 1 1 using System; 2 using System.Text; 2 3 3 4 namespace AllocsFixes.JSON … … 5 6 public class JSONNumber : JSONNode 6 7 { 7 private floatvalue;8 private double value; 8 9 9 public JSONNumber ( floatvalue)10 public JSONNumber (double value) 10 11 { 11 12 this.value = value; 12 13 } 13 14 14 public override string ToString () 15 public double GetDouble () 16 { 17 return value; 18 } 19 20 public int GetInt () 21 { 22 return (int)Math.Round(value); 23 } 24 25 public override string ToString (bool prettyPrint = false, int currentLevel = 0) 15 26 { 16 27 return value.ToString (System.Globalization.CultureInfo.InvariantCulture); 28 } 29 30 public static JSONNumber Parse (string json, ref int offset) 31 { 32 //Log.Out ("ParseNumber enter (" + offset + ")"); 33 StringBuilder sbNum = new StringBuilder (); 34 StringBuilder sbExp = null; 35 bool hasDec = false; 36 bool hasExp = false; 37 while (offset < json.Length) { 38 if (json [offset] >= '0' && json [offset] <= '9') { 39 if (hasExp) 40 sbExp.Append (json [offset]); 41 else 42 sbNum.Append (json [offset]); 43 } else if (json [offset] == '.') { 44 if (hasExp) { 45 throw new MalformedJSONException ("Decimal separator in exponent"); 46 } 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; 54 } 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]); 67 } 68 } else if (json [offset] == 'e' || json [offset] == 'E') { 69 if (hasExp) 70 throw new MalformedJSONException ("Multiple exponential markers in number found"); 71 else if (sbNum.Length == 0) { 72 throw new MalformedJSONException ("No leading digits before exponential marker found"); 73 } else { 74 sbExp = new StringBuilder (); 75 hasExp = true; 76 } 77 } else if (json [offset] == '+') { 78 if (hasExp) { 79 if (sbExp.Length > 0) 80 throw new MalformedJSONException ("Positive sign in exponent after digits"); 81 else 82 sbExp.Append (json [offset]); 83 } else { 84 throw new MalformedJSONException ("Positive sign in mantissa found"); 85 } 86 } else { 87 double number; 88 if (!double.TryParse (sbNum.ToString (), out number)) { 89 throw new MalformedJSONException ("Mantissa is not a valid decimal (\"" + sbNum.ToString () + "\")"); 90 } 91 92 if (hasExp) { 93 int exp; 94 if (!int.TryParse (sbExp.ToString (), out exp)) { 95 throw new MalformedJSONException ("Exponent is not a valid integer (\"" + sbExp.ToString () + "\")"); 96 } 97 98 number = number * Math.Pow (10, exp); 99 } 100 101 //Log.Out ("JSON:Parsed Number: " + number.ToString ()); 102 return new JSONNumber (number); 103 } 104 offset++; 105 } 106 throw new MalformedJSONException ("End of JSON reached before parsing number finished"); 17 107 } 18 108 -
binary-improvements/7dtd-server-fixes/src/JSON/JSONObject.cs
r154 r187 9 9 private Dictionary<string, JSONNode> nodes = new Dictionary<string, JSONNode> (); 10 10 11 public JSONNode this [string name] { 12 get { return nodes [name]; } 13 set { nodes [name] = value; } 14 } 15 16 public int Count { 17 get { return nodes.Count; } 18 } 19 20 public List<string> Keys { 21 get { return new List<string> (nodes.Keys); } 22 } 23 24 public bool ContainsKey (string name) 25 { 26 return nodes.ContainsKey (name); 27 } 28 11 29 public void Add (string name, JSONNode node) 12 30 { … … 14 32 } 15 33 16 public override string ToString ( )34 public override string ToString (bool prettyPrint = false, int currentLevel = 0) 17 35 { 18 36 StringBuilder sb = new StringBuilder ("{"); 37 if (prettyPrint) 38 sb.Append ('\n'); 19 39 foreach (KeyValuePair<string, JSONNode> kvp in nodes) { 40 if (prettyPrint) 41 sb.Append (new String ('\t', currentLevel + 1)); 20 42 sb.Append (String.Format ("\"{0}\":", kvp.Key)); 21 sb.Append (kvp.Value.ToString ()); 43 if (prettyPrint) 44 sb.Append (" "); 45 sb.Append (kvp.Value.ToString (prettyPrint, currentLevel + 1)); 22 46 sb.Append (","); 47 if (prettyPrint) 48 sb.Append ('\n'); 23 49 } 24 50 if (sb.Length > 1) 25 sb.Remove (sb.Length - 1, 1); 51 sb.Remove (sb.Length - (prettyPrint ? 2 : 1), 1); 52 if (prettyPrint) 53 sb.Append (new String ('\t', currentLevel)); 26 54 sb.Append ("}"); 27 55 return sb.ToString (); 56 } 57 58 public static JSONObject Parse (string json, ref int offset) 59 { 60 //Log.Out ("ParseObject enter (" + offset + ")"); 61 JSONObject obj = new JSONObject (); 62 63 bool nextElemAllowed = true; 64 offset++; 65 while (true) { 66 Parser.SkipWhitespace (json, ref offset); 67 switch (json [offset]) { 68 case '"': 69 if (nextElemAllowed) { 70 JSONString key = JSONString.Parse (json, ref offset); 71 Parser.SkipWhitespace (json, ref offset); 72 if (json [offset] != ':') { 73 throw new MalformedJSONException ("Could not parse object, missing colon (\":\") after key"); 74 } 75 offset++; 76 JSONNode val = Parser.ParseInternal (json, ref offset); 77 obj.Add (key.GetString (), val); 78 nextElemAllowed = false; 79 } else { 80 throw new MalformedJSONException ("Could not parse object, found new key without a separating comma"); 81 } 82 break; 83 case ',': 84 if (!nextElemAllowed) { 85 nextElemAllowed = true; 86 offset++; 87 } else 88 throw new MalformedJSONException ("Could not parse object, found a comma without a key/value pair first"); 89 break; 90 case '}': 91 offset++; 92 //Log.Out ("JSON:Parsed Object: " + obj.ToString ()); 93 return obj; 94 } 95 } 28 96 } 29 97 -
binary-improvements/7dtd-server-fixes/src/JSON/JSONString.cs
r162 r187 13 13 } 14 14 15 public override string ToString () 15 public string GetString () 16 { 17 return value; 18 } 19 20 public override string ToString (bool prettyPrint = false, int currentLevel = 0) 16 21 { 17 22 if (value == null || value.Length == 0) { … … 61 66 } 62 67 68 public static JSONString Parse (string json, ref int offset) 69 { 70 //Log.Out ("ParseString enter (" + offset + ")"); 71 StringBuilder sb = new StringBuilder (); 72 offset++; 73 while (offset < json.Length) { 74 switch (json [offset]) { 75 case '\\': 76 offset++; 77 switch (json [offset]) { 78 case '\\': 79 case '"': 80 case '/': 81 sb.Append (json [offset]); 82 break; 83 case 'b': 84 sb.Append ('\b'); 85 break; 86 case 't': 87 sb.Append ('\t'); 88 break; 89 case 'n': 90 sb.Append ('\n'); 91 break; 92 case 'f': 93 sb.Append ('\f'); 94 break; 95 case 'r': 96 sb.Append ('\r'); 97 break; 98 default: 99 sb.Append (json [offset]); 100 break; 101 } 102 offset++; 103 break; 104 case '"': 105 offset++; 106 //Log.Out ("JSON:Parsed String: " + sb.ToString ()); 107 return new JSONString (sb.ToString ()); 108 default: 109 sb.Append (json [offset]); 110 offset++; 111 break; 112 } 113 } 114 throw new MalformedJSONException ("End of JSON reached before parsing string finished"); 115 } 116 63 117 } 64 118 }
Note:
See TracChangeset
for help on using the changeset viewer.