예제 #1
0
        public void ReadDictionary(object reader, IDictionary receiver, BaseDictionaryAttribute attribute,
                                   string modelName, ReadSettings settings, QName root, object serializerData)
        {
            var info = new DictionaryListObjectPropertyInfo(receiver, attribute, root);

            attribute.ReadObject(this, reader, receiver, settings, info, serializerData);
        }
예제 #2
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);
            }
        }
        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);
            }
        }
예제 #4
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;
            }
        }
예제 #5
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);
        }
예제 #6
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();
            }
        }
예제 #7
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);
            }
        }
예제 #8
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);
                        }
                    }
                }
            }
        }
예제 #9
0
        public static void ReadXml(this object receiver, string modelName, string xml,
                                   ReadSettings settings, QName root)
        {
            TkDebug.AssertArgumentNull(root, "root", null);

            ReadFromString(receiver, "Xml", modelName, xml, settings, root);
        }
예제 #10
0
        internal static object InternalGetDefaultValue(Type type, string strValue, object defaultValue,
                                                       ReadSettings settings, bool throwException)
        {
            ITkTypeConverter converter = TkTypeDescriptor.GetConverter(type);

            if (throwException)
            {
                TkDebug.AssertNotNull(converter, string.Format(ObjectUtil.SysCulture,
                                                               "无法获取类型{0}的TypeConverter,请确认是否为其配置TypeConverterAttribute",
                                                               type), null);
            }
            else
            {
                if (converter == null)
                {
                    return(GetTypeDefaultValue(type));
                }
            }
            try
            {
                return(strValue == null?InternalGetDefaultValue(type, defaultValue)
                           : converter.ConvertFromString(strValue, settings));
            }
            catch
            {
                return(InternalGetDefaultValue(type, defaultValue));
            }
        }
예제 #11
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);
            }
        }
예제 #12
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);
        }
예제 #13
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);
                    }
                }
            }
        }
예제 #14
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);
            }
        }
예제 #15
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;
                }
            }
        }
예제 #16
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);
                    }
                }
            }
        }
예제 #17
0
        public static T ReadXmlFromFactory <T>(this string xml, string factoryName,
                                               ReadSettings settings) where T : class
        {
            object result = ReadXmlFromFactory(xml, factoryName, settings);

            return(result as T);
        }
예제 #18
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);
            }
        }
예제 #19
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);
            }
        }
예제 #20
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);
        }
예제 #21
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);
            }
        }
예제 #22
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());
        }
예제 #23
0
 protected override object InternalConvertFromString(string text, ReadSettings settings)
 {
     if (string.IsNullOrEmpty(text))
     {
         return(null);
     }
     return(PlugInFactoryManager.CreateInstance <T>(FactoryName, text));
 }
예제 #24
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);
        }
예제 #25
0
        public object CreateReader(Stream stream, ReadSettings settings)
        {
            XDocument doc = XDocument.Load(stream);

            stream.Close();
            return(new XElementData {
                Root = doc, Current = doc.Root
            });
        }
예제 #26
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);
        }
예제 #27
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);
        }
예제 #28
0
        public object CreateReader(string input, ReadSettings settings)
        {
            XDocument    doc  = XDocument.Parse(input);
            XElementData data = new XElementData {
                Root = doc, Current = doc.Root
            };

            return(data);
        }
 public object CreateReader(Stream stream, ReadSettings settings)
 {
     using (StreamReader reader = new StreamReader(stream, settings.Encoding, true))
     {
         string             text    = reader.ReadToEnd();
         QueryStringBuilder builder = new QueryStringBuilder(text);
         return(builder);
     }
 }
예제 #30
0
        public static object GetValue(object sender, Type type, string strValue,
                                      object defaultValue, ReadSettings settings)
        {
            TkDebug.AssertArgumentNull(type, "type", sender);

            ITkTypeConverter converter = TkTypeDescriptor.GetConverter(type);

            AssertTypeConverter(sender, type, converter);
            return(InternalGetValue(type, strValue, defaultValue, settings, converter));
        }