예제 #1
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);
        }
예제 #2
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);
        }
예제 #3
0
        /// <summary>
        /// Cria um valor compatível para o tipo indicado com base no conteúdo
        /// do modelo deserializado.
        /// </summary>
        /// <param name="type">O tipo do dado esperado.</param>
        /// <param name="node">O modelo deserializado.</param>
        /// <returns>O valor obtido da compatibilização.</returns>
        private object CreateCompatibleValue(Type type, NodeModel node)
        {
            if (node == null)
            {
                return(null);
            }

            if (type == typeof(PropertyCollection))
            {
                return((PropertyCollection)CreateCompatibleValue(typeof(object), node));
            }

            if (type == typeof(NameCollection))
            {
                var target = new NameCollection();
                foreach (var item in node.ChildValues())
                {
                    target.Add(item.Value.ToString());
                }
                return(target);
            }

            if (type == typeof(FieldCollection))
            {
                var target = new FieldCollection();
                foreach (var @object in node.ChildObjects())
                {
                    var field = new Field();
                    CopyNodeProperties(@object, field);
                    target.Add(field);
                }
                return(target);
            }

            if (type == typeof(LinkCollection))
            {
                var target = new LinkCollection();
                foreach (var item in node.Children())
                {
                    var link = new Link();
                    CopyNodeProperties(item, link);
                    target.Add(link);
                }
                return(target);
            }

            if (type == typeof(EntityActionCollection))
            {
                var target = new EntityActionCollection();
                foreach (var item in node.Children())
                {
                    var action = new EntityAction();
                    CopyNodeProperties(item, action);
                    target.Add(action);
                }
                return(target);
            }

            if (type == typeof(EntityCollection))
            {
                var target = new EntityCollection();
                foreach (var item in node.Children())
                {
                    var entity = new Entity();
                    CopyNodeProperties(item, entity);
                    target.Add(entity);
                }
                return(target);
            }

            if (type == typeof(CaseVariantString))
            {
                var text = (node as ValueModel)?.Value.ToString();
                return(text.ChangeCase(TextCase.PascalCase));
            }

            return(CreateCompatibleValue(node));
        }