コード例 #1
0
        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);
        }