private object PopulateDictionary(IWrappedDictionary dictionary, JsonReader reader, JsonDictionaryContract contract, string id) { if (id != null) Serializer.ReferenceResolver.AddReference(this, id, dictionary.UnderlyingDictionary); contract.InvokeOnDeserializing(dictionary.UnderlyingDictionary, Serializer.Context); int initialDepth = reader.Depth; do { switch (reader.TokenType) { case JsonToken.PropertyName: object keyValue = reader.Value; try { try { keyValue = EnsureType(keyValue, CultureInfo.InvariantCulture, contract.DictionaryKeyType); } catch (Exception ex) { throw new JsonSerializationException("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.DictionaryValueType, null)) throw new JsonSerializationException("Unexpected end when deserializing object."); dictionary[keyValue] = CreateValueNonProperty(reader, contract.DictionaryValueType, GetContractSafe(contract.DictionaryValueType)); } catch (Exception ex) { if (IsErrorHandled(dictionary, contract, keyValue, ex)) HandleError(reader, initialDepth); else throw; } break; case JsonToken.Comment: break; case JsonToken.EndObject: contract.InvokeOnDeserialized(dictionary.UnderlyingDictionary, Serializer.Context); return dictionary.UnderlyingDictionary; default: throw new JsonSerializationException("Unexpected token when deserializing object: " + reader.TokenType); } } while (reader.Read()); throw new JsonSerializationException("Unexpected end when deserializing object."); }