string key = state.Current.KeyName;
Debug.Assert(!string.IsNullOrEmpty(key));
- if (!dictionary.Contains(key))
- {
- dictionary.Add(key, value);
- }
- else
- {
- ThrowHelper.ThrowJsonException_DeserializeDuplicateKey(key, reader, state.JsonPath);
- }
+ dictionary[key] = value;
}
else if (state.Current.IsIDictionaryConstructible ||
(state.Current.IsIDictionaryConstructibleProperty && !setPropertyDirectly) ||
string key = state.Current.KeyName;
Debug.Assert(!string.IsNullOrEmpty(key));
- if (!dictionary.Contains(key))
- {
- dictionary.Add(key, value);
- }
- else
- {
- ThrowHelper.ThrowJsonException_DeserializeDuplicateKey(key, reader, state.JsonPath);
- }
+ dictionary[key] = value;
}
else
{
string key = state.Current.KeyName;
Debug.Assert(!string.IsNullOrEmpty(key));
- if (!dictionary.ContainsKey(key)) // The IDictionary.TryAdd extension method is not available in netstandard.
- {
- dictionary.Add(key, value);
- }
- else
- {
- ThrowHelper.ThrowJsonException_DeserializeDuplicateKey(key, reader, state.JsonPath);
- }
+ dictionary[key] = value;
}
else if (state.Current.IsProcessingIDictionaryConstructibleOrKeyValuePair)
{
string key = state.Current.KeyName;
Debug.Assert(!string.IsNullOrEmpty(key));
- if (!dictionary.ContainsKey(key)) // The IDictionary.TryAdd extension method is not available in netstandard.
- {
- dictionary.Add(key, value);
- }
- else
- {
- ThrowHelper.ThrowJsonException_DeserializeDuplicateKey(key, reader, state.JsonPath);
- }
+ dictionary[key] = value;
}
else
{
}
[Fact]
- public static void DuplicateKeysFail()
- {
- // Non-generic IDictionary case.
- Assert.Throws<JsonException>(() => JsonSerializer.Parse<IDictionary>(
- @"{""Hello"":""World"", ""Hello"":""World""}"));
-
- // Strongly-typed IDictionary<,> case.
- Assert.Throws<JsonException>(() => JsonSerializer.Parse<Dictionary<string, string>>(
- @"{""Hello"":""World"", ""Hello"":""World""}"));
-
- // Weakly-typed IDictionary case.
- Assert.Throws<JsonException>(() => JsonSerializer.Parse<Dictionary<string, object>>(
- @"{""Hello"":null, ""Hello"":null}"));
- }
-
- [Fact]
public static void DictionaryOfObject()
{
{
}
[Fact]
+ public static void DeserializeDictionaryWithDuplicateKeys()
+ {
+ // Strongly-typed IDictionary<,> case.
+ Dictionary<string, string> deserialize = JsonSerializer.Parse<Dictionary<string, string>>(@"{""Hello"":""World"", ""Hello"":""NewValue""}");
+ Assert.Equal("NewValue", deserialize["Hello"]);
+
+ deserialize = JsonSerializer.Parse<Dictionary<string, string>>(@"{""Hello"":""World"", ""myKey"" : ""myValue"", ""Hello"":""NewValue""}");
+ Assert.Equal("NewValue", deserialize["Hello"]);
+
+ // Weakly-typed IDictionary case.
+ Dictionary<string, object> deserializeObject = JsonSerializer.Parse<Dictionary<string, object>>(@"{""Hello"":""World"", ""Hello"": null}");
+ Assert.Null(deserializeObject["Hello"]);
+ }
+
+ [Fact]
public static void ClassWithNoSetter()
{
string json = @"{""MyDictionary"":{""Key"":""Value""}}";
{
try
{
- JsonSerializer.Parse<IDictionary<string, int>>(@"{""Key"":1, ""Key"":2}");
- Assert.True(false, "Expected JsonException was not thrown.");
+ JsonSerializer.Parse<IDictionary<string, string>>(@"{""Key"":1, ""Key"":2}");
+ Assert.True(false, "We follow 'Last value wins' approach for duplicate keys.");
}
catch (JsonException e)
{
Assert.Equal(0, e.LineNumber);
- Assert.Equal(17, e.BytePositionInLine);
- Assert.Contains("LineNumber: 0 | BytePositionInLine: 17.", e.Message);
+ Assert.Equal(8, e.BytePositionInLine);
+ Assert.Contains("LineNumber: 0 | BytePositionInLine: 8.", e.Message);
Assert.Contains("$.Key", e.Path);
// Verify Path is not repeated.