1 | using System;
|
---|
2 | using System.Text;
|
---|
3 |
|
---|
4 | namespace AllocsFixes.JSON
|
---|
5 | {
|
---|
6 | public class JSONNumber : JSONNode
|
---|
7 | {
|
---|
8 | private double value;
|
---|
9 |
|
---|
10 | public JSONNumber (double value)
|
---|
11 | {
|
---|
12 | this.value = value;
|
---|
13 | }
|
---|
14 |
|
---|
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)
|
---|
26 | {
|
---|
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");
|
---|
107 | }
|
---|
108 |
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|