Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 184 lines (158 sloc) 4.932 kb
c9ae144 Demis Bellot merge
mythz authored
1 using System;
2 using System.Collections.Generic;
66960dd Demis Bellot Only write JSON Object / Array, bool and numbers as raw values otherwise...
mythz authored
3 using System.IO;
4 using System.Text.RegularExpressions;
10ee32e Demis Bellot Add JsonValue to skip unescaping of JSON strings
mythz authored
5 using ServiceStack.Text.Common;
c9ae144 Demis Bellot 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 Demis Bellot 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 Demis Bellot 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 Demis Bellot Refactor to support PCL
mythz authored
119
120 static readonly Regex NumberRegEx = new Regex(@"^[0-9]*(?:\.[0-9]*)?$", PclExport.Instance.RegexOptions);
121
66960dd Demis Bellot Only write JSON Object / Array, bool and numbers as raw values otherwise...
mythz authored
122 /// <summary>
123 /// Write JSON Array, Object, bool or number values as raw string
124 /// </summary>
125 public static void WriteValue(TextWriter writer, object value)
126 {
127 var strValue = value as string;
128 if (!string.IsNullOrEmpty(strValue))
129 {
130 var firstChar = strValue[0];
10ee32e Demis Bellot Add JsonValue to skip unescaping of JSON strings
mythz authored
131 var lastChar = strValue[strValue.Length - 1];
132 if ((firstChar == JsWriter.MapStartChar && lastChar == JsWriter.MapEndChar)
133 || (firstChar == JsWriter.ListStartChar && lastChar == JsWriter.ListEndChar)
66960dd Demis Bellot Only write JSON Object / Array, bool and numbers as raw values otherwise...
mythz authored
134 || JsonUtils.True == strValue
135 || JsonUtils.False == strValue
136 || NumberRegEx.IsMatch(strValue))
137 {
138 writer.Write(strValue);
139 return;
140 }
141 }
142 JsonUtils.WriteString(writer, strValue);
143 }
c9ae144 Demis Bellot merge
mythz authored
144 }
145
146 public class JsonArrayObjects : List<JsonObject>
147 {
148 public static JsonArrayObjects Parse(string json)
149 {
150 return JsonSerializer.DeserializeFromString<JsonArrayObjects>(json);
151 }
152 }
153
c02c943 Demis Bellot Add new IValueWriter for being able to write raw JsonValue
mythz authored
154 public interface IValueWriter
155 {
156 void WriteTo(ITypeSerializer serializer, TextWriter writer);
157 }
158
159 public struct JsonValue : IValueWriter
10ee32e Demis Bellot Add JsonValue to skip unescaping of JSON strings
mythz authored
160 {
161 private readonly string json;
162
163 public JsonValue(string json)
164 {
165 this.json = json;
166 }
167
168 public T As<T>()
169 {
170 return JsonSerializer.DeserializeFromString<T>(json);
171 }
172
d5b06de Sergey Bogdanov fixed SL5 compilation issues
desunit authored
173 public override string ToString()
10ee32e Demis Bellot Add JsonValue to skip unescaping of JSON strings
mythz authored
174 {
175 return json;
176 }
c02c943 Demis Bellot Add new IValueWriter for being able to write raw JsonValue
mythz authored
177
178 public void WriteTo(ITypeSerializer serializer, TextWriter writer)
179 {
180 writer.Write(json ?? JsonUtils.Null);
181 }
10ee32e Demis Bellot Add JsonValue to skip unescaping of JSON strings
mythz authored
182 }
183
87ef43a Demis Bellot Added convenience methon JsonObject for parsing 3rd party JSON Apis.
mythz authored
184 }
Something went wrong with that request. Please try again.