-
Notifications
You must be signed in to change notification settings - Fork 383
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
base: main
Are you sure you want to change the base?
Support custom JsonSerializerOptions in EFCore.MySql.Json.Microsoft #1827
Conversation
|
||
namespace Pomelo.EntityFrameworkCore.MySql.Json.Microsoft.Infrastructure; | ||
|
||
public interface IMysqlJsonOptions |
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.
This looks like a casing issue; shouldn't it be IMySqlJsonOptions
for consistency?
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.
I will change it
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); |
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.
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.
public interface IMysqlJsonOptions | ||
{ | ||
JsonSerializerOptions JsonSerializerOptions { get; } | ||
} |
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.
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.
bb43601
to
982c46a
Compare
You can use source-generated JsonSerializerOptions or provide additional converters when using JSON columns. This PR makes it possible