Exemple #1
0
        private static object ChangeObjectToJsonObject(object data)
        {
            if (null == data)
            {
                return(data);
            }
            Type t = data.GetType();

            if (ReflectionTool.IsDelegate(t))
            {
                return(null);
            }
            object value = data;

            if (!IsSupportBaseValueParseJson(t))
            {
                if (t.IsArray)
                {
                    value = ListArrayToJsonObject(data, false);
                }
                else if (t.IsClass || t.IsGenericType)
                {
                    if (list_Type.Name == t.Name)
                    {
                        value = ListArrayToJsonObject(data, true);
                    }
                    else if (dictionary_Type.Name == t.Name)
                    {
                        value = DictionaryToJsonObject(data);
                    }
                    else
                    {
                        value = ClassOrStructToJsonObject(data);
                    }
                }
                else
                {
                    value = ClassOrStructToJsonObject(data);
                }
            }
            return(value);
        }
Exemple #2
0
        /// <summary>
        /// 检查属性是否能用于序列化
        /// </summary>
        /// <param name="property"></param>
        /// <returns></returns>
        private static bool CheckPropertyInfo(PropertyInfo property)
        {
            if (property == null)
            {
                return(false);
            }
            if (!(property.CanRead && property.CanWrite))
            {
                return(false);
            }
            //索引器
            if (property.GetIndexParameters().Length > 0)
            {
                return(false);
            }
            if (ReflectionTool.IsDelegate(property.PropertyType))
            {
                return(false);
            }


            return(true);
        }
Exemple #3
0
        private static object ClassOrStructToJsonObject(object data)
        {
            IDictionary <object, object> dic = new Dictionary <object, object>();
            Type type = data.GetType();

            FieldInfo[] fields = type.GetFields(flags);

            for (int i = 0; i < fields.Length; i++)
            {
                FieldInfo f = fields[i];

                if (ReflectionTool.IsDelegate(f.FieldType))
                {
                    continue;
                }
                if (CheckHaveNotJsonSerializedAttribute(f))
                {
                    continue;
                }
                try
                {
                    object v    = f.GetValue(data);
                    string name = f.Name;
                    if (v == null)
                    {
                        continue;
                    }
                    v = ChangeObjectToJsonObject(v);
                    dic.Add(name, v);
                }
                catch
                {
                    continue;
                }
            }

            PropertyInfo[] propertys = type.GetProperties(flags);
            for (int i = 0; i < propertys.Length; i++)
            {
                PropertyInfo p = propertys[i];
                if (CheckPropertyInfo(p))
                {
                    try
                    {
                        object v = p.GetValue(data, null);
                        if (v == null)
                        {
                            continue;
                        }
                        v = ChangeObjectToJsonObject(v);
                        dic.Add(p.Name, v);
                    }
                    catch (Exception e)
                    {
                        Debug.LogError("property :" + p.Name + "\n" + e);
                        continue;
                    }
                }
            }
            return(dic);
        }