您最多选择25个主题
主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
53 行
1.7 KiB
53 行
1.7 KiB
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Converters;
|
|
using Newtonsoft.Json.Utilities;
|
|
using UnityEngine;
|
|
|
|
namespace Unity.Services.Relay.Http
|
|
{
|
|
public class JsonObject
|
|
{
|
|
internal JsonObject(object obj)
|
|
{
|
|
this.obj = obj;
|
|
}
|
|
|
|
internal object obj;
|
|
|
|
public string GetAsString()
|
|
{
|
|
try
|
|
{
|
|
return JsonConvert.SerializeObject(obj);
|
|
}
|
|
catch (System.Exception)
|
|
{
|
|
throw new System.Exception("Failed to convert JsonObject to string.");
|
|
}
|
|
}
|
|
|
|
public T GetAs<T>(DeserializationSettings deserializationSettings = null)
|
|
{
|
|
// Check if derializationSettings is null so we can use the default value.
|
|
deserializationSettings = deserializationSettings ?? new DeserializationSettings();
|
|
JsonSerializerSettings jsonSettings = new JsonSerializerSettings
|
|
{
|
|
MissingMemberHandling = deserializationSettings.MissingMemberHandling == MissingMemberHandling.Error
|
|
? Newtonsoft.Json.MissingMemberHandling.Error
|
|
: Newtonsoft.Json.MissingMemberHandling.Ignore
|
|
};
|
|
try
|
|
{
|
|
return JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(obj), jsonSettings);
|
|
}
|
|
catch (Newtonsoft.Json.JsonSerializationException e)
|
|
{
|
|
throw new DeserializationException(e.Message);
|
|
}
|
|
catch (System.Exception)
|
|
{
|
|
throw new DeserializationException("Unable to deserialize object.");
|
|
}
|
|
}
|
|
}
|
|
}
|