Exemple #1
0
        /// <summary>
        /// 将字符串反序列化成对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public T DeserializeObject <T>(string value, SerializationSetting setting = null)
        {
            if (string.IsNullOrWhiteSpace(value))
            {
                throw new Exception("Value is null");
            }
            setting = setting ?? new SerializationSetting {
                DataTimeFomatter = DefaultDataTimeFomatter, SerializationType = SerializationType.JSON
            };
            Type objectType = typeof(T);

            switch (setting.SerializationType)
            {
            case SerializationType.JSON:
                return((T)JsonDeserializeProcess.Deserialize(value, objectType, setting, _PropertyAccessorManager));

            default:
                return((T)JsonDeserializeProcess.Deserialize(value, objectType, setting, _PropertyAccessorManager));
            }
        }
Exemple #2
0
        /// <summary>
        /// 常用类型对象序列化处理
        /// </summary>
        /// <param name="objectValue"></param>
        /// <param name="valueSb"></param>
        /// <param name="setting"></param>
        /// <param name="isShowPropertyName"></param>
        /// <param name="propertyName"></param>
        private static void Process(object objectValue, StringBuilder valueSb, SerializationSetting setting, bool isShowPropertyName, string propertyName = "")
        {
            Type objectType = objectValue.GetType();

            if (isShowPropertyName)
            {
                valueSb.Append(JsonSymbol.JsonQuotesSymbol);
                valueSb.Append(propertyName);
                valueSb.Append(JsonSymbol.JsonQuotesSymbol);
                valueSb.Append(JsonSymbol.JsonPropertySymbol);
            }
            if (objectValue != null)
            {
                if (objectType == typeof(DateTime))
                {
                    valueSb.Append(JsonSymbol.JsonQuotesSymbol);
                    valueSb.Append(Convert.ToDateTime(objectValue).ToString(setting.DataTimeFomatter));
                    valueSb.Append(JsonSymbol.JsonQuotesSymbol);
                }
                else if (objectType == typeof(Int32) ||
                         objectType == typeof(Decimal) ||
                         objectType == typeof(Guid))
                {
                    valueSb.Append(objectValue.ToString());
                }
                else if (objectType == typeof(String))
                {
                    valueSb.Append(JsonSymbol.JsonQuotesSymbol);
                    valueSb.Append(objectValue.ToString());
                    valueSb.Append(JsonSymbol.JsonQuotesSymbol);
                }
                else if (objectType == typeof(Boolean))
                {
                    valueSb.Append(objectValue.ToString().ToLower());
                }
            }
            else
            {
                valueSb.Append(JsonSymbol.JsonNullSymbol);
            }
        }
Exemple #3
0
        /// <summary>
        /// 根据设置将对象序列化成字符串
        /// </summary>
        /// <param name="serializeObject"></param>
        /// <param name="setting"></param>
        /// <returns></returns>
        public string SerializeObject(object serializeObject, SerializationSetting setting = null)
        {
            if (serializeObject == null)
            {
                throw new Exception("Object is null");
            }
            StringBuilder valueSb = new StringBuilder();

            setting = setting ?? new SerializationSetting {
                DataTimeFomatter = DefaultDataTimeFomatter, SerializationType = SerializationType.JSON
            };
            switch (setting.SerializationType)
            {
            case SerializationType.JSON:
                JsonSerializeProcess.Serialize(serializeObject, setting, false, valueSb, _PropertyAccessorManager);
                break;

            default:
                JsonSerializeProcess.Serialize(serializeObject, setting, false, valueSb, _PropertyAccessorManager);
                break;
            }
            return(valueSb.ToString());
        }
