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.º 2
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.º 3
0
        private void InternalWriteXml(object writer, object receiver, WriteSettings settings,
                                      XElement current, string modelName)
        {
            ObjectInfo info = ObjectInfo.Create(receiver, modelName);

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

            foreach (var attr in info.Attributes)
            {
                SerializerUtil.WritePropertyInfo(this, writer, receiver, settings, attr, current);
            }
            if (info.Content != null)
            {
                SerializerUtil.WritePropertyInfo(this, writer, receiver,
                                                 settings, info.Content, current);
            }
            else
            {
                SerializerUtil.WriteChildElement(this, writer, info.Elements, settings, receiver,
                                                 (objValue, propInfo) => propInfo.GetValue(objValue), current);
            }

            if (info.IsObjectContext)
            {
                BaseGlobalVariable.Current.ObjectContext.Pop();
            }
        }
Exemplo n.º 4
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.º 5
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.º 6
0
        public static void Write(IObjectSerializer serializer, object writer, object receiver,
                                 string modelName, WriteSettings settings)
        {
            ObjectInfo info = ObjectInfo.Create(receiver, modelName);

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

            foreach (var attr in info.Attributes)
            {
                SerializerUtil.WritePropertyInfo(serializer, writer,
                                                 receiver, settings, attr, null);
            }
            if (info.Content != null)
            {
                SerializerUtil.WritePropertyInfo(serializer, writer,
                                                 receiver, settings, info.Content, null);
            }
            else
            {
                SerializerUtil.WriteElements(serializer, info, writer,
                                             receiver, settings);
            }
            if (info.IsObjectContext)
            {
                BaseGlobalVariable.Current.ObjectContext.Pop();
            }
        }
Exemplo n.º 7
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.º 8
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.º 9
0
        public void ReadObject(object reader, object receiver, string modelName,
                               ReadSettings settings, QName root, object serializerData)
        {
            DataRow    row  = reader.Convert <DataRow>();
            ObjectInfo info = ObjectInfo.Create(receiver, modelName);

            if (info.IsObjectContext)
            {
                TkDebug.ThrowIfNoGlobalVariable();
                BaseGlobalVariable.Current.ObjectContext.Push(receiver);
            }
            DataColumnCollection cols = row.Table.Columns;

            foreach (DataColumn column in cols)
            {
                string columnName = column.ColumnName;

                SerializerUtil.ReadProperty(this, reader, receiver, settings,
                                            info, columnName, modelName);
            }
            SerializerUtil.ReadObjectCallBack(receiver);
            if (info.IsObjectContext)
            {
                BaseGlobalVariable.Current.ObjectContext.Pop();
            }
        }
Exemplo n.º 10
0
        private void WriteAObject(object writer, object receiver, string modelName,
                                  WriteSettings settings, JsonTextWriter jsonWriter)
        {
            ObjectInfo info = ObjectInfo.Create(receiver, modelName);

            if (info.IsObjectContext)
            {
                TkDebug.ThrowIfNoGlobalVariable();
                BaseGlobalVariable.Current.ObjectContext.Push(receiver);
            }
            jsonWriter.WriteStartObject();
            foreach (var attr in info.Attributes)
            {
                SerializerUtil.WritePropertyInfo(this, writer, receiver, settings, attr, null);
            }

            if (info.Content != null)
            {
                SerializerUtil.WritePropertyInfo(this, writer, receiver, settings, info.Content, null);
            }
            else
            {
                SerializerUtil.WriteElements(this, info, writer, receiver, settings);
            }
            jsonWriter.WriteEndObject();
            jsonWriter.Flush();

            if (info.IsObjectContext)
            {
                BaseGlobalVariable.Current.ObjectContext.Pop();
            }
        }
Exemplo n.º 11
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.º 12
0
        public void ReadTagElement(TagElementAttribute attribute, object reader,
                                   object receiver, ReadSettings settings, ObjectPropertyInfo info, object serializerData)
        {
            XmlReader xmlReader = reader.Convert <XmlReader>();

            InternalReadElement(xmlReader, receiver, settings, info.QName,
                                attribute.ChildElements, info.ModelName);
            SerializerUtil.CheckElementRequired(attribute.ChildElements, receiver);
        }
Exemplo n.º 13
0
        public void ReadComplexElement(SimpleComplexElementAttribute attribute, object reader,
                                       object receiver, ReadSettings settings, ObjectPropertyInfo info, object serializerData)
        {
            XmlReader xml      = reader.Convert <XmlReader>();
            string    value    = ReadComplexContent(xml, info.QName);
            object    objValue = SerializerUtil.GetPropertyObject(receiver, settings, info, value, false);

            SerializerUtil.AddElementValue(attribute, receiver, info, objValue);
        }
