Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 211 lines (183 sloc) 5.675 kB
c9ae144 @mythz merge
mythz authored
1 using System;
2 using System.Collections.Generic;
66960dd @mythz Only write JSON Object / Array, bool and numbers as raw values otherw…
mythz authored
3 using System.IO;
4 using System.Text.RegularExpressions;
10ee32e @mythz Add JsonValue to skip unescaping of JSON strings
mythz authored
5 using ServiceStack.Text.Common;
c9ae144 @mythz merge
mythz authored
6 using ServiceStack.Text.Json;
7
8 namespace ServiceStack.Text
9 {
10 public static class JsonExtensions
11 {
12 public static T JsonTo<T>(this Dictionary<string, string> map, string key)
13 {
14 return Get<T>(map, key);
15 }
16
17 /// <summary>
18 /// Get JSON string value converted to T
19 /// </summary>
20 public static T Get<T>(this Dictionary<string, string> map, string key)
21 {
22 string strVal;
23 return map.TryGetValue(key, out strVal) ? JsonSerializer.DeserializeFromString<T>(strVal) : default(T);
24 }
25
26 /// <summary>
27 /// Get JSON string value
28 /// </summary>
29 public static string Get(this Dictionary<string, string> map, string key)
30 {
31 string strVal;
32 return map.TryGetValue(key, out strVal) ? JsonTypeSerializer.Instance.UnescapeString(strVal) : null;
33 }
34
35 public static JsonArrayObjects ArrayObjects(this string json)
36 {
37 return Text.JsonArrayObjects.Parse(json);
38 }
39
40 public static List<T> ConvertAll<T>(this JsonArrayObjects jsonArrayObjects, Func<JsonObject, T> converter)
41 {
42 var results = new List<T>();
43
44 foreach (var jsonObject in jsonArrayObjects)
45 {
46 results.Add(converter(jsonObject));
47 }
48
49 return results;
50 }
51
52 public static T ConvertTo<T>(this JsonObject jsonObject, Func<JsonObject, T> converFn)
53 {
54 return jsonObject == null
55 ? default(T)
56 : converFn(jsonObject);
57 }
58
59 public static Dictionary<string, string> ToDictionary(this JsonObject jsonObject)
60 {
61 return jsonObject == null
62 ? new Dictionary<string, string>()
63 : new Dictionary<string, string>(jsonObject);
64 }
65 }
66
67 public class JsonObject : Dictionary<string, string>
68 {
69 /// <summary>
70 /// Get JSON string value
71 /// </summary>
72 public new string this[string key]
73 {
74 get { return this.Get(key); }
75 set { base[key] = value; }
76 }
77
15dfd14 @mythz INCR to v3.9.44
mythz authored
78 public static JsonObject Parse(string json)
79 {
80 return JsonSerializer.DeserializeFromString<JsonObject>(json);
81 }
82
83 public static JsonArrayObjects ParseArray(string json)
84 {
85 return JsonArrayObjects.Parse(json);
86 }
c9ae144 @mythz merge
mythz authored
87
88 public JsonArrayObjects ArrayObjects(string propertyName)
89 {
90 string strValue;
91 return this.TryGetValue(propertyName, out strValue)
92 ? JsonArrayObjects.Parse(strValue)
93 : null;
94 }
95
96 public JsonObject Object(string propertyName)
97 {
98 string strValue;
99 return this.TryGetValue(propertyName, out strValue)
100 ? Parse(strValue)
101 : null;
102 }
103
104 /// <summary>
105 /// Get unescaped string value
106 /// </summary>
107 public string GetUnescaped(string key)
108 {
109 return base[key];
110 }
111
112 /// <summary>
113 /// Get unescaped string value
114 /// </summary>
115 public string Child(string key)
116 {
117 return base[key];
118 }
d69b1ac @mythz Refactor to support PCL
mythz authored
119
66960dd @mythz Only write JSON Object / Array, bool and numbers as raw values otherw…
mythz authored
120 /// <summary>
121 /// Write JSON Array, Object, bool or number values as raw string
122 /// </summary>
123 public static void WriteValue(TextWriter writer, object value)
124 {
125 var strValue = value as string;
126 if (!string.IsNullOrEmpty(strValue))
127 {
128 var firstChar = strValue[0];
10ee32e @mythz Add JsonValue to skip unescaping of JSON strings
mythz authored
129 var lastChar = strValue[strValue.Length - 1];
130 if ((firstChar == JsWriter.MapStartChar && lastChar == JsWriter.MapEndChar)
131 || (firstChar == JsWriter.ListStartChar && lastChar == JsWriter.ListEndChar)
66960dd @mythz Only write JSON Object / Array, bool and numbers as raw values otherw…
mythz authored
132 || JsonUtils.True == strValue
133 || JsonUtils.False == strValue
e5d756e @mythz Remove RegEx in favour of simple logic to determine valid JSON Numbers
mythz authored
134 || IsJavaScriptNumber(strValue))
66960dd @mythz Only write JSON Object / Array, bool and numbers as raw values otherw…
mythz authored
135 {
136 writer.Write(strValue);
137 return;
138 }
139 }
140 JsonUtils.WriteString(writer, strValue);
141 }
85b0a85 @mythz Add range check for inferring JavaScript number
mythz authored
142
143 private static bool IsJavaScriptNumber(string strValue)
144 {
e5d756e @mythz Remove RegEx in favour of simple logic to determine valid JSON Numbers
mythz authored
145 var firstChar = strValue[0];
146 if (firstChar == '0')
147 {
148 if (strValue.Length == 1)
149 return true;
150 if (!strValue.Contains("."))
151 return false;
152 }
153
85b0a85 @mythz Add range check for inferring JavaScript number
mythz authored
154 if (!strValue.Contains("."))
155 {
156 long longValue;
157 if (long.TryParse(strValue, out longValue))
158 {
159 return longValue < JsonUtils.MaxInteger && longValue > JsonUtils.MinInteger;
160 }
161 return false;
162 }
163
164 double doubleValue;
165 if (double.TryParse(strValue, out doubleValue))
166 {
167 return doubleValue < JsonUtils.MaxInteger && doubleValue > JsonUtils.MinInteger;
168 }
169 return false;
170 }
171 }
c9ae144 @mythz merge
mythz authored
172
173 public class JsonArrayObjects : List<JsonObject>
174 {
175 public static JsonArrayObjects Parse(string json)
176 {
177 return JsonSerializer.DeserializeFromString<JsonArrayObjects>(json);
178 }
179 }
180
c02c943 @mythz Add new IValueWriter for being able to write raw JsonValue
mythz authored
181 public interface IValueWriter
182 {
183 void WriteTo(ITypeSerializer serializer, TextWriter writer);
184 }
185
186 public struct JsonValue : IValueWriter
10ee32e @mythz Add JsonValue to skip unescaping of JSON strings
mythz authored
187 {
188 private readonly string json;
189
190 public JsonValue(string json)
191 {
192 this.json = json;
193 }
194
195 public T As<T>()
196 {
197 return JsonSerializer.DeserializeFromString<T>(json);
198 }
199
d5b06de @desunit fixed SL5 compilation issues
desunit authored
200 public override string ToString()
10ee32e @mythz Add JsonValue to skip unescaping of JSON strings
mythz authored
201 {
202 return json;
203 }
c02c943 @mythz Add new IValueWriter for being able to write raw JsonValue
mythz authored
204
205 public void WriteTo(ITypeSerializer serializer, TextWriter writer)
206 {
207 writer.Write(json ?? JsonUtils.Null);
208 }
10ee32e @mythz Add JsonValue to skip unescaping of JSON strings
mythz authored
209 }
210
87ef43a @mythz Added convenience methon JsonObject for parsing 3rd party JSON Apis.
mythz authored
211 }
Something went wrong with that request. Please try again.