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(this Dictionary map, string key) { return Get(map, key); } /// /// Get JSON string value converted to T /// public static T Get(this Dictionary map, string key) { string strVal; return map.TryGetValue(key, out strVal) ? JsonSerializer.DeserializeFromString(strVal) : default(T); } /// /// Get JSON string value /// public static string Get(this Dictionary 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 ConvertAll(this JsonArrayObjects jsonArrayObjects, Func converter) { var results = new List(); foreach (var jsonObject in jsonArrayObjects) { results.Add(converter(jsonObject)); } return results; } public static T ConvertTo(this JsonObject jsonObject, Func converFn) { return jsonObject == null ? default(T) : converFn(jsonObject); } public static Dictionary ToDictionary(this JsonObject jsonObject) { return jsonObject == null ? new Dictionary() : new Dictionary(jsonObject); } } public class JsonObject : Dictionary { /// /// Get JSON string value /// public new string this[string key] { get { return this.Get(key); } set { base[key] = value; } } public static JsonObject Parse(string json) { return JsonSerializer.DeserializeFromString(json); } public static JsonArrayObjects ParseArray(string json) { return JsonArrayObjects.Parse(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; } /// /// Get unescaped string value /// public string GetUnescaped(string key) { return base[key]; } /// /// Get unescaped string value /// 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 /// /// Write JSON Array, Object, bool or number values as raw string /// 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 { public static JsonArrayObjects Parse(string json) { return JsonSerializer.DeserializeFromString(json); } } public struct JsonValue { private readonly string json; public JsonValue(string json) { this.json = json; } public T As() { return JsonSerializer.DeserializeFromString(json); } public override string ToString() { return json; } } }