示例#1
0
        public static void ReadObject(IObjectSerializer serializer, object reader,
                                      object receiver, string modelName, ReadSettings settings, QName root,
                                      BaseObjectAttribute attribute, object serializerData, SerializerOptions options)
        {
            if (receiver is IDictionary)
            {
                BaseDictionaryAttribute attr = attribute as BaseDictionaryAttribute;
                options.CheckReadDictionary(serializer, attr);
                if (attr == null)
                {
                    attr = GetDictionaryAttribute(receiver, root);
                }

                serializer.ReadDictionary(reader, receiver.Convert <IDictionary>(), attr,
                                          modelName, settings, root, serializerData);
            }
            else if (receiver is IList)
            {
                SimpleElementAttribute attr = attribute as SimpleElementAttribute;
                options.CheckReadList(serializer, attr);
                if (attr == null)
                {
                    Type valueType = ObjectUtil.GetListValueType(receiver.GetType(), "", null);
                    attr = GetElementAttribute(receiver, valueType);
                }
                serializer.ReadList(reader, receiver.Convert <IList>(), attr, modelName,
                                    settings, root, serializerData);
            }
            else
            {
                options.CheckReadObject(serializer);
                serializer.ReadObject(reader, receiver, modelName, settings, root, serializerData);
            }
        }
示例#2
0
        public void WriteList(object writer, IEnumerable receiver, SimpleElementAttribute attribute,
                              string modelName, WriteSettings settings, QName root, object serializerData)
        {
            var info = new DictionaryListObjectPropertyInfo(receiver, attribute, root);

            attribute.WriteObject(this, writer, receiver, settings, info, serializerData);
        }
示例#3
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);
            }
        }
示例#4
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);
     }
 }
        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);
            }
        }
示例#6
0
        private static void WriteChildElement(ObjectElementInfo elements, object obj,
                                              Func <object, IElementWriter, object> getValueFunc, Action <ObjectPropertyInfo, object> writeAction,
                                              Action <bool> nullAction, Action <IList> listAction)
        {
            var propertyList = elements.CreateOrderPropertyInfoList();

            foreach (var item in propertyList)
            {
                object value = getValueFunc(obj, item);
                if (value == null)
                {
                    if (nullAction != null)
                    {
                        nullAction(true);
                    }
                    continue;
                }
                ObjectPropertyInfo property;
                if (item.IsSingle)
                {
                    property = item.Content;
                }
                else
                {
                    if (item.IsValueMulitple)
                    {
                        IList list = value as IList;
                        if (list.Count == 0)
                        {
                            continue;
                        }
                        property = item.Get(list[0].GetType());
                    }
                    else
                    {
                        property = item.Get(value.GetType());
                    }
                }
                SimpleElementAttribute attribute = property.Attribute as SimpleElementAttribute;
                if (item.IsSingle)
                {
                    if (attribute == null)
                    {
                        writeAction(property, value);
                    }
                    else
                    {
                        WriteElement(attribute.IsMultiple, value, item,
                                     (propList, objectType) => property, writeAction, nullAction, listAction);
                    }
                }
                else
                {
                    //AssertElementAttribute(property, attribute);
                    WriteElement(attribute.IsMultiple, value, item,
                                 (propList, objectType) => propList.Get(objectType.GetType()),
                                 writeAction, nullAction, listAction);
                }
            }
        }
示例#7
0
        public void ReadList(object reader, IList receiver, SimpleElementAttribute attribute,
                             string modelName, ReadSettings settings, QName root, object serializerData)
        {
            var info = new DictionaryListObjectPropertyInfo(receiver, attribute);

            attribute.ReadObject(this, reader, receiver, settings, info, serializerData);
        }
示例#8
0
 public void CheckReadList(IObjectSerializer serializer, SimpleElementAttribute attribute)
 {
     if (!ReadList)
     {
         throw new ToolkitException(string.Format(ObjectUtil.SysCulture,
                                                  "{0}不支持读取数据存储到列表的操作", serializer), serializer);
     }
     CheckAttribute(serializer, attribute, CheckListAttribute);
 }
