protected override JsonDictionaryContract CreateDictionaryContract(Type objectType)
        {
            JsonDictionaryContract dictionaryContract = base.CreateDictionaryContract(objectType);

            dictionaryContract.ItemConverter = new DictionaryValueConverter(_metadataProvider);
            return(dictionaryContract);
        }
 private ApiModel ResolveApiDictionary(JsonDictionaryContract jsonDictionaryContract)
 {
     return(new ApiDictionary(
                type: jsonDictionaryContract.UnderlyingType,
                keyType: jsonDictionaryContract.DictionaryKeyType ?? typeof(object),
                valueType: jsonDictionaryContract.DictionaryValueType ?? typeof(object)));
 }
        protected override JsonDictionaryContract CreateDictionaryContract(Type objectType)
        {
            JsonDictionaryContract dictionaryContract = base.CreateDictionaryContract(objectType);

            dictionaryContract.ItemConverter = new FixDecimalValueConverter();
            return(dictionaryContract);
        }
Esempio n. 4
0
        private Schema CreateDictionarySchema(JsonDictionaryContract dictionaryContract)
        {
            Type type1 = dictionaryContract.get_DictionaryKeyType();

            if ((object)type1 == null)
            {
                type1 = typeof(object);
            }
            Type enumType = type1;
            Type type2    = dictionaryContract.get_DictionaryValueType();

            if ((object)type2 == null)
            {
                type2 = typeof(object);
            }
            Type valueType = type2;

            if (enumType.IsEnum)
            {
                return new Schema()
                       {
                           type       = "object",
                           properties = (IDictionary <string, Schema>)((IEnumerable <string>)Enum.GetNames(enumType)).ToDictionary <string, string, Schema>((Func <string, string>)(name => dictionaryContract.get_DictionaryKeyResolver()(name)), (Func <string, Schema>)(name => this.CreateInlineSchema(valueType)))
                       }
            }
            ;
            return(new Schema()
            {
                type = "object",
                additionalProperties = this.CreateInlineSchema(valueType)
            });
        }
Esempio n. 5
0
        private Schema CreateDictionarySchema(JsonDictionaryContract dictionaryContract)
        {
            var keyType   = dictionaryContract.DictionaryKeyType ?? typeof(object);
            var valueType = dictionaryContract.DictionaryValueType ?? typeof(object);

            if (keyType.IsEnum)
            {
                return(new Schema
                {
                    type = "object",
                    properties = Enum.GetNames(keyType).ToDictionary(
                        (name) => dictionaryContract.DictionaryKeyResolver(name),
                        (name) => CreateInlineSchema(valueType)
                        )
                });
            }
            else
            {
                return(new Schema
                {
                    type = "object",
                    additionalProperties = CreateInlineSchema(valueType)
                });
            }
        }
        private Schema CreateDictionarySchema(JsonDictionaryContract dictionaryContract, Queue <Type> referencedTypes)
        {
            var keyType   = dictionaryContract.DictionaryKeyType ?? typeof(object);
            var valueType = dictionaryContract.DictionaryValueType ?? typeof(object);

            if (keyType.GetTypeInfo().IsEnum)
            {
                return(new Schema
                {
                    Type = "object",
                    Properties = Enum.GetNames(keyType).ToDictionary(
                        (name) => dictionaryContract.DictionaryKeyResolver(name),
                        (name) => CreateSchema(valueType, referencedTypes)
                        )
                });
            }
            else
            {
                return(new Schema
                {
                    Type = "object",
                    AdditionalProperties = CreateSchema(valueType, referencedTypes)
                });
            }
        }
        protected override JsonDictionaryContract CreateDictionaryContract(Type objectType)
        {
            JsonDictionaryContract contract = base.CreateDictionaryContract(objectType);

            contract.DictionaryKeyResolver = propertyName => propertyName;
            return(contract);
        }
Esempio n. 8
0
        protected override JsonDictionaryContract CreateDictionaryContract(Type objectType)
        {
            // Don't camel case dictionary keys
            JsonDictionaryContract contract = base.CreateDictionaryContract(objectType);

            contract.PropertyNameResolver = new Func <string, string>(s => s);
            return(contract);
        }
