Innofactor / EfCoreJsonValueConverter

JSON ValueConverter for EF Core 3.0+
GNU Lesser General Public License v3.0
96 stars 17 forks source link

Question: Can we pass custom converters #3

Closed attiqe closed 5 years ago

attiqe commented 5 years ago

Hi,

Thanks for a wonderful library, it makes job easier and clean code is a bonus. I have customer converters for some objects. How can we make use of it?

Thanks, Attiqe

Armarr commented 5 years ago

Because this library uses Newtonsoft I would imagine you can use the the JsonConverterAttribute for this.

attiqe commented 5 years ago

@Armarr that's not what I was asking, I know that we can do with it Value Conversions, following is an example of using it in an entity configuration file:

entity
       .Property(s => s.OutstandingMatters)
       .HasColumnType(Constants.DataType.JsonType)
       .HasConversion(
             v => JsonConvert.SerializeObject(v),
             s => JsonConvert.DeserializeObject<List<User>>(s, new UserConverter()));

I wanted to know about the usage of the HasJsonValueConversionfor custom converters.

AlexEngblom commented 5 years ago

Since HasJsonValueConversion is effectively just wrapping Newtonsoft JsonConvert.se-/deserialize calls, I suppose @Armarr suggestion using JsonConverterAttribute should work for passing custom converters on your entities. I'll let you know when I have time to try it out.

Other than that there's no way of passing a converter directly for property at the moment.

AlexEngblom commented 5 years ago

Using [JsonConverter] attribute to pass custom converters works fine. Created an unit test showcasing their usage: caadae68308cd8a2a7b72044efde7ddb21410599