/// <summary>
        /// 反序列化对象
        /// </summary>
        /// <typeparam name="T">目标类型</typeparam>
        /// <param name="reader">读取器</param>
        /// <param name="setting">配置</param>
        /// <returns></returns>
        public static T Deserialize <T>(IDeserializerReader reader, JsonDeserializeSetting setting)
        {
            reader.Reset();
            var build = ZzzZzDeserializerBuilder <T> .Register(setting);

            return(build.Invoke(reader, setting, 0));
        }
        /// <summary>
        /// 装载数据到字典中
        /// </summary>
        /// <typeparam name="V"></typeparam>
        /// <param name="dictionary">The result.</param>
        /// <param name="reader">The reader.</param>
        /// <param name="setting">The setting.</param>
        /// <param name="name">The name.</param>
        /// <param name="arrayLevel"></param>
        public virtual void LoadStringKey <V>(IDictionary <string, V> dictionary, IDeserializerReader reader, JsonDeserializeSetting setting, string name, int arrayLevel)
        {
            var node = reader.Read(name);

            if (node == null)
            {
                if (name != null)
                {
                    return;
                }

                while (true)
                {
                    var item = reader.MoveNext();
                    if (item == null)
                    {
                        break;
                    }

                    var key = ZzzZzDeserializerBuilder <string> .Register(setting).Invoke(ThunderReader.Load(item.Key), setting, arrayLevel);

                    if (string.IsNullOrEmpty(key))
                    {
                        continue;
                    }

                    var value = ZzzZzDeserializerBuilder <V> .Register(setting).Invoke(reader.Parse(item), setting, arrayLevel);

                    dictionary[key] = value;
                }

                return;
            }

            if (node.NodeType == ContentNodeType.String)
            {
                return;
            }

            if (node.NodeType != ContentNodeType.Object)
            {
                throw new ArgumentException(string.Format("字典只能为key-value形式,当前形式为{0}", node.NodeType.ToString()));
            }

            foreach (var n in (IList <JsonContentNode>)node.Node)
            {
                var key = ZzzZzDeserializerBuilder <string> .Register(setting).Invoke(ThunderReader.Load(n.Key), setting, arrayLevel);

                if (string.IsNullOrEmpty(key))
                {
                    continue;
                }

                var value = ZzzZzDeserializerBuilder <V> .Register(setting).Invoke(reader.Parse(n), setting, arrayLevel);

                dictionary[key] = value;
            }
        }
        /// <summary>
        /// 反序列化对象
        /// </summary>
        /// <typeparam name="T">目标类型</typeparam>
        /// <param name="json">源字符串</param>
        /// <param name="setting">配置</param>
        /// <returns></returns>
        public static T Deserialize <T>(string json, JsonDeserializeSetting setting)
        {
            if (string.IsNullOrEmpty(json))
            {
                return(default(T));
            }

            if (json.Equals("null"))
            {
                return(default(T));
            }

            var reader = ThunderReader.Load(json);
            {
                var build = ZzzZzDeserializerBuilder <T> .Register(setting);

                return(build.Invoke(reader, setting, 0));
            }
        }
        /// <summary>
        /// 装载数据到字典中
        /// </summary>
        /// <param name="dictionary">The result.</param>
        /// <param name="reader">The reader.</param>
        /// <param name="setting">The setting.</param>
        /// <param name="name">The name.</param>
        /// <param name="arrayLevel"></param>
        public virtual void LoadStringKeyStringvalue(IDictionary <string, string> dictionary, IDeserializerReader reader, JsonDeserializeSetting setting, string name, int arrayLevel)
        {
            var node = reader.Read(name);

            if (node == null)
            {
                if (name != null)
                {
                    return;
                }

                while (true)
                {
                    var item = reader.MoveNext();
                    if (item == null)
                    {
                        break;
                    }

                    var key = ZzzZzDeserializerBuilder <string> .Register(setting).Invoke(ThunderReader.Load(item.Key), setting, arrayLevel);

                    if (string.IsNullOrEmpty(key))
                    {
                        continue;
                    }

                    var itemValue = item.GetValue();
                    if (itemValue == null)
                    {
                        dictionary[key] = "";
                        continue;
                    }

                    dictionary[item.Key] = item.ToString();
                    //if (item.NodeType == ContentNodeType.String)
                    //{
                    //    dictionary[key] = new string(itemValue);
                    //}
                    //else
                    //{
                    //    dictionary[item.Key] = item.ToString();
                    //}
                }

                return;
            }

            if (node.NodeType == ContentNodeType.String)
            {
                return;
            }

            if (node.NodeType != ContentNodeType.Object)
            {
                throw new ArgumentException(string.Format("字典只能为key-value形式,当前形式为{0}", node.NodeType.ToString()));
            }

            foreach (var item in (IList <JsonContentNode>)node.Node)
            {
                var key = ZzzZzDeserializerBuilder <string> .Register(setting).Invoke(ThunderReader.Load(item.Key), setting, arrayLevel);

                if (string.IsNullOrEmpty(key))
                {
                    continue;
                }

                var nValue = item == null ? ArraySegmentValue.Empty : item.GetValue();
                if (nValue.IsNullOrEmpty)
                {
                    dictionary[key] = "";
                    continue;
                }

                dictionary[item.Key] = item.ToString();
                //if (item.NodeType == ContentNodeType.String)
                //{
                //    dictionary[key] = new string(nValue);
                //}
                //else
                //{
                //    dictionary[item.Key] = item.ToString();
                //}
            }
        }
 /// <summary>
 /// 执行构建序列化,反射用到的
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="reader"></param>
 /// <param name="setting"></param>
 /// <param name="arrayLevel"></param>
 /// <returns></returns>
 private static object DeserializeBuilder <T>(IDeserializerReader reader, JsonDeserializeSetting setting, int arrayLevel)
 {
     return(ZzzZzDeserializerBuilder <T> .Register(setting).Invoke(reader, setting, arrayLevel));
 }
