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.

JSON: Support [JsonStringEnumConverter]

See original GitHub issue

System.Text.Json has [JsonStringEnumConverter], which maps CLR enums to strings in the JSON document.

I’m not sure if we plan to support arbitrary EF value converters for JSON properties (we should probably open an issue for that). If we do, then we could set up the appropriate enum/string converter when we see this attribute.

Originally requested for Npgsql by @sergeyshaykhullin in https://github.com/npgsql/efcore.pg/issues/2325

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:3
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
sergeyshaykhullincommented, Apr 19, 2022

@roji I think that enum property with .HasConversion<string>() should also translate to CAST(p.barcode->>'type' AS text)

0reactions
ajcvickerscommented, Oct 26, 2022

Notes from triage:

  • Enums are stored as strings by default in JSON documents. This is because the type mapping uses EnumToStringConverter by default.
  • Enums can be stored as numbers by configuring the enum-to-number converter instead.
  • An attribute for changing the value converter could be useful here: https://github.com/dotnet/efcore/issues/29426
Read more comments on GitHub >

github_iconTop Results From Across the Web

JsonStringEnumConverter Class (System.Text.Json. ...
JsonStringEnumConverter (). Initializes an instance of the JsonStringEnumConverter class with the default naming policy that allows integer values.
Read more >
c# - Does JsonStringEnumConverter (System.Text.Json) ...
Unfortunately, there is currently no support "out-of-the-box" in System.Text.Json to convert nullable enums. However, there is a solution by ...
Read more >
Data binding and serialization issue with System.Text.Json ...
In Microsoft's documentation I see that using the JsonStringEnumConverter serializes enums, including nullable enums, so if there is an issue ...
Read more >
How to Serialize Enum to a String in C# - Code Maze
In C#, JSON serialization very often needs to deal with enum objects. ... as JsonStringEnumConverter and StringEnumConverter respectively.
Read more >
Serializing enums as strings using System.Text.Json library ...
The new library has got support for all the new features that got ... { Converters ={ new JsonStringEnumConverter() } }; JsonSerializer.
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