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.

to_dict() does not convert enums to strings

See original GitHub issue

As resolved in #51, python enums can be used with dataclasses-json and I have successfully tried that, using the to_json() and from_json() methods.

However, with to_dict(), enum values appear to be untouched, hence the resulting dictionary is not JSON serializable. I assume that this is not the intended behavior, as according to the API docs, to_dict() should return a JSON-compatible dict.

Issue Analytics

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

github_iconTop GitHub Comments

4reactions
RunOrVeithcommented, Jul 29, 2020

you have to do foo.to_dict(encode_json=True)

0reactions
illeatmyhatcommented, Mar 8, 2021

I also have this issue with to_json(), which doesn’t have an encode_json parameter. What I ended up doing was

from dataclasses import dataclass, field
from dataclasses_json import DataClassJsonMixin, config
from enum import Enum
class MyEnum(Enum):
    Foo = "foo"
    Bar = "bar"

@dataclass
class Data(DataClassJsonMixin):
    test: MyEnum = field(metadata=config(encoder=lambda x: x.value, decoder=MyEnum))

foo = Data(MyEnum.Foo)
print(foo.to_json())
assert Data.from_json('{"test": "foo"}').test == MyEnum.Foo
Read more comments on GitHub >

github_iconTop Results From Across the Web

Pydantic enum field does not get converted to string
I am trying to restrict one field in a class to an enum. However, when I try to get a dictionary out of...
Read more >
Enum.ToString Method (System) - Microsoft Learn
This method overload is obsolete; use ToString(). ToString(). Converts the value of this instance to its equivalent string representation.
Read more >
Three Methods to Convert Strings into Enums in C# - Ivan Kahl
Method 1: Enum.​​ NET. It allows you to convert a string into an enum by matching the given string to the enum item...
Read more >
Convert an enum to a list in C# | Techie Delight
Get a list of Enum members. The idea is to use the Enum.GetValues() method to get an array of the enum constants' values....
Read more >
ASP.NET Core: Converting C# enums to JavaScript
GetValues() returns Array and Array doesn't have LINQ extensions available we ... and this method returns enum as JavaScript object string.
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