示例#9
0
 public void CheckWriteList(IObjectSerializer serializer, SimpleElementAttribute attribute)
 {
     if (!WriteList)
     {
         throw new ToolkitException(string.Format(ObjectUtil.SysCulture,
                                                  "{0}不支持列表内容写到指定格式数据的操作", serializer), serializer);
     }
     CheckAttribute(serializer, attribute, CheckListAttribute);
 }
 public SimpleObjectPropertyInfo(object value, Type valueType,
                                 SimpleElementAttribute attribute, ITkTypeConverter converter)
     : base(attribute, null)
 {
     fValue     = value;
     fValueType = value.GetType();
     fLocalName = attribute.LocalName;
     Converter  = converter;
 }
示例#11
0
        public void ReadElement(SimpleElementAttribute attribute, object reader,
                                object receiver, ReadSettings settings, ObjectPropertyInfo info, object serializerData)
        {
            XmlReader xml      = reader.Convert <XmlReader>();
            string    value    = ReadString(xml);
            object    objValue = SerializerUtil.GetPropertyObject(receiver, settings, info,
                                                                  value, attribute.AutoTrim);

            SerializerUtil.AddElementValue(attribute, receiver, info, objValue);
        }
示例#12
0
        public void ReadElement(SimpleElementAttribute attribute, object reader,
                                object receiver, ReadSettings settings, ObjectPropertyInfo info, object serializerData)
        {
            XElement current = XElementData.GetCurrent(reader);

            string value    = current.Value;
            object objValue = SerializerUtil.GetPropertyObject(receiver, settings, info,
                                                               value, attribute.AutoTrim);

            SerializerUtil.AddElementValue(attribute, receiver, info, objValue);
        }
示例#13
0
 public void WriteElement(SimpleElementAttribute attribute, object writer, object value,
                          WriteSettings settings, ObjectPropertyInfo info, object serializerData)
 {
     if (attribute.IsMultiple)
     {
         throw new NotSupportedException();
     }
     else
     {
         SetValue(writer, info.LocalName, value, info, settings);
     }
 }
示例#14
0
 public static void AddElementValue(SimpleElementAttribute attribute, object receiver,
                                    ObjectPropertyInfo info, object objValue)
 {
     if (attribute.IsMultiple)
     {
         IList list = attribute.GetList(receiver, info);
         list.Add(objValue);
     }
     else
     {
         info.SetValue(receiver, objValue);
     }
 }
示例#15
0
        public void WriteElement(SimpleElementAttribute attribute, object writer,
                                 object value, WriteSettings settings, ObjectPropertyInfo info, object serializerData)
        {
            if (!attribute.IsMultiple && ObjectUtil.IsDefaultValue(attribute.DefaultValue, value))
            {
                return;
            }

            var      current = serializerData.Convert <XElement>();
            XElement element = new XElement(info.QName.ToXName(),
                                            ObjectUtil.ToString(info.Converter, value, settings));

            current.Add(element);
        }
示例#16
0
        public void ReadElement(SimpleElementAttribute attribute, object reader, object receiver,
                                ReadSettings settings, ObjectPropertyInfo info, object serializerData)
        {
            if (attribute.IsMultiple)
            {
                throw new NotSupportedException();
            }

            string value    = GetValue(reader, info.LocalName);
            object objValue = SerializerUtil.GetPropertyObject(receiver, settings, info,
                                                               value, attribute.AutoTrim);

            SerializerUtil.AddElementValue(attribute, receiver, info, objValue);
        }
