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

Last change on this file since 331 was 325, checked in by alloc, 6 years ago

Code style cleanup (mostly whitespace changes, enforcing braces, using cleanup)

File size: 3.1 KB
RevLine 
[154]1using System;
[187]2using System.Text;
[154]3
[325]4namespace AllocsFixes.JSON {
5 public class JSONNumber : JSONValue {
6 private readonly double value;
[154]7
[325]8 public JSONNumber (double value) {
[154]9 this.value = value;
10 }
11
[325]12 public double GetDouble () {
[187]13 return value;
14 }
15
[325]16 public int GetInt () {
17 return (int) Math.Round (value);
[187]18 }
19
[325]20 public override void ToString (StringBuilder stringBuilder, bool prettyPrint = false, int currentLevel = 0) {
[309]21 stringBuilder.Append (value.ToCultureInvariantString ());
[154]22 }
23
[325]24 public static JSONNumber Parse (string json, ref int offset) {
[187]25 //Log.Out ("ParseNumber enter (" + offset + ")");
26 StringBuilder sbNum = new StringBuilder ();
27 StringBuilder sbExp = null;
28 bool hasDec = false;
29 bool hasExp = false;
30 while (offset < json.Length) {
31 if (json [offset] >= '0' && json [offset] <= '9') {
[325]32 if (hasExp) {
[187]33 sbExp.Append (json [offset]);
[325]34 } else {
[187]35 sbNum.Append (json [offset]);
[325]36 }
[187]37 } else if (json [offset] == '.') {
38 if (hasExp) {
39 throw new MalformedJSONException ("Decimal separator in exponent");
40 }
[325]41
42 if (hasDec) {
43 throw new MalformedJSONException ("Multiple decimal separators in number found");
44 }
45
46 if (sbNum.Length == 0) {
47 throw new MalformedJSONException ("No leading digits before decimal separator found");
48 }
49
50 sbNum.Append ('.');
51 hasDec = true;
52 } else if (json [offset] == '-') {
[187]53 if (hasExp) {
[325]54 if (sbExp.Length > 0) {
[187]55 throw new MalformedJSONException ("Negative sign in exponent after digits");
[325]56 }
57
58 sbExp.Append (json [offset]);
[187]59 } else {
[325]60 if (sbNum.Length > 0) {
[187]61 throw new MalformedJSONException ("Negative sign in mantissa after digits");
[325]62 }
63
64 sbNum.Append (json [offset]);
[187]65 }
66 } else if (json [offset] == 'e' || json [offset] == 'E') {
[325]67 if (hasExp) {
[187]68 throw new MalformedJSONException ("Multiple exponential markers in number found");
[325]69 }
70
71 if (sbNum.Length == 0) {
[187]72 throw new MalformedJSONException ("No leading digits before exponential marker found");
73 }
[325]74
75 sbExp = new StringBuilder ();
76 hasExp = true;
[187]77 } else if (json [offset] == '+') {
78 if (hasExp) {
[325]79 if (sbExp.Length > 0) {
[187]80 throw new MalformedJSONException ("Positive sign in exponent after digits");
[325]81 }
82
83 sbExp.Append (json [offset]);
[187]84 } else {
85 throw new MalformedJSONException ("Positive sign in mantissa found");
86 }
87 } else {
88 double number;
[325]89 if (!StringParsers.TryParseDouble (sbNum.ToString (), out number)) {
90 throw new MalformedJSONException ("Mantissa is not a valid decimal (\"" + sbNum + "\")");
[187]91 }
92
93 if (hasExp) {
94 int exp;
95 if (!int.TryParse (sbExp.ToString (), out exp)) {
[325]96 throw new MalformedJSONException ("Exponent is not a valid integer (\"" + sbExp + "\")");
[187]97 }
98
99 number = number * Math.Pow (10, exp);
100 }
101
102 //Log.Out ("JSON:Parsed Number: " + number.ToString ());
103 return new JSONNumber (number);
104 }
[325]105
[187]106 offset++;
107 }
[325]108
[187]109 throw new MalformedJSONException ("End of JSON reached before parsing number finished");
110 }
[154]111 }
[325]112}
Note: See TracBrowser for help on using the repository browser.