-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSONValue.cs
More file actions
172 lines (148 loc) · 4.3 KB
/
Copy pathJSONValue.cs
File metadata and controls
172 lines (148 loc) · 4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
using System;
namespace HGSDK
{
public enum JSONValueType
{
String,
Double,
Float,
Int,
Boolean,
Null
}
public class JSONValue : JSONData
{
private JSONValueType _type = JSONValueType.Null;
private string _string;
private double _double;
private float _float;
private int _int;
private bool _boolean;
internal static JSONValue Parse(string value, bool isString)
{
var ret = new JSONValue();
if (isString)
{
ret._string = value;
ret._type = JSONValueType.String;
}
else
{
if (double.TryParse(value, out ret._double)) ret._type = JSONValueType.Double;
if (float.TryParse(value, out ret._float)) ret._type = JSONValueType.Float;
if (int.TryParse(value, out ret._int)) ret._type = JSONValueType.Int;
if (bool.TryParse(value, out ret._boolean)) ret._type = JSONValueType.Boolean;
}
return ret;
}
public JSONValue()
{
}
public JSONValue(string value)
{
_type = JSONValueType.String;
_string = value;
}
public JSONValue(float value)
{
_type = JSONValueType.Float;
_float = value;
}
public JSONValue(double value)
{
_type = JSONValueType.Double;
_double = value;
}
public JSONValue(int value)
{
_type = JSONValueType.Int;
_int = value;
}
public JSONValue(bool value)
{
_type = JSONValueType.Boolean;
_boolean = value;
}
public override string ToString()
{
switch (_type)
{
case JSONValueType.String:
return "\"" + Escape(_string) + "\"";
case JSONValueType.Double:
return _double.ToString();
case JSONValueType.Float:
return _float.ToString();
case JSONValueType.Int:
return _int.ToString();
case JSONValueType.Boolean:
return _boolean.ToString().ToLower();
case JSONValueType.Null:
return "null";
default:
return "JSONValue";
}
}
public override string ToString(string prefix)
{
return ToString();
}
public override bool IsNull
{
get { return _type == JSONValueType.Null; }
}
public override bool IsString
{
get { return _type == JSONValueType.String; }
}
public override string String
{
get { return IsString ? _string : null; }
set
{
_type = JSONValueType.String;
_string = value;
}
}
public override bool IsInt { get { return _type == JSONValueType.Int; } }
public override int Int
{
get { return IsInt ? _int : 0; }
set
{
_type = JSONValueType.Int;
_int = value;
}
}
public override bool IsFloat { get { return _type == JSONValueType.Float; } }
public override float Float
{
get { return IsFloat ? _float : 0.0f; }
set
{
_type = JSONValueType.Float;
_float = value;
}
}
public override bool IsDouble { get { return _type == JSONValueType.Double; } }
public override double Double
{
get { return IsDouble ? _double : 0.0; }
set
{
_type = JSONValueType.Double;
_double = value;
}
}
public override bool IsBoolean { get { return _type == JSONValueType.Boolean; } }
public override bool Boolean
{
get { return IsBoolean ? _boolean : false; }
set
{
_type = JSONValueType.Boolean;
_boolean = value;
}
}
}
}