forked from ServiceStack/ServiceStack.Text
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonObject.cs
More file actions
171 lines (150 loc) · 4.49 KB
/
JsonObject.cs
File metadata and controls
171 lines (150 loc) · 4.49 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using ServiceStack.Text.Common;
using ServiceStack.Text.Json;
namespace ServiceStack.Text
{
public static class JsonExtensions
{
public static T JsonTo<T>(this Dictionary<string, string> map, string key)
{
return Get<T>(map, key);
}
/// <summary>
/// Get JSON string value converted to T
/// </summary>
public static T Get<T>(this Dictionary<string, string> map, string key)
{
string strVal;
return map.TryGetValue(key, out strVal) ? JsonSerializer.DeserializeFromString<T>(strVal) : default(T);
}
/// <summary>
/// Get JSON string value
/// </summary>
public static string Get(this Dictionary<string, string> map, string key)
{
string strVal;
return map.TryGetValue(key, out strVal) ? JsonTypeSerializer.Instance.UnescapeString(strVal) : null;
}
public static JsonArrayObjects ArrayObjects(this string json)
{
return Text.JsonArrayObjects.Parse(json);
}
public static List<T> ConvertAll<T>(this JsonArrayObjects jsonArrayObjects, Func<JsonObject, T> converter)
{
var results = new List<T>();
foreach (var jsonObject in jsonArrayObjects)
{
results.Add(converter(jsonObject));
}
return results;
}
public static T ConvertTo<T>(this JsonObject jsonObject, Func<JsonObject, T> converFn)
{
return jsonObject == null
? default(T)
: converFn(jsonObject);
}
public static Dictionary<string, string> ToDictionary(this JsonObject jsonObject)
{
return jsonObject == null
? new Dictionary<string, string>()
: new Dictionary<string, string>(jsonObject);
}
}
public class JsonObject : Dictionary<string, string>
{
/// <summary>
/// Get JSON string value
/// </summary>
public new string this[string key]
{
get { return this.Get(key); }
set { base[key] = value; }
}
public static JsonObject Parse(string json)
{
return JsonSerializer.DeserializeFromString<JsonObject>(json);
}
public JsonArrayObjects ArrayObjects(string propertyName)
{
string strValue;
return this.TryGetValue(propertyName, out strValue)
? JsonArrayObjects.Parse(strValue)
: null;
}
public JsonObject Object(string propertyName)
{
string strValue;
return this.TryGetValue(propertyName, out strValue)
? Parse(strValue)
: null;
}
/// <summary>
/// Get unescaped string value
/// </summary>
public string GetUnescaped(string key)
{
return base[key];
}
/// <summary>
/// Get unescaped string value
/// </summary>
public string Child(string key)
{
return base[key];
}
#if !SILVERLIGHT && !MONOTOUCH
static readonly Regex NumberRegEx = new Regex(@"^[0-9]*(?:\.[0-9]*)?$", RegexOptions.Compiled);
#else
static readonly Regex NumberRegEx = new Regex(@"^[0-9]*(?:\.[0-9]*)?$");
#endif
/// <summary>
/// Write JSON Array, Object, bool or number values as raw string
/// </summary>
public static void WriteValue(TextWriter writer, object value)
{
var strValue = value as string;
if (!string.IsNullOrEmpty(strValue))
{
var firstChar = strValue[0];
var lastChar = strValue[strValue.Length - 1];
if ((firstChar == JsWriter.MapStartChar && lastChar == JsWriter.MapEndChar)
|| (firstChar == JsWriter.ListStartChar && lastChar == JsWriter.ListEndChar)
|| JsonUtils.True == strValue
|| JsonUtils.False == strValue
|| NumberRegEx.IsMatch(strValue))
{
writer.Write(strValue);
return;
}
}
JsonUtils.WriteString(writer, strValue);
}
}
public class JsonArrayObjects : List<JsonObject>
{
public static JsonArrayObjects Parse(string json)
{
return JsonSerializer.DeserializeFromString<JsonArrayObjects>(json);
}
}
public struct JsonValue
{
private readonly string json;
public JsonValue(string json)
{
this.json = json;
}
public T As<T>()
{
return JsonSerializer.DeserializeFromString<T>(json);
}
public override string ToString()
{
return json;
}
}
}