using System; using Newtonsoft.Json; namespace BITKit { public class JsonHelper { public static bool IsJson(string json) { switch (json.Substring(0, 1)) { case "{": case "[": case "\"": return true; default: return false; } } public static byte[] GetBytes(string json) { return StringHelper.GetBytes(json); } public static byte[] GetBytes(T obj) { return GetBytes(JsonConvert.SerializeObject(obj, Formatting.Indented)); } public static T Get(string json) { return JsonConvert.DeserializeObject(json); } public static string Get(T obj) { return JsonConvert.SerializeObject(obj,Formatting.Indented); } public static T Get(byte[] buffer) { return Get(StringHelper.GetString(buffer)); } public static object Get(Type type, byte[] buffer) { var json = StringHelper.GetString(buffer); return JsonConvert.DeserializeObject(json, type); } } }