Ejemplo n.º 1
0
        /// <summary>
        /// 反序列化出一个对象
        /// </summary>
        public object ToDeserialize(Type type)
        {
            switch (ValueType)
            {
            case ValueType.ARRAY:
            {
                return((mValue as JArray).ToDeserialize(type));
            }

            case ValueType.OBJECT:
            {
                return((mValue as JObject).ToDeserialize(type));
            }

            case ValueType.BOOLEAN:
            {
                return("true" == (mValue as string) ? true : false);
            }

            case ValueType.NUMBER:
            {
                return(JUtil.ToObject(type, mValue as string));
            }

            case ValueType.NULL:
            {
                return(null);
            }

            case ValueType.STRING:
            {
                return(JUtil.ToObject(type, mValue as string));
            }

            default:
            {
                return(JUtil.CreateInstance(type));
            }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 转object为JObject
        /// </summary>
        private static JObject ConvertToObject(object obj)
        {
            JObject res = new JObject();

            PropertyInfo[] propertys = JUtil.GetSerializableProperties(obj.GetType());

            if (null != propertys)
            {
                for (long i = 0; i < propertys.LongLength; ++i)
                {
                    try
                    {
                        if (propertys[i].CanRead)
                        {
                            res.Add(propertys[i].Name, ConvertToJValue(propertys[i].GetValue(obj, null)));
                        }
                    }
                    catch { }
                }
            }

            FieldInfo[] fields = JUtil.GetSerializableFields(obj.GetType());

            if (null != fields)
            {
                for (long i = 0; i < fields.LongLength; ++i)
                {
                    try
                    {
                        res.Add(fields[i].Name, ConvertToJValue(fields[i].GetValue(obj)));
                    }
                    catch { }
                }
            }

            return(res);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 转 IDictionary<,>为JObject
        /// </summary>
        private static JObject ConvertDictionaryGenericToObject(object obj)
        {
            JObject     res            = new JObject();
            IEnumerable tempEnumerable = obj as IEnumerable;
            IEnumerator tempEnumerator = tempEnumerable.GetEnumerator();

            while (tempEnumerator.MoveNext())
            {
                object tempKV = tempEnumerator.Current;
                object key    = null;
                if (JUtil.GetProperty(tempKV.GetType(), "Key", tempKV, ref key))
                {
                    if (null != key)
                    {
                        object value = null;
                        if (JUtil.GetProperty(tempKV.GetType(), "Value", tempKV, ref value))
                        {
                            res.Add(key.ToString(), ConvertToJValue(value));
                        }
                    }
                }
            }
            return(res);
        }
Ejemplo n.º 4
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)));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 序列化对象
        /// </summary>
        public object ToDeserialize(Type type)
        {
            if (!JUtil.CanInstance(type))
            {
                return(null);
            }

            if (type.IsArray)
            {
                object defaultValue = JUtil.CreateInstance(type, mValues.Count);
                Type   eleType      = type.GetElementType();
                for (int i = 0; i < mValues.Count; ++i)
                {
                    type.GetMethod("SetValue", new Type[] { typeof(object), typeof(int) }).Invoke(defaultValue, new object[] { mValues[i].ToDeserialize(eleType), i });
                }
                return(defaultValue);
            }
            else
            {
                object defaultValue = JUtil.CreateInstance(type);

                if (JUtil.IsListGenericType(type))
                {
                    if (JUtil.IsListGenericType(type))
                    {
                        Type t = type.GetGenericArguments()[0];
                        for (int i = 0; i < mValues.Count; ++i)
                        {
                            type.GetMethod("Insert", new Type[] { typeof(int), t }).Invoke(defaultValue, new object[] { i, mValues[i].ToDeserialize(t) });
                        }
                        return(defaultValue);
                    }
                }

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

                    Type keyValueType = typeof(KeyValuePair <,>).MakeGenericType(new Type[] { keyType, valueType });

                    for (int i = 0; i < mValues.Count; ++i)
                    {
                        object keyVlaue = mValues[i].ToDeserialize(keyValueType);

                        object key = null, value = null;
                        if (JUtil.GetProperty(keyValueType, "Key", keyVlaue, ref key))
                        {
                            JUtil.GetProperty(keyValueType, "Value", keyVlaue, ref value);
                        }

                        if (null != key)
                        {
                            type.GetMethod("Add", new Type[] { keyType, valueType }).Invoke(defaultValue, new object[] { key, value });
                        }
                    }
                }

                return(defaultValue);
            }
        }
Ejemplo n.º 6
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);
        }