Exemplo n.º 14
0
        public void ReadTagElement(TagElementAttribute attribute, object reader,
                                   object receiver, ReadSettings settings, ObjectPropertyInfo info, object serializerData)
        {
            XElementData data = reader.Convert <XElementData>();

            //XElementData next = new XElementData { Current = data.Current.Elements().First(), Root = data.Root };
            InternalReadElement(data, receiver, settings, info.QName, attribute.ChildElements, info.ModelName);
            SerializerUtil.CheckElementRequired(attribute.ChildElements, receiver);
        }
Exemplo n.º 15
0
        public void WriteTagElement(TagElementAttribute 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);
            SerializerUtil.WriteChildElement(this, writer, attribute.ChildElements, settings,
                                             value, (objValue, propInfo) => value, element);
        }
Exemplo n.º 16
0
        public void ReadTextContent(TextContentAttribute attribute, object reader,
                                    object receiver, ReadSettings settings, ObjectPropertyInfo info, object serializerData)
        {
            XmlReader xml = reader.Convert <XmlReader>();

            string value = ReadString(xml);

            SerializerUtil.CheckRequiredContent(attribute, receiver, info, value);
            SerializerUtil.SetObjectValue(receiver, settings, info, value, attribute.AutoTrim);
        }
Exemplo n.º 17
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);
        }
Exemplo n.º 18
0
        public void ReadComplexContent(ComplexContentAttribute attribute, object reader,
                                       object receiver, ReadSettings settings, ObjectPropertyInfo info, object serializerData)
        {
            XElement current = XElementData.GetCurrent(reader);

            string value = current.HasElements ? current.FirstNode.ToString() : null;

            SerializerUtil.CheckRequiredContent(attribute, receiver, info, value);
            SerializerUtil.SetObjectValue(receiver, settings, info, value, false);
        }
Exemplo n.º 19
0
        public void ReadComplexElement(SimpleComplexElementAttribute attribute, object reader,
                                       object receiver, ReadSettings settings, ObjectPropertyInfo info, object serializerData)
        {
            XElement current = XElementData.GetCurrent(reader);

            string value    = current.HasElements ? current.FirstNode.ToString() : null;
            object objValue = SerializerUtil.GetPropertyObject(receiver, settings, info, value, false);

            SerializerUtil.AddElementValue(attribute, receiver, info, objValue);
        }
Exemplo n.º 20
0
        public void ReadTextContent(TextContentAttribute attribute, object reader,
                                    object receiver, ReadSettings settings, ObjectPropertyInfo info, object serializerData)
        {
            XElement current = XElementData.GetCurrent(reader);

            string value = current.Value;

            SerializerUtil.CheckRequiredContent(attribute, receiver, info, value);
            SerializerUtil.SetObjectValue(receiver, settings, info, value, attribute.AutoTrim);
        }
Exemplo n.º 21
0
        public void ReadObject(object reader, object receiver, string modelName, ReadSettings settings,
                               QName root, object serializerData)
        {
            TkDebug.AssertArgumentNull(receiver, "receiver", this);
            TkDebug.AssertArgumentNull(settings, "settings", this);
            TkDebug.AssertArgumentNull(root, "root", this);

            XElementData currentData = reader.Convert <XElementData>();
            XElement     current     = currentData.Current;
            ObjectInfo   info        = ObjectInfo.Create(receiver, modelName);

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

            foreach (var item in info.Attributes)
            {
                item.Attribute.ReadObject(this, reader, receiver, settings, item, serializerData);
            }

            if (current.IsEmpty)
            {
                SerializerUtil.ReadObjectCallBack(receiver);
                if (info.IsObjectContext)
                {
                    BaseGlobalVariable.Current.ObjectContext.Pop();
                }
                return;
            }

            bool readElement = false;

            if (info.Content != null)
            {
                info.Content.Attribute.ReadObject(this, reader, receiver, settings,
                                                  info.Content, serializerData);
            }
            else
            {
                readElement = true;
                InternalReadElement(currentData, receiver, settings, root, info.Elements, modelName);
            }

            SerializerUtil.ReadObjectCallBack(receiver);
            if (readElement)
            {
                SerializerUtil.CheckElementRequired(info.Elements, receiver);
            }
            if (info.IsObjectContext)
            {
                BaseGlobalVariable.Current.ObjectContext.Pop();
            }
        }
Exemplo n.º 22
0
        public void WriteTagElement(TagElementAttribute 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);
            SerializerUtil.WriteChildElement(this, writer, attribute.ChildElements, settings,
                                             value, (objValue, propInfo) => value, prefixTable);
            xmlWriter.WriteEndElement();
        }
Exemplo n.º 23
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);
        }
