Пример #1
0
        private JsonArrayContract GetArrayContract(Type objectType)
        {
            JsonContract contract = Serializer.ContractResolver.ResolveContract(objectType);

            if (contract == null)
            {
                throw new JsonSerializationException("Could not resolve type '{0}' to a JsonContract.".FormatWith(CultureInfo.InvariantCulture, objectType));
            }

            JsonArrayContract arrayContract = contract as JsonArrayContract;

            if (arrayContract == null)
            {
                throw new JsonSerializationException("Expected a JsonArrayContract for type '{0}', got '{1}'.".FormatWith(CultureInfo.InvariantCulture, objectType, contract.GetType()));
            }

            return(arrayContract);
        }
Пример #2
0
        public void Populate(JsonReader reader, object target)
        {
            ValidationUtils.ArgumentNotNull(target, "target");

            Type objectType = target.GetType();

            JsonContract contract = Serializer.ContractResolver.ResolveContract(objectType);

            if (reader.TokenType == JsonToken.None)
            {
                reader.Read();
            }

            if (reader.TokenType == JsonToken.StartArray)
            {
                PopulateList(CollectionUtils.CreateCollectionWrapper(target), reader, null, GetArrayContract(objectType));
            }
            else if (reader.TokenType == JsonToken.StartObject)
            {
                CheckedRead(reader);

                string id = null;
                if (reader.TokenType == JsonToken.PropertyName && string.Equals(reader.Value.ToString(), JsonTypeReflector.IdPropertyName, StringComparison.Ordinal))
                {
                    CheckedRead(reader);
                    id = reader.Value.ToString();
                    CheckedRead(reader);
                }

                if (contract is JsonDictionaryContract)
                {
                    PopulateDictionary(CollectionUtils.CreateDictionaryWrapper(target), reader, (JsonDictionaryContract)contract, id);
                }
                else if (contract is JsonObjectContract)
                {
                    PopulateObject(target, reader, (JsonObjectContract)contract, id);
                }
                else
                {
                    throw new JsonSerializationException("Expected a JsonObjectContract or JsonDictionaryContract for type '{0}', got '{1}'.".FormatWith(CultureInfo.InvariantCulture, objectType, contract.GetType()));
                }
            }
        }
Пример #3
0
        private object CreateObject(JsonReader reader, Type objectType, object existingValue)
        {
            CheckedRead(reader);

            string id = null;

            if (reader.TokenType == JsonToken.PropertyName)
            {
                bool specialProperty;

                do
                {
                    string propertyName = reader.Value.ToString();

                    if (string.Equals(propertyName, JsonTypeReflector.RefPropertyName, StringComparison.Ordinal))
                    {
                        CheckedRead(reader);
                        string reference = reader.Value.ToString();

                        CheckedRead(reader);
                        return(Serializer.ReferenceResolver.ResolveReference(reference));
                    }
                    else if (string.Equals(propertyName, JsonTypeReflector.TypePropertyName, StringComparison.Ordinal))
                    {
                        CheckedRead(reader);
                        string qualifiedTypeName = reader.Value.ToString();

                        CheckedRead(reader);

                        if (Serializer.TypeNameHandling != TypeNameHandling.None)
                        {
                            string typeName;
                            string assemblyName;
                            ReflectionUtils.SplitFullyQualifiedTypeName(qualifiedTypeName, out typeName, out assemblyName);

                            Type specifiedType;
                            try
                            {
                                specifiedType = Serializer.Binder.BindToType(assemblyName, typeName);
                            }
                            catch (Exception ex)
                            {
                                throw new JsonSerializationException("Error resolving type specified in JSON '{0}'.".FormatWith(CultureInfo.InvariantCulture, qualifiedTypeName), ex);
                            }

                            if (specifiedType == null)
                            {
                                throw new JsonSerializationException("Type specified in JSON '{0}' was not resolved.".FormatWith(CultureInfo.InvariantCulture, qualifiedTypeName));
                            }

                            if (objectType != null && !objectType.IsAssignableFrom(specifiedType))
                            {
                                throw new JsonSerializationException("Type specified in JSON '{0}' is not compatible with '{1}'.".FormatWith(CultureInfo.InvariantCulture, specifiedType.AssemblyQualifiedName, objectType.AssemblyQualifiedName));
                            }

                            objectType = specifiedType;
                        }
                        specialProperty = true;
                    }
                    else if (string.Equals(propertyName, JsonTypeReflector.IdPropertyName, StringComparison.Ordinal))
                    {
                        CheckedRead(reader);

                        id = reader.Value.ToString();
                        CheckedRead(reader);
                        specialProperty = true;
                    }
                    else if (string.Equals(propertyName, JsonTypeReflector.ArrayValuesPropertyName, StringComparison.Ordinal))
                    {
                        CheckedRead(reader);
                        object list = CreateList(reader, objectType, existingValue, id);
                        CheckedRead(reader);
                        return(list);
                    }
                    else
                    {
                        specialProperty = false;
                    }
                } while (specialProperty &&
                         reader.TokenType == JsonToken.PropertyName);
            }

            if (!HasDefinedType(objectType))
            {
                return(CreateJObject(reader));
            }

            JsonContract contract = Serializer.ContractResolver.ResolveContract(objectType);

            if (contract == null)
            {
                throw new JsonSerializationException("Could not resolve type '{0}' to a JsonContract.".FormatWith(CultureInfo.InvariantCulture, objectType));
            }

            if (contract is JsonDictionaryContract)
            {
                if (existingValue == null)
                {
                    return(CreateAndPopulateDictionary(reader, (JsonDictionaryContract)contract, id));
                }

                return(PopulateDictionary(CollectionUtils.CreateDictionaryWrapper(existingValue), reader, (JsonDictionaryContract)contract, id));
            }

            if (contract is JsonObjectContract)
            {
                if (existingValue == null)
                {
                    return(CreateAndPopulateObject(reader, (JsonObjectContract)contract, id));
                }

                return(PopulateObject(existingValue, reader, (JsonObjectContract)contract, id));
            }

            throw new JsonSerializationException("Expected a JsonObjectContract or JsonDictionaryContract for type '{0}', got '{1}'.".FormatWith(CultureInfo.InvariantCulture, objectType, contract.GetType()));
        }