Net.Like.Xue.Tokyo/Assets/BITKit/Core/Utility/JsonHelper.cs

47 lines
1.2 KiB
C#

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>(T obj)
{
return GetBytes(JsonConvert.SerializeObject(obj, Formatting.Indented));
}
public static T Get<T>(string json)
{
return JsonConvert.DeserializeObject<T>(json);
}
public static string Get<T>(T obj)
{
return JsonConvert.SerializeObject(obj,Formatting.Indented);
}
public static T Get<T>(byte[] buffer)
{
return Get<T>(StringHelper.GetString(buffer));
}
public static object Get(Type type, byte[] buffer)
{
var json = StringHelper.GetString(buffer);
return JsonConvert.DeserializeObject(json, type);
}
}
}