Esempio n. 9
0
        protected override JsonDictionaryContract CreateDictionaryContract(Type objectType)
        {
            // Don't camel case dictionary keys
            JsonDictionaryContract contract = base.CreateDictionaryContract(objectType);

            contract.DictionaryKeyResolver = s => s;
            return(contract);
        }
Esempio n. 10
0
        protected override JsonDictionaryContract CreateDictionaryContract(Type type)
        {
            // We need to preserve case for keys in the PropertiesCollection
            JsonDictionaryContract contract = base.CreateDictionaryContract(type);

            contract.DictionaryKeyResolver = (name) => name;
            return(contract);
        }
        public void NonGenericDictionary_KeyValueTypes()
        {
            DefaultContractResolver resolver = new DefaultContractResolver();

            JsonDictionaryContract c = (JsonDictionaryContract)resolver.ResolveContract(typeof(IDictionary));

            Assert.IsNull(c.DictionaryKeyType);
            Assert.IsNull(c.DictionaryValueType);
        }
        protected override JsonDictionaryContract CreateDictionaryContract(Type objectType)
        {
            JsonDictionaryContract contract = base.CreateDictionaryContract(objectType);

            // don't camel-case the key names of a dictionary
            contract.PropertyNameResolver = x => x;

            return(contract);
        }
Esempio n. 13
0
        private static void PopulateDictionary(JObject jObject, JsonDictionaryContract dictionaryContract, IDictionary source, IDictionary target)
        {
            var valueContract = InternalPrivateContractResolver.ResolveContract(dictionaryContract.DictionaryValueType);

            foreach (DictionaryEntry entry in source)
            {
                var key      = entry.Key;
                var value    = entry.Value;
                var setValue = true;
                if (value != null && target.Contains(key))
                {
                    var existingValue = target[key];
                    if (existingValue != null)
                    {
                        var jToken = jObject[key];
                        switch (valueContract)
                        {
                        case JsonObjectContract objectContract:
                            PopulateObject((JObject)jToken, objectContract, (ExtensibleObject)value, (ExtensibleObject)existingValue);
                            setValue = false;
                            break;

                        case JsonDictionaryContract jsonDictionaryContract:
                            PopulateDictionary((JObject)jToken, jsonDictionaryContract, (IDictionary)value, (IDictionary)existingValue);
                            setValue = false;
                            break;

                        case JsonArrayContract arrayContract:
                            PopulateArray((JArray)jToken, arrayContract, (IList)value, (IList)existingValue);
                            setValue = false;
                            break;
                        }
                    }
                }
                if (setValue)
                {
                    target[key] = value;
                }
            }
            var entriesToDelete = new List <DictionaryEntry>();

            foreach (DictionaryEntry entry in target)
            {
                if (!source.Contains(entry.Key))
                {
                    entriesToDelete.Add(entry);
                }
            }
            foreach (var entryToDelete in entriesToDelete)
            {
                target.Remove(entryToDelete.Key);
                if (entryToDelete.Value is DirtyExtensibleObject dirtyExtensibleObject)
                {
                    dirtyExtensibleObject.ClearPropertyChangedEvent();
                }
            }
        }
Esempio n. 14
0
        protected override JsonDictionaryContract CreateDictionaryContract(Type objectType)
        {
            JsonDictionaryContract dictionaryContract = base.CreateDictionaryContract(objectType);

            if (Enumerable.Any <object>((IEnumerable <object>)objectType.GetCustomAttributes(typeof(JsonPreserveCaseDictionaryAttribute), true)))
            {
                dictionaryContract.PropertyNameResolver = (Func <string, string>)(propertyName => propertyName);
            }
            return(dictionaryContract);
        }
Esempio n. 15
0
        private Schema CreateDictionarySchema(JsonDictionaryContract dictionaryContract)
        {
            var valueType = dictionaryContract.DictionaryValueType ?? typeof(object);

            return(new Schema
            {
                type = "object",
                additionalProperties = CreateInlineSchema(valueType)
            });
        }
