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 +++++++++++++++ .../XUtliPoolLib/DeJson/Deserializer.cs.meta | 12 + .../Scripts/XUtliPoolLib/DeJson/Serializer.cs | 426 +++++++++++++++++++++ .../Scripts/XUtliPoolLib/DeJson/Serializer.cs.meta | 12 + 4 files changed, 768 insertions(+) create mode 100644 Client/Assets/Scripts/XUtliPoolLib/DeJson/Deserializer.cs create mode 100644 Client/Assets/Scripts/XUtliPoolLib/DeJson/Deserializer.cs.meta create mode 100644 Client/Assets/Scripts/XUtliPoolLib/DeJson/Serializer.cs create mode 100644 Client/Assets/Scripts/XUtliPoolLib/DeJson/Serializer.cs.meta (limited to 'Client/Assets/Scripts/XUtliPoolLib/DeJson') 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; + } + } +} diff --git a/Client/Assets/Scripts/XUtliPoolLib/DeJson/Deserializer.cs.meta b/Client/Assets/Scripts/XUtliPoolLib/DeJson/Deserializer.cs.meta new file mode 100644 index 00000000..f0743a55 --- /dev/null +++ b/Client/Assets/Scripts/XUtliPoolLib/DeJson/Deserializer.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: a7c295911059ac440a342c3b4aa2355d +timeCreated: 1611465738 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Client/Assets/Scripts/XUtliPoolLib/DeJson/Serializer.cs b/Client/Assets/Scripts/XUtliPoolLib/DeJson/Serializer.cs new file mode 100644 index 00000000..9f807611 --- /dev/null +++ b/Client/Assets/Scripts/XUtliPoolLib/DeJson/Serializer.cs @@ -0,0 +1,426 @@ +using System; +using System.Collections; +using System.Globalization; +using System.Reflection; +using System.Text; + +namespace DeJson +{ + public class Serializer + { + private StringBuilder m_builder; + + private bool m_includeTypeInfoForDerivedTypes; + + private bool m_prettyPrint; + + private string m_prefix; + + public static string Serialize(object obj, bool includeTypeInfoForDerivedTypes = false, bool prettyPrint = false) + { + Serializer serializer = new Serializer(includeTypeInfoForDerivedTypes, prettyPrint); + serializer.SerializeValue(obj); + return serializer.GetJson(); + } + + private Serializer(bool includeTypeInfoForDerivedTypes, bool prettyPrint) + { + this.m_builder = new StringBuilder(); + this.m_includeTypeInfoForDerivedTypes = includeTypeInfoForDerivedTypes; + this.m_prettyPrint = prettyPrint; + this.m_prefix = ""; + } + + private string GetJson() + { + return this.m_builder.ToString(); + } + + private void Indent() + { + bool prettyPrint = this.m_prettyPrint; + if (prettyPrint) + { + this.m_prefix += " "; + } + } + + private void Outdent() + { + bool prettyPrint = this.m_prettyPrint; + if (prettyPrint) + { + this.m_prefix = this.m_prefix.Substring(2); + } + } + + private void AddIndent() + { + bool prettyPrint = this.m_prettyPrint; + if (prettyPrint) + { + this.m_builder.Append(this.m_prefix); + } + } + + private void AddLine() + { + bool prettyPrint = this.m_prettyPrint; + if (prettyPrint) + { + this.m_builder.Append("\n"); + } + } + + private void AddSpace() + { + bool prettyPrint = this.m_prettyPrint; + if (prettyPrint) + { + this.m_builder.Append(" "); + } + } + + private void SerializeValue(object obj) + { + bool flag = obj == null; + if (flag) + { + this.m_builder.Append("undefined"); + } + else + { + Type type = obj.GetType(); + bool isArray = type.IsArray; + if (isArray) + { + this.SerializeArray(obj); + } + else + { + bool flag2 = type == typeof(string); + if (flag2) + { + this.SerializeString(obj as string); + } + else + { + bool flag3 = type == typeof(char); + if (flag3) + { + this.SerializeString(obj.ToString()); + } + else + { + bool flag4 = type == typeof(bool); + if (flag4) + { + this.m_builder.Append(((bool)obj) ? "true" : "false"); + } + else + { + bool flag5 = type == typeof(bool); + if (flag5) + { + this.m_builder.Append(((bool)obj) ? "true" : "false"); + this.m_builder.Append(Convert.ChangeType(obj, typeof(string))); + } + else + { + bool flag6 = type == typeof(int); + if (flag6) + { + this.m_builder.Append(obj); + } + else + { + bool flag7 = type == typeof(byte); + if (flag7) + { + this.m_builder.Append(obj); + } + else + { + bool flag8 = type == typeof(sbyte); + if (flag8) + { + this.m_builder.Append(obj); + } + else + { + bool flag9 = type == typeof(short); + if (flag9) + { + this.m_builder.Append(obj); + } + else + { + bool flag10 = type == typeof(ushort); + if (flag10) + { + this.m_builder.Append(obj); + } + else + { + bool flag11 = type == typeof(int); + if (flag11) + { + this.m_builder.Append(obj); + } + else + { + bool flag12 = type == typeof(uint); + if (flag12) + { + this.m_builder.Append(obj); + } + else + { + bool flag13 = type == typeof(long); + if (flag13) + { + this.m_builder.Append(obj); + } + else + { + bool flag14 = type == typeof(ulong); + if (flag14) + { + this.m_builder.Append(obj); + } + else + { + bool flag15 = type == typeof(float); + if (flag15) + { + this.m_builder.Append(((float)obj).ToString("R", CultureInfo.InvariantCulture)); + } + else + { + bool flag16 = type == typeof(double); + if (flag16) + { + this.m_builder.Append(((double)obj).ToString("R", CultureInfo.InvariantCulture)); + } + else + { + bool flag17 = type == typeof(float); + if (flag17) + { + this.m_builder.Append(((float)obj).ToString("R", CultureInfo.InvariantCulture)); + } + else + { + bool flag18 = type == typeof(double); + if (flag18) + { + this.m_builder.Append(((double)obj).ToString("R", CultureInfo.InvariantCulture)); + } + else + { + bool isValueType = type.IsValueType; + if (isValueType) + { + this.SerializeObject(obj); + } + else + { + bool isClass = type.IsClass; + if (!isClass) + { + throw new InvalidOperationException("unsupport type: " + type.Name); + } + this.SerializeObject(obj); + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + + private void SerializeArray(object obj) + { + this.m_builder.Append("["); + this.AddLine(); + this.Indent(); + Array array = obj as Array; + bool flag = true; + foreach (object obj2 in array) + { + bool flag2 = !flag; + if (flag2) + { + this.m_builder.Append(","); + this.AddLine(); + } + this.AddIndent(); + this.SerializeValue(obj2); + flag = false; + } + this.AddLine(); + this.Outdent(); + this.AddIndent(); + this.m_builder.Append("]"); + } + + private void SerializeDictionary(IDictionary obj) + { + bool flag = true; + foreach (object obj2 in obj.Keys) + { + bool flag2 = !flag; + if (flag2) + { + this.m_builder.Append(','); + this.AddLine(); + } + this.AddIndent(); + this.SerializeString(obj2.ToString()); + this.m_builder.Append(':'); + this.AddSpace(); + this.SerializeValue(obj[obj2]); + flag = false; + } + } + + private void SerializeObject(object obj) + { + this.m_builder.Append("{"); + this.AddLine(); + this.Indent(); + bool flag = true; + bool includeTypeInfoForDerivedTypes = this.m_includeTypeInfoForDerivedTypes; + if (includeTypeInfoForDerivedTypes) + { + Type type = obj.GetType(); + Type baseType = type.BaseType; + bool flag2 = baseType != null && baseType != typeof(object); + if (flag2) + { + this.AddIndent(); + this.SerializeString("$dotNetType"); + this.m_builder.Append(":"); + this.AddSpace(); + this.SerializeString(type.FullName); + } + } + IDictionary obj2; + bool flag3 = (obj2 = (obj as IDictionary)) != null; + if (flag3) + { + this.SerializeDictionary(obj2); + } + else + { + FieldInfo[] fields = obj.GetType().GetFields(); + foreach (FieldInfo fieldInfo in fields) + { + bool isStatic = fieldInfo.IsStatic; + if (!isStatic) + { + object value = fieldInfo.GetValue(obj); + bool flag4 = value != null; + if (flag4) + { + bool flag5 = !flag; + if (flag5) + { + this.m_builder.Append(","); + this.AddLine(); + } + this.AddIndent(); + this.SerializeString(fieldInfo.Name); + this.m_builder.Append(":"); + this.AddSpace(); + this.SerializeValue(value); + flag = false; + } + } + } + } + this.AddLine(); + this.Outdent(); + this.AddIndent(); + this.m_builder.Append("}"); + } + + private void SerializeString(string str) + { + this.m_builder.Append('"'); + char[] array = str.ToCharArray(); + char[] array2 = array; + int i = 0; + while (i < array2.Length) + { + char c = array2[i]; + char c2 = c; + switch (c2) + { + case '\b': + this.m_builder.Append("\\b"); + break; + case '\t': + this.m_builder.Append("\\t"); + break; + case '\n': + this.m_builder.Append("\\n"); + break; + case '\v': + goto IL_F2; + case '\f': + this.m_builder.Append("\\f"); + break; + case '\r': + this.m_builder.Append("\\r"); + break; + default: + if (c2 != '"') + { + if (c2 != '\\') + { + goto IL_F2; + } + this.m_builder.Append("\\\\"); + } + else + { + this.m_builder.Append("\\\""); + } + break; + } + IL_150: + i++; + continue; + IL_F2: + int num = Convert.ToInt32(c); + bool flag = num >= 32 && num <= 126; + if (flag) + { + this.m_builder.Append(c); + } + else + { + this.m_builder.Append("\\u"); + this.m_builder.Append(num.ToString("x4")); + } + goto IL_150; + } + this.m_builder.Append('"'); + } + } +} diff --git a/Client/Assets/Scripts/XUtliPoolLib/DeJson/Serializer.cs.meta b/Client/Assets/Scripts/XUtliPoolLib/DeJson/Serializer.cs.meta new file mode 100644 index 00000000..7198cf9d --- /dev/null +++ b/Client/Assets/Scripts/XUtliPoolLib/DeJson/Serializer.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 22c7aad9d168e1144ac7c2f16f522d47 +timeCreated: 1611465299 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: -- cgit v1.1-26-g67d0