示例#17
0
        public SingleElementWriter(ObjectPropertyInfo propertyInfo)
        {
            Content    = propertyInfo;
            fAttribute = Content.Attribute.Convert <NamedAttribute>();
            SimpleElementAttribute eleAttr = fAttribute as SimpleElementAttribute;

            if (eleAttr != null)
            {
                IsValueMulitple = eleAttr.IsMultiple;
            }
            IOrder order = fAttribute as IOrder;

            Order        = order != null ? order.Order : int.MaxValue;
            PropertyName = propertyInfo.PropertyName;
        }
 public void WriteElement(SimpleElementAttribute attribute, object writer,
                          object value, WriteSettings settings, ObjectPropertyInfo info, object serializerData)
 {
     if (attribute.IsMultiple)
     {
         IList list = value.Convert <IList>();
         foreach (object itemValue in list)
         {
             SetValue(writer, info, info.LocalName, itemValue, settings);
         }
     }
     else
     {
         SetValue(writer, info, info.LocalName, value, settings);
     }
 }
 public void WriteElement(SimpleElementAttribute attribute, object writer,
                          object value, WriteSettings settings, ObjectPropertyInfo info, object serializerData)
 {
     if (attribute.IsMultiple)
     {
         IList         list    = value.Convert <IList>();
         List <string> strList = new List <string>(list.Count);
         foreach (object itemValue in list)
         {
             string item = ObjectUtil.ToString(info.Converter, itemValue, settings);
             strList.Add(item);
         }
         SetValue(writer, info.LocalName, strList);
     }
     else
     {
         SetValue(writer, info, info.LocalName, value, settings);
     }
 }
示例#20
0
        private static SimpleElementAttribute GetElementAttribute(object receiver, Type valueType)
        {
            ITkTypeConverter       converter = TkTypeDescriptor.GetSimpleConverter(valueType);
            SimpleElementAttribute attr;

            if (converter == null)
            {
                attr = new ObjectElementAttribute
                {
                    ObjectType = valueType,
                    IsMultiple = true
                }
            }
            ;
            else
            {
                attr = new SimpleElementAttribute
                {
                    ObjectType = valueType,
                    IsMultiple = true
                }
            };
            return(attr);
        }
示例#21
0
        public void WriteElement(SimpleElementAttribute attribute, object writer,
                                 object value, WriteSettings settings, ObjectPropertyInfo info, object serializerData)
        {
            if (!attribute.IsMultiple && ObjectUtil.IsDefaultValue(attribute.DefaultValue, value))
            {
                return;
            }

            XmlWriter   xmlWriter   = writer.Convert <XmlWriter>();
            PrefixTable prefixTable = serializerData.Convert <PrefixTable>();

            WriteStartElement(xmlWriter, info.QName, prefixTable);
            string data = ObjectUtil.ToString(info.Converter, value, settings);

            if (attribute.UseCData)
            {
                xmlWriter.WriteCData(data);
            }
            else
            {
                xmlWriter.WriteString(data);
            }
            xmlWriter.WriteEndElement();
        }
示例#22
0
 public void ReadElement(SimpleElementAttribute attribute, object reader,
                         object receiver, ReadSettings settings, ObjectPropertyInfo info, object serializerData)
 {
     throw new NotSupportedException();
 }
示例#23
0
 public void WriteList(object writer, IEnumerable receiver, SimpleElementAttribute attribute,
                       string modelName, WriteSettings settings, QName root, object serializerData)
 {
     throw new NotImplementedException();
 }
示例#24
0
 public void WriteElement(SimpleElementAttribute attribute, object writer,
                          object value, WriteSettings settings, ObjectPropertyInfo info, object serializerData)
 {
     throw new NotImplementedException();
 }
示例#25
0
 public void ReadList(object reader, IList receiver, SimpleElementAttribute attribute,
                      string modelName, ReadSettings settings, QName root, object serializerData)
 {
     throw new NotSupportedException();
 }
