- Timestamp:
- Sep 10, 2014, 8:09:28 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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
Note:
See TracChangeset
for help on using the changeset viewer.