コード例 #1
0
        public void ReadObjectDictionary(ObjectDictionaryAttribute attribute, object reader,
                                         object receiver, ReadSettings settings, ObjectPropertyInfo info, object serializerData)
        {
            JsonTextReader jsonReader = reader.Convert <JsonTextReader>();

            AssertRead(jsonReader);
            AssertReadState(jsonReader, JsonToken.StartObject, info.PropertyName);
            IDictionary dict       = attribute.GetDictionary(receiver, info);
            Type        objectType = info.ObjectType;

            while (jsonReader.Read())
            {
                if (jsonReader.TokenType == JsonToken.EndObject)
                {
                    break;
                }
                AssertReadState(jsonReader, JsonToken.PropertyName, info.PropertyName);
                string name = jsonReader.Value.ToString();
                AssertRead(jsonReader);
                // 支持内嵌Json数组
                AssertReadState(jsonReader, JsonToken.StartObject, JsonToken.StartArray);
                object subObject = ReadObject(attribute.UseConstructor, settings, jsonReader,
                                              objectType, receiver, info.ModelName);
                dict[name] = subObject;
                //dict.Add(name, value);
            }
        }
コード例 #2
0
        public void ReadObjectDictionary(ObjectDictionaryAttribute attribute, object reader,
                                         object receiver, ReadSettings settings, ObjectPropertyInfo info, object serializerData)
        {
            DataTable table = serializerData.Convert <DataTable>();

            if (table.Rows.Count != 1)
            {
                return;
            }
            DataRow     row  = table.Rows[0];
            IDictionary dict = attribute.GetDictionary(receiver, info);

            foreach (DataColumn column in table.Columns)
            {
                //dict[column.ColumnName] = row[column];
                object rowValue = row[column];
                string xml      = rowValue == DBNull.Value ? null : rowValue.ToString();
                if (string.IsNullOrEmpty(xml))
                {
                    dict[column.ColumnName] = null;
                }
                else
                {
                    Type   objectType = info.ObjectType;
                    object subObject  = attribute.UseConstructor ?
                                        ObjectUtil.CreateObjectWithCtor(objectType) : ObjectUtil.CreateObject(objectType);
                    subObject.ReadXml(info.ModelName, xml, settings, QName.ToolkitNoNS);
                    dict[column.ColumnName] = subObject;
                }
            }
        }
コード例 #3
0
        public void ReadObjectDictionary(ObjectDictionaryAttribute attribute, object reader,
                                         object receiver, ReadSettings settings, ObjectPropertyInfo info, object serializerData)
        {
            XmlReader xml = reader.Convert <XmlReader>();

            QName       name       = info.QName;
            IDictionary dict       = attribute.GetDictionary(receiver, info);
            Type        objectType = info.ObjectType;

            if (xml.IsEmptyElement)
            {
                return;
            }

            while (xml.Read())
            {
                if (xml.NodeType == XmlNodeType.EndElement)
                {
                    QName nodeName = QName.Get(xml);
                    if (nodeName == name)
                    {
                        return;
                    }
                }

                if (xml.NodeType == XmlNodeType.Element)
                {
                    QName  nodeName  = QName.Get(xml);
                    object subObject = attribute.UseConstructor ? ObjectUtil.CreateObjectWithCtor(objectType)
                        : ObjectUtil.CreateObject(objectType);
                    Read(reader, subObject, info.ModelName, settings, nodeName, null);
                    SerializerUtil.SetParent(receiver, subObject);

                    //string nodeValue = ReadString(xml);
                    //object objValue = SerializerUtil.GetPropertyObject(receiver, settings,
                    //    info, nodeValue, objectType);
                    dict[nodeName.LocalName] = subObject;
                }
            }
        }
コード例 #4
0
        public void ReadObjectDictionary(ObjectDictionaryAttribute attribute, object reader,
                                         object receiver, ReadSettings settings, ObjectPropertyInfo info, object serializerData)
        {
            XElementData currentData = reader.Convert <XElementData>();
            XElement     current     = currentData.Current;
            IDictionary  dict        = attribute.GetDictionary(receiver, info);
            Type         objectType  = info.ObjectType;

            foreach (XElement child in current.Elements())
            {
                string nodeName  = child.Name.LocalName;
                object subObject = attribute.UseConstructor ?
                                   ObjectUtil.CreateObjectWithCtor(objectType) : ObjectUtil.CreateObject(objectType);

                XElementData next = new XElementData {
                    Current = child, Root = currentData.Root
                };
                Read(next, subObject, info.ModelName, settings, QName.Get(nodeName), null);
                SerializerUtil.SetParent(receiver, subObject);
                dict[nodeName] = subObject;
            }
        }