Example #1
0
        internal Base Deserialize(ClassMapping mapping, Base existing = null)
        {
            if (mapping == null)
            {
                throw Error.ArgumentNull(nameof(mapping));
            }

            if (existing == null)
            {
                var fac = new DefaultModelFactory();
                existing = (Base)fac.Create(mapping.NativeType);
            }
            else
            {
                if (mapping.NativeType != existing.GetType())
                {
                    throw Error.Argument(nameof(existing), "Existing instance is of type {0}, but data indicates resource is a {1}".FormatWith(existing.GetType().Name, mapping.NativeType.Name));
                }
            }

            var members = _current.Text != null ?
                          new[] { SourceNode.Valued("value", _current.Text) }.Union(_current.Children()) :
            _current.Children();

            read(mapping, members, existing);

            return(existing);
        }
        internal Base Deserialize(ClassMapping mapping, Base existing = null)
        {
            if (mapping == null)
            {
                throw Error.ArgumentNull("mapping");
            }

            if (existing == null)
            {
                var fac = new DefaultModelFactory();
                existing = (Base)fac.Create(mapping.NativeType);
            }
            else
            {
                if (mapping.NativeType != existing.GetType())
                {
                    throw Error.Argument("existing", "Existing instance is of type {0}, but data indicates resource is a {1}".FormatWith(existing.GetType().Name, mapping.NativeType.Name));
                }
            }

            IEnumerable <Tuple <string, IFhirReader> > members = null;

            members = _current.GetMembers();
            read(mapping, members, existing);

            return(existing);
        }
        internal object Deserialize(ClassMapping mapping, object existing = null)
        {
            if (mapping == null)
            {
                throw Error.ArgumentNull("mapping");
            }

            if (existing != null)
            {
                if (mapping.NativeType != existing.GetType())
                {
                    throw Error.Argument("existing", "Existing instance is of type {0}, but type parameter indicates data type is a {1}", existing.GetType().Name, mapping.NativeType.Name);
                }
            }
            else
            {
                var fac = new DefaultModelFactory();
                existing = fac.Create(mapping.NativeType);
            }

            IEnumerable <Tuple <string, IFhirReader> > members = null;

            if (_current.CurrentToken == TokenType.Object)
            {
                members = _current.GetMembers();
            }
            else if (_current.IsPrimitive())
            {
                // Ok, we expected a complex type, but we found a primitive instead. This may happen
                // in Json where the value property and the other elements are separately put into
                // member and _member. In this case, we will parse the primitive into the Value property
                // of the complex type
                if (!mapping.HasPrimitiveValueMember)
                {
                    throw Error.Format("Complex object does not have a value property, yet the reader is at a primitive", _current);
                }

                // Simulate this as actually receiving a member "Value" in a normal complex object,
                // and resume normally
                var valueMember = Tuple.Create(mapping.PrimitiveValueProperty.Name, _current);
                members = new List <Tuple <string, IFhirReader> > {
                    valueMember
                };
            }
            else
            {
                throw Error.Format("Trying to read a complex object, but reader is not at the start of an object or primitive", _current);
            }

            read(mapping, members, existing);

            return(existing);
        }
        public void TestUnsupportedTypes()
        {
            var factory = new DefaultModelFactory();

            Assert.IsFalse(factory.CanCreate(typeof(TestCreatePrivateConstructor)));
            Assert.IsFalse(factory.CanCreate(typeof(TestCreateArgConstructor)));

            // Cannot create interface types
            Assert.IsFalse(factory.CanCreate(typeof(ICloneable)));

            // Cannot create arrays, since we don't know size upfront
            Assert.IsFalse(factory.CanCreate(typeof(int[])));
        }
        public void FindModelClassFactory()
        {
            ModelFactoryList facs = new ModelFactoryList();

            var specificFactory = new SpecificModelClassFactory();
            facs.Add(specificFactory);
            var defaultFactory = new DefaultModelFactory();
            facs.Add(defaultFactory);

            var selectedFactory = facs.FindFactory(typeof(SpecificModelClass));
            Assert.AreEqual(specificFactory, selectedFactory);

            selectedFactory = facs.FindFactory(typeof(GenericModelClass));
            Assert.AreEqual(defaultFactory, selectedFactory);
        }
        public object Deserialize(object existing = null, bool nested = false)
        {
            if (_reader.CurrentToken == TokenType.Object)
            {
                // If there's no a priori knowledge of the type of Resource we will encounter,
                // we'll have to determine from the data itself.
                var resourceType = _reader.GetResourceTypeName(nested);
                var mappedType   = _inspector.FindClassMappingForResource(resourceType);

                if (mappedType == null)
                {
                    // Special courtesy case
                    if (resourceType == "feed" || resourceType == "Bundle")
                    {
                        throw Error.Format("Encountered a feed instead of a resource", _reader);
                    }
                    else
                    {
                        throw Error.Format("Encountered unknown resource type {0}", _reader, resourceType);
                    }
                }

                if (existing == null)
                {
                    var fac = new DefaultModelFactory();
                    existing = fac.Create(mappedType.NativeType);
                }
                else
                {
                    if (mappedType.NativeType != existing.GetType())
                    {
                        throw Error.Argument("existing", "Existing instance is of type {0}, but data indicates resource is a {1}", existing.GetType().Name, resourceType);
                    }
                }

                // Delegate the actual work to the ComplexTypeReader, since
                // the serialization of Resources and ComplexTypes are virtually the same
                var cplxReader = new ComplexTypeReader(_reader);
                return(cplxReader.Deserialize(mappedType, existing));
            }
            else
            {
                throw Error.Format("Trying to read a resource, but reader is not at the start of an object", _reader);
            }
        }
        internal object Deserialize(ClassMapping mapping, object existing=null)
        {
            if (mapping == null) throw Error.ArgumentNull("mapping");

            if (existing != null)
            {
                if (mapping.NativeType != existing.GetType())
                    throw Error.Argument("existing", "Existing instance is of type {0}, but type parameter indicates data type is a {1}", existing.GetType().Name, mapping.NativeType.Name);
            }
            else
            {
                var fac = new DefaultModelFactory();
                existing = fac.Create(mapping.NativeType);
            }

            IEnumerable<Tuple<string, IFhirReader>> members = null;

            if (_current.CurrentToken == TokenType.Object)
            {
                members = _current.GetMembers();
            }
            else if(_current.IsPrimitive())
            {
                // Ok, we expected a complex type, but we found a primitive instead. This may happen
                // in Json where the value property and the other elements are separately put into
                // member and _member. In this case, we will parse the primitive into the Value property
                // of the complex type
                if (!mapping.HasPrimitiveValueMember)
                    throw Error.Format("Complex object does not have a value property, yet the reader is at a primitive", _current);

                // Simulate this as actually receiving a member "Value" in a normal complex object,
                // and resume normally
                var valueMember = Tuple.Create(mapping.PrimitiveValueProperty.Name, _current);
                members = new List<Tuple<string, IFhirReader>> { valueMember };
            }
            else
                throw Error.Format("Trying to read a complex object, but reader is not at the start of an object or primitive", _current);

            read(mapping, members, existing);

            return existing;

        }
