Exemplo n.º 1
0
        public void WriteTextContent(TextContentAttribute attribute, object writer,
                                     object value, WriteSettings settings, ObjectPropertyInfo info, object serializerData)
        {
            XmlWriter xmlWriter = writer.Convert <XmlWriter>();

            xmlWriter.WriteString(ObjectUtil.ToString(info.Converter, value, settings));
        }
Exemplo n.º 2
0
        internal static void SetObjectValue(object receiver, ReadSettings settings,
                                            ObjectPropertyInfo info, string value, bool autoTrim)
        {
            object obj = GetPropertyObject(receiver, settings, info, value, autoTrim);

            info.SetValue(receiver, obj);
        }
Exemplo n.º 3
0
 private static void WriteElement(bool isMultiple, object value, IElementWriter list,
                                  Func <IElementWriter, object, ObjectPropertyInfo> getPropertyFunc,
                                  Action <ObjectPropertyInfo, object> writeAction, Action <bool> nullAction, Action <IList> listAction)
 {
     if (nullAction != null)
     {
         nullAction(false);
     }
     if (isMultiple)
     {
         IList listValue = value.Convert <IList>();
         if (listAction != null)
         {
             listAction(listValue);
         }
         foreach (object itemValue in listValue)
         {
             ObjectPropertyInfo property = getPropertyFunc(list, itemValue);
             if (property != null)
             {
                 writeAction(property, itemValue);
             }
         }
     }
     else
     {
         ObjectPropertyInfo property = getPropertyFunc(list, value);
         if (property != null)
         {
             writeAction(property, value);
         }
     }
 }
Exemplo n.º 4
0
        public void ReadObjectElement(ObjectElementAttribute attribute, object reader, object receiver,
                                      ReadSettings settings, ObjectPropertyInfo info, object serializerData)
        {
            DataTable table = serializerData.Convert <DataTable>();

            if (table.Rows.Count == 0)
            {
                return;
            }

            Type objectType = info.ObjectType;
            IObjectSerializer rowSerializer = ObjectExtension.CreateSerializer("DataRow");

            if (attribute.IsMultiple)
            {
                IList list = attribute.GetList(receiver, info);
                foreach (DataRow row in table.Rows)
                {
                    object subObject = ReadObject(attribute.UseConstructor, settings, row, objectType,
                                                  receiver, rowSerializer, info.ModelName);
                    list.Add(subObject);
                }
            }
            else
            {
                object subObject = ReadObject(attribute.UseConstructor, settings, table.Rows[0],
                                              objectType, receiver, rowSerializer, info.ModelName);
                info.SetValue(receiver, subObject);
            }
        }
Exemplo n.º 5
0
        public static void ReadProperty(IObjectSerializer serializer, object reader,
                                        object receiver, ReadSettings settings, ObjectInfo info, string localName, string modelName)
        {
            ObjectPropertyInfo property = info.GetAttribute(localName);

            if (property != null)
            {
                property.Attribute.ReadObject(serializer, reader, receiver, settings, property, null);
            }
            else
            {
                property = info.Elements[localName];
                if (property != null)
                {
                    property.Attribute.ReadObject(serializer, reader, receiver, settings, property, null);
                }
                else if (info.Content != null && localName == ObjectInfo.CONTENT_NAME)
                {
                    info.Content.Attribute.ReadObject(serializer, reader, receiver, settings, info.Content, null);
                }
                else
                {
                    property = SerializerUtil.CustomRead(receiver, localName, modelName, () => null);
                    if (property != null)
                    {
                        property.Attribute.ReadObject(serializer, reader, receiver, settings, property, null);
                    }
                }
            }
        }
Exemplo n.º 6
0
        public void WriteObject(object writer, object receiver, string modelName,
                                WriteSettings settings, QName root, object serializerData)
        {
            ObjectInfo info = ObjectInfo.Create(receiver, modelName);

            if (info.IsObjectContext)
            {
                TkDebug.ThrowIfNoGlobalVariable();
                BaseGlobalVariable.Current.ObjectContext.Push(receiver);
            }

            var elements      = info.Elements.CreateOrderPropertyInfoList();
            var writeElements = from item in elements
                                where item.IsSingle && IsSupport(item.Content.Attribute)
                                select item;

            foreach (var item in writeElements)
            {
                ObjectPropertyInfo property = item.Content;
                object             value    = property.GetValue(receiver);
                if (value == null)
                {
                    continue;
                }

                item.Content.Attribute.WriteObject(this, writer, value, settings,
                                                   item.Content, serializerData);
            }

            if (info.IsObjectContext)
            {
                BaseGlobalVariable.Current.ObjectContext.Pop();
            }
        }
