Ejemplo n.º 1
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;
                }
            }
        }
Ejemplo n.º 2
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);
            }
        }
Ejemplo n.º 3
0
        public void WriteObjectDictionary(ObjectDictionaryAttribute attribute, object writer,
                                          object value, WriteSettings settings, ObjectPropertyInfo info, object serializerData)
        {
            string  tableName = info.LocalName;
            DataSet ds        = writer.Convert <DataSet>();

            if (ds.Tables.Contains(tableName))
            {
                return;
            }

            IDictionary dictionary = value as IDictionary;

            if (dictionary != null)
            {
                DataTable table = new DataTable(tableName)
                {
                    Locale = ObjectUtil.SysCulture
                };
                var cols = table.Columns;
                foreach (string key in dictionary.Keys)
                {
                    if (!string.IsNullOrEmpty(key))
                    {
                        cols.Add(key);
                    }
                }

                DataRow row = table.NewRow();
                row.BeginEdit();
                try
                {
                    foreach (DataColumn column in table.Columns)
                    {
                        string xml = dictionary[column.ColumnName].WriteXml(info.ModelName,
                                                                            settings, QName.ToolkitNoNS);
                        if (xml == null)
                        {
                            row[column] = DBNull.Value;
                        }
                        else
                        {
                            row[column] = xml;
                        }
                    }
                }
                finally
                {
                    row.EndEdit();
                }
                table.Rows.Add(row);

                ds.Tables.Add(table);
            }
        }
        public void WriteObjectDictionary(ObjectDictionaryAttribute attribute, object writer,
                                          object value, WriteSettings settings, ObjectPropertyInfo info, object serializerData)
        {
            IDictionary       dict    = value.Convert <IDictionary>();
            DictionaryBuilder builder = writer.Convert <DictionaryBuilder>();
            QName             name    = info.QName;

            DictionaryBuilder subDict = builder.CreateBuilder();

            foreach (DictionaryEntry item in dict)
            {
                DictionaryBuilder subDictItem = builder.CreateBuilder();
                WriteObject(subDictItem, item.Value, info.ModelName, settings, name, null);
                subDict.Add(item.Key.ToString(), subDictItem);
            }
            builder.Add(name.LocalName, subDict);
        }
Ejemplo n.º 5
0
        public void WriteObjectDictionary(ObjectDictionaryAttribute attribute, object writer,
                                          object value, WriteSettings settings, ObjectPropertyInfo info, object serializerData)
        {
            IDictionary    dict       = value.Convert <IDictionary>();
            JsonTextWriter jsonWriter = writer.Convert <JsonTextWriter>();
            QName          name       = info.QName;

            if (info.WriteMode == SerializerWriteMode.WriteName)
            {
                jsonWriter.WritePropertyName(name.LocalName);
            }
            jsonWriter.WriteStartObject();
            foreach (DictionaryEntry item in dict)
            {
                jsonWriter.WritePropertyName(item.Key.ToString());
                Write(writer, item.Value, info.ModelName, settings, name, serializerData, null);
            }
            jsonWriter.WriteEndObject();
        }
Ejemplo n.º 6
0
        public void WriteObjectDictionary(ObjectDictionaryAttribute attribute, object writer,
                                          object value, WriteSettings settings, ObjectPropertyInfo info, object serializerData)
        {
            var current = serializerData.Convert <XElement>();
            //XElement element = new XElement(info.QName.ToXName());
            //current.Add(element);
            IDictionary dict = value as IDictionary;

            if (dict != null)
            {
                foreach (DictionaryEntry item in dict)
                {
                    XElement subItem = new XElement(item.Key.ToString());
                    current.Add(subItem);
                    Write(writer, item.Value, info.ModelName, settings, QName.Get(item.Key.ToString()), subItem, null);
                    //InternalWriteXml(writer, item.Value, settings, element, info.ModelName);
                }
            }
        }
Ejemplo n.º 7
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;
                }
            }
        }
Ejemplo n.º 8
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;
            }
        }
Ejemplo n.º 9
0
        public void WriteObjectDictionary(ObjectDictionaryAttribute attribute, object writer,
                                          object value, WriteSettings settings, ObjectPropertyInfo info, object serializerData)
        {
            XmlWriter   xmlWriter   = writer.Convert <XmlWriter>();
            PrefixTable prefixTable = serializerData.Convert <PrefixTable>();

            WriteStartElement(xmlWriter, info.QName, prefixTable);
            IDictionary dict = value as IDictionary;

            if (dict != null)
            {
                foreach (DictionaryEntry item in dict)
                {
                    Write(writer, item.Value, info.ModelName, settings,
                          QName.Get(item.Key.ToString()), prefixTable, null);
                    //xmlWriter.WriteStartElement(item.Key.ToString());
                    //InternalWriteXml(writer, item.Value, info.ModelName, settings, prefixTable);
                    //xmlWriter.WriteEndElement();
                }
            }

            xmlWriter.WriteEndElement();
        }
Ejemplo n.º 10
0
        private static BaseDictionaryAttribute GetDictionaryAttribute(object receiver, QName root)
        {
            Type                    valueType = ObjectUtil.GetDictionaryValueType(receiver.GetType(), "", null);
            ITkTypeConverter        converter = TkTypeDescriptor.GetSimpleConverter(valueType);
            BaseDictionaryAttribute attr;

            if (converter == null)
            {
                attr = new ObjectDictionaryAttribute {
                    ObjectType = valueType
                }
            }
            ;
            else
            {
                attr = new DictionaryAttribute {
                    ObjectType = valueType
                }
            };

            attr.Assign(root);
            return(attr);
        }
Ejemplo n.º 11
0
 public void ReadObjectDictionary(ObjectDictionaryAttribute attribute, object reader,
                                  object receiver, ReadSettings settings, ObjectPropertyInfo info, object serializerData)
 {
     throw new NotSupportedException();
 }
Ejemplo n.º 12
0
 public void WriteObjectDictionary(ObjectDictionaryAttribute attribute, object writer,
                                   object value, WriteSettings settings, ObjectPropertyInfo info, object serializerData)
 {
     throw new NotImplementedException();
 }