WXMC/.svn/pristine/8c/8c2164c6ac1fd67a724209ba9b216f2536532f5c.svn-base
2024-12-04 16:18:46 +08:00

143 lines
4.2 KiB
Plaintext

using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using CommonSerializeDefine;
using System.Linq;
public class UnitySerializer
{
public readonly static CommonSerializing.SerializeSetting Settings = new CommonSerializing.SerializeSetting()
{
AdditionalSerializers = new List<CommonSerializeDefine.ITypeSerializer>()
{
new GameObjectSerializer(),
new ComponentSerializer(),
new ScriptableObjectSerializer(),
new AddressableSerializer(),
},
};
private class CustomJsonConverter : JsonConverter
{
public List<ITypeSerializer> AllTypeSerializers;
private Dictionary<Type, ITypeSerializer> m_typeSerializers = new Dictionary<Type, ITypeSerializer>();
public override bool CanConvert(Type objectType)
{
if (m_typeSerializers.ContainsKey(objectType))
{
return true;
}
foreach (var ser in AllTypeSerializers)
{
if (ser.CanSerialize(objectType))
{
m_typeSerializers.Add(objectType, ser);
return true;
}
}
return false;
}
public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
{
JObject jo = JObject.Load(reader);
return m_typeSerializers[objectType].Deserialize((string)jo["Content"], objectType, null);
}
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
{
Type type = value.GetType();
JObject jo = new JObject();
jo["Content"] = m_typeSerializers[type].Serialize(value, type, null);
jo.WriteTo(writer);
}
}
private static readonly JsonSerializerSettings s_serializerSettings = new JsonSerializerSettings
{
Converters = new JsonConverter[]
{
new StringEnumConverter(),
new CustomJsonConverter()
{
AllTypeSerializers = Settings.AdditionalSerializers.ToList(),
},
}, // Use StringEnumConverter to serialize enums as strings
NullValueHandling = NullValueHandling.Ignore, // Ignore null values during serialization
Formatting = Formatting.Indented,
};
private static UnitySerializer s_instance;
public static UnitySerializer Instance
{
get
{
s_instance = s_instance ?? new UnitySerializer();
return s_instance;
}
}
private JsonSerializer m_serializer;
private JsonSerializer GetOrCreateSerializer()
{
m_serializer = m_serializer ?? JsonSerializer.Create(s_serializerSettings);
return m_serializer;
}
public string Serialize(object obj)
{
if (obj == null)
{
return null;
}
var serializer = GetOrCreateSerializer();
using (MemoryStream ms = new MemoryStream())
{
using (StreamWriter writer = new StreamWriter(ms))
{
serializer.Serialize(writer, obj);
writer.Flush(); // Ensure all data is written to the MemoryStream
ms.Position = 0; // Reset the position to the beginning of the stream
using (StreamReader reader = new StreamReader(ms))
{
return reader.ReadToEnd(); // Read the serialized data as a string
}
}
}
}
public T Deserialize<T>(string content)
{
return (T)Deserialize(typeof(T), content);
}
public object Deserialize(Type type, string content)
{
if (string.IsNullOrEmpty(content))
{
return null;
}
JsonSerializer serializer = GetOrCreateSerializer();
using (var reader = new JsonTextReader(new System.IO.StringReader(content)))
{
return serializer.Deserialize(reader, type);
}
}
}