Esempio n. 1
0
        private void write(PropertyMapping prop, object instance, ComplexTypeWriter.SerializationMode mode)
        {
            // If this is a primitive type, no classmappings and reflection is involved,
            // just serialize the primitive to the writer
            if (prop.IsPrimitive)
            {
                var writer = new PrimitiveValueWriter(_current);
                writer.Serialize(instance, prop.SerializationHint);
                return;
            }

            // A Choice property that contains a choice of any resource
            // (as used in Resource.contained)
            if (prop.Choice == ChoiceType.ResourceChoice)
            {
                var writer = new ResourceWriter(_current);
                writer.Serialize(instance, contained: true);
                return;
            }

            ClassMapping mapping = _inspector.ImportType(instance.GetType());

            if (mode == ComplexTypeWriter.SerializationMode.AllMembers || mode == ComplexTypeWriter.SerializationMode.NonValueElements)
            {
                var cplxWriter = new ComplexTypeWriter(_current);
                cplxWriter.Serialize(mapping, instance, mode);
            }
            else
            {
                object value = mapping.PrimitiveValueProperty.GetValue(instance);
                write(mapping.PrimitiveValueProperty, value, ComplexTypeWriter.SerializationMode.AllMembers);
            }
        }
Esempio n. 2
0
        // If we have a normal complex property, for which the type has a primitive value member...
        private bool serializedIntoTwoProperties(PropertyMapping prop, object instance)
        {
            if (instance is IList)
            {
                instance = ((IList)instance)[0];
            }

            if (!prop.IsPrimitive && prop.Choice != ChoiceType.ResourceChoice)
            {
                var mapping = _inspector.ImportType(instance.GetType());
                return(mapping.HasPrimitiveValueMember);
            }
            else
            {
                return(false);
            }
        }
Esempio n. 3
0
        public void TypeDataTypeNameResolving()
        {
            var inspector = new ModelInspector();

            inspector.ImportType(typeof(AnimalName));
            inspector.ImportType(typeof(NewAnimalName));

            var result = inspector.FindClassMappingForFhirDataType("animalname");

            Assert.IsNotNull(result);
            Assert.AreEqual(result.NativeType, typeof(NewAnimalName));

            // Validate a mapping for a type will return the newest registration
            result = inspector.FindClassMappingByType(typeof(AnimalName));
            Assert.IsNotNull(result);
            Assert.AreEqual(typeof(NewAnimalName), result.NativeType);
        }
Esempio n. 4
0
#pragma warning restore 612,618

        public object Deserialize(PropertyMapping prop, string memberName, object existing = null)
        {
            if (prop == null)
            {
                throw Error.ArgumentNull(nameof(prop));
            }

            // ArrayMode avoid the dispatcher making nested calls into the RepeatingElementReader again
            // when reading array elements. FHIR does not support nested arrays, and this avoids an endlessly
            // nesting series of dispatcher calls
            if (!_arrayMode && prop.IsCollection)
            {
                if (existing != null && !(existing is IList))
                {
                    throw Error.Argument(nameof(existing), "Can only read repeating elements into a type implementing IList");
                }
                var reader = new RepeatingElementReader(_inspector, _current, Settings);
                return(reader.Deserialize(prop, memberName, (IList)existing));
            }

            // If this is a primitive type, no classmappings and reflection is involved,
            // just parse the primitive from the input
            // NB: no choices for primitives!
            if (prop.IsPrimitive)
            {
                var reader = new PrimitiveValueReader(_current);
                return(reader.Deserialize(prop.ImplementingType));
            }

            // A Choice property that contains a choice of any resource
            // (as used in Resource.contained)
            if (prop.Choice == ChoiceType.ResourceChoice)
            {
                var reader = new ComplexTypeReader(_inspector, _current, Settings);
                return(reader.Deserialize(null));
            }

            if (_current.InstanceType is null)
            {
                ComplexTypeReader.RaiseFormatError(
                    "Underlying data source was not able to provide the actual instance type of the resource.", _current.Location);
            }

            ClassMapping mapping = prop.Choice == ChoiceType.DatatypeChoice
                ? getMappingForType(memberName, _current.InstanceType)
                : _inspector.ImportType(prop.ImplementingType);

            // Handle other Choices having any datatype or a list of datatypes

            if (existing != null && !(existing is Resource) && !(existing is Element))
            {
                throw Error.Argument(nameof(existing), "Can only read complex elements into types that are Element or Resource");
            }
            var cplxReader = new ComplexTypeReader(_inspector, _current, Settings);

            return(cplxReader.Deserialize(mapping, (Base)existing));
        }
        public object Deserialize(PropertyMapping prop, string memberName, object existing = null)
        {
            if (prop == null)
            {
                throw Error.ArgumentNull("prop");
            }

            // ArrayMode avoid the dispatcher making nested calls into the RepeatingElementReader again
            // when reading array elements. FHIR does not support nested arrays, and this avoids an endlessly
            // nesting series of dispatcher calls
            if (!_arrayMode && prop.IsCollection)
            {
                var reader = new RepeatingElementReader(_current);
                return(reader.Deserialize(prop, memberName, existing));
            }

            // If this is a primitive type, no classmappings and reflection is involved,
            // just parse the primitive from the input
            // NB: no choices for primitives!
            if (prop.IsPrimitive)
            {
                var reader = new PrimitiveValueReader(_current);
                return(reader.Deserialize(prop.ElementType));
            }

            // A Choice property that contains a choice of any resource
            // (as used in Resource.contained)
            if (prop.Choice == ChoiceType.ResourceChoice)
            {
                var reader = new ResourceReader(_current);
                return(reader.Deserialize(existing, nested: true));
            }

            ClassMapping mapping;

            // Handle other Choices having any datatype or a list of datatypes
            if (prop.Choice == ChoiceType.DatatypeChoice)
            {
                // For Choice properties, determine the actual type of the element using
                // the suffix of the membername (i.e. deceasedBoolean, deceasedDate)
                // This function implements type substitution.
                mapping = determineElementPropertyType(prop, memberName);
            }
            // Else use the actual return type of the property
            else
            {
                mapping = _inspector.ImportType(prop.ElementType);
            }

            var cplxReader = new ComplexTypeReader(_current);

            return(cplxReader.Deserialize(mapping, existing));
        }
        public void TestResourceNameResolving()
        {
            var inspector = new ModelInspector(Specification.FhirRelease.STU3);

            inspector.ImportType(typeof(Way));
            inspector.ImportType(typeof(Way2));

            var way = inspector.FindClassMapping("Way");

            Assert.IsNotNull(way);
            Assert.AreEqual(way.NativeType, typeof(Way));

            var way2 = inspector.FindClassMapping("Way2");

            Assert.IsNotNull(way2);
            Assert.AreEqual(way2.NativeType, typeof(Way2));

            var noway = inspector.FindClassMapping("nonexistent");

            Assert.IsNull(noway);
        }
