neogeny / TatSu

竜 TatSu generates Python parsers from grammars in a variation of EBNF
https://tatsu.readthedocs.io/
Other
408 stars 48 forks source link

[model] Fall back to repr() for objects not handled by json module #268

Closed dnicolodi closed 2 years ago

dnicolodi commented 2 years ago

The str() method of Node returns a JSON representation of the Node. This fails if the Node contains anything that cannot be represented by plain JSON. Notable examples are datetime and date objects that are not so unrealistic for a parser to return.

Use a custom JSON encoder that fall back to repr() for these objects so that a string representation for a Node can always be returned.

Fixes #262.

apalala commented 2 years ago

A complete fix would have asjson() handle most types of interest, but this is a very reasonable fallback.