Exemple #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);
            }
        }
Exemple #2
0
        internal void Serialize(PropertyMapping prop, object instance, 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, mode);
                }

                _current.WriteEndArray();
            }
            else
            {
                write(prop, instance, mode);
            }
        }
        public void Serialize(PropertyMapping prop, object instance, 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, mode);
            }

            _current.WriteEndArray();
        }
Exemple #4
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();
        }
Exemple #5
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);
            }
        }