Ejemplo n.º 1
0
        internal void Serialize(PropertyMapping prop, object instance, Rest.SummaryType summary, ComplexTypeWriter.SerializationMode mode)
        {
            if (prop == null)
            {
                throw Error.ArgumentNull(nameof(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(nameof(elements), "Can only write repeating elements from a type implementing IList");
                }

                _writer.WriteStartArray();

                foreach (var element in elements)
                {
                    if (element != null)
                    {
                        write(prop, element, summary, mode);
                    }
                }

                _writer.WriteEndArray();
            }
            else
            {
                write(prop, instance, summary, mode);
            }
        }
Ejemplo n.º 2
0
 private void writeProperty(ClassMapping mapping, object instance, Rest.SummaryType summary, SerializationMode mode, PropertyMapping prop)
 {
     if (instance is Bundle && !(summary == Rest.SummaryType.Count && prop.Name.ToLower() == "entry") ||
         prop.Name == "id" ||
         summary == Rest.SummaryType.True && prop.InSummary ||
         summary == Rest.SummaryType.False ||
         summary == Rest.SummaryType.Data && !(prop.Name.ToLower() == "text" && prop.ElementType.Name == "Narrative") ||
         summary == Rest.SummaryType.Text && ((prop.Name.ToLower() == "text" && prop.ElementType.Name == "Narrative") || (prop.Name.ToLower() == "meta" && prop.ElementType.Name == "Meta") || prop.IsMandatoryElement)
         )
     {
         write(mapping, instance, (summary == Rest.SummaryType.Text && prop.Name.ToLower() == "meta" && prop.ElementType.Name == "Meta") ? Rest.SummaryType.False : summary, prop, mode);
     }
 }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
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();
        }
Ejemplo n.º 6
0
        private void write(PropertyMapping prop, object instance, Rest.SummaryType 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(_writer);
                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(_writer);
                writer.Serialize(instance, summary, contained: true);
                return;
            }

            ClassMapping mapping = _inspector.ImportType(instance.GetType());
            var          st      = summary;

            if (prop.IsMandatoryElement && st == Rest.SummaryType.Text && mapping.HasPrimitiveValueMember)
            {
                st = Rest.SummaryType.False;
            }

            if (mode == ComplexTypeWriter.SerializationMode.AllMembers || mode == ComplexTypeWriter.SerializationMode.NonValueElements)
            {
                var cplxWriter = new ComplexTypeWriter(_writer);
                cplxWriter.Serialize(mapping, instance, st, mode);
            }
            else
            {
                object value = mapping.PrimitiveValueProperty.GetValue(instance);
                write(mapping.PrimitiveValueProperty, value, st, ComplexTypeWriter.SerializationMode.AllMembers);
            }
        }
Ejemplo n.º 7
0
        internal void Serialize(ClassMapping mapping, object instance, Rest.SummaryType summary, SerializationMode mode = SerializationMode.AllMembers)
        {
            if (mapping == null)
            {
                throw Error.ArgumentNull(nameof(mapping));
            }

            _writer.WriteStartComplexContent();

            // Emit members that need xml /attributes/ first (to facilitate stream writer API)
            foreach (var prop in mapping.PropertyMappings.Where(pm => pm.SerializationHint == XmlSerializationHint.Attribute))
            {
                writeProperty(mapping, instance, summary, mode, prop);
            }

            // Then emit the rest
            foreach (var prop in mapping.PropertyMappings.Where(pm => pm.SerializationHint != XmlSerializationHint.Attribute))
            {
                writeProperty(mapping, instance, summary, mode, prop);
            }

            _writer.WriteEndComplexContent();
        }
Ejemplo n.º 8
0
        public void Serialize(PropertyMapping prop, object instance, Rest.SummaryType summary, ComplexTypeWriter.SerializationMode mode)
        {
            if (prop == null)
            {
                throw Error.ArgumentNull(nameof(prop));
            }

            var elements = instance as IList;

            if (elements == null)
            {
                throw Error.Argument(nameof(elements), "Can only write repeating elements from a type implementing IList");
            }

            _writer.WriteStartArray();

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

            _writer.WriteEndArray();
        }
Ejemplo n.º 9
0
        private void write(ClassMapping mapping, object instance, Rest.SummaryType summary, PropertyMapping prop, SerializationMode mode)
        {
            if (Settings.CustomSerializer != null)
            {
#pragma warning disable 618
                bool done = Settings.CustomSerializer.OnBeforeSerializeProperty(prop.Name, instance, _writer);
#pragma warning restore
                if (done)
                {
                    return;
                }
            }

            // Check whether we are asked to just serialize the value element (Value members of primitive Fhir datatypes)
            // or only the other members (Extension, Id etc in primitive Fhir datatypes)
            // Default is all
            if (mode == SerializationMode.ValueElement && !prop.RepresentsValueElement)
            {
                return;
            }
            if (mode == SerializationMode.NonValueElements && prop.RepresentsValueElement)
            {
                return;
            }

            object value        = prop.GetValue(instance);
            var    isEmptyArray = (value as IList) != null && ((IList)value).Count == 0;

            //   Message.Info("Handling member {0}.{1}", mapping.Name, prop.Name);

            if ((value != null || prop.RepresentsValueElement && prop.ElementType.IsEnum() && !string.IsNullOrEmpty(((Primitive)instance).ObjectValue as string)) && !isEmptyArray)
            {
                string memberName = prop.Name;

                // Enumerated Primitive.Value of Code<T> will always serialize the ObjectValue, not the derived enumeration
                if (prop.RepresentsValueElement && prop.ElementType.IsEnum())
                {
                    value = ((Primitive)instance).ObjectValue;
                    //var rawValueProp = ReflectionHelper.FindPublicProperty(mapping.NativeType, "RawValue");
                    //var rawValue = rawValueProp.GetValue(instance, null);
                    //if (rawValue != null)
                    //    value = rawValue;
                }

                // For Choice properties, determine the actual name of the element
                // by appending its type to the base property name (i.e. deceasedBoolean, deceasedDate)
                if (prop.Choice == ChoiceType.DatatypeChoice)
                {
                    memberName = determineElementMemberName(prop.Name, GetSerializationTypeForDataTypeChoiceElements(prop, value));
                }

                _writer.WriteStartProperty(memberName);

                var writer = new DispatchingWriter(_writer, Settings);

                // Now, if our writer does not use dual properties for primitive values + rest (xml),
                // or this is a complex property without value element, serialize data normally
                if (!_writer.HasValueElementSupport || !serializedIntoTwoProperties(prop, value))
                {
                    writer.Serialize(prop, value, summary, SerializationMode.AllMembers);
                }
                else
                {
                    // else split up between two properties, name and _name
                    writer.Serialize(prop, value, summary, SerializationMode.ValueElement);
                    _writer.WriteEndProperty();
                    _writer.WriteStartProperty("_" + memberName);
                    writer.Serialize(prop, value, summary, SerializationMode.NonValueElements);
                }

                _writer.WriteEndProperty();
            }
        }