Esempio n. 7
0
        public void TestResourceNameResolving()
        {
            var inspector = new ModelInspector(ModelInspector.R3_VERSION);

            inspector.ImportType(typeof(Way));
            inspector.ImportType(typeof(Way2));

            var way = inspector.FindClassMappingByName("wAy");

            Assert.IsNotNull(way);
            Assert.AreEqual(way.NativeType, typeof(Way));

            var way2 = inspector.FindClassMappingByName("Way2");

            Assert.IsNotNull(way2);
            Assert.AreEqual(way2.NativeType, typeof(Way2));

            var noway = inspector.FindClassMappingByName("nonexistent");

            Assert.IsNull(noway);
        }
Esempio n. 8
0
        internal PocoElementNode(ModelInspector inspector, Base root, string rootName = null)
        {
            Current         = root;
            _inspector      = inspector;
            _myClassMapping = _inspector.ImportType(root.GetType());

            InstanceType = ((IStructureDefinitionSummary)_myClassMapping).TypeName;
            Definition   = ElementDefinitionSummary.ForRoot(_myClassMapping, rootName ?? root.TypeName);

            Location  = InstanceType;
            ShortPath = InstanceType;
        }
Esempio n. 9
0
        public void TestResourceNameResolving()
        {
            var inspector = new ModelInspector();


            inspector.ImportType(typeof(Road));
            inspector.ImportType(typeof(Way));
            inspector.ImportType(typeof(ProfiledWay));
            inspector.ImportType(typeof(NewStreet));
            //inspector.Process();

            var road = inspector.FindClassMappingForResource("roAd");

            Assert.IsNotNull(road);
            Assert.AreEqual(road.NativeType, typeof(Road));

            var way = inspector.FindClassMappingForResource("Way");

            Assert.IsNotNull(way);
            Assert.AreEqual(way.NativeType, typeof(Way));

            var pway = inspector.FindClassMappingForResource("way", "http://nu.nl/profile#street");

            Assert.IsNotNull(pway);
            Assert.AreEqual(pway.NativeType, typeof(ProfiledWay));

            var pway2 = inspector.FindClassMappingForResource("way", "http://nux.nl/profile#street");

            Assert.IsNotNull(pway2);
            Assert.AreEqual(pway2.NativeType, typeof(Way));

            var street = inspector.FindClassMappingForResource("Street");

            Assert.IsNotNull(street);
            Assert.AreEqual(street.NativeType, typeof(NewStreet));

            var noway = inspector.FindClassMappingForResource("nonexistent");

            Assert.IsNull(noway);
        }
