public static void RegisterClassTypeSerializeFunction(Type classType, ClassTypeToJsonFunction classTypeSerializeFunction, bool bOverride = false) { if (classType == null || !classType.IsClass) { #if UNITY_EDITOR VeerDebug.LogWarning(" RegisterTypeSerializeFunction Type 为 null 或不是 Class ..."); #endif return; } if (bOverride) { _ClassTypeToJsonFunctionMap.SetAddValue(classType, classTypeSerializeFunction); } else { if (_ClassTypeToJsonFunctionMap.ContainsKey(classType)) { return; } else { _ClassTypeToJsonFunctionMap.Add(classType, classTypeSerializeFunction); } } }
private static ClassTypeToJsonFunction CreateTypeSerializeFunctionForDictionary(Type targetType) { // 1.check type if (!targetType.IsGenericType || targetType.GetGenericTypeDefinition() != typeof(Dictionary <,>)) { VeerDebug.LogWarning(" CreateTypeSerializeFunctionForDictionary but target type is not dictionary : " + targetType); return(null); } // 2.check dictionary key type is string Type keyType = targetType.GetGenericArguments()[0]; if (keyType != typeof(string)) { VeerDebug.LogWarning(" ToJsonObject 不支持 KeyType 不是 string 的 Dictionary : " + targetType); return(null); } // 3.check register value type Type dicValueType = targetType.GetGenericArguments()[1]; CheckRegister(dicValueType); // 4.create function ClassTypeToJsonFunction function = (object dictionaryObj) => { JSONObject jsonDictionary = JSONObject.obj; if (dictionaryObj == null) { return(jsonDictionary); } IDictionary iDictionary = (IDictionary)dictionaryObj; IDictionaryEnumerator enumerator = iDictionary.GetEnumerator(); if (_BasicTypeToJsonFunctionMap.ContainsKey(dicValueType)) { while (enumerator.MoveNext()) { _BasicTypeToJsonFunctionMap[dicValueType](jsonDictionary, enumerator.Key.ToString(), enumerator.Value); } } else if (_ClassTypeToJsonFunctionMap.ContainsKey(dicValueType)) { ClassTypeToJsonFunction toJsonFunc = _ClassTypeToJsonFunctionMap[dicValueType]; if (toJsonFunc != null) { while (enumerator.MoveNext()) { jsonDictionary.AddField(enumerator.Key.ToString(), toJsonFunc(enumerator.Value)); } } } return(jsonDictionary); }; return(function); }
private static ClassTypeToJsonFunction CreateTypeSerializeFunction(Type targetType) { IsCreatingFuncTypeList.Add(targetType); ClassTypeToJsonFunction toJsonObjectFunction = null; if (targetType.IsEnum) { #if UNITY_EDITOR VeerDebug.LogWarning(" 目前不支持 Enum 类型 ToJsonObject ..."); #endif toJsonObjectFunction = null; } else if (targetType.IsArray || targetType.IsGenericType && targetType.GetGenericTypeDefinition() == typeof(List <>)) { toJsonObjectFunction = CreateTypeSerializeFunctionForArrayList(targetType); } else if (targetType.IsGenericType && targetType.GetGenericTypeDefinition() == typeof(Dictionary <,>)) { toJsonObjectFunction = CreateTypeSerializeFunctionForDictionary(targetType); } else if (targetType.IsClass) { toJsonObjectFunction = CreateTypeSerializeFunctionForClass(targetType); } // 注册方法 RegisterClassTypeSerializeFunction(targetType, toJsonObjectFunction); IsCreatingFuncTypeList.Remove(targetType); return(toJsonObjectFunction); }
private static ClassTypeToJsonFunction CreateTypeSerializeFunctionForArrayList(Type targetType) { // 1.check type Type arrayListElementType = null; if (targetType.IsArray) { arrayListElementType = targetType.GetElementType(); } else if (targetType.IsGenericType && targetType.GetGenericTypeDefinition() == typeof(List <>)) { arrayListElementType = targetType.GetGenericArguments()[0]; } if (arrayListElementType == null) { VeerDebug.LogWarning(" CreateTypeSerializeFunctionForArrayList but target type is not arrar or list : " + targetType); return(null); } // 2.check register element type CheckRegister(arrayListElementType); // 3.create function ClassTypeToJsonFunction function = (object arrayListObj) => { JSONObject jsonArray = JSONObject.arr; if (arrayListObj == null) { return(jsonArray); } IEnumerable elementArray = arrayListObj as IEnumerable; if (_BasicTypeToJsonFunctionMap.ContainsKey(arrayListElementType)) { foreach (var element in elementArray) { _BasicTypeToJsonFunctionMap[arrayListElementType](jsonArray, null, element); } } else if (_ClassTypeToJsonFunctionMap.ContainsKey(arrayListElementType)) { ClassTypeToJsonFunction toJsonFunc = _ClassTypeToJsonFunctionMap[arrayListElementType]; if (toJsonFunc != null) { foreach (var element in elementArray) { jsonArray.Add(toJsonFunc(element)); } } } return(jsonArray); }; return(function); }
// 核心方法 private static JSONObject ToJsonObject_Internal(object target) { if (!_Inited) { Init(); } // 检查空值 if (target == null) { return(null); } // 检查是否为 类的实例 Type targetType = target.GetType(); if (!targetType.IsClass) { VeerDebug.LogWarning(" 不支持对非类的实例进行 ToJsonObject 操作 : " + targetType.Name); return(null); } // register CheckRegister(targetType); // 检查是否已有 ToJson方法 if (_ClassTypeToJsonFunctionMap.ContainsKey(targetType)) { ClassTypeToJsonFunction existingFunction = _ClassTypeToJsonFunctionMap[targetType]; if (existingFunction == null) { VeerDebug.LogWarning(" 对无法支持的类型进行 ToJsonObject 操作 : " + targetType.Name); return(null); } // 使用已有方法进行 ToJson return(_ClassTypeToJsonFunctionMap[targetType](target)); } else { return(null); } }
private static ClassTypeToJsonFunction CreateTypeSerializeFunctionForClass(Type targetType) { // 1.check type if (!targetType.IsClass) { VeerDebug.LogWarning(" CreateTypeSerializeFunctionForClass but target type is not Class : " + targetType); return(null); } // 2.找到全部 targetType 需要 ToJson 的字段 List <FieldInfo> needToJsonFieldInfos = new List <FieldInfo>(); BindingFlags flag = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; foreach (FieldInfo field in targetType.GetFields(flag)) { Type fieldType = field.GetType(); if (_BasicTypeToJsonFunctionMap.ContainsKey(fieldType)) { needToJsonFieldInfos.Add(field); } else if (fieldType.IsClass) { needToJsonFieldInfos.Add(field); } } // 3.register all types foreach (FieldInfo field in needToJsonFieldInfos) { Type fieldType = field.FieldType; if (!IsCreatingFuncTypeList.Contains(fieldType)) { CheckRegister(fieldType); } } // 4.create function ClassTypeToJsonFunction function = (object classInstance) => { JSONObject json = JSONObject.obj; if (classInstance == null) { return(JSONObject.nullJO); } foreach (var field in needToJsonFieldInfos) { object fieldValue = field.GetValue(classInstance); Type fieldType = field.FieldType; if (_BasicTypeToJsonFunctionMap.ContainsKey(fieldType)) { _BasicTypeToJsonFunctionMap[fieldType](json, field.Name, fieldValue); } else if (_ClassTypeToJsonFunctionMap.ContainsKey(fieldType)) { ClassTypeToJsonFunction toJsonFunc = _ClassTypeToJsonFunctionMap[fieldType]; if (toJsonFunc != null) { json.AddField(field.Name, toJsonFunc(fieldValue)); } } } return(json); }; return(function); }