//--------------------------------------------------------------------------------- // DeserializeListElement //--------------------------------------------------------------------------------- private static object DeserializeListElement(JSONInStream stream, Type type) { switch (type.ToString()) { case "System.String": string resS; stream.Content(0, out resS); return(resS); case "System.Single": float resF; stream.Content(0, out resF); return(resF); case "System.Int32": int resI; stream.Content(0, out resI); return(resI); case "System.Boolean": bool resB; stream.Content(0, out resB); return(resB); case "UnityEngine.Vector3": Vector3 resV; stream.Content(0, out resV); return(resV); case "UnityEngine.Quaternion": Quaternion resQ; stream.Content(0, out resQ); return(resQ); case "UnityEngine.Color": Color resC; stream.Content(0, out resC); return(resC); case "UnityEngine.Rect": Rect resR; stream.Content(0, out resR); return(resR); case "UnityEngine.Vector2": Vector2 resV2; stream.Content(0, out resV2); return(resV2); default: stream.Start(0); object created = DeserializeObject(stream, type); stream.End(); return(created); } }
void ExampleBasicJSON() { // Let's serialize a simple address book JSONOutStream outStream = new JSONOutStream(); outStream.Content("a", 1) .Content("b", 2) .Start("c") .Content("d", 3) .End(); string serialized = outStream.Serialize(); // serialized outputs this JSON structure: // {a:1, b: 2, c: {d: 3}} // Deserialize it JSONInStream inStream = new JSONInStream(serialized); int a, b, d; inStream.Content("a", out a) .Content("b", out b) .Start("c") .Content("d", out d) .End(); Debug.Log("SERIALIZED JSON STRING: " + serialized); Debug.Log("JSON DESERIALIZATION of a:" + a + ", b: " + b + ", d: " + d); }
protected override int ParseArrayElement(JSONInStream stream) { int val = -1; stream.Content(out val); return(val); }
protected override string ParseArrayElement(JSONInStream stream) { string val; stream.Content(out val); return(val); }
void ExampleBasicJSON() { // Let's serialize a simple address book JSONOutStream outStream = new JSONOutStream(); outStream.Content("a", 1) .Content("b", 2) .Start("c") .Content("d", 3) .End(); string serialized = outStream.Serialize(); // serialized outputs this JSON structure: // {a:1, b: 2, c: {d: 3}} // Deserialize it JSONInStream inStream = new JSONInStream(serialized); int a, b, d; inStream.Content("a", out a) .Content("b", out b) .Start("c") .Content("d", out d) .End(); Debug.Log("SERIALIZED JSON STRING: " + serialized); Debug.Log("JSON DESERIALIZATION of a:" + a + ", b: " + b + ", d: " + d ); }
/// <summary> /// 解析reslist.json文件,返回其內容 /// </summary> /// <param name="outputPath"></param> /// <param name="reslistJsonVer">配置文件版本號</param> /// <returns></returns> static private Dictionary <string, AssetBundleInfo> LoadResConf(string outputPath, out int reslistJsonVer, out FileInfo fileInfo) { //資源信息 Dictionary <string, AssetBundleInfo> ret = new Dictionary <string, AssetBundleInfo>(); //讀取現有的資源配置文件 reslist //先獲取當前已經打包的AssetBundle資源的信息 //然後再這些信息的基礎上做升級 fileInfo = new FileInfo(outputPath + "/reslist.json"); reslistJsonVer = 0; if (fileInfo.Exists) { JSONInStream jInStream = new JSONInStream(File.ReadAllText(fileInfo.FullName)); jInStream.Content("version", out reslistJsonVer) .List("assets", delegate(int index, JSONInStream jsonInStream) { string assetbundlename = null; int version = 0; string hash = null; int size = 0; jsonInStream.Start(0) .Content("assetbundlename", out assetbundlename) .Content("version", out version) .Content("size", out size) .Content("hash", out hash) .End(); //Debug.LogFormat("------- {0}, {1}", index, assetbundlename); AssetBundleInfo info; info.Packaged = false; info.Path = assetbundlename; info.Size = size; info.Version = version; info.Hash = hash; ret.Add(assetbundlename, info); } ) .End(); } else { reslistJsonVer = 1; //沒有reslist.json文件,創建文件 if (!fileInfo.Directory.Exists) { Directory.CreateDirectory(fileInfo.Directory.FullName); } fileInfo.Create().Close(); } return(ret); }
private static object DeserializeListElement(JSONInStream stream, Type type) { //IL_01a8: Unknown result type (might be due to invalid IL or missing references) //IL_01ba: Unknown result type (might be due to invalid IL or missing references) //IL_01cc: Unknown result type (might be due to invalid IL or missing references) //IL_01de: Unknown result type (might be due to invalid IL or missing references) //IL_01f0: Unknown result type (might be due to invalid IL or missing references) if (!type.IsEnum) { switch (type.ToString()) { case "System.String": stream.Content(0, out string value13); return(value13); case "System.Single": stream.Content(0, out float value12); return(value12); case "System.Double": stream.Content(0, out double value11); return(value11); case "System.Int32": stream.Content(0, out int value10); return(value10); case "System.Boolean": stream.Content(0, out bool value9); return(value9); case "UnityEngine.Vector3": stream.Content(0, out Vector3 value8); return(value8); case "UnityEngine.Quaternion": stream.Content(0, out Quaternion value7); return(value7); case "UnityEngine.Color": stream.Content(0, out Color value6); return(value6); case "UnityEngine.Rect": stream.Content(0, out Rect value5); return(value5); case "UnityEngine.Vector2": stream.Content(0, out Vector2 value4); return(value4); case "XorInt": stream.Content(0, out XorInt value3); return(value3); case "XorUInt": stream.Content(0, out XorUInt value2); return(value2); case "XorFloat": stream.Content(0, out XorFloat value); return(value); default: { object result = DeserializeObject(stream, type); stream.End(); return(result); } } } stream.Content(0, out int value14); return(Enum.Parse(type, value14.ToString())); }
private static object DeserializeObject(JSONInStream stream, Type type) { //IL_0291: Unknown result type (might be due to invalid IL or missing references) //IL_02c4: Unknown result type (might be due to invalid IL or missing references) //IL_02f7: Unknown result type (might be due to invalid IL or missing references) //IL_032a: Unknown result type (might be due to invalid IL or missing references) //IL_035d: Unknown result type (might be due to invalid IL or missing references) MethodInfo method = type.GetMethod("FromJSON"); if (method != null) { return(method.Invoke(null, new object[1] { stream })); } object obj = Activator.CreateInstance(type); IEnumerable <FieldInfo> targetFields = GetTargetFields(type); foreach (FieldInfo item in targetFields) { switch (item.FieldType.ToString()) { case "System.String": if (stream.Has(GetName(item))) { stream.Content(GetName(item), out string value7); item.SetValue(obj, value7); } break; case "System.Single": if (stream.Has(GetName(item))) { stream.Content(GetName(item), out float value12); item.SetValue(obj, value12); } break; case "System.Double": if (stream.Has(GetName(item))) { stream.Content(GetName(item), out double value16); item.SetValue(obj, value16); } break; case "System.Int32": if (stream.Has(GetName(item))) { stream.Content(GetName(item), out int value9); item.SetValue(obj, value9); } break; case "System.Boolean": if (stream.Has(GetName(item))) { stream.Content(GetName(item), out bool value5); item.SetValue(obj, value5); } break; case "UnityEngine.Vector3": if (stream.Has(GetName(item))) { stream.Content(GetName(item), out Vector3 value14); item.SetValue(obj, value14); } break; case "UnityEngine.Quaternion": if (stream.Has(GetName(item))) { stream.Content(GetName(item), out Quaternion value10); item.SetValue(obj, value10); } break; case "UnityEngine.Color": if (stream.Has(GetName(item))) { stream.Content(GetName(item), out Color value8); item.SetValue(obj, value8); } break; case "UnityEngine.Rect": if (stream.Has(GetName(item))) { stream.Content(GetName(item), out Rect value6); item.SetValue(obj, value6); } break; case "UnityEngine.Vector2": if (stream.Has(GetName(item))) { stream.Content(GetName(item), out Vector2 value4); item.SetValue(obj, value4); } break; case "XorInt": if (stream.Has(GetName(item))) { stream.Content(GetName(item), out XorInt value15); item.SetValue(obj, value15); } break; case "XorUInt": if (stream.Has(GetName(item))) { stream.Content(GetName(item), out XorUInt value13); item.SetValue(obj, value13); } break; case "XorFloat": if (stream.Has(GetName(item))) { stream.Content(GetName(item), out XorFloat value11); item.SetValue(obj, value11); } break; default: if (stream.Has(GetName(item))) { if (item.FieldType.IsEnum) { stream.Content(GetName(item), out string value); item.SetValue(obj, Enum.Parse(item.FieldType, value)); } else if (item.FieldType.IsGenericType) { Type containedType = item.FieldType.GetGenericArguments()[0]; Type typeFromHandle = typeof(List <>); Type type2 = typeFromHandle.MakeGenericType(containedType); MethodInfo addMethod = type2.GetMethod("Add"); object list = Activator.CreateInstance(type2); stream.List(GetName(item), delegate(int i, JSONInStream stream2) { object obj3 = DeserializeListElement(stream2, containedType); addMethod.Invoke(list, new object[1] { obj3 }); }); item.SetValue(obj, list); } else if (item.FieldType.IsArray) { Type containedType2 = item.FieldType.GetElementType(); Type typeFromHandle2 = typeof(List <>); Type type3 = typeFromHandle2.MakeGenericType(containedType2); MethodInfo addMethod2 = type3.GetMethod("Add"); MethodInfo method2 = type3.GetMethod("ToArray"); object list2 = Activator.CreateInstance(type3); stream.List(GetName(item), delegate(int i, JSONInStream stream2) { object obj2 = DeserializeListElement(stream2, containedType2); addMethod2.Invoke(list2, new object[1] { obj2 }); }); object value2 = method2.Invoke(list2, new object[0]); item.SetValue(obj, value2); } else { stream.Start(GetName(item)); object value3 = DeserializeObject(stream, item.FieldType); stream.End(); item.SetValue(obj, value3); } } break; } } return(obj); }
//--------------------------------------------------------------------------------- // DeserializeObject //--------------------------------------------------------------------------------- private static object DeserializeObject(JSONInStream stream, Type type) { MethodInfo fromJSON = type.GetMethod("FromJSON"); if (fromJSON != null) { return(fromJSON.Invoke(null, new object[] { stream })); } object data = Activator.CreateInstance(type); System.Reflection.FieldInfo[] fieldInfo = type.GetFields(); foreach (System.Reflection.FieldInfo info in fieldInfo) { switch (info.FieldType.ToString()) { case "System.String": string resS; if (stream.Has(info.Name)) { stream.Content(info.Name, out resS); info.SetValue(data, resS); } break; case "System.Single": float resF; if (stream.Has(info.Name)) { stream.Content(info.Name, out resF); info.SetValue(data, resF); } break; case "System.Int32": int resI; if (stream.Has(info.Name)) { stream.Content(info.Name, out resI); info.SetValue(data, resI); } break; case "System.Boolean": bool resB; if (stream.Has(info.Name)) { stream.Content(info.Name, out resB); info.SetValue(data, resB); } break; case "UnityEngine.Vector3": Vector3 resV; if (stream.Has(info.Name)) { stream.Content(info.Name, out resV); info.SetValue(data, resV); } break; case "UnityEngine.Quaternion": Quaternion resQ; if (stream.Has(info.Name)) { stream.Content(info.Name, out resQ); info.SetValue(data, resQ); } break; case "UnityEngine.Color": Color resC; if (stream.Has(info.Name)) { stream.Content(info.Name, out resC); info.SetValue(data, resC); } break; case "UnityEngine.Rect": Rect resR; if (stream.Has(info.Name)) { stream.Content(info.Name, out resR); info.SetValue(data, resR); } break; case "UnityEngine.Vector2": Vector2 resV2; if (stream.Has(info.Name)) { stream.Content(info.Name, out resV2); info.SetValue(data, resV2); } break; default: if (stream.Has(info.Name)) { if (info.FieldType.IsEnum) // Enum { string e; stream.Content(info.Name, out e); info.SetValue(data, Enum.Parse(info.FieldType, e)); } else if (info.FieldType.IsGenericType) // can only be a List { Type containedType = info.FieldType.GetGenericArguments()[0]; Type typeList = typeof(List <>); Type actualType = typeList.MakeGenericType(containedType); MethodInfo addMethod = actualType.GetMethod("Add"); object list = Activator.CreateInstance(actualType); stream.List(info.Name, delegate(int i, JSONInStream stream2){ object o = DeserializeListElement(stream2, containedType); addMethod.Invoke(list, new object[] { o }); }); info.SetValue(data, list); } else if (info.FieldType.IsArray) // Array { Type containedType = info.FieldType.GetElementType(); Type typeList = typeof(List <>); Type actualType = typeList.MakeGenericType(containedType); MethodInfo addMethod = actualType.GetMethod("Add"); MethodInfo toArrayMethod = actualType.GetMethod("ToArray"); object list = Activator.CreateInstance(actualType); stream.List(info.Name, delegate(int i, JSONInStream stream2){ object o = DeserializeListElement(stream2, containedType); addMethod.Invoke(list, new object[] { o }); }); object array = toArrayMethod.Invoke(list, new object[] {}); info.SetValue(data, array); } else // Embedded Object { stream.Start(info.Name); object created = DeserializeObject(stream, info.FieldType); stream.End(); info.SetValue(data, created); } } break; } } return(data); }