Exemple #4
0
        /// <summary>
        /// 序列化成JSON字符串
        /// </summary>
        /// <param name="serializeObject"></param>
        /// <param name="setting"></param>
        /// <param name="isShowPropertyName"></param>
        /// <param name="valueSb"></param>
        /// <param name="propertyAccessorManager"></param>
        /// <param name="propertyName"></param>
        public static void Serialize(object serializeObject, SerializationSetting setting, bool isShowPropertyName, StringBuilder valueSb, IPropertyAccessorManager propertyAccessorManager, string propertyName = "")
        {
            if (serializeObject == null)
            {
                if (isShowPropertyName && !string.IsNullOrWhiteSpace(propertyName))
                {
                    valueSb.Append(JsonSymbol.JsonQuotesSymbol);
                    valueSb.Append(propertyName);
                    valueSb.Append(JsonSymbol.JsonQuotesSymbol);
                    valueSb.Append(JsonSymbol.JsonPropertySymbol);
                    valueSb.Append(JsonSymbol.JsonNullSymbol);
                }
                else
                {
                    valueSb.Append(JsonSymbol.JsonNullSymbol);
                }
                return;
            }
            Type serializeObjectType = serializeObject.GetType();

            if (typeof(IEnumerable).IsAssignableFrom(serializeObjectType) && serializeObjectType != typeof(String) && !typeof(IDictionary).IsAssignableFrom(serializeObjectType))
            {
                IEnumerable objectValue    = (IEnumerable)serializeObject;
                IEnumerator enumeratorList = objectValue.GetEnumerator();
                int         objCount       = enumeratorList.GetEnumeratorCount();
                enumeratorList.Reset();
                int tmpObjCount = 0;
                if (isShowPropertyName && !string.IsNullOrWhiteSpace(propertyName))
                {
                    valueSb.Append(JsonSymbol.JsonQuotesSymbol);
                    valueSb.Append(propertyName);
                    valueSb.Append(JsonSymbol.JsonQuotesSymbol);
                    valueSb.Append(JsonSymbol.JsonPropertySymbol);
                }
                valueSb.Append(JsonSymbol.JsonArraySymbol_Begin);
                while (enumeratorList.MoveNext())
                {
                    Type enumeratorCurrentType = enumeratorList.Current.GetType();
                    if (enumeratorCurrentType.IsNormalType())
                    {
                        Serialize(enumeratorList.Current, setting, false, valueSb, propertyAccessorManager);
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(propertyName))
                        {
                            Serialize(enumeratorList.Current, setting, false, valueSb, propertyAccessorManager);
                        }
                        else
                        {
                            Serialize(enumeratorList.Current, setting, true, valueSb, propertyAccessorManager);
                        }
                    }
                    tmpObjCount++;
                    if (tmpObjCount < objCount)
                    {
                        valueSb.Append(JsonSymbol.JsonSeparateSymbol);
                    }
                }
                valueSb.Append(JsonSymbol.JsonArraySymbol_End);
            }
            else if (typeof(IDictionary).IsAssignableFrom(serializeObjectType))
            {
                IDictionary objectValue    = (IDictionary)serializeObject;
                int         objCount       = objectValue.Count;
                ICollection keyList        = objectValue.Keys;
                IEnumerator enumeratorList = keyList.GetEnumerator();
                int         tmpObjCount    = 0;
                if (isShowPropertyName && !string.IsNullOrWhiteSpace(propertyName))
                {
                    valueSb.Append(JsonSymbol.JsonQuotesSymbol);
                    valueSb.Append(propertyName);
                    valueSb.Append(JsonSymbol.JsonQuotesSymbol);
                    valueSb.Append(JsonSymbol.JsonPropertySymbol);
                }
                valueSb.Append(JsonSymbol.JsonObjectSymbol_Begin);
                while (enumeratorList.MoveNext())
                {
                    if (enumeratorList.Current == null)
                    {
                        throw new Exception("Key Is Not Null");
                    }
                    Type enumeratorCurrentType = objectValue[enumeratorList.Current].GetType();
                    Serialize(objectValue[enumeratorList.Current], setting, true, valueSb, propertyAccessorManager, enumeratorList.Current.ToString());
                    tmpObjCount++;
                    if (tmpObjCount < objCount)
                    {
                        valueSb.Append(JsonSymbol.JsonSeparateSymbol);
                    }
                }
                valueSb.Append(JsonSymbol.JsonObjectSymbol_End);
            }
            else if (serializeObjectType.IsNormalType())
            {
                Process(serializeObject, valueSb, setting, isShowPropertyName, propertyName);
            }
            else
            {
                if (isShowPropertyName && !string.IsNullOrWhiteSpace(propertyName))
                {
                    valueSb.Append(JsonSymbol.JsonQuotesSymbol);
                    valueSb.Append(propertyName);
                    valueSb.Append(JsonSymbol.JsonQuotesSymbol);
                    valueSb.Append(JsonSymbol.JsonPropertySymbol);
                }
                valueSb.Append(JsonSymbol.JsonObjectSymbol_Begin);
                PropertyInfo[] propertyInfoList = serializeObject.GetType().GetProperties();
                int            propertyCount    = propertyInfoList.Length;
                int            propertyIndex    = 0;
                foreach (PropertyInfo propertyInfoItem in propertyInfoList)
                {
                    string propertyKey = serializeObjectType.FullName + "." + propertyInfoItem.Name;
                    PropertyInfoContainer container = new PropertyInfoContainer {
                        PropertyName = propertyInfoItem.Name, InstanceTypeHandle = serializeObjectType.TypeHandle, PropertyTypeHandle = propertyInfoItem.PropertyType.TypeHandle
                    };
                    object objectValue = propertyAccessorManager.GetPropertyAccessor(propertyKey, container).GetValue(serializeObject); //propertyInfoItem.GetValue(serializeObject);//
                    Serialize(objectValue, setting, true, valueSb, propertyAccessorManager, propertyInfoItem.Name);
                    propertyIndex++;
                    if (propertyIndex < propertyCount)
                    {
                        valueSb.Append(JsonSymbol.JsonSeparateSymbol);
                    }
                }
                valueSb.Append(JsonSymbol.JsonObjectSymbol_End);
            }
        }
        public static object Deserialize(string jsonString, Type objectType, SerializationSetting setting, IPropertyAccessorManager propertyAccessorManager)
        {
            Stack <char> jsonStringStack = new Stack <char>();
            Stack <DeserializeObjectContainer> containerStack = new Stack <DeserializeObjectContainer>();
            JsonDeserializeEventArgs           args           = new JsonDeserializeEventArgs {
                RootType = objectType, ContainerStack = containerStack, JsonStringStack = jsonStringStack, PropertyAccessorManager = propertyAccessorManager
            };

            char[] jsonCharList = jsonString.ToCharArray();
            foreach (char charitem in jsonCharList)
            {
                jsonStringStack.Push(charitem);
                args.CurrentCharItem = charitem;
                //数组开始
                if (charitem == JsonSymbol.JsonArraySymbol_Begin)
                {
                    if (JsonDeserializeArraySymbol_Begin_Event != null)
                    {
                        Stopwatch stopwatch = new Stopwatch();
                        stopwatch.Start();
                        JsonDeserializeArraySymbol_Begin_Event(null, args);
                        stopwatch.Stop();
                        ProcessTime.ArraySymbol_Begin_Time += stopwatch.Elapsed.TotalMilliseconds;
                    }
                }
                //数组结束
                else if (charitem == JsonSymbol.JsonArraySymbol_End)
                {
                    if (JsonDeserializeArraySymbol_End_Event != null)
                    {
                        Stopwatch stopwatch = new Stopwatch();
                        stopwatch.Start();
                        JsonDeserializeArraySymbol_End_Event(null, args);
                        stopwatch.Stop();
                        ProcessTime.ArraySymbol_End_Time += stopwatch.Elapsed.TotalMilliseconds;
                    }
                }
                //对象开始
                else if (charitem == JsonSymbol.JsonObjectSymbol_Begin)
                {
                    if (JsonDeserializeObjectSymbol_Begin_Event != null)
                    {
                        Stopwatch stopwatch = new Stopwatch();
                        stopwatch.Start();
                        JsonDeserializeObjectSymbol_Begin_Event(null, args);
                        stopwatch.Stop();
                        ProcessTime.ObjectSymbol_Begin_Time += stopwatch.Elapsed.TotalMilliseconds;
                    }
                }
                //属性名
                else if (charitem == JsonSymbol.JsonPropertySymbol && IsPropertyHandler(args))
                {
                    if (JsonDeserializePropertySymbol_Event != null)
                    {
                        Stopwatch stopwatch = new Stopwatch();
                        stopwatch.Start();
                        JsonDeserializePropertySymbol_Event(null, args);
                        stopwatch.Stop();
                        ProcessTime.PropertySymbol_Time += stopwatch.Elapsed.TotalMilliseconds;
                    }
                }
                //对象结束
                else if (charitem == JsonSymbol.JsonObjectSymbol_End)
                {
                    if (JsonDeserializeObjectSymbol_End_Event != null)
                    {
                        Stopwatch stopwatch = new Stopwatch();
                        stopwatch.Start();
                        JsonDeserializeObjectSymbol_End_Event(null, args);
                        stopwatch.Stop();
                        ProcessTime.ObjectSymbol_End_Time += stopwatch.Elapsed.TotalMilliseconds;
                    }
                }
                //逗号
                else if (charitem == JsonSymbol.JsonSeparateSymbol)
                {
                    if (JsonDeserializeSeparateSymbol_Event != null)
                    {
                        Stopwatch stopwatch = new Stopwatch();
                        stopwatch.Start();
                        JsonDeserializeSeparateSymbol_Event(null, args);
                        stopwatch.Stop();
                        ProcessTime.SeparateSymbol_Time += stopwatch.Elapsed.TotalMilliseconds;
                    }
                }
            }
            return(containerStack.Pop().ContainerObject);
        }