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

Relational: Allow a custom serializer for JSON columns #28043

Open
Tracked by #22953
AndriySvyryd opened this issue May 17, 2022 · 16 comments
Open
Tracked by #22953

Relational: Allow a custom serializer for JSON columns #28043

AndriySvyryd opened this issue May 17, 2022 · 16 comments

Comments

@AndriySvyryd
Copy link
Member

AndriySvyryd commented May 17, 2022

Related to #17306

@AndriySvyryd AndriySvyryd added this to the Backlog milestone May 17, 2022
@AndriySvyryd AndriySvyryd changed the title Allow a custom serializer for JSON columns Relational: Allow a custom serializer for JSON columns May 17, 2022
@SaifAqqad
Copy link

Is this still planned for release with .NET 8?

@LuohuaRain
Copy link

Is this still planned for release with .NET 8?

I'd like to know as well.

@ajcvickers ajcvickers modified the milestone: Backlog Sep 26, 2023
@ajcvickers
Copy link
Member

@SaifAqqad @LuohuaRain We made significant changes to the JSON infrastructure in 8, which means some additional design is now needed to expose customization of what is now already a custom serialization mechanism.

@ajcvickers ajcvickers added consider-for-next-release punted-for-8.0 Originally planned for the EF Core 8.0 (EF8) release, but moved out due to resource constraints. and removed consider-for-current-release punted-for-8.0 Originally planned for the EF Core 8.0 (EF8) release, but moved out due to resource constraints. labels Oct 3, 2023
@shahrzadabedi
Copy link

Do you have any plans to provide an update to make an option to store Json Entity field with Non-ASCII characters without escaping it on EF Core 7?

@VahidMhrb
Copy link

@shahrzadabedi

Do you have any plans to provide an update to make an option to store Json Entity field with Non-ASCII characters without escaping it on EF Core 7?

I think we can use it in 8.

@ajcvickers
Copy link
Member

@shahrzadabedi Not currently. I filed #32152 to track this.

@Timovzl
Copy link

Timovzl commented Oct 25, 2023

Is it currently possible to work around the lack of this feature by using a custom converter? I understand that this suggestion is almost like not using ToJson() altogether, but it still provides the ability to query into the JSON contents.

@wjrogers
Copy link

wjrogers commented Oct 25, 2023

@Timovzl

Is it currently possible to work around the lack of this feature by using a custom converter?

I have a plugin here that implements custom serialization for the npgsql provider. It's slightly more involved than a custom converter because it also influences the SQL generation for JSON fields.

@roji
Copy link
Member

roji commented Oct 25, 2023

@wjrogers the Npgsql provider's support for JSON which this affects is very different from the EF owned entity support for JSON (ToJson()) that's discussed here. For 8.0 I'm working on bringing the latter into Npgsql as well.

@shahrzadabedi
Copy link

shahrzadabedi commented Oct 25, 2023

@shahrzadabedi Not currently. I filed #32152 to track this.

Thanks Arthur

@shahrzadabedi
Copy link

@wjrogers the Npgsql provider's support for JSON which this affects is very different from the EF owned entity support for JSON (ToJson()) that's discussed here. For 8.0 I'm working on bringing the latter into Npgsql as well.

Thank you for the clarification, Shay.

@wizofaus
Copy link

wizofaus commented Jan 23, 2024

Definitely need this - I was hoping to use built-in JSON serialization, but it's not handling badly formed data well at all (the exceptions don't even tell me where the error is!).
But none of the JsonConvertor (or other Json-) attributes I'm adding seem to be getting acknowledged at all.

@romanov
Copy link

romanov commented Jan 26, 2024

Is there any temp solution for now? Like global setting, or EF uses built-in serialization?

@wizofaus
Copy link

wizofaus commented Jan 27, 2024

I got it working the way I needed using

        public static Builders.PropertyBuilder<T> HasJsonConversion<T>(this Builders.PropertyBuilder<T> propertyBuilder) where T : class, new()
        {
            ValueConverter<T, string> converter = new ValueConverter<T, string>
            (
                v => JsonSerializer.Serialize(v, default(JsonSerializerOptions)),
                v => JsonSerializer.Deserialize<T>(v, new JsonSerializerOptions { NumberHandling = JsonNumberHandling.AllowReadingFromString }) ?? new T()
            );
            propertyBuilder.HasConversion(converter);
            propertyBuilder.Metadata.SetValueConverter(converter);
            return propertyBuilder;
        }

which you can use in the OnModelCreating as needed, e.g.:

modelBuilder.Entity<MyRowType>().Property(expr=> expr.JsonField).HasJsonConversion();

That way it honors all the attributes that normally work to control JSON serialisation (though it doesn't allow using DI-managed JsonSerializerOptions).

@dev-mfm
Copy link

dev-mfm commented Jul 23, 2024

I got it working the way I needed using

        public static Builders.PropertyBuilder<T> HasJsonConversion<T>(this Builders.PropertyBuilder<T> propertyBuilder) where T : class, new()
        {
            ValueConverter<T, string> converter = new ValueConverter<T, string>
            (
                v => JsonSerializer.Serialize(v, default(JsonSerializerOptions)),
                v => JsonSerializer.Deserialize<T>(v, new JsonSerializerOptions { NumberHandling = JsonNumberHandling.AllowReadingFromString }) ?? new T()
            );
            propertyBuilder.HasConversion(converter);
            propertyBuilder.Metadata.SetValueConverter(converter);
            return propertyBuilder;
        }

which you can use in the OnModelCreating as needed, e.g.:

modelBuilder.Entity<MyRowType>().Property(expr=> expr.JsonField).HasJsonConversion();

That way it honors all the attributes that normally work to control JSON serialisation (though it doesn't allow using DI-managed JsonSerializerOptions).

I tried your solution but it didn't work.

@wizofaus
Copy link

wizofaus commented Jul 27, 2024

Didn't work in what sense? Don't have access to the codebase I was working on where I did that now but it was definitely doing the job for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests