Example #1
0
        /// <summary>
        /// 转为JValue
        /// </summary>
        private static JValue ConvertToJValue(object obj)
        {
            if (null == obj)
            {
                return(new JValue(ValueType.NULL, "null"));
            }

            if (typeof(JArray) == obj.GetType())
            {
                return(new JValue(ValueType.ARRAY, obj));
            }

            if (typeof(JObject) == obj.GetType())
            {
                return(new JValue(ValueType.OBJECT, obj));
            }
            if (obj.GetType().IsEnum)
            {
                return(new JValue(ValueType.NUMBER, ((int)obj).ToString()));
            }
            if (typeof(bool) == obj.GetType() || typeof(Boolean) == obj.GetType())
            {
                return(new JValue(ValueType.BOOLEAN, ((bool)obj) ? "true" : "false"));
            }

            if (typeof(NameValueCollection) == obj.GetType() || typeof(NameValueCollection).IsAssignableFrom(obj.GetType()))
            {
                return(new JValue(ValueType.OBJECT, ConvertNameValueCollectionToObject(obj as NameValueCollection)));
            }

            if (JUtil.IsNumberType(obj.GetType()))
            {
                return(new JValue(ValueType.NUMBER, obj.ToString()));
            }

            if (JUtil.IsStringType(obj.GetType()))
            {
                if (typeof(DateTime) == obj.GetType())
                {
                    return(new JValue(ValueType.STRING, ((DateTime)obj).ToString("yyyy-MM-dd HH:mm:ss.fff")));
                }
                if (typeof(string) == obj.GetType())
                {
                    return(new JValue(ValueType.STRING, JUtil.SerializeString(obj.ToString())));
                }
                return(new JValue(ValueType.STRING, obj.ToString()));
            }

            if (JUtil.IsValueKeyDictionaryGenericType(obj.GetType()))
            {
                return(new JValue(ValueType.OBJECT, ConvertDictionaryGenericToObject(obj)));
            }

            if (JUtil.IsKeyValuePairGenericType(obj.GetType()))
            {
                return(new JValue(ValueType.OBJECT, ConvertKeyValuePairGenericToObject(obj)));
            }

            if (JUtil.IsListType(obj.GetType()))
            {
                return(new JValue(ValueType.ARRAY, ConvertListToArray(obj as IList)));
            }

            if (JUtil.IsIEnumerableType(obj.GetType()))
            {
                return(new JValue(ValueType.ARRAY, ConvertIEnumerableToArray(obj as IEnumerable)));
            }

            return(new JValue(ValueType.OBJECT, ConvertToObject(obj)));
        }
Example #2
0
        /// <summary>
        /// 序列化对象
        /// </summary>
        public object ToDeserialize(Type type)
        {
            if (!JUtil.CanInstance(type))
            {
                return(null);
            }
            object defalutValue = JUtil.CreateInstance(type);

            if (JUtil.IsValueKeyDictionaryGenericType(type))
            {
                Type keyType   = type.GetGenericArguments()[0];
                Type valueType = type.GetGenericArguments()[1];

                foreach (KeyValuePair <string, JValue> tempKV in mSortPropertys)
                {
                    object key = JUtil.ToObject(keyType, tempKV.Key);
                    if (!(bool)type.GetMethod("ContainsKey", new Type[] { keyType }).Invoke(defalutValue, new object[] { key }))
                    {
                        type.GetMethod("Add", new Type[] { keyType, valueType }).Invoke(defalutValue, new object[] { key, tempKV.Value.ToDeserialize(valueType) });
                    }
                }
                return(defalutValue);
            }

            if (JUtil.IsNameValueCollectionType(type))
            {
                NameValueCollection nameValue = defalutValue as NameValueCollection;

                foreach (KeyValuePair <string, JValue> tempKV in mSortPropertys)
                {
                    nameValue[tempKV.Key] = tempKV.Value.Value.ToString();
                }
                return(defalutValue);
            }

            if (JUtil.IsKeyValuePairGenericType(type))
            {
                Type keyType   = type.GetGenericArguments()[0];
                Type valueType = type.GetGenericArguments()[1];

                object key = null, value = null;

                if (JUtil.IsValueType(keyType))
                {
                    if (mSortPropertys.Count > 0)
                    {
                        KeyValuePair <string, JValue> tempKV = mSortPropertys[0];
                        key   = JUtil.ToObject(keyType, tempKV.Key);
                        value = tempKV.Value.ToDeserialize(valueType);
                    }
                }
                else
                {
                    if (mPropertys.ContainsKey("Key"))
                    {
                        key   = mPropertys["Key"].ToDeserialize(keyType);
                        value = mPropertys["Value"].ToDeserialize(valueType);
                    }
                }

                if (null != key)
                {
                    defalutValue = JUtil.CreateInstance(type, key, value);
                }

                return(defalutValue);
            }

            PropertyInfo[] propertyInfo = JUtil.GetSerializableProperties(type);

            if (null != propertyInfo)
            {
                for (int i = 0; i < propertyInfo.Length; ++i)
                {
                    if (propertyInfo[i].CanWrite)
                    {
                        string name = propertyInfo[i].Name;
                        if (mPropertys.ContainsKey(name))
                        {
                            propertyInfo[i].SetValue(defalutValue, mPropertys[name].ToDeserialize(propertyInfo[i].PropertyType), null);
                        }
                    }
                }
            }

            FieldInfo[] fieldInfo = JUtil.GetSerializableFields(type);

            if (null != fieldInfo)
            {
                for (int i = 0; i < fieldInfo.Length; ++i)
                {
                    string name = fieldInfo[i].Name;
                    if (mPropertys.ContainsKey(name))
                    {
                        fieldInfo[i].SetValue(defalutValue, mPropertys[name].ToDeserialize(fieldInfo[i].FieldType));
                    }
                }
            }

            return(defalutValue);
        }