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

            IList result = existing;

            if (_current.CurrentToken != TokenType.Object
                )  // Xml has repeating members, so this results in an "array" of just 1 member
            {
                throw Error.Format("Expecting to be either at a repeating complex element or an array when parsing a repeating member.", _current);
            }

            if (result == null)
            {
                result = ReflectionHelper.CreateGenericList(prop.ElementType);
            }

            var reader = new DispatchingReader(_current, arrayMode: true);

            result.Add(reader.Deserialize(prop, memberName));

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

            if (existing != null && !(existing is IList) ) throw Error.Argument("existing", "Can only read repeating elements into a type implementing IList");

            IList result = existing as IList;

            bool overwriteMode;
            IEnumerable<IFhirReader> elements;

            if(_current.CurrentToken == TokenType.Array)        // Json has members that are arrays, if we encounter multiple, update the old values of the array
            {
                overwriteMode = result != null && result.Count > 0;
                elements = _current.GetArrayElements();
            }
            else if(_current.CurrentToken == TokenType.Object)  // Xml has repeating members, so this results in an "array" of just 1 member
            {
                //TODO: This makes   member : {x} in Json valid too,
                //even if json should have member : [{x}]
                overwriteMode = false;
                elements = new List<IFhirReader>() { _current };
            }
            else
                throw Error.Format("Expecting to be either at a repeating complex element or an array when parsing a repeating member.", _current);

            if (result == null) result = ReflectionHelper.CreateGenericList(prop.ElementType);

            var position = 0;
            foreach(var element in elements)
            {
                var reader = new DispatchingReader(element, arrayMode: true);

                if(overwriteMode)
                {
                    if (position >= result.Count)
                        throw Error.Format("The value and extension array are not well-aligned", _current);

                    // Arrays may contain null values as placeholders
                    if(element.CurrentToken != TokenType.Null)
                        result[position] = reader.Deserialize(prop, memberName, existing: result[position]);
                }
                else
                {                   
                    object item = null;
                    if (element.CurrentToken != TokenType.Null)
                        item = reader.Deserialize(prop, memberName);
                    else
                        item = null;  // Arrays may contain null values as placeholders
                    
                    result.Add(item);
                }

                position++;
            }

            return result;
        }
        public IList Deserialize(PropertyMapping prop, string memberName, IList existing=null)
        {
            if (prop == null) throw Error.ArgumentNull("prop");

            IList result = existing;

            if (result == null) result = ReflectionHelper.CreateGenericList(prop.ElementType);

            var reader = new DispatchingReader(_current, arrayMode: true);                 
            result.Add(reader.Deserialize(prop, memberName));

            return result;
        }
        private void read(ClassMapping mapping, IEnumerable <Tuple <string, IFhirReader> > members, object existing)
        {
            //bool hasMember;

            foreach (var memberData in members)
            {
                //hasMember = true;
                var memberName = memberData.Item1;  // tuple: first is name of member

                // Find a property on the instance that matches the element found in the data
                // NB: This function knows how to handle suffixed names (e.g. xxxxBoolean) (for choice types).
                var mappedProperty = mapping.FindMappedElementByName(memberName);

                if (mappedProperty != null)
                {
                    //   Message.Info("Handling member {0}.{1}", mapping.Name, memberName);

                    object value = null;

                    // For primitive members we can save time by net calling the getter
                    if (!mappedProperty.IsPrimitive)
                    {
                        value = mappedProperty.GetValue(existing);
                    }

                    var reader = new DispatchingReader(memberData.Item2);
                    value = reader.Deserialize(mappedProperty, memberName, value);

                    mappedProperty.SetValue(existing, value);
                }
                else
                {
                    if (SerializationConfig.AcceptUnknownMembers == false)
                    {
                        throw Error.Format(Messages.DeserializeUnknownMember, _current, memberName);
                    }
                    else
                    {
                        Message.Info("Skipping unknown member " + memberName);
                    }
                }
            }

            // Not sure if the reader needs to verify this. Certainly, I want to accept empty elements for the
            // pseudo-resource TagList (no tags) and probably others.
            //if (!hasMember)
            //    throw Error.Format("Fhir serialization does not allow nor support empty elements");
        }
        public IList Deserialize(PropertyMapping prop, string memberName, IList existing=null)
        {
            if (prop == null) throw Error.ArgumentNull("prop");

            IList result = existing;

            if(_current.CurrentToken != TokenType.Object)  // Xml has repeating members, so this results in an "array" of just 1 member
                throw Error.Format("Expecting to be either at a repeating complex element or an array when parsing a repeating member.", _current);

            if (result == null) result = ReflectionHelper.CreateGenericList(prop.ElementType);

            var reader = new DispatchingReader(_current, arrayMode: true);                 
            result.Add(reader.Deserialize(prop, memberName));

            return result;
        }
        private void read(ClassMapping mapping, IEnumerable <Tuple <string, IFhirReader> > members, Base existing)
        {
            //bool hasMember;

            foreach (var memberData in members)
            {
                //hasMember = true;
                var memberName = memberData.Item1;  // tuple: first is name of member

                // Find a property on the instance that matches the element found in the data
                // NB: This function knows how to handle suffixed names (e.g. xxxxBoolean) (for choice types).
                var mappedProperty = mapping.FindMappedElementByName(memberName);

                if (mappedProperty != null)
                {
                    //   Message.Info("Handling member {0}.{1}", mapping.Name, memberName);

                    object value = null;

                    // For primitive members we can save time by not calling the getter
                    if (!mappedProperty.IsPrimitive)
                    {
                        value = mappedProperty.GetValue(existing);
                    }

                    var reader = new DispatchingReader(memberData.Item2);
                    value = reader.Deserialize(mappedProperty, memberName, value);

                    mappedProperty.SetValue(existing, value);
                }
                else
                {
                    if (SerializationConfig.AcceptUnknownMembers == false)
                    {
                        throw Error.Format("Encountered unknown member '{0}' while de-serializing", _current, memberName);
                    }
                    else
                    {
                        Message.Info("Skipping unknown member " + memberName);
                    }
                }
            }
        }