Esempio n. 16
0
    // Token: 0x060009A9 RID: 2473 RVA: 0x00038134 File Offset: 0x00036334
    private void method_37(JsonWriter jsonWriter_0, IDictionary idictionary_0, JsonDictionaryContract jsonDictionaryContract_0, JsonProperty jsonProperty_0, JsonContainerContract jsonContainerContract_0, JsonProperty jsonProperty_1)
    {
        Interface1 @interface;
        object     obj = ((@interface = (idictionary_0 as Interface1)) != null) ? @interface.UnderlyingDictionary : idictionary_0;

        this.method_17(jsonWriter_0, jsonDictionaryContract_0, obj);
        this.list_0.Add(obj);
        this.method_21(jsonWriter_0, obj, jsonDictionaryContract_0, jsonProperty_0, jsonContainerContract_0, jsonProperty_1);
        if (jsonDictionaryContract_0.EwhtYgWttB() == null)
        {
            jsonDictionaryContract_0.method_5(this.jsonSerializer_0.icontractResolver_0.ResolveContract(jsonDictionaryContract_0.DictionaryValueType ?? typeof(object)));
        }
        if (jsonDictionaryContract_0.method_7() == null)
        {
            jsonDictionaryContract_0.method_8(this.jsonSerializer_0.icontractResolver_0.ResolveContract(jsonDictionaryContract_0.DictionaryKeyType ?? typeof(object)));
        }
        int top = jsonWriter_0.Top;

        using (IDictionaryEnumerator enumerator = idictionary_0.GetEnumerator())
        {
            while (enumerator.MoveNext())
            {
                DictionaryEntry entry = enumerator.Entry;
                bool            escape;
                string          text = this.method_38(jsonWriter_0, entry.Key, jsonDictionaryContract_0.method_7(), out escape);
                text = ((jsonDictionaryContract_0.DictionaryKeyResolver != null) ? jsonDictionaryContract_0.DictionaryKeyResolver(text) : text);
                try
                {
                    object       value         = entry.Value;
                    JsonContract jsonContract_ = jsonDictionaryContract_0.method_6() ?? this.method_7(value);
                    if (this.method_11(value, null, jsonContract_, jsonDictionaryContract_0, jsonProperty_0))
                    {
                        jsonWriter_0.WritePropertyName(text, escape);
                        this.method_14(jsonWriter_0, value);
                    }
                    else if (this.method_13(jsonWriter_0, value, null, jsonContract_, jsonDictionaryContract_0, jsonProperty_0))
                    {
                        jsonWriter_0.WritePropertyName(text, escape);
                        this.method_9(jsonWriter_0, value, jsonContract_, null, jsonDictionaryContract_0, jsonProperty_0);
                    }
                }
                catch (Exception exception_)
                {
                    if (!base.method_4(obj, jsonDictionaryContract_0, text, null, jsonWriter_0.xEuXaafal7(), exception_))
                    {
                        throw;
                    }
                    this.method_39(jsonWriter_0, top);
                }
            }
        }
        jsonWriter_0.WriteEndObject();
        this.list_0.RemoveAt(this.list_0.Count - 1);
        this.method_18(jsonWriter_0, jsonDictionaryContract_0, obj);
    }
Esempio n. 17
0
        protected override JsonDictionaryContract CreateDictionaryContract(Type objectType)
        {
            // Fix PropertyNameResolver : https://github.com/JamesNK/Newtonsoft.Json/commit/0929c2b4c3349ae1fb3d22c522504fbf57c8268e
            JsonDictionaryContract dictionaryContract = base.CreateDictionaryContract(objectType);

            if (Enumerable.Any(objectType.GetCustomAttributes(typeof(JsonPreserveCaseDictionaryAttribute), true)))
            {
                dictionaryContract.DictionaryKeyResolver = propertyName => propertyName;
            }
            return(dictionaryContract);
        }
