-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Fix to #30604 - Implement JSON serialization/deserialization via Utf8JsonReader/Utf8JsonWriter #31160
Merged
Fix to #30604 - Implement JSON serialization/deserialization via Utf8JsonReader/Utf8JsonWriter #31160
Changes from all commits
Commits
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
8 changes: 8 additions & 0 deletions
8
src/EFCore.Relational/Properties/RelationalStrings.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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 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 |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
using System.Text.Json; | ||
using Microsoft.EntityFrameworkCore.Internal; | ||
using Microsoft.EntityFrameworkCore.Query.Internal; | ||
using Microsoft.EntityFrameworkCore.Storage.Json; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Query; | ||
|
||
|
@@ -67,8 +68,8 @@ private static readonly MethodInfo MaterializeJsonEntityMethodInfo | |
private static readonly MethodInfo MaterializeJsonEntityCollectionMethodInfo | ||
= typeof(ShaperProcessingExpressionVisitor).GetTypeInfo().GetDeclaredMethod(nameof(MaterializeJsonEntityCollection))!; | ||
|
||
private static readonly MethodInfo ExtractJsonPropertyMethodInfo | ||
= typeof(ShaperProcessingExpressionVisitor).GetTypeInfo().GetDeclaredMethod(nameof(ExtractJsonProperty))!; | ||
private static readonly MethodInfo InverseCollectionFixupMethod | ||
= typeof(ShaperProcessingExpressionVisitor).GetTypeInfo().GetDeclaredMethod(nameof(InverseCollectionFixup))!; | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
private static TValue ThrowReadValueException<TValue>( | ||
|
@@ -123,13 +124,6 @@ private static TValue ThrowExtractJsonPropertyException<TValue>( | |
exception); | ||
} | ||
|
||
private static T? ExtractJsonProperty<T>(JsonElement element, string propertyName, bool nullable) | ||
=> nullable | ||
? element.TryGetProperty(propertyName, out var jsonValue) | ||
? jsonValue.Deserialize<T>() | ||
: default | ||
: element.GetProperty(propertyName).Deserialize<T>(); | ||
|
||
private static void IncludeReference<TEntity, TIncludingEntity, TIncludedEntity>( | ||
QueryContext queryContext, | ||
TEntity entity, | ||
|
@@ -869,105 +863,189 @@ static async Task<RelationalDataReader> InitializeReaderAsync( | |
dataReaderContext.HasNext = false; | ||
} | ||
|
||
private static void IncludeJsonEntityReference<TIncludingEntity, TIncludedEntity>( | ||
private static TEntity? MaterializeJsonEntity<TEntity>( | ||
QueryContext queryContext, | ||
JsonElement? jsonElement, | ||
object[] keyPropertyValues, | ||
TIncludingEntity entity, | ||
Func<QueryContext, object[], JsonElement, TIncludedEntity> innerShaper, | ||
Action<TIncludingEntity, TIncludedEntity> fixup) | ||
where TIncludingEntity : class | ||
where TIncludedEntity : class | ||
JsonReaderData? jsonReaderData, | ||
bool nullable, | ||
Func<QueryContext, object[], JsonReaderData, TEntity> shaper) | ||
where TEntity : class | ||
{ | ||
if (jsonElement.HasValue && jsonElement.Value.ValueKind != JsonValueKind.Null) | ||
if (jsonReaderData == null) | ||
maumar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
var included = innerShaper(queryContext, keyPropertyValues, jsonElement.Value); | ||
fixup(entity, included); | ||
return nullable | ||
? null | ||
: throw new InvalidOperationException( | ||
RelationalStrings.JsonRequiredEntityWithNullJson(typeof(TEntity).Name)); | ||
} | ||
|
||
var manager = new Utf8JsonReaderManager(jsonReaderData); | ||
var tokenType = manager.CurrentReader.TokenType; | ||
|
||
if (tokenType == JsonTokenType.Null) | ||
{ | ||
return nullable | ||
? null | ||
: throw new InvalidOperationException( | ||
RelationalStrings.JsonRequiredEntityWithNullJson(typeof(TEntity).Name)); | ||
} | ||
|
||
if (tokenType != JsonTokenType.StartObject) | ||
{ | ||
throw new InvalidOperationException( | ||
RelationalStrings.JsonReaderInvalidTokenType(tokenType.ToString())); | ||
} | ||
|
||
manager.CaptureState(); | ||
var result = shaper(queryContext, keyPropertyValues, jsonReaderData); | ||
|
||
return result; | ||
} | ||
|
||
private static void IncludeJsonEntityCollection<TIncludingEntity, TIncludedCollectionElement>( | ||
private static TResult? MaterializeJsonEntityCollection<TEntity, TResult>( | ||
QueryContext queryContext, | ||
JsonElement? jsonElement, | ||
object[] keyPropertyValues, | ||
TIncludingEntity entity, | ||
Func<QueryContext, object[], JsonElement, TIncludedCollectionElement> innerShaper, | ||
Action<TIncludingEntity, TIncludedCollectionElement> fixup) | ||
where TIncludingEntity : class | ||
where TIncludedCollectionElement : class | ||
JsonReaderData? jsonReaderData, | ||
INavigationBase navigation, | ||
Func<QueryContext, object[], JsonReaderData, TEntity> innerShaper) | ||
where TEntity : class | ||
where TResult : ICollection<TEntity> | ||
{ | ||
if (jsonElement.HasValue && jsonElement.Value.ValueKind != JsonValueKind.Null) | ||
if (jsonReaderData == null) | ||
{ | ||
var newKeyPropertyValues = new object[keyPropertyValues.Length + 1]; | ||
Array.Copy(keyPropertyValues, newKeyPropertyValues, keyPropertyValues.Length); | ||
return default; | ||
} | ||
|
||
var manager = new Utf8JsonReaderManager(jsonReaderData); | ||
var tokenType = manager.CurrentReader.TokenType; | ||
|
||
if (tokenType == JsonTokenType.Null) | ||
{ | ||
return default; | ||
} | ||
|
||
var i = 0; | ||
foreach (var jsonArrayElement in jsonElement.Value.EnumerateArray()) | ||
if (tokenType != JsonTokenType.StartArray) | ||
{ | ||
throw new InvalidOperationException( | ||
RelationalStrings.JsonReaderInvalidTokenType(tokenType.ToString())); | ||
} | ||
|
||
var collectionAccessor = navigation.GetCollectionAccessor(); | ||
var result = (TResult)collectionAccessor!.Create(); | ||
|
||
var newKeyPropertyValues = new object[keyPropertyValues.Length + 1]; | ||
Array.Copy(keyPropertyValues, newKeyPropertyValues, keyPropertyValues.Length); | ||
|
||
tokenType = manager.MoveNext(); | ||
|
||
var i = 0; | ||
while (tokenType != JsonTokenType.EndArray) | ||
{ | ||
newKeyPropertyValues[^1] = ++i; | ||
|
||
if (tokenType == JsonTokenType.StartObject) | ||
{ | ||
newKeyPropertyValues[^1] = ++i; | ||
manager.CaptureState(); | ||
var entity = innerShaper(queryContext, newKeyPropertyValues, jsonReaderData); | ||
result.Add(entity); | ||
manager = new Utf8JsonReaderManager(manager.Data); | ||
|
||
var resultElement = innerShaper(queryContext, newKeyPropertyValues, jsonArrayElement); | ||
if (manager.CurrentReader.TokenType != JsonTokenType.EndObject) | ||
{ | ||
throw new InvalidOperationException( | ||
RelationalStrings.JsonReaderInvalidTokenType(tokenType.ToString())); | ||
} | ||
|
||
fixup(entity, resultElement); | ||
tokenType = manager.MoveNext(); | ||
} | ||
} | ||
|
||
manager.CaptureState(); | ||
|
||
return result; | ||
} | ||
|
||
private static TEntity? MaterializeJsonEntity<TEntity>( | ||
private static void IncludeJsonEntityReference<TIncludingEntity, TIncludedEntity>( | ||
QueryContext queryContext, | ||
JsonElement? jsonElement, | ||
object[] keyPropertyValues, | ||
bool nullable, | ||
Func<QueryContext, object[], JsonElement, TEntity> shaper) | ||
where TEntity : class | ||
JsonReaderData? jsonReaderData, | ||
TIncludingEntity entity, | ||
Func<QueryContext, object[], JsonReaderData, TIncludedEntity> innerShaper, | ||
Action<TIncludingEntity, TIncludedEntity> fixup, | ||
bool trackingQuery) | ||
where TIncludingEntity : class | ||
where TIncludedEntity : class | ||
{ | ||
if (jsonElement.HasValue && jsonElement.Value.ValueKind != JsonValueKind.Null) | ||
if (jsonReaderData == null) | ||
{ | ||
var result = shaper(queryContext, keyPropertyValues, jsonElement.Value); | ||
|
||
return result; | ||
return; | ||
} | ||
|
||
if (nullable) | ||
var included = innerShaper(queryContext, keyPropertyValues, jsonReaderData); | ||
|
||
if (!trackingQuery) | ||
{ | ||
return default; | ||
fixup(entity, included); | ||
} | ||
|
||
throw new InvalidOperationException( | ||
RelationalStrings.JsonRequiredEntityWithNullJson(typeof(TEntity).Name)); | ||
} | ||
|
||
private static TResult? MaterializeJsonEntityCollection<TEntity, TResult>( | ||
private static void IncludeJsonEntityCollection<TIncludingEntity, TIncludedCollectionElement>( | ||
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. This can be refactored to share implementation with MaterializeJsonEntityCollection |
||
QueryContext queryContext, | ||
JsonElement? jsonElement, | ||
object[] keyPropertyValues, | ||
INavigationBase navigation, | ||
Func<QueryContext, object[], JsonElement, TEntity> innerShaper) | ||
where TEntity : class | ||
where TResult : ICollection<TEntity> | ||
JsonReaderData? jsonReaderData, | ||
TIncludingEntity entity, | ||
Func<QueryContext, object[], JsonReaderData, TIncludedCollectionElement> innerShaper, | ||
Action<TIncludingEntity, TIncludedCollectionElement> fixup, | ||
bool trackingQuery) | ||
where TIncludingEntity : class | ||
where TIncludedCollectionElement : class | ||
{ | ||
if (jsonElement.HasValue && jsonElement.Value.ValueKind != JsonValueKind.Null) | ||
if (jsonReaderData == null) | ||
{ | ||
var collectionAccessor = navigation.GetCollectionAccessor(); | ||
var result = (TResult)collectionAccessor!.Create(); | ||
return; | ||
} | ||
|
||
var manager = new Utf8JsonReaderManager(jsonReaderData); | ||
var tokenType = manager.CurrentReader.TokenType; | ||
|
||
if (tokenType != JsonTokenType.StartArray) | ||
{ | ||
throw new InvalidOperationException( | ||
RelationalStrings.JsonReaderInvalidTokenType(tokenType.ToString())); | ||
} | ||
|
||
var newKeyPropertyValues = new object[keyPropertyValues.Length + 1]; | ||
Array.Copy(keyPropertyValues, newKeyPropertyValues, keyPropertyValues.Length); | ||
var newKeyPropertyValues = new object[keyPropertyValues.Length + 1]; | ||
Array.Copy(keyPropertyValues, newKeyPropertyValues, keyPropertyValues.Length); | ||
|
||
var i = 0; | ||
foreach (var jsonArrayElement in jsonElement.Value.EnumerateArray()) | ||
tokenType = manager.MoveNext(); | ||
|
||
var i = 0; | ||
while (tokenType != JsonTokenType.EndArray) | ||
{ | ||
newKeyPropertyValues[^1] = ++i; | ||
|
||
if (tokenType == JsonTokenType.StartObject) | ||
{ | ||
newKeyPropertyValues[^1] = ++i; | ||
manager.CaptureState(); | ||
var resultElement = innerShaper(queryContext, newKeyPropertyValues, jsonReaderData); | ||
|
||
if (!trackingQuery) | ||
{ | ||
fixup(entity, resultElement); | ||
} | ||
|
||
var resultElement = innerShaper(queryContext, newKeyPropertyValues, jsonArrayElement); | ||
manager = new Utf8JsonReaderManager(manager.Data); | ||
if (manager.CurrentReader.TokenType != JsonTokenType.EndObject) | ||
{ | ||
throw new InvalidOperationException( | ||
RelationalStrings.JsonReaderInvalidTokenType(tokenType.ToString())); | ||
} | ||
|
||
result.Add(resultElement); | ||
tokenType = manager.MoveNext(); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
return default; | ||
manager.CaptureState(); | ||
} | ||
|
||
private static async Task TaskAwaiter(Func<Task>[] taskFactories) | ||
|
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.
Is this line still needed?