Support custom JsonSerializerOptions in EFCore.MySql.Json.Microsoft#1827
Support custom JsonSerializerOptions in EFCore.MySql.Json.Microsoft#1827trejjam wants to merge 4 commits intoPomeloFoundation:mainfrom
Conversation
|
|
||
| namespace Pomelo.EntityFrameworkCore.MySql.Json.Microsoft.Infrastructure; | ||
|
|
||
| public interface IMysqlJsonOptions |
There was a problem hiding this comment.
This looks like a casing issue; shouldn't it be IMySqlJsonOptions for consistency?
| 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.
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.
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