Exemplo n.º 24
0
        private static object ReadObject(bool usector, ReadSettings settings, DataRow row,
                                         Type objectType, object receiver, IObjectSerializer datarowSerializer, string modelName)
        {
            object subObject = usector ? ObjectUtil.CreateObjectWithCtor(objectType)
                : ObjectUtil.CreateObject(objectType);
            object rowReader = datarowSerializer.CreateCustomReader(row);

            datarowSerializer.ReadObject(rowReader, subObject, modelName, settings, QName.Toolkit, null);
            SerializerUtil.SetParent(receiver, subObject);

            return(subObject);
        }
        public void ReadObject(object reader, object receiver, string modelName,
                               ReadSettings settings, QName root, object serializerData)
        {
            QueryStringBuilder builder = reader.Convert <QueryStringBuilder>();
            ObjectInfo         info    = ObjectInfo.Create(receiver, modelName);

            foreach (string key in builder.AllKeys)
            {
                SerializerUtil.ReadProperty(this, reader, receiver, settings, info, key, modelName);
            }
            SerializerUtil.ReadObjectCallBack(receiver);
        }
Exemplo n.º 26
0
        private object ReadObject(bool usector, ReadSettings settings, JsonTextReader jsonReader,
                                  Type objectType, object receiver, string modelName)
        {
            object subObject = usector ? ObjectUtil.CreateObjectWithCtor(objectType)
                : ObjectUtil.CreateObject(objectType);

            //InternalRead(jsonReader, subObject, modelName, settings);
            //允许递归调用,内嵌的如果不是对象,是数组或者Dictionary,都支持了
            Read(jsonReader, subObject, modelName, settings, null, null);
            SerializerUtil.SetParent(receiver, subObject);
            return(subObject);
        }
Exemplo n.º 27
0
        public void ReadDynamicDictionary(DynamicDictionaryAttribute 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);

            if (xml.IsEmptyElement)
            {
                return;
            }
            var configData = attribute.PlugInFactory.ConfigData;
            //var propertyInfo = info.Convert<ReflectorObjectPropertyInfo>().Property;
            var elementReader = new ConfigFactoryElementReader(attribute, info, info.ModelName);

            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);
                    ObjectPropertyInfo subPropertyInfo;
                    if (elementReader.SupportVersion)
                    {
                        string version = xml.GetAttribute(ToolkitConst.VERSION);
                        subPropertyInfo = elementReader[nodeName, version];
                    }
                    else
                    {
                        subPropertyInfo = elementReader[nodeName];
                    }
                    if (subPropertyInfo != null)
                    {
                        ObjectElementAttribute elementAttr = subPropertyInfo.Attribute.Convert <ObjectElementAttribute>();
                        Type   objectType = elementAttr.ObjectType;
                        object subObject  = elementAttr.UseConstructor ? ObjectUtil.CreateObjectWithCtor(objectType)
                            : ObjectUtil.CreateObject(objectType);
                        Read(reader, subObject, info.ModelName, settings, nodeName, null);
                        SerializerUtil.SetParent(receiver, subObject);
                        dict[nodeName.LocalName] = subObject;
                    }
                }
            }
        }
Exemplo n.º 28
0
        private void WriteAObject(object writer, object receiver, string modelName,
                                  WriteSettings settings)
        {
            ObjectInfo info = ObjectInfo.Create(receiver, modelName);

            foreach (var attr in info.Attributes)
            {
                SerializerUtil.WritePropertyInfo(this, writer, receiver, settings, attr, null);
            }

            SerializerUtil.WriteElements(this, info, writer, receiver, settings);
        }
Exemplo n.º 29
0
        public void ReadAttribute(SimpleAttributeAttribute attribute, object reader,
                                  object receiver, ReadSettings settings, ObjectPropertyInfo info, object serializerData)
        {
            XElement current = XElementData.GetCurrent(reader);

            XName      name  = info.QName.ToXName();
            XAttribute attr  = current.Attribute(name);
            string     value = attr == null ? null : attr.Value;

            SerializerUtil.CheckRequiredAttribute(attribute, receiver, info, value);
            SerializerUtil.SetObjectValue(receiver, settings, info, value, attribute.AutoTrim);
        }
Exemplo n.º 30
0
        public void ReadObjectElement(ObjectElementAttribute attribute, object reader,
                                      object receiver, ReadSettings settings, ObjectPropertyInfo info, object serializerData)
        {
            Type   objectType = info.ObjectType;
            object subObject  = attribute.UseConstructor ? ObjectUtil.CreateObjectWithCtor(objectType)
                : ObjectUtil.CreateObject(objectType);

            Read(reader, subObject, info.ModelName, settings, info.QName, null);
            SerializerUtil.SetParent(receiver, subObject);

            SerializerUtil.AddElementValue(attribute, receiver, info, subObject);
        }