ramonhagenaars / jsons

🐍 A Python lib for (de)serializing Python objects to/from JSON
https://jsons.readthedocs.io
MIT License
289 stars 41 forks source link

Literal (and Union[Literal]) support #170

Open davetapley opened 2 years ago

davetapley commented 2 years ago

Feature request for:

@dataclass
class HasLiteral:
  foo: Literal["one"]

jsons.loads('{"foo": "one"}', HasLiteral)

Currently gives:

DeserializationError: No deserializer for type "typing.Literal"

And more realistic use case:

@dataclass
class HasLiteral:
  foo: Union[Literal["one"], Literal["two"]]

Currently gives:

DeserializationError: Could not match the object of type "str" to any type of the Union: Literal, Literal

I can have a go if a PR would be accepted?

ramonhagenaars commented 2 years ago

Hi @davetapley. I think your feature request makes sense, I would gladly accept a PR on this 😃