/// <summary> /// 写入流中 /// </summary> /// <param name="dictionary"></param> /// <param name="name">The writer.</param> /// <param name="setting">The setting.</param> /// <param name="reader">The array.</param> /// <param name="arrayLevel"></param> public virtual void Load(IDictionary 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; } dictionary[item.Key] = item.ToString(); //if (item.NodeType == ContentNodeType.String) //{ // dictionary[item.Key] = item.ToString(); //} //else //{ // dictionary[item.Key] = item.ToString(); // //dictionary[item.Key] = reader.Read(item.StartIndex - 1, item.EndIndex + 1); //} } 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) { dictionary[item.Key] = item.ToString(); //if (item.NodeType == ContentNodeType.String) //{ // dictionary[item.Key] = new string(item.GetValue()); //} //else //{ // dictionary[item.Key] = item.ToString(); // //dictionary[n.Key] = reader.Read(n.StartIndex - 1, n.EndIndex + 1); //} } }
/// <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> /// <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] = this.MethodProvider.Parse(reader, setting, item, true); } 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 == null || nodeValue.Length == 0) { 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] = (this.MethodProvider.Parse(reader, setting, nodes[i], true)); } return(list); }
/// <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(); //} } }