Esempio n. 18
0
        /// <inheritdoc />
        protected override JsonDictionaryContract CreateDictionaryContract(Type objectType)
        {
            JsonDictionaryContract contract = base.CreateDictionaryContract(objectType);
            var func = _customJsonContract.DictionaryKeyResolverByType(objectType);

            if (func != null)
            {
                contract.DictionaryKeyResolver = func;
            }

            return(contract);
        }
        /// <inheritdoc />
        protected override JsonDictionaryContract CreateDictionaryContract(Type objectType)
        {
            JsonDictionaryContract contract = base.CreateDictionaryContract(objectType);

            object[] ignoreCamelCase = objectType.GetCustomAttributes(typeof(JsonIgnoreCamelCaseDictionary), true);
            if (ignoreCamelCase.Any())
            {
                contract.DictionaryKeyResolver = propertyName => propertyName;
            }

            return(contract);
        }
Esempio n. 20
0
        protected override JsonDictionaryContract CreateDictionaryContract(Type objectType)
        {
            if (objectType != typeof(DataDictionary) && objectType != typeof(SettingsDictionary))
            {
                return(base.CreateDictionaryContract(objectType));
            }

            JsonDictionaryContract contract = base.CreateDictionaryContract(objectType);

            contract.DictionaryKeyResolver = propertyName => propertyName;
            return(contract);
        }
Esempio n. 21
0
        private void method_34(JsonWriter jsonWriter_0, IDictionary idictionary_0, JsonDictionaryContract jsonDictionaryContract_0, JsonProperty jsonProperty_0, JsonContainerContract jsonContainerContract_0, JsonProperty jsonProperty_1)
        {
            Interface16 interface2 = idictionary_0 as Interface16;
            object      obj2       = (interface2 != null) ? interface2.Object_0 : idictionary_0;

            this.method_15(jsonWriter_0, jsonDictionaryContract_0, obj2);
            this.list_0.Add(obj2);
            this.method_19(jsonWriter_0, obj2, jsonDictionaryContract_0, jsonProperty_0, jsonContainerContract_0, jsonProperty_1);
            if (jsonDictionaryContract_0.JsonContract_0 == null)
            {
                jsonDictionaryContract_0.JsonContract_0 = base.jsonSerializer_0.icontractResolver_0.ResolveContract(jsonDictionaryContract_0.DictionaryValueType ?? typeof(object));
            }
            if (jsonDictionaryContract_0.JsonContract_2 == null)
            {
                jsonDictionaryContract_0.JsonContract_2 = base.jsonSerializer_0.icontractResolver_0.ResolveContract(jsonDictionaryContract_0.DictionaryKeyType ?? typeof(object));
            }
            int top = jsonWriter_0.Top;

            foreach (DictionaryEntry entry in idictionary_0)
            {
                bool   flag;
                string arg = this.method_35(jsonWriter_0, entry, jsonDictionaryContract_0.JsonContract_2, out flag);
                arg = (jsonDictionaryContract_0.PropertyNameResolver != null) ? jsonDictionaryContract_0.PropertyNameResolver(arg) : arg;
                try
                {
                    object       obj3     = entry.Value;
                    JsonContract contract = jsonDictionaryContract_0.JsonContract_1 ?? this.method_5(obj3);
                    if (this.method_9(obj3, null, contract, jsonDictionaryContract_0, jsonProperty_0))
                    {
                        jsonWriter_0.WritePropertyName(arg, flag);
                        this.method_12(jsonWriter_0, obj3);
                    }
                    else if (this.method_11(jsonWriter_0, obj3, null, contract, jsonDictionaryContract_0, jsonProperty_0))
                    {
                        jsonWriter_0.WritePropertyName(arg, flag);
                        this.method_7(jsonWriter_0, obj3, contract, null, jsonDictionaryContract_0, jsonProperty_0);
                    }
                }
                catch (Exception exception)
                {
                    if (!base.method_2(obj2, jsonDictionaryContract_0, arg, null, jsonWriter_0.String_0, exception))
                    {
                        throw;
                    }
                    this.method_36(jsonWriter_0, top);
                }
            }
            jsonWriter_0.WriteEndObject();
            this.list_0.RemoveAt(this.list_0.Count - 1);
            this.method_16(jsonWriter_0, jsonDictionaryContract_0, obj2);
        }
        private ApiType createDictionaryType(JsonDictionaryContract dictionaryContract, Queue <Type> typeReferences)
        {
            var keyType   = dictionaryContract.DictionaryKeyType ?? typeof(object);
            var valueType = dictionaryContract.DictionaryValueType ?? typeof(object);

            return(new ApiType
            {
                Name = "Object",
                AdditionalTypes = new[]
                {
                    createType(keyType, typeReferences),
                    createType(valueType, typeReferences)
                },
            });
        }
 public static JsonDictionaryContract Copy(this JsonDictionaryContract source) =>
 new JsonDictionaryContract(source.UnderlyingType)
 {
     Converter                 = source.Converter,
     CreatedType               = source.CreatedType,
     DefaultCreator            = source.DefaultCreator,
     DefaultCreatorNonPublic   = source.DefaultCreatorNonPublic,
     IsReference               = source.IsReference,
     ItemConverter             = source.ItemConverter,
     ItemIsReference           = source.ItemIsReference,
     ItemReferenceLoopHandling = source.ItemReferenceLoopHandling,
     ItemTypeNameHandling      = source.ItemTypeNameHandling,
     DictionaryKeyResolver     = source.DictionaryKeyResolver,
     HasParameterizedCreator   = source.HasParameterizedCreator,
     OverrideCreator           = source.OverrideCreator
 };