#pragma warning restore 612, 618

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

            IList result = existing;

            if (result == null)
            {
                result = ReflectionHelper.CreateGenericList(prop.ElementType);
            }

            var reader = new DispatchingReader(_current, Settings, arrayMode: true);

            result.Add(reader.Deserialize(prop, memberName));

            return(result);
        }
        private void read(ClassMapping mapping, IEnumerable <Tuple <string, IFhirReader> > members, Base existing)
        {
            //bool hasMember;

            foreach (var memberData in members)
            {
                //hasMember = true;
                var memberName = memberData.Item1;  // tuple: first is name of member

                // Find a property on the instance that matches the element found in the data
                // NB: This function knows how to handle suffixed names (e.g. xxxxBoolean) (for choice types).
                var mappedProperty = mapping.FindMappedElementByName(memberName);

                if (mappedProperty != null)
                {
                    //   Message.Info("Handling member {0}.{1}", mapping.Name, memberName);

                    object value = null;

                    // For primitive members we can save time by not calling the getter
                    if (!mappedProperty.IsPrimitive)
                    {
                        value = mappedProperty.GetValue(existing);
                    }

                    var reader = new DispatchingReader(memberData.Item2, Settings, arrayMode: false);
                    value = reader.Deserialize(mappedProperty, memberName, value);

                    if (mappedProperty.RepresentsValueElement && mappedProperty.ElementType.IsEnum() && value is String)
                    {
                        if (!Settings.AllowUnrecognizedEnums)
                        {
                            if (EnumUtility.ParseLiteral((string)value, mappedProperty.ElementType) == null)
                            {
                                throw Error.Format("Literal '{0}' is not a valid value for enumeration '{1}'".FormatWith(value, mappedProperty.ElementType.Name), _current);
                            }
                        }

                        ((Primitive)existing).ObjectValue = value;
                        //var prop = ReflectionHelper.FindPublicProperty(mapping.NativeType, "RawValue");
                        //prop.SetValue(existing, value, null);
                        //mappedProperty.SetValue(existing, null);
                    }
                    else
                    {
                        mappedProperty.SetValue(existing, value);
                    }
                }
                else
                {
                    if (Settings.AcceptUnknownMembers == false)
                    {
                        throw Error.Format("Encountered unknown member '{0}' while de-serializing".FormatWith(memberName), _current);
                    }
                    else
                    {
                        Message.Info("Skipping unknown member " + memberName);
                    }
                }
            }
        }
        //this should be refactored into read(ITypedElement parent, Base existing)

        private void read(ClassMapping mapping, IEnumerable <ITypedElement> members, Base existing)
        {
            foreach (var memberData in members)
            {
                var memberName = memberData.Name;  // tuple: first is name of member

                // Find a property on the instance that matches the element found in the data
                // NB: This function knows how to handle suffixed names (e.g. xxxxBoolean) (for choice types).
                var mappedProperty = mapping.FindMappedElementByName(memberName);

                if (mappedProperty != null)
                {
                    //   Message.Info("Handling member {0}.{1}", mapping.Name, memberName);

                    object value = null;

                    // For primitive members we can save time by not calling the getter
                    if (!mappedProperty.IsPrimitive)
                    {
                        value = mappedProperty.GetValue(existing);

                        if (value != null && !mappedProperty.IsCollection)
                        {
                            RaiseFormatError($"Element '{mappedProperty.Name}' must not repeat", memberData.Location);
                        }
                    }

                    var reader = new DispatchingReader(memberData, Settings, arrayMode: false);

                    // Since we're still using both ClassMappings and the newer IElementDefinitionSummary provider at the same time,
                    // the member might be known in the one (POCO), but unknown in the provider. This is only in theory, since the
                    // provider should provide exactly the same information as the mappings. But better to get a clear exception
                    // when this happens.
                    value = reader.Deserialize(mappedProperty, memberName, value);

                    if (mappedProperty.RepresentsValueElement && mappedProperty.ImplementingType.IsEnum() && value is String)
                    {
                        if (!Settings.AllowUnrecognizedEnums)
                        {
                            if (EnumUtility.ParseLiteral((string)value, mappedProperty.ImplementingType) == null)
                            {
                                RaiseFormatError($"Literal '{value}' is not a valid value for enumeration '{mappedProperty.ImplementingType.Name}'", _current.Location);
                            }
                        }

                        ((Primitive)existing).ObjectValue = value;
                        //var prop = ReflectionHelper.FindPublicProperty(mapping.NativeType, "RawValue");
                        //prop.SetValue(existing, value, null);
                        //mappedProperty.SetValue(existing, null);
                    }
                    else
                    {
                        mappedProperty.SetValue(existing, value);
                    }
                }
                else
                {
                    if (Settings.AcceptUnknownMembers == false)
                    {
                        RaiseFormatError($"Encountered unknown member '{memberName}' while de-serializing", memberData.Location);
                    }
                    else
                    {
                        Message.Info("Skipping unknown member " + memberName);
                    }
                }
            }
        }
        public object Deserialize(PropertyMapping prop, string memberName, object existing = null)
        {
            if (prop == null)
            {
                throw Error.ArgumentNull("prop");
            }

            if (existing != null && !(existing is IList))
            {
                throw Error.Argument("existing", "Can only read repeating elements into a type implementing IList");
            }

            IList result = existing as IList;

            bool overwriteMode;
            IEnumerable <IFhirReader> elements;

            if (_current.CurrentToken == TokenType.Array)        // Json has members that are arrays, if we encounter multiple, update the old values of the array
            {
                overwriteMode = result != null && result.Count > 0;
                elements      = _current.GetArrayElements();
            }
            else if (_current.CurrentToken == TokenType.Object)  // Xml has repeating members, so this results in an "array" of just 1 member
            {
                //TODO: This makes   member : {x} in Json valid too,
                //even if json should have member : [{x}]
                overwriteMode = false;
                elements      = new List <IFhirReader>()
                {
                    _current
                };
            }
            else
            {
                throw Error.Format("Expecting to be either at a repeating complex element or an array when parsing a repeating member.", _current);
            }

            if (result == null)
            {
                result = ReflectionHelper.CreateGenericList(prop.ElementType);
            }

            var position = 0;

            foreach (var element in elements)
            {
                var reader = new DispatchingReader(element, arrayMode: true);

                if (overwriteMode)
                {
                    if (position >= result.Count)
                    {
                        throw Error.Format("The value and extension array are not well-aligned", _current);
                    }

                    // Arrays may contain null values as placeholders
                    if (element.CurrentToken != TokenType.Null)
                    {
                        result[position] = reader.Deserialize(prop, memberName, existing: result[position]);
                    }
                }
                else
                {
                    object item = null;
                    if (element.CurrentToken != TokenType.Null)
                    {
                        item = reader.Deserialize(prop, memberName);
                    }
                    else
                    {
                        item = null;  // Arrays may contain null values as placeholders
                    }
                    result.Add(item);
                }

                position++;
            }

            return(result);
        }
