1
This commit is contained in:
51
Packages/Core/Utility/JsonHelper.cs
Normal file
51
Packages/Core/Utility/JsonHelper.cs
Normal file
@@ -0,0 +1,51 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
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);
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user