Esempio n. 24
0
        private Schema CreateInlineSchema(Type type)
        {
            if (this._customSchemaMappings.ContainsKey(type))
            {
                return(this._customSchemaMappings[type]());
            }
            JsonContract jsonContract = this._contractResolver.ResolveContract(type);

            if (jsonContract is JsonPrimitiveContract)
            {
                return(this.CreatePrimitiveSchema((JsonPrimitiveContract)jsonContract));
            }
            JsonDictionaryContract jsonDictionaryContract = jsonContract as JsonDictionaryContract;

            if (jsonDictionaryContract != null)
            {
                if (!jsonDictionaryContract.IsSelfReferencing())
                {
                    return(this.CreateDictionarySchema(jsonDictionaryContract));
                }
                return(this.CreateRefSchema(type));
            }
            else
            {
                JsonArrayContract jsonArrayContract = jsonContract as JsonArrayContract;
                if (jsonArrayContract != null)
                {
                    if (!jsonArrayContract.IsSelfReferencing())
                    {
                        return(this.CreateArraySchema(jsonArrayContract));
                    }
                    return(this.CreateRefSchema(type));
                }
                else
                {
                    JsonObjectContract jsonObjectContract = jsonContract as JsonObjectContract;
                    if (jsonObjectContract != null && jsonObjectContract.IsInferrable())
                    {
                        return(this.CreateRefSchema(type));
                    }
                    return(this.CreateRefSchema(typeof(object)));
                }
            }
        }
Esempio n. 25
0
        protected override JsonDictionaryContract CreateDictionaryContract(Type type)
        {
            // We need to preserve case for keys in the PropertiesCollection and optionally use integer values for enum keys
            JsonDictionaryContract contract = base.CreateDictionaryContract(type);

            Type    keyType   = contract.DictionaryKeyType;
            Boolean isEnumKey = keyType != null ? keyType.IsEnum : false;

            if (isEnumKey)
            {
                contract.DictionaryKeyResolver = (name) => ((int)Enum.Parse(keyType, name)).ToString();
            }
            else
            {
                contract.DictionaryKeyResolver = (name) => name;
            }

            return(contract);
        }
