This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/
JsonPrimitive.cs
173 lines (144 loc) · 4.63 KB
/
JsonPrimitive.cs
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
173
// Licensed to the .NET Foundation under one or more agreements.
// See the LICENSE file in the project root for more information.
using System.Globalization;
using System.IO;
using System.Text;
namespace System.Json
{
public class JsonPrimitive : JsonValue
{
private static readonly byte[] s_trueBytes = Encoding.UTF8.GetBytes("true");
private static readonly byte[] s_falseBytes = Encoding.UTF8.GetBytes("false");
private readonly object _value;
public JsonPrimitive(bool value)
{
_value = value;
}
public JsonPrimitive(byte value)
{
_value = value;
}
public JsonPrimitive(char value)
{
_value = value;
}
public JsonPrimitive(decimal value)
{
_value = value;
}
public JsonPrimitive(double value)
{
_value = value;
}
public JsonPrimitive(float value)
{
_value = value;
}
public JsonPrimitive(int value)
{
_value = value;
}
public JsonPrimitive(long value)
{
_value = value;
}
[CLSCompliant(false)]
public JsonPrimitive(sbyte value)
{
_value = value;
}
public JsonPrimitive(short value)
{
_value = value;
}
public JsonPrimitive(string value)
{
_value = value;
}
public JsonPrimitive(DateTime value)
{
_value = value;
}
[CLSCompliant(false)]
public JsonPrimitive(uint value)
{
_value = value;
}
[CLSCompliant(false)]
public JsonPrimitive(ulong value)
{
_value = value;
}
[CLSCompliant(false)]
public JsonPrimitive(ushort value)
{
_value = value;
}
public JsonPrimitive(DateTimeOffset value)
{
_value = value;
}
public JsonPrimitive(Guid value)
{
_value = value;
}
public JsonPrimitive(TimeSpan value)
{
_value = value;
}
public JsonPrimitive(Uri value)
{
_value = value;
}
internal object Value => _value;
public override JsonType JsonType =>
_value == null || _value.GetType() == typeof(char) || _value.GetType() == typeof(string) || _value.GetType() == typeof(DateTime) || _value.GetType() == typeof(object) ? JsonType.String : // DateTimeOffset || Guid || TimeSpan || Uri
_value.GetType() == typeof(bool) ? JsonType.Boolean :
JsonType.Number;
public override void Save(Stream stream)
{
switch (JsonType)
{
case JsonType.Boolean:
byte[] boolBytes = (bool)_value ? s_trueBytes : s_falseBytes;
stream.Write(boolBytes, 0, boolBytes.Length);
break;
case JsonType.String:
stream.WriteByte((byte)'\"');
byte[] bytes = Encoding.UTF8.GetBytes(EscapeString(_value.ToString()));
stream.Write(bytes, 0, bytes.Length);
stream.WriteByte((byte)'\"');
break;
default:
bytes = Encoding.UTF8.GetBytes(GetFormattedString());
stream.Write(bytes, 0, bytes.Length);
break;
}
}
internal string GetFormattedString()
{
switch (JsonType)
{
case JsonType.String:
if (_value is string || _value == null)
{
return (string)_value;
}
if (_value is char)
{
return _value.ToString();
}
throw new NotImplementedException(SR.Format(SR.NotImplemented_GetFormattedString, _value.GetType()));
case JsonType.Number:
string s = _value is float || _value is double ?
((IFormattable)_value).ToString("R", CultureInfo.InvariantCulture) : // Use "round-trip" format
((IFormattable)_value).ToString("G", CultureInfo.InvariantCulture);
return s == "NaN" || s == "Infinity" || s == "-Infinity" ?
"\"" + s + "\"" :
s;
default:
throw new InvalidOperationException();
}
}
}
}