ramonhagenaars / jsons

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

add default (de)serializer for Literal values #180

Open buckley-w-david opened 2 years ago

buckley-w-david commented 2 years ago

I implemented these for a project I'm working on that uses jsons, then noticed https://github.com/ramonhagenaars/jsons/issues/170 and figured I may as well see if you like the implementation enough to go with it.

I included the strictly_equal_literal as a compromise between correctness and utility.

From https://peps.python.org/pep-0586/#equivalence-of-two-literals

Two types Literal[v1] and Literal[v2] are equivalent when both of the following conditions are true:

  1. type(v1) == type(v2)
  2. v1 == v2

I took that to mean that for a value to really match a literal it should match in both type and value. The only problem with that is that I've found it to be very useful to allow jsons to "coerce" values that are equal in value into the literal value, the use case that prompted me to implement this actually relies on that behaviour.