Ejemplo n.º 1
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));
        }
Ejemplo n.º 2
0
        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));
        }
Ejemplo n.º 3
0
        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)
            {
                if (existing != null && !(existing is IList) ) throw Error.Argument("existing", "Can only read repeating elements into a type implementing IList");
                var reader = new RepeatingElementReader(_current);
                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.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(null);
            }

            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);
            }

            if (existing != null && !(existing is Resource) && !(existing is Element) ) throw Error.Argument("existing", "Can only read complex elements into types that are Element or Resource");
            var cplxReader = new ComplexTypeReader(_current);
            return cplxReader.Deserialize(mapping, (Base)existing);
        }
Ejemplo n.º 4
0
        public Resource Deserialize(Resource existing=null)
        {
            // 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 resourceTypeName = _reader.GetResourceTypeName();
            var mapping = _inspector.FindClassMappingForResource(resourceTypeName);

            if (mapping == null)
                throw Error.Format("Asked to deserialize unknown resource '" + resourceTypeName + "'", _reader);
             
            // Delegate the actual work to the ComplexTypeReader, since
            // the serialization of Resources and ComplexTypes are virtually the same
            var cplxReader = new ComplexTypeReader(_reader);
            return (Resource)cplxReader.Deserialize(mapping, existing);
        }
Ejemplo n.º 5
0
        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);
            }
        }
Ejemplo n.º 6
0
        public Resource Deserialize(Resource existing = null)
        {
            // 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 resourceTypeName = _reader.GetResourceTypeName();
            var mapping          = _inspector.FindClassMappingForResource(resourceTypeName);

            if (mapping == null)
            {
                throw Error.Format("Asked to deserialize unknown resource '" + resourceTypeName + "'", _reader);
            }

            // Delegate the actual work to the ComplexTypeReader, since
            // the serialization of Resources and ComplexTypes are virtually the same
            var cplxReader = new ComplexTypeReader(_reader, Settings);

            return((Resource)cplxReader.Deserialize(mapping, existing));
        }
Ejemplo n.º 7
0
#pragma warning restore 612,618

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

            var mapping = _inspector.FindClassMappingForResource(_reader.InstanceType);

            if (mapping == null)
            {
                ComplexTypeReader.RaiseFormatError($"Asked to deserialize unknown resource '{_reader.InstanceType}'", _reader.Location);
            }

            // Delegate the actual work to the ComplexTypeReader, since
            // the serialization of Resources and ComplexTypes are virtually the same
            var cplxReader = new ComplexTypeReader(_reader, Settings);

            return((Resource)cplxReader.Deserialize(mapping, existing));
        }
Ejemplo n.º 8
0
        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);
        }