示例#26
0
        public static void WriteObject(IObjectSerializer serializer, object writer,
                                       object receiver, string modelName, WriteSettings settings, QName root,
                                       BaseObjectAttribute attribute, object serializerData, SerializerOptions options)
        {
            Type type = receiver.GetType();

            if (receiver is IDictionary)
            {
                BaseDictionaryAttribute attr = attribute as BaseDictionaryAttribute;
                options.CheckWriteDictionary(serializer, attr);
                if (attr == null)
                {
                    attr = GetDictionaryAttribute(receiver, root);
                }
                serializer.WriteDictionary(writer, receiver.Convert <IDictionary>(), attr,
                                           modelName, settings, root, serializerData);
            }
            else if (type.IsArray || receiver is IList)
            {
                SimpleElementAttribute attr = attribute as SimpleElementAttribute;
                options.CheckWriteList(serializer, attr);
                if (attr == null)
                {
                    Type valueType;
                    if (type.IsArray)
                    {
                        Array arr = receiver as Array;
                        if (arr.Length == 0)
                        {
                            return;
                        }
                        var enumerator = arr.GetEnumerator();
                        enumerator.MoveNext();
                        valueType = enumerator.Current.GetType();
                    }
                    else
                    {
                        valueType = ObjectUtil.GetListValueType(type, "", null);
                    }
                    attr = GetElementAttribute(receiver, valueType);
                }
                serializer.WriteList(writer, receiver.Convert <IEnumerable>(), attr,
                                     modelName, settings, root, serializerData);
            }
            else
            {
                options.CheckWriteObject(serializer);
                ITkTypeConverter converter = TkTypeDescriptor.GetSimpleConverter(type);
                if (converter != null)
                {
                    SimpleElementAttribute simpleAttr = new SimpleElementAttribute
                    {
                        LocalName    = root.LocalName,
                        NamespaceUri = root.Namespace
                    };
                    var info = new SimpleObjectPropertyInfo(receiver, type, simpleAttr, converter);
                    serializer.WriteElement(simpleAttr, writer, receiver, settings, info, serializerData);
                }
                else
                {
                    serializer.WriteObject(writer, receiver, modelName, settings, root, serializerData);
                }
            }
        }
示例#27
0
        internal void ReadElementAttribute(Type type, PropertyInfo property, string modelName,
                                           object[] propertyAttributes)
        {
            object dynamicAttribute = (from attr in propertyAttributes
                                       where attr is DynamicElementAttribute
                                       select attr).FirstOrDefault();

            if (dynamicAttribute != null)
            {
                DynamicElementAttribute attribute = dynamicAttribute as DynamicElementAttribute;
                BaseXmlConfigFactory    factory   = attribute.PlugInFactory;
                if (fXmlConfigFactories == null)
                {
                    fXmlConfigFactories = new Dictionary <string, BaseXmlConfigFactory>();
                }
                if (!fXmlConfigFactories.ContainsKey(factory.Name))
                {
                    fXmlConfigFactories.Add(factory.Name, factory);
                }
                //factory.AddElements(this, type, property, modelName, attribute);
                ReflectorObjectPropertyInfo objInfo = new ReflectorObjectPropertyInfo(property, attribute, modelName);
                var dyn = new ConfigFactoryElementReader(attribute, objInfo, modelName);
                fMultiElements.Add(dyn);
                fElementWriter.Add(dyn);
            }
            else
            {
                //foreach (var attr in propertyAttributes)
                //{
                //    if (attr is BaseDictionaryAttribute)
                //        InternalAdd(type, property, attr as NamedAttribute, modelName);
                //    else
                //    {
                //        SimpleElementAttribute eleAttr = attr as SimpleElementAttribute;
                //        if (eleAttr != null)
                //            AddAttribute(eleAttr, type, property, modelName);
                //    }
                //}

                var objElemAttrs = (from item in propertyAttributes
                                    where item is ObjectElementAttribute
                                    select(ObjectElementAttribute) item).ToArray();
                if (objElemAttrs.Length == 0)
                {
                    foreach (var attr in propertyAttributes)
                    {
                        if (attr is BaseDictionaryAttribute)
                        {
                            AddSingleAttrible(type, property, attr as NamedAttribute, modelName);
                        }
                        else
                        {
                            SimpleElementAttribute eleAttr = attr as SimpleElementAttribute;
                            if (eleAttr != null)
                            {
                                AddSingleAttrible(type, property, eleAttr, modelName);
                            }
                        }
                    }
                }
                else if (objElemAttrs.Length == 1)
                {
                    AddSingleAttrible(type, property, objElemAttrs[0], modelName);
                }
                else
                {
                    var multi = new MultipleElementReader(property, modelName, objElemAttrs);
                    fMultiElements.Add(multi);
                    fElementWriter.Add(multi);
                }
            }
        }