Exemplo n.º 7
0
        public void WriteObjectElement(ObjectElementAttribute 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;
            }

            IEnumerable list;

            if (attribute.IsMultiple)
            {
                list = value as IList;
            }
            else
            {
                list = EnumUtil.Convert(value);
            }

            DataTable table = list.CreateTable(tableName, info.ModelName, settings);

            if (table != null)
            {
                ds.Tables.Add(table);
            }
        }
Exemplo n.º 8
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;
                }
            }
        }
Exemplo n.º 9
0
        public void WriteDynamicDictionary(DynamicDictionaryAttribute 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)
            {
                var configData = attribute.PlugInFactory.ConfigData;
                foreach (DictionaryEntry item in dict)
                {
                    var   itemAttr = configData[item.Value.GetType()];
                    QName itemName = itemAttr.GetQName(itemAttr.LocalName);
                    Write(writer, item.Value, info.ModelName, settings, itemName, prefixTable, null);
                    //WriteStartElement(xmlWriter, itemName, prefixTable);
                    //InternalWriteXml(writer, item.Value, info.ModelName, settings, prefixTable);
                    //xmlWriter.WriteEndElement();
                }
            }

            xmlWriter.WriteEndElement();
        }
Exemplo n.º 10
0
        public void WriteTextContent(TextContentAttribute attribute, object writer,
                                     object value, WriteSettings settings, ObjectPropertyInfo info, object serializerData)
        {
            var current = serializerData.Convert <XElement>();

            current.Value = ObjectUtil.ToString(info.Converter, value, settings);
        }
Exemplo n.º 11
0
        internal IList GetList(object receiver, ObjectPropertyInfo info)
        {
            object value = ObjectUtil.GetPropertyValue(receiver, CollectionType, info);
            IList  list  = value.Convert <IList>();

            return(list);
        }
Exemplo n.º 12
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);
            }
        }
Exemplo n.º 13
0
        public void ReadDynamicDictionary(DynamicDictionaryAttribute 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);

            var configData = attribute.PlugInFactory.ConfigData;
            //var propertyInfo = info.Convert<ReflectorObjectPropertyInfo>().Property;
            var elementReader = new ConfigFactoryElementReader(attribute, info, info.ModelName);

            while (jsonReader.Read())
            {
                if (jsonReader.TokenType == JsonToken.EndObject)
                {
                    break;
                }
                AssertReadState(jsonReader, JsonToken.PropertyName, info.PropertyName);
                string name = jsonReader.Value.ToString();
                AssertRead(jsonReader);

                AssertReadState(jsonReader, JsonToken.StartObject, info.PropertyName);
                ObjectPropertyInfo subPropertyInfo = elementReader[name];
                Type   objectType     = subPropertyInfo.ObjectType;
                bool   useConstructor = subPropertyInfo.Attribute.Convert <ObjectElementAttribute>().UseConstructor;
                object subObject      = ReadObject(useConstructor, settings, jsonReader,
                                                   objectType, receiver, info.ModelName);
                dict[name] = subObject;
                //dict.Add(name, value);
            }
        }
Exemplo n.º 14
0
        //private void InternalWriteXml(object writer, object receiver, string modelName,
        //    WriteSettings settings, PrefixTable prefixTable)
        //{
        //}

        private void InternalReadElement(XmlReader reader, object receiver, ReadSettings settings,
                                         QName root, ObjectElementInfo elements, string modelName)
        {
            while (reader.Read())
            {
                XmlNodeType nodeType = reader.NodeType;
                if (nodeType == XmlNodeType.EndElement)
                {
                    QName name = QName.Get(reader);
                    if (name == root)
                    {
                        break;
                    }
                }
                if (nodeType == XmlNodeType.Element)
                {
                    QName name = QName.Get(reader);
                    ObjectPropertyInfo element = elements.GetObjectPerpertyInfo(name,
                                                                                () => reader.GetAttribute(ToolkitConst.VERSION));
                    if (element != null)
                    {
                        element.Attribute.ReadObject(this, reader, receiver, settings, element, null);
                    }
                    else
                    {
                        element = SerializerUtil.CustomRead(receiver, name.LocalName, modelName,
                                                            () => reader.GetAttribute(ToolkitConst.VERSION));
                        if (element != null)
                        {
                            element.Attribute.ReadObject(this, reader, receiver, settings, element, null);
                        }
                    }
                }
            }
        }