Esempio n. 10
0
        internal void Serialize(Base instance, Rest.SummaryType summary, SerializationMode mode = SerializationMode.AllMembers, string root = null)
        {
            if (instance == null)
            {
                throw Error.ArgumentNull(nameof(instance));
            }

            ClassMapping mapping = _inspector.ImportType(instance.GetType());

            if (mapping == null)
            {
                throw Error.Format($"Asked to serialize unknown type '{instance.GetType()}'");
            }

            var rootName = root ?? mapping.Name;

            _writer.WriteStartProperty(rootName);

            Serialize(mapping, instance, summary, mode);

            _writer.WriteEndProperty();
        }
Esempio n. 11
0
        public void Serialize(Resource instance, Rest.SummaryType summary, bool contained = false)
        {
            if (instance == null)
            {
                throw Error.ArgumentNull(nameof(instance));
            }

            var mapping = _inspector.ImportType(instance.GetType());

            if (mapping == null)
            {
                throw Error.Format($"Asked to serialize unknown resource type '{instance.GetType()}'");
            }

            _writer.WriteStartRootObject(mapping.Name, contained);

            var    complexSerializer  = new ComplexTypeWriter(_writer, Settings);
            Coding subsettedTag       = null;
            bool   createdMetaElement = false;

            if (summary != Rest.SummaryType.False && instance is Resource)
            {
                var resource = instance as Resource;

                if (resource.Meta == null)
                {
                    resource.Meta      = new Meta();
                    createdMetaElement = true;
                }

                if (!resource.Meta.Tag.Any(t => t.System == "http://hl7.org/fhir/v3/ObservationValue" && t.Code == "SUBSETTED"))
                {
                    subsettedTag = new Coding("http://hl7.org/fhir/v3/ObservationValue", "SUBSETTED");
                    resource.Meta.Tag.Add(subsettedTag);
                }
            }
            complexSerializer.Serialize(mapping, instance, summary);

            Resource r = (instance as Resource);

            if (subsettedTag != null)
            {
                r.Meta.Tag.Remove(subsettedTag);
            }

            if (createdMetaElement)
            {
                r.Meta = null; // remove the meta element again.
            }
            _writer.WriteEndRootObject(contained);
        }
        private PocoElementNode(ModelInspector inspector, Base instance, PocoElementNode parent, PropertyMapping definition, string location, string shortPath)
        {
            Current    = instance;
            _inspector = inspector;

            var instanceType = determineInstanceType(instance.GetType(), definition);

            _myClassMapping = _inspector.ImportType(instanceType);
            InstanceType    = ((IStructureDefinitionSummary)_myClassMapping).TypeName;
            Definition      = definition ?? throw Error.ArgumentNull(nameof(definition));

            ExceptionHandler = parent.ExceptionHandler;
            Location         = location;
            ShortPath        = shortPath;
        }
Esempio n. 13
0
        public void Serialize(object instance, bool summary, bool contained = false)
        {
            if (instance == null)
            {
                throw Error.ArgumentNull("instance");
            }

            var mapping = _inspector.ImportType(instance.GetType());

            _writer.WriteStartRootObject(mapping.Name, contained);

            var complexSerializer = new ComplexTypeWriter(_writer);

            complexSerializer.Serialize(mapping, instance, summary);

            _writer.WriteEndRootObject(contained);
        }
Esempio n. 14
0
        public void Serialize(object instance, Rest.SummaryType summary, bool contained = false, string root = null)
        {
            if (instance == null)
            {
                throw Error.ArgumentNull(nameof(instance));
            }

            var mapping = _inspector.ImportType(instance.GetType());

            var rootName = root ?? mapping.Name;

            _writer.WriteStartRootObject(rootName, contained);

            var    complexSerializer = new ComplexTypeWriter(_writer);
            Coding subsettedTag      = null;

            if (summary != Rest.SummaryType.False && instance is Resource)
            {
                Resource r = (instance as Resource);
                if (r != null)
                {
                    // If we are subsetting the instance during serialization, ensure that there
                    // is a meta element with that subsetting in it
                    // (Helps make it easier to create conformant instances)
                    if (r.Meta == null)
                    {
                        r.Meta = new Meta();
                    }
                    if (r.Meta.Tag.Where(t => t.System == "http://hl7.org/fhir/v3/ObservationValue" && t.Code == "SUBSETTED").Count() == 0)
                    {
                        subsettedTag = new Coding("http://hl7.org/fhir/v3/ObservationValue", "SUBSETTED");
                        r.Meta.Tag.Add(subsettedTag);
                    }
                }
            }
            complexSerializer.Serialize(mapping, instance, summary);

            if (subsettedTag != null)
            {
                Resource r = (instance as Resource);
                r.Meta.Tag.Remove(subsettedTag);
            }

            _writer.WriteEndRootObject(contained);
        }