예제 #1
0
        private void write(PropertyMapping prop, object instance, bool summary, 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, summary, 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, summary, mode);
            }
            else
            {
                object value = mapping.PrimitiveValueProperty.GetValue(instance);
                write(mapping.PrimitiveValueProperty, value, summary, ComplexTypeWriter.SerializationMode.AllMembers);
            }
        }
예제 #2
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);
            }
        }
예제 #3
0
        internal void Serialize(PropertyMapping prop, object instance, bool summary, ComplexTypeWriter.SerializationMode mode)
        {
            if (prop == null) throw Error.ArgumentNull("prop");

            // ArrayMode avoid the dispatcher making nested calls into the RepeatingElementWriter again
            // when writing array elements. FHIR does not support nested arrays, and this avoids an endlessly
            // nesting series of dispatcher calls
            if (prop.IsCollection)
            {
                var elements = instance as IList;
                if (elements == null) throw Error.Argument("existing", "Can only write repeating elements from a type implementing IList");

                _current.WriteStartArray();

                foreach (var element in elements)
                {
                    if (element == null) throw Error.Format("The FHIR serialization does not support arrays with empty (null) elements", null);

                    write(prop, element, summary, mode);
                }

                _current.WriteEndArray();
            }
            else
                write(prop, instance, summary, mode);
        }
예제 #4
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);
        }
예제 #5
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);
        }
        public void Serialize(PropertyMapping prop, object instance, bool summary, ComplexTypeWriter.SerializationMode mode)
        {
            if (prop == null) throw Error.ArgumentNull("prop");

            var elements = instance as IList;                       
            if(elements == null) throw Error.Argument("existing", "Can only write repeating elements from a type implementing IList");

            _current.WriteStartArray();

            foreach(var element in elements)
            {
                var writer = new DispatchingWriter(_current);
                writer.Serialize(prop, element, summary, mode);
            }

            _current.WriteEndArray();
        }
예제 #7
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);
        }
예제 #8
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);
        }