Ejemplo n.º 1
0
 public static object ToDict(JsonNode rJsonNode, Type rDictType, Type rKeyType, Type rValueType)
 {
     return(rJsonNode.ToDict(rDictType, rKeyType, rValueType));
 }
Ejemplo n.º 2
0
 public static object ToList(JsonNode rJsonNode, Type rType, Type rElemType)
 {
     return(rJsonNode.ToList(rType, rElemType));
 }
Ejemplo n.º 3
0
 public JsonSymbolItem(JsonSymbolItem rItem)
 {
     this.value = rItem.value;
     this.type  = rItem.type;
     this.node  = rItem.node;
 }
Ejemplo n.º 4
0
 public static object ToObject(JsonNode rJsonNode, Type rType)
 {
     return(rJsonNode.ToObject(rType));
 }
Ejemplo n.º 5
0
        /// <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
                {
                    Debug.LogError(string.Format("Type = {0}, 不支持序列化的变量类型!", rObject.GetType()));
                }
            }
            return(rRootNode);
        }