/// <summary> /// 从对象到JsonNode /// </summary> public static JsonNode ToJsonNode(object rObject) { Type rType = rObject.GetType(); JsonNode rRootNode = null; //如果是List if (rType.IsGenericType && typeof(IList).IsAssignableFrom(rType.GetGenericTypeDefinition())) { rRootNode = new JsonArray(); IList rListObj = (IList)rObject; foreach (var rItem in rListObj) { JsonNode rNode = ToJsonNode(rItem); rRootNode.Add(rNode); } } else if (rType.IsArray) //如果是Array { rRootNode = new JsonArray(); Array rArrayObj = (Array)rObject; foreach (var rItem in rArrayObj) { JsonNode rNode = ToJsonNode(rItem); rRootNode.Add(rNode); } } else if (rType.IsGenericType && typeof(IDictionary).IsAssignableFrom(rType.GetGenericTypeDefinition())) { //如果是Dictionary rRootNode = new JsonClass(); IDictionary rDictObj = (IDictionary)rObject; foreach (var rKey in rDictObj.Keys) { JsonNode rValueNode = ToJsonNode(rDictObj[rKey]); rRootNode.Add(rKey.ToString(), rValueNode); } } else if (rType.IsGenericType && typeof(IDict).IsAssignableFrom(rType.GetGenericTypeDefinition())) { rRootNode = new JsonClass(); IDict rDictObj = (IDict)rObject; foreach (var rItem in rDictObj.OriginCollection) { JsonNode rValueNode = ToJsonNode(rItem.Value); rRootNode.Add(rItem.Key.ToString(), rValueNode); } } else if (rType.IsClass) //如果是Class,获取Class所有的public的字段和属性 { if (rType == typeof(string)) { rRootNode = new JsonData((string)rObject); } else { rRootNode = new JsonClass(); // 所有公共的属性 PropertyInfo[] rPropInfos = rType.GetProperties(ReflectionAssist.flags_public); for (int i = 0; i < rPropInfos.Length; i++) { if (rPropInfos[i].IsDefined(typeof(JsonIgnoreAttribute), false)) { continue; } object rValueObj = rPropInfos[i].GetValue(rObject, null); JsonNode rValueNode = ToJsonNode(rValueObj); rRootNode.Add(rPropInfos[i].Name, rValueNode); } // 所有公共的字段 FieldInfo[] rFieldInfos = rType.GetFields(ReflectionAssist.flags_public); for (int i = 0; i < rFieldInfos.Length; i++) { if (rFieldInfos[i].IsDefined(typeof(JsonIgnoreAttribute), false)) { continue; } object rValueObj = rFieldInfos[i].GetValue(rObject); JsonNode rValueNode = ToJsonNode(rValueObj); rRootNode.Add(rFieldInfos[i].Name, rValueNode); } // 所有预定义的序列化属性的private的字段 rPropInfos = rType.GetProperties(ReflectionAssist.flags_nonpublic); for (int i = 0; i < rPropInfos.Length; i++) { if (!rPropInfos[i].IsDefined(typeof(JsonEnableAttribute), false)) { continue; } object rValueObj = rPropInfos[i].GetValue(rObject, null); JsonNode rValueNode = ToJsonNode(rValueObj); rRootNode.Add(rPropInfos[i].Name, rValueNode); } rFieldInfos = rType.GetFields(ReflectionAssist.flags_nonpublic); for (int i = 0; i < rFieldInfos.Length; i++) { if (!rFieldInfos[i].IsDefined(typeof(JsonEnableAttribute), false)) { continue; } object rValueObj = rFieldInfos[i].GetValue(rObject); JsonNode rValueNode = ToJsonNode(rValueObj); rRootNode.Add(rFieldInfos[i].Name, rValueNode); } } } else if (rType.IsPrimitive) //如果是实例 { if (rType == typeof(int)) { rRootNode = new JsonData((int)rObject); } else if (rType == typeof(uint)) { rRootNode = new JsonData((uint)rObject); } else if (rType == typeof(long)) { rRootNode = new JsonData((long)rObject); } else if (rType == typeof(ulong)) { rRootNode = new JsonData((ulong)rObject); } else if (rType == typeof(float)) { rRootNode = new JsonData((float)rObject); } else if (rType == typeof(double)) { rRootNode = new JsonData((double)rObject); } else if (rType == typeof(bool)) { rRootNode = new JsonData((bool)rObject); } else if (rType == typeof(string)) { rRootNode = new JsonData((string)rObject); } else if (rType == typeof(byte)) { rRootNode = new JsonData((byte)rObject); } else if (rType == typeof(short)) { rRootNode = new JsonData((short)rObject); } else if (rType == typeof(ushort)) { rRootNode = new JsonData((ushort)rObject); } else { Debug.LogError(string.Format("Type = {0}, 不支持序列化的变量类型!", rObject.GetType())); } } else if (rType.IsEnum) { rRootNode = new JsonData((int)rObject); } return(rRootNode); }