Example #8
0
        internal Base Deserialize(ClassMapping mapping, Base existing = null)
        {
            var mappingToUse = mapping;

            if (mappingToUse == null)
            {
                if (_current.InstanceType is null)
                {
                    throw Error.Format("Underlying data source was not able to provide the actual instance type of the resource.");
                }

                mappingToUse = (ClassMapping)_inspector.Provide(_current.InstanceType);

                if (mappingToUse == null)
                {
                    RaiseFormatError($"Asked to deserialize unknown type '{_current.InstanceType}'", _current.Location);
                }
            }

            if (existing == null)
            {
                var fac = new DefaultModelFactory();
                existing = (Base)fac.Create(mappingToUse.NativeType);
            }
            else
            {
                if (mappingToUse.NativeType != existing.GetType())
                {
                    throw Error.Argument(nameof(existing), "Existing instance is of type {0}, but data indicates resource is a {1}".FormatWith(existing.GetType().Name, mappingToUse.NativeType.Name));
                }
            }

            // The older code for read() assumes the primitive value member is represented as a separate child element named "value",
            // while the newer ITypedElement represents this as a special Value property. We simulate the old behaviour here, by
            // explicitly adding the value property as a child and making it act like a typed node.
            var members = _current.Value != null ?
                          new[] { new ValuePropertyTypedElement(_current) }.Union(_current.Children()) : _current.Children();

            read(mappingToUse, members, existing);
            return(existing);
        }
        public void TestSupportedTypes()
        {
            var factory = new DefaultModelFactory();

            // Test creation of a class with no constructor
            Assert.IsTrue(factory.CanCreate(typeof(TestCreate)));
            Assert.IsNotNull(factory.Create(typeof(TestCreate)));
            
            // Test creation of class with a public no-args constructor
            Assert.IsTrue(factory.CanCreate(typeof(TestCreatePublicConstructor)));
            Assert.IsNotNull(factory.Create(typeof(TestCreatePublicConstructor)));
            
            // Test creation of primitives
            Assert.IsTrue(factory.CanCreate(typeof(int)));
            Assert.IsNotNull(factory.Create(typeof(int)));

            // Test creation of Nullable<T>
            Assert.IsTrue(factory.CanCreate(typeof(int?)));
            Assert.IsNotNull(factory.Create(typeof(int?)));

            // Test handling of collection interfaces
            object collection = null;
            Assert.IsTrue(factory.CanCreate(typeof(ICollection<string>)));
            collection = factory.Create(typeof(ICollection<string>));
            Assert.IsNotNull(collection);
            Assert.IsTrue(collection is List<string>);

            Assert.IsTrue(factory.CanCreate(typeof(IList<HumanName>)));
            Assert.IsNotNull(factory.Create(typeof(ICollection<HumanName>)));
            
            Assert.IsTrue(factory.CanCreate(typeof(IList<int?>)));
            collection = factory.Create(typeof(ICollection<int?>));
            Assert.IsNotNull(collection);
            Assert.IsTrue(collection is List<int?>);

            // Test handling of closed generics
            Assert.IsTrue(factory.CanCreate(typeof(Code<Address.AddressUse>)));
            Assert.IsNotNull(factory.Create(typeof(Code<Address.AddressUse>)));
        }
