Skip to content
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

Support custom JsonSerializerOptions in EFCore.MySql.Json.Microsoft #1827

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

trejjam
Copy link
Contributor

@trejjam trejjam commented Jan 8, 2024

You can use source-generated JsonSerializerOptions or provide additional converters when using JSON columns. This PR makes it possible


namespace Pomelo.EntityFrameworkCore.MySql.Json.Microsoft.Infrastructure;

public interface IMysqlJsonOptions

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a casing issue; shouldn't it be IMySqlJsonOptions for consistency?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will change it

Comment on lines 12 to 23
public MySqlJsonMicrosoftPocoValueConverter(JsonSerializerOptions jsonSerializerOptions)
: base(
v => ConvertToProviderCore(v),
v => ConvertFromProviderCore(v))
v => ConvertToProviderCore(v, jsonSerializerOptions),
v => ConvertFromProviderCore(v, jsonSerializerOptions))
{
}

private static string ConvertToProviderCore(T v)
=> JsonSerializer.Serialize(v);
private static string ConvertToProviderCore(T v, JsonSerializerOptions jsonSerializerOptions)
=> JsonSerializer.Serialize(v, jsonSerializerOptions);

private static T ConvertFromProviderCore(string v)
=> JsonSerializer.Deserialize<T>(v);
private static T ConvertFromProviderCore(string v, JsonSerializerOptions jsonSerializerOptions)
=> JsonSerializer.Deserialize<T>(v, jsonSerializerOptions);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure at the moment, that we want to force the use of a jsonSerializerOptions parameter. We might want to make it optional, falling back on the default options.

Comment on lines +8 to +11
public interface IMysqlJsonOptions
{
JsonSerializerOptions JsonSerializerOptions { get; }
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not convinced that this is the right approach. I think users should have the ability to set JsonSerializerOptions using the model, not injecting them via a service.

They should be able to set a default on the model itself and then also concrete options on (JSON) properties of entities (that override the default options). They also need to be supplied in a JSON provider specific fashion (since we support Microsoft and Newtonsoft JSON implementations). So this requires JSON provider specific extension methods.

@trejjam trejjam force-pushed the feature/json-serializer-options branch from bb43601 to 982c46a Compare August 5, 2024 17:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants