source: binary-improvements/7dtd-server-fixes/src/JSON/JSONNumber.cs@ 246

Last change on this file since 246 was 187, checked in by alloc, 10 years ago

fixes

File size: 3.1 KB
RevLine 
[154]1using System;
[187]2using System.Text;
[154]3
4namespace AllocsFixes.JSON
5{
6 public class JSONNumber : JSONNode
7 {
[187]8 private double value;
[154]9
[187]10 public JSONNumber (double value)
[154]11 {
12 this.value = value;
13 }
14
[187]15 public double GetDouble ()
[154]16 {
[187]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)
26 {
[154]27 return value.ToString (System.Globalization.CultureInfo.InvariantCulture);
28 }
29
[187]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");
107 }
108
[154]109 }
110}
111
Note: See TracBrowser for help on using the repository browser.