JsonValue.cs
4.05 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
using System;
namespace UniJSON
{
public struct JsonValue : IListTreeItem, IValue<JsonValue>
{
public Utf8String Segment;
public ArraySegment<Byte> Bytes { get { return Segment.Bytes; } }
public void SetBytesCount(int count)
{
Segment = new Utf8String(new ArraySegment<byte>(Bytes.Array, Bytes.Offset, count));
}
public ValueNodeType ValueType
{
get;
private set;
}
public int ParentIndex
{
get;
private set;
}
int _childCount;
public int ChildCount
{
get { return _childCount; }
}
public void SetChildCount(int count)
{
_childCount = count;
}
public JsonValue(Utf8String segment, ValueNodeType valueType, int parentIndex) : this()
{
Segment = segment;
ValueType = valueType;
ParentIndex = parentIndex;
}
public JsonValue New(ArraySegment<byte> bytes, ValueNodeType valueType, int parentIndex)
{
return new JsonValue(new Utf8String(bytes), valueType, parentIndex);
}
public JsonValue Key(Utf8String key, int parentIndex)
{
return new JsonValue(JsonString.Quote(key), ValueNodeType.String, parentIndex);
}
public override string ToString()
{
switch (ValueType)
{
case ValueNodeType.Null:
case ValueNodeType.Boolean:
case ValueNodeType.Integer:
case ValueNodeType.Number:
case ValueNodeType.Array:
case ValueNodeType.Object:
case ValueNodeType.String:
case ValueNodeType.NaN:
case ValueNodeType.Infinity:
case ValueNodeType.MinusInfinity:
return Segment.ToString();
default:
throw new NotImplementedException();
}
}
static Utf8String s_true = Utf8String.From("true");
static Utf8String s_false = Utf8String.From("false");
public Boolean GetBoolean()
{
if (Segment == s_true)
{
return true;
}
else if (Segment == s_false)
{
return false;
}
else
{
throw new DeserializationException("invalid boolean: " + Segment.ToString());
}
}
public SByte GetSByte() { return Segment.ToSByte(); }
public Int16 GetInt16() { return Segment.ToInt16(); }
public Int32 GetInt32() { return Segment.ToInt32(); }
public Int64 GetInt64() { return Segment.ToInt64(); }
public Byte GetByte() { return Segment.ToByte(); }
public UInt16 GetUInt16() { return Segment.ToUInt16(); }
public UInt32 GetUInt32() { return Segment.ToUInt32(); }
public UInt64 GetUInt64() { return Segment.ToUInt64(); }
public Single GetSingle() { return Segment.ToSingle(); }
public Double GetDouble() { return Segment.ToDouble(); }
public String GetString() { return JsonString.Unquote(Segment.ToString()); }
public Utf8String GetUtf8String() { return JsonString.Unquote(Segment); }
public T GetValue<T>()
{
switch (ValueType)
{
case ValueNodeType.Null: return GenericCast<object, T>.Null();
case ValueNodeType.Boolean: return GenericCast<bool, T>.Cast(GetBoolean());
case ValueNodeType.Integer: return GenericCast<int, T>.Cast(GetInt32());
case ValueNodeType.Number:
case ValueNodeType.NaN:
case ValueNodeType.Infinity:
case ValueNodeType.MinusInfinity:
return GenericCast<double, T>.Cast(GetDouble());
case ValueNodeType.String: return GenericCast<string, T>.Cast(GetString());
}
throw new NotImplementedException();
}
}
}