Exemplo n.º 15
0
        public void ReadDictionary(DictionaryAttribute attribute, object reader,
                                   object receiver, ReadSettings settings, ObjectPropertyInfo info, object serializerData)
        {
            JsonTextReader jsonReader = reader.Convert <JsonTextReader>();

            // 考虑支持多层次Dictionary读取,外层Object可能读取过,所以这里做判断对此进行屏蔽 2019.6.24
            if (jsonReader.TokenType != JsonToken.StartObject)
            {
                AssertRead(jsonReader);
            }
            AssertReadState(jsonReader, JsonToken.StartObject, info.PropertyName);
            IDictionary dict = attribute.GetDictionary(receiver, info);

            while (jsonReader.Read())
            {
                if (jsonReader.TokenType == JsonToken.EndObject)
                {
                    break;
                }
                AssertReadState(jsonReader, JsonToken.PropertyName, info.PropertyName);
                string name = jsonReader.Value.ToString();
                AssertRead(jsonReader);
                object value = GetSimpleValue(jsonReader, receiver, settings,
                                              info, attribute.AutoTrim);
                dict[name] = value;
            }
        }
Exemplo n.º 16
0
        public string GetPropertyName(string localName)
        {
            ObjectPropertyInfo prop = null;

            if (fAttributes.Contains(localName))
            {
                prop = fAttributes[localName];
            }
            else if (fContent != null)
            {
                if (localName == CONTENT_NAME)
                {
                    prop = fContent;
                }
            }
            else
            {
                prop = fElements[localName];
            }

            if (prop != null)
            {
                return(prop.PropertyName);
            }

            return(null);
        }
Exemplo n.º 17
0
        public void ReadTagElement(TagElementAttribute attribute, object reader,
                                   object receiver, ReadSettings settings, ObjectPropertyInfo info, object serializerData)
        {
            JsonTextReader jsonReader = reader.Convert <JsonTextReader>();

            AssertRead(jsonReader);
            AssertReadState(jsonReader, JsonToken.StartObject, info.PropertyName);
            while (jsonReader.Read())
            {
                if (jsonReader.TokenType == JsonToken.EndObject)
                {
                    break;
                }

                AssertReadState(jsonReader, JsonToken.PropertyName, info.PropertyName);
                string             localName = jsonReader.Value.ToString();
                ObjectPropertyInfo childInfo = attribute.ChildElements[localName];
                TkDebug.AssertNotNull(childInfo, string.Format(ObjectUtil.SysCulture,
                                                               "{0}没有在TagElement类型中声明,无法读取", localName), this);

                childInfo.Attribute.ReadObject(this, reader, receiver, settings, childInfo, serializerData);
            }

            SerializerUtil.CheckElementRequired(attribute.ChildElements, receiver);
        }
Exemplo n.º 18
0
        private void InternalReadElement(XElementData reader, object receiver, ReadSettings settings,

                                         QName root, ObjectElementInfo elements, string modelName)
        {
            foreach (var item in reader.Current.Elements())
            {
                QName name = item.Name.ToQName();
                ObjectPropertyInfo element = elements.GetObjectPerpertyInfo(name,
                                                                            () => reader.Current.Attribute(ToolkitConst.VERSION).Value);
                if (element != null)
                {
                    XElementData next = new XElementData {
                        Current = item, Root = reader.Root
                    };
                    element.Attribute.ReadObject(this, next, receiver, settings, element, null);
                }
                else
                {
                    element = SerializerUtil.CustomRead(receiver, name.LocalName, modelName,
                                                        () => reader.Current.Attribute(ToolkitConst.VERSION).Value);
                    if (element != null)
                    {
                        element.Attribute.ReadObject(this, reader, receiver, settings, element, null);
                    }
                }
            }
        }