Beispiel #6
0
        /// <summary>
        /// 写入流中
        /// </summary>
        /// <param name="name">The writer.</param>
        /// <param name="setting">The setting.</param>
        /// <param name="reader">The array.</param>
        /// <param name="arrayLevel">下一个数组层次,如果为1,则表示数组连续,比如2维数据</param>
        public virtual T[] Parse(IDeserializerReader reader, JsonDeserializeSetting setting, string name, int arrayLevel)
        {
            var node = reader.Read(name);

            if (node == null)
            {
                if (name != null)
                {
                    return(new T[0]);
                }

                if (reader.ContainerSignal != ContainerSignal.Array)
                {
                    return(new T[0]);
                }

                var temp = new T[reader.Count];
                for (var i = 0; i < reader.Count; i++)
                {
                    var item = reader.MoveNext();
                    if (item == null)
                    {
                        break;
                    }

                    temp[i] = ZzzZzDeserializerBuilder <T> .Register(setting).Invoke(reader.Parse(item), setting, arrayLevel);
                }

                return(temp);
            }

            if (node.NodeType != ContentNodeType.Array)
            {
                return(new T[0]);
            }

            var nodes = node.Node as IList <JsonContentNode>;

            if (nodes == null)
            {
                return(new T[0]);
            }

            if (nodes.Count == 1)
            {
                var subNodes = nodes[0].Node as IList <JsonContentNode>;
                if (subNodes == null || subNodes.Count <= 0)
                {
                    var nodeValue = nodes[0] == null ? ArraySegmentValue.Empty : nodes[0].GetValue();
                    if (nodeValue.IsNullOrEmpty)
                    {
                        return(new T[0]);
                    }
                }
            }

            if (nodes[0].ArrayLevel != arrayLevel)
            {
                return(new T[0]);
            }

            var list = new T[nodes.Count];

            for (var i = 0; i < nodes.Count; i++)
            {
                var nodeValue = nodes[i] == null ? ArraySegmentValue.Empty : nodes[i].GetValue();
                if (nodes[i].NodeType == ContentNodeType.String)
                {
                    if (StringMethodProvider.Default.IsNullValue(nodeValue))
                    {
                        list[i] = default(T);
                        continue;
                    }
                }

                list[i] = ZzzZzDeserializerBuilder <T> .Register(setting).Invoke(reader.Parse(nodes[i]), setting, arrayLevel);
            }

            return(list);
        }