112 lines
3.6 KiB
C#
112 lines
3.6 KiB
C#
using System;
|
|
using System.IO;
|
|
using Cysharp.Threading.Tasks;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
using PhxhSDK;
|
|
using UnityEngine;
|
|
|
|
namespace Framework
|
|
{
|
|
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.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> LoadFromAddressble<T>(string path)
|
|
{
|
|
var loadAsset = await AssetManager.Instance.LoadAssetAsync<TextAsset>(path);
|
|
if (loadAsset == null)
|
|
{
|
|
return default(T);
|
|
}
|
|
var text = loadAsset.text;
|
|
return LoadJsonFromText<T>(text);
|
|
}
|
|
|
|
private static T LoadJsonFromText<T>(string text)
|
|
{
|
|
var serializer = new JsonSerializer();
|
|
serializer.NullValueHandling = NullValueHandling.Ignore;
|
|
serializer.ObjectCreationHandling = ObjectCreationHandling.Replace;
|
|
|
|
using (var sr = new StringReader(text))
|
|
{
|
|
using (var reader = new JsonTextReader(sr))
|
|
{
|
|
return serializer.Deserialize<T>(reader);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
} |