示例#1
0
        /// <summary>
        /// Copia as propriedades do modelo deserializado para o objeto indicado.
        /// As propriedades são correspondidas por nome insensível a caso.
        /// </summary>
        /// <param name="sourceNode">O modelo deserializado.</param>
        /// <param name="targetGraph">O objeto destino das propriedades.</param>
        private void CopyNodeProperties(NodeModel sourceNode, object targetGraph)
        {
            foreach (var property in sourceNode.ChildProperties())
            {
                var info = targetGraph._GetPropertyInfo(property.Name);
                if (info == null)
                {
                    continue;
                }

                var propertyValue = CreateCompatibleValue(info.PropertyType, property.Value);

                if (propertyValue != null)
                {
                    var isCaseVariantString =
                        info
                        .GetCustomAttributes(true)
                        .OfType <CaseVariantStringAttribute>()
                        .Any();

                    if (isCaseVariantString)
                    {
                        propertyValue = propertyValue.ToString().ChangeCase(TextCase.PascalCase);
                    }
                }

                targetGraph._Set(property.Name, propertyValue);
            }
        }
示例#2
0
        /// <summary>
        /// Cria um valor compatível com base no conteúdo do modelo deserializado.
        /// </summary>
        /// <param name="node">O modelo deserializado.</param>
        /// <returns>O valor obtido da compatibilização.</returns>
        private object CreateCompatibleValue(NodeModel node)
        {
            {
                if (node is ValueModel)
                {
                    return(((ValueModel)node).Value);
                }

                if (node is ObjectModel)
                {
                    var propertyCollection = new PropertyCollection();
                    foreach (var child in node.ChildProperties())
                    {
                        var value = child.Value;

                        object convertedValue;

                        if (value is ValueModel)
                        {
                            convertedValue = ((ValueModel)value).Value;
                        }
                        else
                        {
                            convertedValue = CreateCompatibleValue(typeof(object), value);
                        }

                        var property = new Property();
                        property.Name  = child.Name.ChangeCase(TextCase.PascalCase);
                        property.Value = convertedValue;

                        propertyCollection.Add(property);
                    }
                    return(propertyCollection);
                }

                if (node is CollectionModel)
                {
                    var collection = new List <object>();
                    foreach (var value in node.Children())
                    {
                        object convertedValue;

                        if (value is ValueModel)
                        {
                            convertedValue = ((ValueModel)value).Value;
                        }
                        else
                        {
                            convertedValue = CreateCompatibleValue(typeof(object), value);
                        }

                        collection.Add(convertedValue);
                    }
                    return(collection);
                }
            }

            throw new Exception("Conteúdo não suportado: " + node.GetType().FullName);
        }
示例#3
0
        private ObjectModel GetObject(NodeModel node, string propertyName)
        {
            var @object = (
                from property in node.ChildProperties()
                where property.Name.EqualsIgnoreCase(propertyName)
                let value = property.Value as ObjectModel
                            where value != null
                            select value
                ).FirstOrDefault();

            return(@object);
        }
示例#4
0
        private string GetString(NodeModel node, string propertyName)
        {
            var text = (
                from property in node.ChildProperties()
                where property.Name.EqualsIgnoreCase(propertyName)
                let value = property.Value as ValueModel
                            where value != null
                            select value.Value as string
                ).FirstOrDefault();

            return(text);
        }
示例#5
0
        private ObjectModel[] GetObjectArray(NodeModel node, string propertyName)
        {
            var objects = (
                from property in node.ChildProperties()
                where property.Name.EqualsIgnoreCase(propertyName)
                let values = property.Value as CollectionModel
                             where values != null
                             from value in values.ChildObjects()
                             select value
                ).ToArray();

            return(objects);
        }
示例#6
0
        private string[] GetStringArray(NodeModel node, string propertyName)
        {
            var classes = (
                from property in node.ChildProperties()
                where property.Name.EqualsIgnoreCase(propertyName)
                let values = property.Value as CollectionModel
                             where values != null
                             from value in values.ChildValues()
                             select value.Value as string
                ).ToArray();

            return(classes);
        }
示例#7
0
        public object CreateGraph(NodeModel document, Type graphType)
        {
            if (document.IsDocument)
            {
                document = ((DocumentModel)document).Root;
            }

            if (typeof(IGraphDeserializer).IsAssignableFrom(graphType))
            {
                var graph = (IGraphDeserializer)Activator.CreateInstance(graphType);
                graph.Deserialize(document, this);
                return(graph);
            }

            if (document.IsObject)
            {
                var graph = Activator.CreateInstance(graphType);
                foreach (var property in document.ChildProperties())
                {
                    SetProperty(graph, property.Name, property.Value);
                }
                return(graph);
            }

            if (document.IsCollection)
            {
                var list     = new ArrayList();
                var itemType = TypeOf.CollectionElement(graphType);
                foreach (var child in document.Children())
                {
                    var item = CreateGraph(child, itemType);
                    list.Add(item);
                }
                var graph = (ICollection)Change.To(list, graphType);
                return(graph);
            }

            return(document.SerializationValue);
        }