Esempio n. 26
0
        private Schema CreateInlineSchema(Type type)
        {
            JsonContract jsonContract = this._contractResolver.ResolveContract(type);

            if (this._customSchemaMappings.ContainsKey(type))
            {
                return(this.FilterSchema(this._customSchemaMappings[type](), jsonContract));
            }
            if (jsonContract is JsonPrimitiveContract)
            {
                return(this.FilterSchema(this.CreatePrimitiveSchema((JsonPrimitiveContract)jsonContract), jsonContract));
            }
            JsonDictionaryContract dictionaryContract = jsonContract as JsonDictionaryContract;

            if (dictionaryContract != null)
            {
                if (!dictionaryContract.IsSelfReferencing())
                {
                    return(this.FilterSchema(this.CreateDictionarySchema(dictionaryContract), jsonContract));
                }
                return(this.CreateRefSchema(type));
            }
            JsonArrayContract arrayContract = jsonContract as JsonArrayContract;

            if (arrayContract != null)
            {
                if (!arrayContract.IsSelfReferencing())
                {
                    return(this.FilterSchema(this.CreateArraySchema(arrayContract), jsonContract));
                }
                return(this.CreateRefSchema(type));
            }
            JsonObjectContract objectContract = jsonContract as JsonObjectContract;

            if (objectContract != null && !objectContract.IsAmbiguous())
            {
                return(this.CreateRefSchema(type));
            }
            return(this.FilterSchema(new Schema()
            {
                type = "object"
            }, jsonContract));
        }
    string ResolveDictionaryKey(JsonDictionaryContract contract, string value)
    {
        var keyType = contract.DictionaryKeyType;

        if (keyType == typeof(Guid))
        {
            if (scrubber.TryParseConvertGuid(value, out var result))
            {
                return(result);
            }
        }

        if (keyType == typeof(DateTimeOffset))
        {
            if (scrubber.TryParseConvertDateTimeOffset(value, out var result))
            {
                return(result);
            }
        }

        if (keyType == typeof(DateTime))
        {
            if (scrubber.TryParseConvertDateTime(value, out var result))
            {
                return(result);
            }
        }

        if (keyType == typeof(Type))
        {
            var type = Type.GetType(value);
            if (type == null)
            {
                throw new($"Could not load type `{value}`.");
            }

            return(TypeNameConverter.GetName(type));
        }

        return(value);
    }
Esempio n. 28
0
        private Schema CreateDictionarySchema(string paramName, JsonDictionaryContract dictionaryContract,
                                              Queue <Type> referencedTypes)
        {
            var keyType   = dictionaryContract.DictionaryKeyType ?? typeof(object);
            var valueType = dictionaryContract.DictionaryValueType ?? typeof(object);

            if (keyType.GetTypeInfo().IsEnum)
            {
                return(new Schema
                {
                    Type = "object",
                    Properties = Enum.GetNames(keyType).ToDictionary(
                        name => dictionaryContract.DictionaryKeyResolver(name),
                        name => CreateSchema(valueType, referencedTypes)
                        )
                });
            }

            if (!string.IsNullOrEmpty(paramName))
            {
                return(new Schema
                {
                    Type = "object",
                    Properties = new Dictionary <string, Schema>
                    {
                        {
                            paramName,
                            CreateSchema(valueType, referencedTypes)
                        }
                    }
                });
            }

            return(new Schema
            {
                Type = "object",
                AdditionalProperties = CreateSchema(valueType, referencedTypes)
            });
        }
Esempio n. 29
0
        private Schema CreateDictionarySchema(
            JsonDictionaryContract dictionaryContract,
            Queue <Type> referencedTypes)
        {
            Type type1 = dictionaryContract.DictionaryKeyType;

            if ((object)type1 == null)
            {
                type1 = typeof(object);
            }
            Type type2 = type1;
            Type type3 = dictionaryContract.DictionaryValueType;

            if ((object)type3 == null)
            {
                type3 = typeof(object);
            }
            Type valueType = type3;

            if (type2.GetTypeInfo().IsEnum)
            {
                return new Schema()
                       {
                           Type       = "object",
                           Properties =
                               (IDictionary <string, Schema>)((IEnumerable <string>)Enum.GetNames(type2))
                               .ToDictionary <string, string, Schema>(
                                   (Func <string, string>)(name => dictionaryContract.DictionaryKeyResolver(name)),
                                   (Func <string, Schema>)(name => this.CreateSchema(valueType, referencedTypes)))
                       }
            }
            ;
            return(new Schema()
            {
                Type = "object",
                AdditionalProperties = this.CreateSchema(valueType, referencedTypes)
            });
        }
