From 6eb915c129fc90c6f4c82ae097dd6ffad5239efc Mon Sep 17 00:00:00 2001 From: chai Date: Mon, 25 Jan 2021 14:28:30 +0800 Subject: +scripts --- .../Scripts/XUtliPoolLib/DeJson/Deserializer.cs | 318 +++++++++++++++++++++ 1 file changed, 318 insertions(+) create mode 100644 Client/Assets/Scripts/XUtliPoolLib/DeJson/Deserializer.cs (limited to 'Client/Assets/Scripts/XUtliPoolLib/DeJson/Deserializer.cs') diff --git a/Client/Assets/Scripts/XUtliPoolLib/DeJson/Deserializer.cs b/Client/Assets/Scripts/XUtliPoolLib/DeJson/Deserializer.cs new file mode 100644 index 00000000..d51e375f --- /dev/null +++ b/Client/Assets/Scripts/XUtliPoolLib/DeJson/Deserializer.cs @@ -0,0 +1,318 @@ +using System; +using System.Collections.Generic; +using System.Reflection; +using MiniJSON; + +namespace DeJson +{ + public class Deserializer + { + private Dictionary m_creators; + + public abstract class CustomCreator + { + public abstract object Create(Dictionary src, Dictionary parentSrc); + + public abstract Type TypeToCreate(); + } + + public Deserializer() + { + this.m_creators = new Dictionary(); + } + + public T Deserialize(string json) + { + object o = Json.Deserialize(json); + return this.Deserialize(o); + } + + public T Deserialize(object o) + { + return (T)((object)this.ConvertToType(o, typeof(T), null)); + } + + public void RegisterCreator(Deserializer.CustomCreator creator) + { + Type key = creator.TypeToCreate(); + this.m_creators[key] = creator; + } + + private object DeserializeO(Type destType, Dictionary src, Dictionary parentSrc) + { + object obj = null; + bool flag = destType == typeof(Dictionary); + object result; + if (flag) + { + result = src; + } + else + { + Deserializer.CustomCreator customCreator; + bool flag2 = this.m_creators.TryGetValue(destType, out customCreator); + if (flag2) + { + obj = customCreator.Create(src, parentSrc); + } + bool flag3 = obj == null; + if (flag3) + { + object obj2; + bool flag4 = src.TryGetValue("$dotNetType", out obj2); + if (flag4) + { + destType = Type.GetType((string)obj2); + } + obj = Activator.CreateInstance(destType); + } + this.DeserializeIt(obj, src); + result = obj; + } + return result; + } + + private void DeserializeIt(object dest, Dictionary src) + { + Type type = dest.GetType(); + FieldInfo[] fields = type.GetFields(); + this.DeserializeClassFields(dest, fields, src); + } + + private void DeserializeClassFields(object dest, FieldInfo[] fields, Dictionary src) + { + foreach (FieldInfo fieldInfo in fields) + { + bool isStatic = fieldInfo.IsStatic; + if (!isStatic) + { + object value; + bool flag = src.TryGetValue(fieldInfo.Name, out value); + if (flag) + { + this.DeserializeField(dest, fieldInfo, value, src); + } + } + } + } + + private void DeserializeField(object dest, FieldInfo info, object value, Dictionary src) + { + Type fieldType = info.FieldType; + object obj = this.ConvertToType(value, fieldType, src); + bool flag = fieldType.IsAssignableFrom(obj.GetType()); + if (flag) + { + info.SetValue(dest, obj); + } + } + + private object ConvertToType(object value, Type type, Dictionary src) + { + bool isArray = type.IsArray; + object result; + if (isArray) + { + result = this.ConvertToArray(value, type, src); + } + else + { + bool isGenericType = type.IsGenericType; + if (isGenericType) + { + result = this.ConvertToList(value, type, src); + } + else + { + bool flag = type == typeof(string); + if (flag) + { + result = Convert.ToString(value); + } + else + { + bool flag2 = type == typeof(byte); + if (flag2) + { + result = Convert.ToByte(value); + } + else + { + bool flag3 = type == typeof(sbyte); + if (flag3) + { + result = Convert.ToSByte(value); + } + else + { + bool flag4 = type == typeof(short); + if (flag4) + { + result = Convert.ToInt16(value); + } + else + { + bool flag5 = type == typeof(ushort); + if (flag5) + { + result = Convert.ToUInt16(value); + } + else + { + bool flag6 = type == typeof(int); + if (flag6) + { + result = Convert.ToInt32(value); + } + else + { + bool flag7 = type == typeof(uint); + if (flag7) + { + result = Convert.ToUInt32(value); + } + else + { + bool flag8 = type == typeof(long); + if (flag8) + { + result = Convert.ToInt64(value); + } + else + { + bool flag9 = type == typeof(ulong); + if (flag9) + { + result = Convert.ToUInt64(value); + } + else + { + bool flag10 = type == typeof(char); + if (flag10) + { + result = Convert.ToChar(value); + } + else + { + bool flag11 = type == typeof(double); + if (flag11) + { + result = Convert.ToDouble(value); + } + else + { + bool flag12 = type == typeof(float); + if (flag12) + { + result = Convert.ToSingle(value); + } + else + { + bool flag13 = type == typeof(int); + if (flag13) + { + result = Convert.ToInt32(value); + } + else + { + bool flag14 = type == typeof(float); + if (flag14) + { + result = Convert.ToSingle(value); + } + else + { + bool flag15 = type == typeof(double); + if (flag15) + { + result = Convert.ToDouble(value); + } + else + { + bool flag16 = type == typeof(bool); + if (flag16) + { + result = Convert.ToBoolean(value); + } + else + { + bool flag17 = type == typeof(bool); + if (flag17) + { + result = Convert.ToBoolean(value); + } + else + { + bool isValueType = type.IsValueType; + if (isValueType) + { + result = this.DeserializeO(type, (Dictionary)value, src); + } + else + { + bool isClass = type.IsClass; + if (isClass) + { + result = this.DeserializeO(type, (Dictionary)value, src); + } + else + { + result = value; + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + return result; + } + + private object ConvertToArray(object value, Type type, Dictionary src) + { + List list = (List)value; + int count = list.Count; + Type elementType = type.GetElementType(); + Array array = Array.CreateInstance(elementType, count); + int num = 0; + foreach (object value2 in list) + { + object value3 = this.ConvertToType(value2, elementType, src); + array.SetValue(value3, num); + num++; + } + return array; + } + + private object ConvertToList(object value, Type type, Dictionary src) + { + object obj = Activator.CreateInstance(type); + MethodInfo method = type.GetMethod("Add"); + List list = (List)value; + Type returnType = type.GetMethod("Find").ReturnType; + foreach (object value2 in list) + { + object obj2 = this.ConvertToType(value2, returnType, src); + method.Invoke(obj, new object[] + { + obj2 + }); + } + return obj; + } + } +} -- cgit v1.1-26-g67d0