question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Switch to JSON.NET

See original GitHub issue

Hello,

This feels more like a question that an actual issue. But didn’t know for sure where to ask otherwise

These are early days for EF Core JSON support, and you’ll likely run into some limitations. Please let us know how the current features are working for you and what you’d like to see.

So I’m playing a bit with the JSON support and I really like it. There is one problem I’m having since internally the ‘new’ System.Text.Json api is used there is no support for polymorphic serialization and deserialization (https://github.com/dotnet/runtime/issues/30083)

I could easily solve this in the following way:

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<RootEntity>()
                .Property(e => e.ChildrenA)
                .HasConversion(
                    v => JsonConvert.SerializeObject(v, JsonSettings.JsonSerializerSettings),
                    v => JsonConvert.DeserializeObject<ImmutableList<AbstractClassA>>(v, JsonSettings.JsonSerializerSettings));
            
            modelBuilder.Entity<RootEntity>()
                .Property(e => e.ChildrenB)
                .HasConversion(
                    v => JsonConvert.SerializeObject(v, JsonSettings.JsonSerializerSettings),
                    v => JsonConvert.DeserializeObject<ImmutableList<AbstractClassB>>(v, JsonSettings.JsonSerializerSettings));
        }
    }

    public static class JsonSettings
    {
        public static readonly JsonSerializerSettings JsonSerializerSettings = new JsonSerializerSettings
        {
            TypeNameHandling = TypeNameHandling.Auto,
            MetadataPropertyHandling = MetadataPropertyHandling.ReadAhead
        };
    }

This perfectly works. But it’s a bit tedious to always add this in the model creation. Is there a better solution like globally replacing System.Text.Json to good 'ol JSON.NET?

Btw I’m using the preview of EF Core 6.

Thanks in advance, Sincerely, Brecht

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
NinoFloriscommented, Aug 2, 2021

There should be converter samples in those issues (though deserialization is a different matter) that you could copy paste into your app but I think that all hinges on https://github.com/npgsql/efcore.pg/issues/1107?

1reaction
rojicommented, Aug 2, 2021

One main reason why JSON.NET wasn’t implemented, was that we don’t want to add a reference to JSON.NET from EFCore.PG itself - that would force it upon users and potentially created dependency hell situations (System.Text.Json has the advantage of being built into the framework). EF Core does have a plugin model which allows having external nugets which add the reference, but the JSON support requires certain advanced functionality which isn’t possible from a plugin.

Then there’s the burden of maintaining two JSON implementations rather than just one… System.Text.Json has generally been improving and closing the gap, so this doesn’t really seem like a good way forward.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Migrate from Newtonsoft.Json to System.Text.Json - .NET
The System.Text.Json namespace provides functionality for serializing to and deserializing from JavaScript Object Notation (JSON). The System.
Read more >
Serializing and Deserializing JSON
The quickest method of converting between JSON text and a .NET object is using the JsonSerializer. The JsonSerializer converts .NET objects into their...
Read more >
How to serialize and deserialize JSON using C# - .NET
A common way to deserialize JSON is to first create a class with properties and fields that represent one or more of the...
Read more >
How to Turn a C# Object Into a JSON String in .NET?
Here, we turn an object into a JSON string by calling the SerializeObject() static method of the JsonConvert ...
Read more >
How do I turn a C# object into a JSON string in .NET?
A new JSON serializer is available in the System.Text.Json namespace. It's included in the .NET Core 3.0 shared framework and is in a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found