-
Notifications
You must be signed in to change notification settings - Fork 5k
Option to disallow duplicate JSON properties #115856
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
PranavSenthilnathan
wants to merge
7
commits into
dotnet:main
Choose a base branch
from
PranavSenthilnathan:json-no-duplicate-prop
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,596
−118
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7946802
Add AllowDuplicateProperties
PranavSenthilnathan 337d9b9
Support dictionary property dedup
PranavSenthilnathan e6487c2
Consolidate bit arrays, fix tests
PranavSenthilnathan 768b743
Add more tests
PranavSenthilnathan 96cee10
Move validation to end of parse
PranavSenthilnathan cefb57b
cleanup
PranavSenthilnathan 4823ec2
Deep duplicate validation
PranavSenthilnathan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,7 +46,7 @@ public sealed partial class JsonDocument | |
/// </exception> | ||
public static JsonDocument Parse(ReadOnlyMemory<byte> utf8Json, JsonDocumentOptions options = default) | ||
{ | ||
return Parse(utf8Json, options.GetReaderOptions()); | ||
return Parse(utf8Json, options.GetReaderOptions(), allowDuplicateProperties: options.AllowDuplicateProperties); | ||
} | ||
|
||
/// <summary> | ||
|
@@ -80,7 +80,7 @@ public static JsonDocument Parse(ReadOnlySequence<byte> utf8Json, JsonDocumentOp | |
|
||
if (utf8Json.IsSingleSegment) | ||
{ | ||
return Parse(utf8Json.First, readerOptions); | ||
return Parse(utf8Json.First, readerOptions, allowDuplicateProperties: options.AllowDuplicateProperties); | ||
} | ||
|
||
int length = checked((int)utf8Json.Length); | ||
|
@@ -89,7 +89,11 @@ public static JsonDocument Parse(ReadOnlySequence<byte> utf8Json, JsonDocumentOp | |
try | ||
{ | ||
utf8Json.CopyTo(utf8Bytes.AsSpan()); | ||
return Parse(utf8Bytes.AsMemory(0, length), readerOptions, utf8Bytes); | ||
return Parse( | ||
utf8Bytes.AsMemory(0, length), | ||
readerOptions, | ||
utf8Bytes, | ||
allowDuplicateProperties: options.AllowDuplicateProperties); | ||
} | ||
catch | ||
{ | ||
|
@@ -123,7 +127,7 @@ public static JsonDocument Parse(Stream utf8Json, JsonDocumentOptions options = | |
Debug.Assert(drained.Array != null); | ||
try | ||
{ | ||
return Parse(drained.AsMemory(), options.GetReaderOptions(), drained.Array); | ||
return Parse(drained.AsMemory(), options.GetReaderOptions(), drained.Array, allowDuplicateProperties: options.AllowDuplicateProperties); | ||
} | ||
catch | ||
{ | ||
|
@@ -140,7 +144,8 @@ internal static JsonDocument ParseRented(PooledByteBufferWriter utf8Json, JsonDo | |
utf8Json.WrittenMemory, | ||
options.GetReaderOptions(), | ||
extraRentedArrayPoolBytes: null, | ||
extraPooledByteBufferWriter: utf8Json); | ||
extraPooledByteBufferWriter: utf8Json, | ||
allowDuplicateProperties: options.AllowDuplicateProperties); | ||
} | ||
|
||
internal static JsonDocument ParseValue(Stream utf8Json, JsonDocumentOptions options) | ||
|
@@ -157,15 +162,21 @@ internal static JsonDocument ParseValue(Stream utf8Json, JsonDocumentOptions opt | |
drained.AsSpan().Clear(); | ||
ArrayPool<byte>.Shared.Return(drained.Array); | ||
|
||
return ParseUnrented(owned.AsMemory(), options.GetReaderOptions()); | ||
return ParseUnrented( | ||
owned.AsMemory(), | ||
options.GetReaderOptions(), | ||
allowDuplicateProperties: options.AllowDuplicateProperties); | ||
} | ||
|
||
internal static JsonDocument ParseValue(ReadOnlySpan<byte> utf8Json, JsonDocumentOptions options) | ||
{ | ||
byte[] owned = new byte[utf8Json.Length]; | ||
utf8Json.CopyTo(owned); | ||
|
||
return ParseUnrented(owned.AsMemory(), options.GetReaderOptions()); | ||
return ParseUnrented( | ||
owned.AsMemory(), | ||
options.GetReaderOptions(), | ||
allowDuplicateProperties: options.AllowDuplicateProperties); | ||
} | ||
|
||
internal static JsonDocument ParseValue(string json, JsonDocumentOptions options) | ||
|
@@ -209,7 +220,11 @@ private static async Task<JsonDocument> ParseAsyncCore( | |
Debug.Assert(drained.Array != null); | ||
try | ||
{ | ||
return Parse(drained.AsMemory(), options.GetReaderOptions(), drained.Array); | ||
return Parse( | ||
drained.AsMemory(), | ||
options.GetReaderOptions(), | ||
drained.Array, | ||
allowDuplicateProperties: options.AllowDuplicateProperties); | ||
} | ||
catch | ||
{ | ||
|
@@ -235,7 +250,10 @@ internal static async Task<JsonDocument> ParseAsyncCoreUnrented( | |
drained.AsSpan().Clear(); | ||
ArrayPool<byte>.Shared.Return(drained.Array); | ||
|
||
return ParseUnrented(owned.AsMemory(), options.GetReaderOptions()); | ||
return ParseUnrented( | ||
owned.AsMemory(), | ||
options.GetReaderOptions(), | ||
allowDuplicateProperties: options.AllowDuplicateProperties); | ||
} | ||
|
||
/// <summary> | ||
|
@@ -271,7 +289,8 @@ public static JsonDocument Parse([StringSyntax(StringSyntaxAttribute.Json)] Read | |
return Parse( | ||
utf8Bytes.AsMemory(0, actualByteCount), | ||
options.GetReaderOptions(), | ||
utf8Bytes); | ||
utf8Bytes, | ||
allowDuplicateProperties: options.AllowDuplicateProperties); | ||
} | ||
catch | ||
{ | ||
|
@@ -304,7 +323,10 @@ internal static JsonDocument ParseValue(ReadOnlyMemory<char> json, JsonDocumentO | |
ArrayPool<byte>.Shared.Return(utf8Bytes); | ||
} | ||
|
||
return ParseUnrented(owned.AsMemory(), options.GetReaderOptions()); | ||
return ParseUnrented( | ||
owned.AsMemory(), | ||
options.GetReaderOptions(), | ||
allowDuplicateProperties: options.AllowDuplicateProperties); | ||
} | ||
|
||
/// <summary> | ||
|
@@ -406,9 +428,12 @@ public static bool TryParseValue(ref Utf8JsonReader reader, [NotNullWhen(true)] | |
/// <exception cref="JsonException"> | ||
/// A value could not be read from the reader. | ||
/// </exception> | ||
public static JsonDocument ParseValue(ref Utf8JsonReader reader) | ||
public static JsonDocument ParseValue(ref Utf8JsonReader reader) => | ||
ParseValue(ref reader, allowDuplicateProperties: true); | ||
|
||
internal static JsonDocument ParseValue(ref Utf8JsonReader reader, bool allowDuplicateProperties) | ||
{ | ||
bool ret = TryParseValue(ref reader, out JsonDocument? document, shouldThrow: true, useArrayPools: true); | ||
bool ret = TryParseValue(ref reader, out JsonDocument? document, shouldThrow: true, useArrayPools: true, allowDuplicateProperties); | ||
|
||
Debug.Assert(ret, "TryParseValue returned false with shouldThrow: true."); | ||
Debug.Assert(document != null, "null document returned with shouldThrow: true."); | ||
|
@@ -419,7 +444,8 @@ internal static bool TryParseValue( | |
ref Utf8JsonReader reader, | ||
[NotNullWhen(true)] out JsonDocument? document, | ||
bool shouldThrow, | ||
bool useArrayPools) | ||
bool useArrayPools, | ||
bool allowDuplicateProperties = true) | ||
{ | ||
JsonReaderState state = reader.CurrentState; | ||
CheckSupportedOptions(state.Options, nameof(reader)); | ||
|
@@ -629,7 +655,7 @@ internal static bool TryParseValue( | |
valueSpan.CopyTo(rentedSpan); | ||
} | ||
|
||
document = Parse(rented.AsMemory(0, length), state.Options, rented); | ||
document = Parse(rented.AsMemory(0, length), state.Options, rented, allowDuplicateProperties: allowDuplicateProperties); | ||
} | ||
catch | ||
{ | ||
|
@@ -654,7 +680,7 @@ internal static bool TryParseValue( | |
owned = valueSpan.ToArray(); | ||
} | ||
|
||
document = ParseUnrented(owned, state.Options, reader.TokenType); | ||
document = ParseUnrented(owned, state.Options, reader.TokenType, allowDuplicateProperties: allowDuplicateProperties); | ||
} | ||
|
||
return true; | ||
|
@@ -688,7 +714,8 @@ private static JsonDocument Parse( | |
ReadOnlyMemory<byte> utf8Json, | ||
JsonReaderOptions readerOptions, | ||
byte[]? extraRentedArrayPoolBytes = null, | ||
PooledByteBufferWriter? extraPooledByteBufferWriter = null) | ||
PooledByteBufferWriter? extraPooledByteBufferWriter = null, | ||
bool allowDuplicateProperties = true) | ||
{ | ||
ReadOnlySpan<byte> utf8JsonSpan = utf8Json.Span; | ||
var database = MetadataDb.CreateRented(utf8Json.Length, convertToAlloc: false); | ||
|
@@ -708,13 +735,16 @@ private static JsonDocument Parse( | |
stack.Dispose(); | ||
} | ||
|
||
return new JsonDocument(utf8Json, database, extraRentedArrayPoolBytes, extraPooledByteBufferWriter); | ||
JsonDocument document = new JsonDocument(utf8Json, database, extraRentedArrayPoolBytes, extraPooledByteBufferWriter); | ||
ValidateDocument(document, allowDuplicateProperties); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens if |
||
return document; | ||
} | ||
|
||
private static JsonDocument ParseUnrented( | ||
ReadOnlyMemory<byte> utf8Json, | ||
JsonReaderOptions readerOptions, | ||
JsonTokenType tokenType = JsonTokenType.None) | ||
JsonTokenType tokenType = JsonTokenType.None, | ||
bool allowDuplicateProperties = true) | ||
{ | ||
// These tokens should already have been processed. | ||
Debug.Assert( | ||
|
@@ -746,7 +776,9 @@ private static JsonDocument ParseUnrented( | |
} | ||
} | ||
|
||
return new JsonDocument(utf8Json, database, isDisposable: false); | ||
JsonDocument document = new JsonDocument(utf8Json, database, isDisposable: false); | ||
ValidateDocument(document, allowDuplicateProperties); | ||
return document; | ||
} | ||
|
||
private static ArraySegment<byte> ReadToEnd(Stream stream) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given that we've settled on a design where we don't just throw on duplicate JSON properties in the strictly syntactic sense (case insensitivity, dictionary key conflicts) I'm starting to wonder if the term "duplicate" is fit for purpose. Is the term "conflicting property" more appropriate perhaps?