Esempio n. 30
0
 internal static bool IsSelfReferencing(this JsonDictionaryContract dictionaryContract)
 {
     return(dictionaryContract.UnderlyingType == dictionaryContract.DictionaryValueType);
 }
        private void SerializeDictionary(JsonWriter writer, IDictionary values, JsonDictionaryContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
        {
            IWrappedDictionary wrappedDictionary = values as IWrappedDictionary;
            object underlyingDictionary = wrappedDictionary != null ? wrappedDictionary.UnderlyingDictionary : values;

            OnSerializing(writer, contract, underlyingDictionary);
            _serializeStack.Add(underlyingDictionary);

            WriteObjectStart(writer, underlyingDictionary, contract, member, collectionContract, containerProperty);

            if (contract.ItemContract == null)
                contract.ItemContract = Serializer._contractResolver.ResolveContract(contract.DictionaryValueType ?? typeof(object));

            if (contract.KeyContract == null)
                contract.KeyContract = Serializer._contractResolver.ResolveContract(contract.DictionaryKeyType ?? typeof(object));

            int initialDepth = writer.Top;

            foreach (DictionaryEntry entry in values)
            {
                bool escape;
                string propertyName = GetPropertyName(writer, entry.Key, contract.KeyContract, out escape);

                propertyName = (contract.PropertyNameResolver != null)
                    ? contract.PropertyNameResolver(propertyName)
                    : propertyName;

                try
                {
                    object value = entry.Value;
                    JsonContract valueContract = contract.FinalItemContract ?? GetContractSafe(value);

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

                        writer.WritePropertyName(propertyName, escape);

                        SerializeValue(writer, value, valueContract, null, contract, member);
                    }
                }
                catch (Exception ex)
                {
                    if (IsErrorHandled(underlyingDictionary, contract, propertyName, null, writer.ContainerPath, ex))
                        HandleError(writer, initialDepth);
                    else
                        throw;
                }
            }

            writer.WriteEndObject();

            _serializeStack.RemoveAt(_serializeStack.Count - 1);

            OnSerialized(writer, contract, underlyingDictionary);
        }
        private object PopulateDictionary(IDictionary dictionary, JsonReader reader, JsonDictionaryContract contract, JsonProperty containerProperty, string id)
        {
            IWrappedDictionary wrappedDictionary = dictionary as IWrappedDictionary;
            object underlyingDictionary = wrappedDictionary != null ? wrappedDictionary.UnderlyingDictionary : dictionary;

            if (id != null)
                AddReference(reader, id, underlyingDictionary);

            OnDeserializing(reader, contract, underlyingDictionary);

            int initialDepth = reader.Depth;

            if (contract.KeyContract == null)
                contract.KeyContract = GetContractSafe(contract.DictionaryKeyType);

            if (contract.ItemContract == null)
                contract.ItemContract = GetContractSafe(contract.DictionaryValueType);

            JsonConverter dictionaryValueConverter = contract.ItemConverter ?? GetConverter(contract.ItemContract, null, contract, containerProperty);
            PrimitiveTypeCode keyTypeCode = (contract.KeyContract is JsonPrimitiveContract) ? ((JsonPrimitiveContract)contract.KeyContract).TypeCode : PrimitiveTypeCode.Empty;

            bool finished = false;
            do
            {
                switch (reader.TokenType)
                {
                    case JsonToken.PropertyName:
                        object keyValue = reader.Value;
                        if (CheckPropertyName(reader, keyValue.ToString()))
                            continue;

                        try
                        {
                            try
                            {
                                DateParseHandling dateParseHandling;
                                switch (keyTypeCode)
                                {
                                    case PrimitiveTypeCode.DateTime:
                                    case PrimitiveTypeCode.DateTimeNullable:
                                        dateParseHandling = DateParseHandling.DateTime;
                                        break;
#if !NET20
                                    case PrimitiveTypeCode.DateTimeOffset:
                                    case PrimitiveTypeCode.DateTimeOffsetNullable:
                                        dateParseHandling = DateParseHandling.DateTimeOffset;
                                        break;
#endif
                                    default:
                                        dateParseHandling = DateParseHandling.None;
                                        break;
                                }

                                // this is for correctly reading ISO and MS formatted dictionary keys
                                object dt;
                                if (dateParseHandling != DateParseHandling.None && DateTimeUtils.TryParseDateTime(keyValue.ToString(), dateParseHandling, reader.DateTimeZoneHandling, reader.DateFormatString, reader.Culture, out dt))
                                    keyValue = dt;
                                else
                                    keyValue = EnsureType(reader, keyValue, CultureInfo.InvariantCulture, contract.KeyContract, contract.DictionaryKeyType);
                            }
                            catch (Exception ex)
                            {
                                throw JsonSerializationException.Create(reader, "Could not convert string '{0}' to dictionary key type '{1}'. Create a TypeConverter to convert from the string to the key type object.".FormatWith(CultureInfo.InvariantCulture, reader.Value, contract.DictionaryKeyType), ex);
                            }

                            if (!ReadForType(reader, contract.ItemContract, dictionaryValueConverter != null))
                                throw JsonSerializationException.Create(reader, "Unexpected end when deserializing object.");

                            object itemValue;
                            if (dictionaryValueConverter != null && dictionaryValueConverter.CanRead)
                                itemValue = DeserializeConvertable(dictionaryValueConverter, reader, contract.DictionaryValueType, null);
                            else
                                itemValue = CreateValueInternal(reader, contract.DictionaryValueType, contract.ItemContract, null, contract, containerProperty, null);

                            dictionary[keyValue] = itemValue;
                        }
                        catch (Exception ex)
                        {
                            if (IsErrorHandled(underlyingDictionary, contract, keyValue, reader as IJsonLineInfo, reader.Path, ex))
                                HandleError(reader, true, initialDepth);
                            else
                                throw;
                        }
                        break;
                    case JsonToken.Comment:
                        break;
                    case JsonToken.EndObject:
                        finished = true;
                        break;
                    default:
                        throw JsonSerializationException.Create(reader, "Unexpected token when deserializing object: " + reader.TokenType);
                }
            } while (!finished && reader.Read());

            if (!finished)
                ThrowUnexpectedEndException(reader, contract, underlyingDictionary, "Unexpected end when deserializing object.");

            OnDeserialized(reader, contract, underlyingDictionary);
            return underlyingDictionary;
        }
        private IDictionary CreateNewDictionary(JsonReader reader, JsonDictionaryContract contract, out bool createdFromNonDefaultCreator)
        {
            if (contract.IsReadOnlyOrFixedSize)
            {
                createdFromNonDefaultCreator = true;
                return contract.CreateTemporaryDictionary();
            }
            else if (contract.DefaultCreator != null && (!contract.DefaultCreatorNonPublic || Serializer._constructorHandling == ConstructorHandling.AllowNonPublicDefaultConstructor))
            {
                object dictionary = contract.DefaultCreator();

                if (contract.ShouldCreateWrapper)
                    dictionary = contract.CreateWrapper(dictionary);

                createdFromNonDefaultCreator = false;
                return (IDictionary)dictionary;
            }
            else if (contract.HasParametrizedCreator)
            {
                createdFromNonDefaultCreator = true;
                return contract.CreateTemporaryDictionary();
            }
            else
            {
                if (!contract.IsInstantiable)
                    throw JsonSerializationException.Create(reader, "Could not create an instance of type {0}. Type is an interface or abstract class and cannot be instantiated.".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType));

                throw JsonSerializationException.Create(reader, "Unable to find a default constructor to use for type {0}.".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType));
            }
        }