Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,10 @@ internal static JsonObject ReadAsJsonNode(ref Utf8JsonReader reader, JsonNodeOpt
reader.Read(); // Move to the value token.
JsonNode? value = JsonNodeConverter.ReadAsJsonNode(ref reader, options);

// To have parity with the lazy JsonObject, we throw on duplicates.
jObject.Add(propertyName, value);
if (!jObject.TryAdd(propertyName, value))
{
ThrowHelper.ThrowJsonException_DuplicatePropertyNotAllowed(propertyName);
}
}

// JSON is invalid so reader would have already thrown.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1736,9 +1736,9 @@ private static void AssertDuplicatePropertyThrows<T>(string jsonPayload, bool is
}
else
{
AssertExtensions.ThrowsContains<ArgumentException>(
AssertExtensions.ThrowsContains<JsonException>(
() => JsonSerializer.Deserialize<T>(jsonPayload, JsonTestSerializerOptions.DisallowDuplicateProperties),
"An item with the same key has already been added.");
"Duplicate property");

// Default options don't throw on deserialize but will throw when accessed
T node = JsonSerializer.Deserialize<T>(jsonPayload);
Expand All @@ -1756,14 +1756,14 @@ public static void JsonObject_DuplicatePropertyCaseInsensitiveThrows()
string jsonPayload = """{"a":1,"A":2}""";

_ = JsonSerializer.Deserialize<JsonObject>(jsonPayload); // Assert no throw
AssertExtensions.ThrowsContains<ArgumentException>(
AssertExtensions.ThrowsContains<JsonException>(
() => JsonSerializer.Deserialize<JsonObject>(jsonPayload, options),
"An item with the same key has already been added.");
"Duplicate property");

_ = JsonSerializer.Deserialize<JsonNode>(jsonPayload); // Assert no throw
AssertExtensions.ThrowsContains<ArgumentException>(
AssertExtensions.ThrowsContains<JsonException>(
() => JsonSerializer.Deserialize<JsonNode>(jsonPayload, options),
"An item with the same key has already been added.");
"Duplicate property");
}

[Fact]
Expand All @@ -1774,14 +1774,14 @@ public static void JsonObject_NestedDuplicatePropertyCaseInsensitiveThrows()
string jsonPayload = """[{"a":1,"A":2}]""";

_ = JsonSerializer.Deserialize<JsonArray>(jsonPayload); // Assert no throw
AssertExtensions.ThrowsContains<ArgumentException>(
AssertExtensions.ThrowsContains<JsonException>(
() => JsonSerializer.Deserialize<JsonArray>(jsonPayload, options),
"An item with the same key has already been added.");
"Duplicate property");

_ = JsonSerializer.Deserialize<JsonNode>(jsonPayload); // Assert no throw
AssertExtensions.ThrowsContains<ArgumentException>(
AssertExtensions.ThrowsContains<JsonException>(
() => JsonSerializer.Deserialize<JsonNode>(jsonPayload, options),
"An item with the same key has already been added.");
"Duplicate property");
}

[Theory]
Expand Down
Loading