public object Deserialize(object existing = null, bool nested = false)
        {
            if (_reader.CurrentToken == TokenType.Object)
            {
                // If there's no a priori knowledge of the type of Resource we will encounter,
                // we'll have to determine from the data itself.
                var resourceType = _reader.GetResourceTypeName(nested);
                var mappedType   = _inspector.FindClassMappingForResource(resourceType);

                if (mappedType == null)
                {
                    // Special courtesy case
                    if (resourceType == "feed" || resourceType == "Bundle")
                    {
                        throw Error.Format("Encountered a feed instead of a resource", _reader);
                    }
                    else
                    {
                        throw Error.Format("Encountered unknown resource type {0}", _reader, resourceType);
                    }
                }

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

                // Delegate the actual work to the ComplexTypeReader, since
                // the serialization of Resources and ComplexTypes are virtually the same
                var cplxReader = new ComplexTypeReader(_reader);
                return(cplxReader.Deserialize(mappedType, existing));
            }
            else
            {
                throw Error.Format("Trying to read a resource, but reader is not at the start of an object", _reader);
            }
        }
Example #2
0
        public Resource Deserialize(Resource existing = null)
        {
            // If there's no a priori knowledge of the type of Resource we will encounter,
            // we'll have to determine from the data itself.
            var resourceTypeName = _reader.GetResourceTypeName();
            var mapping          = _inspector.FindClassMappingForResource(resourceTypeName);

            if (mapping == null)
            {
                throw Error.Format("Asked to deserialize unknown resource '" + resourceTypeName + "'", _reader);
            }

            // Delegate the actual work to the ComplexTypeReader, since
            // the serialization of Resources and ComplexTypes are virtually the same
            var cplxReader = new ComplexTypeReader(_reader, Settings);

            return((Resource)cplxReader.Deserialize(mapping, existing));
        }