Exemplo n.º 19
0
        public IDictionary GetDictionary(object receiver, ObjectPropertyInfo info)
        {
            object      value      = ObjectUtil.GetPropertyValue(receiver, CollectionType, info);
            IDictionary dictionary = value.Convert <IDictionary>();

            return(dictionary);
        }
Exemplo n.º 20
0
        public void WriteObjectElement(ObjectElementAttribute attribute, object writer, object value,
                                       WriteSettings settings, ObjectPropertyInfo info, object serializerData)
        {
            string xml = value.WriteXml(settings, QName.ToolkitNoNS);

            SetValue(writer, info.LocalName, xml, info, settings);
        }
Exemplo n.º 21
0
        private static void SetValue(object writer, ObjectPropertyInfo info, string fieldName,
                                     object value, WriteSettings settings)
        {
            DictionaryBuilder builder = writer.Convert <DictionaryBuilder>();

            builder.Add(fieldName, ObjectUtil.ToString(info.Converter, value, settings));
        }
Exemplo n.º 22
0
        public void WriteObjectElement(ObjectElementAttribute attribute, object writer,
                                       object value, WriteSettings settings, ObjectPropertyInfo info, object serializerData)
        {
            DictionaryBuilder builder = writer.Convert <DictionaryBuilder>();
            QName             name    = info.QName;

            if (attribute.IsMultiple)
            {
                IList list = value.Convert <IList>();
                List <Dictionary <string, object> > itemList =
                    new List <Dictionary <string, object> >(list.Count);
                foreach (var itemValue in list)
                {
                    DictionaryBuilder subBuilder = builder.CreateBuilder();
                    WriteObject(subBuilder, itemValue, info.ModelName, settings, name, null);
                    itemList.Add(subBuilder.Data);
                }
                builder.Add(name.LocalName, itemList);
            }
            else
            {
                DictionaryBuilder subBuilder = builder.CreateBuilder();
                WriteObject(subBuilder, value, info.ModelName, settings, name, null);
                builder.Add(name.LocalName, subBuilder);
            }
        }
Exemplo n.º 23
0
 public void WriteElement(SimpleElementAttribute attribute, object writer, object value,
                          WriteSettings settings, ObjectPropertyInfo info, object serializerData)
 {
     if (attribute.IsMultiple)
     {
         var        list       = value.Convert <IEnumerable>();
         JsonWriter jsonWriter = writer.Convert <JsonWriter>();
         QName      name       = info.QName;
         if (info.WriteMode == SerializerWriteMode.WriteName)
         {
             jsonWriter.WritePropertyName(name.LocalName);
         }
         jsonWriter.WriteStartArray();
         foreach (var itemValue in list)
         {
             WriteSimpleJsonValue(jsonWriter, itemValue, settings, info, attribute.UseSourceType);
         }
         //jsonWriter.WriteValue(ObjectUtil.ToString(info, itemValue, settings));
         jsonWriter.WriteEndArray();
     }
     else
     {
         WriteSimpleElement(attribute, writer, value, settings, info, attribute.UseSourceType);
     }
 }
Exemplo n.º 24
0
        public void ReadComplexElement(SimpleComplexElementAttribute attribute, object reader,
                                       object receiver, ReadSettings settings, ObjectPropertyInfo info, object serializerData)
        {
            if (attribute.IsMultiple)
            {
                JsonTextReader jsonReader = reader.Convert <JsonTextReader>();
                IList          list       = attribute.GetList(receiver, info);
                AssertRead(jsonReader);
                AssertReadState(jsonReader, JsonToken.StartArray, info.PropertyName);
                while (jsonReader.Read())
                {
                    if (jsonReader.TokenType == JsonToken.EndArray)
                    {
                        break;
                    }

                    object value = ReadComplexValue(jsonReader, receiver, settings, info);
                    list.Add(value);
                }
            }
            else
            {
                ReadComplexValue(reader, receiver, settings, info);
            }
        }
        public void ReadElement(SimpleElementAttribute attribute, object reader,
                                object receiver, ReadSettings settings, ObjectPropertyInfo info, object serializerData)
        {
            if (attribute.IsMultiple)
            {
                QueryStringValue itemValue = GetValue(reader, info.LocalName);
                if (itemValue == null)
                {
                    return;
                }
                var itemValues = itemValue.Values;
                if (itemValues == null)
                {
                    return;
                }

                foreach (var item in itemValues)
                {
                    object objValue = SerializerUtil.GetPropertyObject(receiver, settings, info,
                                                                       item, attribute.AutoTrim);
                    SerializerUtil.AddElementValue(attribute, receiver, info, objValue);
                }
            }
            else
            {
                SerializerUtil.SetObjectValue(receiver, settings, info,
                                              GetValue(reader, info.LocalName).ConvertToString(), attribute.AutoTrim);
            }
        }
