Пример #1
0
        internal object Deserialize(ClassMapping mapping, object existing = null)
        {
            if (mapping == null)
            {
                throw Error.ArgumentNull("mapping");
            }

            if (existing != null)
            {
                if (mapping.NativeType != existing.GetType())
                {
                    throw Error.Argument("existing", "Existing instance is of type {0}, but type parameter indicates data type is a {1}", existing.GetType().Name, mapping.NativeType.Name);
                }
            }
            else
            {
                var fac = new DefaultModelFactory();
                existing = fac.Create(mapping.NativeType);
            }

            IEnumerable <Tuple <string, IFhirReader> > members = null;

            if (_current.CurrentToken == TokenType.Object)
            {
                members = _current.GetMembers();
            }
            else if (_current.IsPrimitive())
            {
                // Ok, we expected a complex type, but we found a primitive instead. This may happen
                // in Json where the value property and the other elements are separately put into
                // member and _member. In this case, we will parse the primitive into the Value property
                // of the complex type
                if (!mapping.HasPrimitiveValueMember)
                {
                    throw Error.Format("Complex object does not have a value property, yet the reader is at a primitive", _current);
                }

                // Simulate this as actually receiving a member "Value" in a normal complex object,
                // and resume normally
                var valueMember = Tuple.Create(mapping.PrimitiveValueProperty.Name, _current);
                members = new List <Tuple <string, IFhirReader> > {
                    valueMember
                };
            }
            else
            {
                throw Error.Format("Trying to read a complex object, but reader is not at the start of an object or primitive", _current);
            }

            read(mapping, members, existing);

            return(existing);
        }
        internal object Deserialize(Type nativeType)
        {
            if (nativeType == null)
            {
                throw Error.ArgumentNull("nativeType");
            }

            if (_current.IsPrimitive())
            {
                return(read(nativeType));
            }
            else
            {
                throw Error.Format("Trying to read a value, but reader is not at the start of a primitive", _current);
            }
        }