using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.Serialization; using System.Runtime.Serialization.Json; using System.Web; using System.IO; using Newtonsoft.Json; using Newtonsoft.Json.Linq; namespace DAL{ public class JsonHelper { public static byte[] ToArray(object o) { return Encoding.UTF8.GetBytes(ToJsonString(o)); } public static string ToJsonString(object o) { JsonSerializer ser = new JsonSerializer(); StringWriter write = new StringWriter(); ser.Serialize(write, o); return write.GetStringBuilder().ToString(); } public static string BuffToText(byte[] buf, int offset, int length) { return Encoding.UTF8.GetString(buf, offset, length); } public static string BuffToText(byte[] buf) { return Encoding.UTF8.GetString(buf); } public static string BytesToText(byte[] buf, int index, int len) { return Encoding.UTF8.GetString(buf, index, len); } public static void toJson<T>(string key, T v, StringBuilder builder) { JSON.SerializeString(key, builder); builder.Append(":"); if (v is string) { JSON.SerializeString(v as string, builder); } else { builder.Append(v.ToString()); } } public static void toFirstJson<T>(string key, T v, StringBuilder builder) { builder.Append("{"); toJson(key, v, builder); } public static void toMiddleJson<T>(string key, T v, StringBuilder builder) { builder.Append(","); toJson(key, v, builder); } public static void toLastJson<T>(string key, T v, StringBuilder builder) { toMiddleJson(key, v, builder); builder.Append("}"); } public static string toJson(Dictionary<string, object> dict) { StringBuilder builder = new StringBuilder(); toJson(dict, builder); return builder.ToString(); } public static void toJson(Dictionary<string, object> dict, StringBuilder builder) { builder.Append("{"); bool first = true; foreach (KeyValuePair<string, object> kv in dict) { if (first == false) builder.Append(","); else first = false; JSON.SerializeString(kv.Key, builder); builder.Append(":"); if (kv.Value is string) { JSON.SerializeString((string)kv.Value, builder); } else if (kv.Value is int) { builder.Append(kv.Value.ToString()); } else { JSON.SerializeString(kv.Value.ToString(), builder); } } builder.Append("}"); } public static string ObjectToJson(object item) { using (MemoryStream ms = ObjectToJsonStream(item)) { StringBuilder sb = new StringBuilder(); sb.Append(Encoding.UTF8.GetString(ms.ToArray())); return sb.ToString(); } } public static MemoryStream ObjectToJsonStream(object item) { DataContractJsonSerializer serializer = new DataContractJsonSerializer(item.GetType()); MemoryStream ms = new MemoryStream(); serializer.WriteObject(ms, item); return ms; } public static T JsonToObject<T>(string jsonString) { DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T)); using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString))) { T jsonObject = (T)ser.ReadObject(ms); return jsonObject; } } public static T BuffToObject<T>(byte[] buff) { DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T)); using (MemoryStream ms = new MemoryStream(buff)) { T jsonObject = (T)ser.ReadObject(ms); return jsonObject; } } public static T JsonToObject<T>(Stream ms) { DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T)); T jsonObject = (T)ser.ReadObject(ms); return jsonObject; } public static JToken ToJson(string str) { return JToken.Parse(str); } public static JToken ToJson(byte[] buf) { var str = Encoding.UTF8.GetString(buf); return JToken.Parse(str); } public static JToken ToJson(Stream ms) { var m = ms as MemoryStream; if (m == null) { m = new MemoryStream(); ms.CopyTo(m); } return ToJson(m.ToArray()); } public static byte[] ObjectToBytes(object o) { return Encoding.UTF8.GetBytes(ObjectToJsonString(o)); } public static string ObjectToJsonString(object o) { return JsonConvert.SerializeObject(o); } public static T BytesToObjectT<T>(byte[] buf) { return JsonConvert.DeserializeObject<T>(Encoding.UTF8.GetString(buf)); } public static T ParseStream<T>(Stream ms) { var s = new MemoryStream(); ms.CopyTo(s); return BytesToObjectT<T>(s.ToArray()); } public static T ParseStr<T>(string tx) { return JsonConvert.DeserializeObject<T>(tx); } public static T ParseBuf<T>(byte[] buf) { return JsonConvert.DeserializeObject<T>(Encoding.UTF8.GetString(buf)); } public static Dictionary<string, T> ToDict<T>(JObject json) { Dictionary<string, T> d = new Dictionary<string, T>(); foreach (KeyValuePair<string, JToken> kv in json) { d[kv.Key] = TokenToT<T>(kv.Value); } return d; } public static T[] ArrayToT<T>(JToken a) { List<T> list = new List<T>(); foreach (JToken m in a) { list.Add(TokenToT<T>(m)); } return list.ToArray(); } private static JValue EnsureValue(JToken value) { if (value == null) throw new ArgumentNullException("value"); if (value is JProperty) value = ((JProperty)value).Value; JValue v = value as JValue; return v; } public static T TokenToT<T>(JToken a) { JValue v = EnsureValue(a); return (T)(v.Value); } public static object ParseToObject(byte[] buff) { string txt = Encoding.UTF8.GetString(buff); try { var v = JToken.Parse(txt); return v; //JObject o = J.Parse(txt); //return o; } catch { } JArray oo = JArray.Parse(txt); return oo; } public static Stream ToStream(object o) { var str = JsonConvert.SerializeObject(o); var buf = Encoding.UTF8.GetBytes(str); return new MemoryStream(buf); } public static string ToStr(object o) { return JsonConvert.SerializeObject(o); } public static byte[] ToBuf(object o) { var str = JsonConvert.SerializeObject(o); return Encoding.UTF8.GetBytes(str); } } }