Beispiel #11
0
        private void read(ClassMapping mapping, IEnumerable<Tuple<string,IFhirReader>> members, Base existing)
        {
            //bool hasMember;

            foreach (var memberData in members)
            {
                //hasMember = true;
                var memberName = memberData.Item1;  // tuple: first is name of member
             
                // Find a property on the instance that matches the element found in the data
                // NB: This function knows how to handle suffixed names (e.g. xxxxBoolean) (for choice types).
                var mappedProperty = mapping.FindMappedElementByName(memberName);

                if (mappedProperty != null)
                {
                    //   Message.Info("Handling member {0}.{1}", mapping.Name, memberName);

                    object value = null;

                    // For primitive members we can save time by not calling the getter
                    if (!mappedProperty.IsPrimitive)
                        value = mappedProperty.GetValue(existing);

                    var reader = new DispatchingReader(memberData.Item2);
                    value = reader.Deserialize(mappedProperty, memberName, value);

                    mappedProperty.SetValue(existing, value);
                }
                else
                {
                    if (SerializationConfig.AcceptUnknownMembers == false)
                        throw Error.Format("Encountered unknown member '{0}' while de-serializing", _current, memberName);
                    else
                        Message.Info("Skipping unknown member " + memberName);
                }
            }

        }
        private void read(ClassMapping mapping, IEnumerable<Tuple<string,IFhirReader>> members, object existing)
        {
            //bool hasMember;

            foreach (var memberData in members)
            {
                //hasMember = true;
                var memberName = memberData.Item1;  // tuple: first is name of member

                // Find a property on the instance that matches the element found in the data
                // NB: This function knows how to handle suffixed names (e.g. xxxxBoolean) (for choice types).
                var mappedProperty = mapping.FindMappedElementByName(memberName);

                if (mappedProperty != null)
                {
                 //   Message.Info("Handling member {0}.{1}", mapping.Name, memberName);

                    object value = null;

                    // For primitive members we can save time by net calling the getter
                    if (!mappedProperty.IsPrimitive)
                        value = mappedProperty.GetValue(existing);
                   
                    var reader = new DispatchingReader(memberData.Item2);
                    value = reader.Deserialize(mappedProperty, memberName, value);

                    mappedProperty.SetValue(existing, value);
                }
                else
                {
                    if (SerializationConfig.AcceptUnknownMembers == false)
                        throw Error.Format(Messages.DeserializeUnknownMember, _current, memberName);
                    else
                        Message.Info("Skipping unknown member " + memberName);
                }
            }

            // Not sure if the reader needs to verify this. Certainly, I want to accept empty elements for the
            // pseudo-resource TagList (no tags) and probably others.
            //if (!hasMember)
            //    throw Error.Format("Fhir serialization does not allow nor support empty elements");
        }