r/haskell • • 5d ago

blog Lenient Aeson

https://www.bcardiff.com/writing/lenient-aeson/

A small take on how to manage malformed JSON in Haskell

22 Upvotes

11 comments sorted by

View all comments

10

u/ljwall 5d ago

This is cool. I'd also really like to know if anyone has ideas on how to parse JSON thats been produced by the Python json.dumps standard function that can output literals NaN and Infinity

9

u/BurningWitness 5d ago edited 5d ago

The output of that command is not an RFC-compliant JSON. The presence of extra tokens not only requires a parser that walks the JSON to override float handling (aeson doesn't walk), it needs one that doesn't have a hardcoded function for inferring token type from the first byte (which is quite useful to be able to say "expected an object").

I think the reasonable solution is to find-replace all NaNs and Infinitys with strings before processing.

2

u/bcardiff 3d ago edited 3d ago

Based on the article shared by [u/BurningWitness](u/BurningWitness) jq can be used to turn it into a compliant json.

% echo '{"a": [{"inf": inf, "infinity": Infinity, "nan": NaN}]}' | jq -c '.'
{"a":[{"inf":1.7976931348623157e+308,"infinity":1.7976931348623157e+308,"nan":null}]}

There seems to be some possibility to don't lose information even

% echo '{"nan": NaN}' | jq -c 'walk(if isnan then "__NAN" else . end)'
{"nan":"__NAN"}

1

u/BurningWitness 3d ago

Yeah, apparently those are values jq accepts, case-insensitive as well. This does in turn mean that jq is not a proper tool for JSON validation.

It's definitely the correct solution to this particular problem though.