Example #10
0
        internal Base Deserialize(ClassMapping mapping, Base existing=null)
        {
            if (mapping == null) throw Error.ArgumentNull("mapping");

            if (existing == null)
            {
                var fac = new DefaultModelFactory();
                existing = (Base)fac.Create(mapping.NativeType);
            }
            else
            {
                if (mapping.NativeType != existing.GetType())
                    throw Error.Argument("existing", "Existing instance is of type {0}, but data indicates resource is a {1}", existing.GetType().Name, mapping.NativeType.Name);
            }

            IEnumerable<Tuple<string, IFhirReader>> members = null;

            members = _current.GetMembers();
            read(mapping, members, existing);

            return existing;

        }
        internal Base Deserialize(ClassMapping mapping, Base existing = null)
        {
            if (mapping == null)
            {
                throw Error.ArgumentNull(nameof(mapping));
            }

            if (existing == null)
            {
                var fac = new DefaultModelFactory();
                existing = (Base)fac.Create(mapping.NativeType);
            }
            else
            {
                if (mapping.NativeType != existing.GetType())
                {
                    throw Error.Argument(nameof(existing), "Existing instance is of type {0}, but data indicates resource is a {1}".FormatWith(existing.GetType().Name, mapping.NativeType.Name));
                }
            }

            // The older code for read() assumes the primitive value member is represented as a separate child element named "value",
            // while the newer ITypedElement represents this as a special Value property. We simulate the old behaviour here, by
            // explicitly adding the value property as a child and making it act like a typed node.
            var members = _current.Value != null ?
                          new[] { new ValuePropertyTypedElement(_current) }.Union(_current.Children()) : _current.Children();

            try
            {
                read(mapping, members, existing);
            }
            catch (StructuralTypeException ste)
            {
                throw Error.Format(ste.Message);
            }

            return(existing);
        }
        public object Deserialize(object existing=null, bool nested = false)
        {
            if (_reader.CurrentToken == TokenType.Object)
            {
                // If there's no a priori knowledge of the type of Resource we will encounter,
                // we'll have to determine from the data itself. 
                var resourceType = _reader.GetResourceTypeName(nested);
                var mappedType = _inspector.FindClassMappingForResource(resourceType);

                if (mappedType == null)
                {
                    // Special courtesy case
                    if (resourceType == "feed" || resourceType == "Bundle")
                        throw Error.Format("Encountered a feed instead of a resource", _reader);
                    else
                        throw Error.Format("Encountered unknown resource type {0}", _reader, resourceType);
                }

                if (existing == null)
                {
                    var fac = new DefaultModelFactory();
                    existing = fac.Create(mappedType.NativeType);
                }
                else
                {
                    if (mappedType.NativeType != existing.GetType())
                        throw Error.Argument("existing", "Existing instance is of type {0}, but data indicates resource is a {1}", existing.GetType().Name, resourceType);
                }
               
                // Delegate the actual work to the ComplexTypeReader, since
                // the serialization of Resources and ComplexTypes are virtually the same
                var cplxReader = new ComplexTypeReader(_reader);
                return cplxReader.Deserialize(mappedType, existing);
            }
            else
                throw Error.Format("Trying to read a resource, but reader is not at the start of an object", _reader);
        }