Exemplo n.º 26
0
        private object GetSimpleValue(JsonTextReader reader, object receiver,
                                      ReadSettings settings, ObjectPropertyInfo info, bool autoTrim)
        {
            switch (reader.TokenType)
            {
            case JsonToken.Null:
            case JsonToken.Undefined:
                return(null);

            case JsonToken.Integer:
            case JsonToken.Float:
            case JsonToken.Boolean:
            case JsonToken.String:
                string value = reader.Value.ToString();
                return(SerializerUtil.GetPropertyObject(receiver, settings, info, value, autoTrim));

            case JsonToken.Date:
            case JsonToken.Bytes:
                return(reader.Value);

            default:
                TkDebug.ThrowToolkitException(string.Format(ObjectUtil.SysCulture,
                                                            "读取属性{3}时发生JSON错误。当前的状态是{0},不是有效的值状态,在第{1}行,第{2}列",
                                                            reader.TokenType, reader.CurrentLineNumber, reader.CurrentLinePosition,
                                                            info.PropertyName), this);
                return(null);
            }
        }
Exemplo n.º 27
0
        public void ReadElement(SimpleElementAttribute attribute, object reader,
                                object receiver, ReadSettings settings, ObjectPropertyInfo info, object serializerData)
        {
            if (attribute.IsMultiple)
            {
                JsonTextReader jsonReader = reader.Convert <JsonTextReader>();
                IList          list       = attribute.GetList(receiver, info);
                // 考虑支持多层次Array读取,外层Object可能读取过,所以这里做判断对此进行屏蔽 2019.6.24
                if (jsonReader.TokenType != JsonToken.StartArray)
                {
                    AssertRead(jsonReader);
                }
                AssertReadState(jsonReader, JsonToken.StartArray, info.PropertyName);
                while (jsonReader.Read())
                {
                    if (jsonReader.TokenType == JsonToken.EndArray)
                    {
                        break;
                    }

                    object value = GetSimpleValue(jsonReader, receiver, settings,
                                                  info, attribute.AutoTrim);
                    list.Add(value);
                }
            }
            else
            {
                ReadSimpleValue(reader, receiver, settings, info, attribute.AutoTrim);
            }
        }
Exemplo n.º 28
0
        public void WriteComplexContent(ComplexContentAttribute attribute, object writer,
                                        object value, WriteSettings settings, ObjectPropertyInfo info, object serializerData)
        {
            XmlWriter xmlWriter = writer.Convert <XmlWriter>();
            string    text      = ObjectUtil.ToString(info.Converter, value, settings);

            WriteComplexContent(xmlWriter, text);
        }
Exemplo n.º 29
0
        public void ReadAttribute(SimpleAttributeAttribute attribute, object reader,
                                  object receiver, ReadSettings settings, ObjectPropertyInfo info, object serializerData)
        {
            object value = ReadSimpleValue(reader, receiver, settings, info, attribute.AutoTrim);

            SerializerUtil.CheckRequiredAttribute(attribute, receiver, info,
                                                  value.ConvertToString());
        }
Exemplo n.º 30
0
        public void WriteTextContent(TextContentAttribute attribute, object writer,
                                     object value, WriteSettings settings, ObjectPropertyInfo info, object serializerData)
        {
            JsonWriter jsonWriter = writer.Convert <JsonWriter>();

            jsonWriter.WritePropertyName(CONTENT_NAME);
            WriteSimpleJsonValue(jsonWriter, value, settings, info, attribute.UseSourceType);
        }