Forest_Client/Forest/Assets/PhxhSDK/Phxh/JsonHelper.cs

149 lines
4.7 KiB
C#

using System;
using System.IO;
using Cysharp.Threading.Tasks;
using LC.Newtonsoft.Json;
using LC.Newtonsoft.Json.Linq;
using UnityEngine;
namespace PhxhSDK.Phxh
{
public static class JsonHelper
{
#if UNITY_EDITOR
public static T LoadJson<T>(string path)
{
var serializer = new JsonSerializer();
serializer.NullValueHandling = NullValueHandling.Ignore;
serializer.ObjectCreationHandling = ObjectCreationHandling.Replace;
if (File.Exists(path))
{
using (var sr = new StreamReader(path))
{
using (var reader = new JsonTextReader(sr))
{
return serializer.Deserialize<T>(reader);
}
}
}
else
{
return default(T);
}
}
public static void SaveJson(string path, object obj)
{
JsonSerializer serializer = new JsonSerializer();
serializer.NullValueHandling = NullValueHandling.Ignore;
serializer.Formatting = Formatting.Indented;
// serializer.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
var pathWithoutFile = Path.GetDirectoryName(path);
if (!Directory.Exists(pathWithoutFile))
{
Directory.CreateDirectory(pathWithoutFile);
}
if (File.Exists(path))
{
File.Delete(path);
}
using (StreamWriter sw = File.CreateText(path))
using (JsonWriter writer = new JsonTextWriter(sw))
{
serializer.Serialize(writer, obj);
}
}
#endif
public static async UniTask<T> LoadFromAddressable<T>(string path)
{
try
{
var loadAsset = await AssetManager.Instance.LoadAssetAsync<TextAsset>(path);
if (loadAsset == null)
{
return default(T);
}
var text = loadAsset.text;
return LoadJsonFromText<T>(text);
}
catch (Exception e)
{
DebugUtil.LogError("JsonHelper.LoadFromAddressable Fail. Error: {0}", e);
return default(T);
}
}
public static T LoadFromAddressableSync<T>(string path)
{
try
{
var loadAsset = AssetManager.Instance.LoadAsset<TextAsset>(path);
if (loadAsset == null)
{
return default(T);
}
var text = loadAsset.text;
return LoadJsonFromText<T>(text);
}
catch (Exception e)
{
DebugUtil.LogError("JsonHelper.LoadFromAddressableSync Fail. Error: {0}", e);
return default(T);
}
}
private static T LoadJsonFromText<T>(string text)
{
var serializer = new JsonSerializer();
serializer.NullValueHandling = NullValueHandling.Ignore;
serializer.ObjectCreationHandling = ObjectCreationHandling.Replace;
try
{
using (var sr = new StringReader(text))
{
using (var reader = new JsonTextReader(sr))
{
return serializer.Deserialize<T>(reader);
}
}
}
catch (Exception e)
{
Debug.LogError($"JsonHelp.LoadJsonFromText. Error: {e}");
return default(T);
}
}
}
public class ColorJsonConverter : JsonConverter<Color>
{
public override void WriteJson(JsonWriter writer, Color value, JsonSerializer serializer)
{
JObject colorObject = new JObject();
colorObject.Add("r", value.r);
colorObject.Add("g", value.g);
colorObject.Add("b", value.b);
colorObject.Add("a", value.a);
colorObject.WriteTo(writer);
}
public override Color ReadJson(JsonReader reader, System.Type objectType, Color existingValue,
bool hasExistingValue, JsonSerializer serializer)
{
JObject colorObject = JObject.Load(reader);
float r = colorObject.GetValue("r").ToObject<float>();
float g = colorObject.GetValue("g").ToObject<float>();
float b = colorObject.GetValue("b").ToObject<float>();
float a = colorObject.GetValue("a").ToObject<float>();
return new Color(r, g, b, a);
}
}
}