/// <summary>
        /// Creates a <see cref="JsonDictionaryContract"/> for the given type.
        /// </summary>
        /// <param name="objectType">Type of the object.</param>
        /// <returns>A <see cref="JsonDictionaryContract"/> for the given type.</returns>
        protected virtual JsonDictionaryContract CreateDictionaryContract(Type objectType)
        {
            JsonDictionaryContract contract = new JsonDictionaryContract(objectType);

            InitializeContract(contract);

            return(contract);
        }
        private object CreateAndPopulateDictionary(JsonReader reader, JsonDictionaryContract contract, string id)
        {
            IWrappedDictionary dictionary = CollectionUtils.CreateDictionaryWrapper(Activator.CreateInstance(contract.DictionaryTypeToCreate));

            PopulateDictionary(dictionary, reader, contract, id);

            return(dictionary.UnderlyingDictionary);
        }
Exemple #3
0
        private void SerializeDictionary(JsonWriter writer, IDictionary values, JsonDictionaryContract contract)
        {
            contract.InvokeOnSerializing(values);

            SerializeStack.Add(values);
            writer.WriteStartObject();

            bool isReference = contract.IsReference ?? HasFlag(_serializer.PreserveReferencesHandling, PreserveReferencesHandling.Objects);

            if (isReference)
            {
                writer.WritePropertyName(JsonTypeReflector.IdPropertyName);
                writer.WriteValue(_serializer.ReferenceResolver.GetReference(values));
            }
            if (HasFlag(_serializer.TypeNameHandling, TypeNameHandling.Objects))
            {
                WriteTypeProperty(writer, values.GetType());
            }

            foreach (DictionaryEntry entry in values)
            {
                string propertyName = entry.Key.ToString();
                object value        = entry.Value;

                if (ShouldWriteReference(value, null))
                {
                    writer.WritePropertyName(propertyName);
                    WriteReference(writer, value);
                }
                else
                {
                    if (!CheckForCircularReference(value, null))
                    {
                        continue;
                    }

                    writer.WritePropertyName(propertyName);
                    SerializeValue(writer, value, null);
                }
            }

            writer.WriteEndObject();
            SerializeStack.RemoveAt(SerializeStack.Count - 1);

            contract.InvokeOnSerialized(values);
        }
        private IDictionary PopulateDictionary(IWrappedDictionary dictionary, JsonReader reader, JsonDictionaryContract contract, string id)
        {
            if (id != null)
            {
                _serializer.ReferenceResolver.AddReference(id, dictionary.UnderlyingDictionary);
            }

            contract.InvokeOnDeserializing(dictionary.UnderlyingDictionary);

            do
            {
                switch (reader.TokenType)
                {
                case JsonToken.PropertyName:
                    object keyValue = EnsureType(reader.Value, contract.DictionaryKeyType);
                    CheckedRead(reader);

                    dictionary.Add(keyValue, CreateValue(reader, contract.DictionaryValueType, null, null));
                    break;

                case JsonToken.EndObject:
                    contract.InvokeOnDeserialized(dictionary.UnderlyingDictionary);

                    return(dictionary);

                default:
                    throw new JsonSerializationException("Unexpected token when deserializing object: " + reader.TokenType);
                }
            } while (reader.Read());

            throw new JsonSerializationException("Unexpected end when deserializing object.");
        }