I'm writing a json codec, and hoping to find a ppx to make that easier. So far I've tried ppx_deriving_yojson and ppx_yojson_conv, but neither of them have very good error messages. I would love to have more context for parse failures, eg. the path where the issue was encountered. Is there a way to get that with either of these, or another ppx? I'm not wedded to yojson either (though I gather that is the standard)
#JSON codec ppx with better parse errors
1 messages · Page 1 of 1 (latest)
We have implemented melange-json (https://github.com/melange-community/melange-json). Despide the clear melange branding, it has a native implementation with yojson under the package melange-json-native
We worked a bit on the errors, you might want to take a look at some here https://github.com/melange-community/melange-json/blob/main/ppx/test/errors.t/run.t
are you aware of jsont and its associated ppx that was released recently?
Nope! I’m very new to OCaml, basically going off AI guidance in the first instance
Ok, from these it looks like they’re improved by the expected vs. actual context, but what I’d really like is to report where in the json an error was encountered
I hear you, that should be doable
i wrote a JSON parser that does this, using Angstrom: https://gist.github.com/yawaramin/75f0a35ece0cc7cf208d6c04e6e82bb9
it has pretty good error reporting: https://x.com/yawaramin/status/1921385346480128235
it can be used with ppx_deriving_yojson, because parsing and decoding are two different functions
Oh interesting! But wait, are you just saying it gives good parse errors for the string -> JSON AST step? I’m more interested in the AST -> my type step, and that’s where a ppx comes in handy
Because if a user provides malformed json that’s their business, but if it violates my schema, it’s my responsibility to help them fix
yeah the parse step is the string->AST step
Aha
also the thing is your question doesn't quite make sense given the new context. because you say you want location info for errors in the AST->custom type step. but the AST input doesn't have location info like a string would
we can have exact location info at the parse step (input string), or we can have exact decode error info at the decode step (input AST), but we can't have both...
For me location info would be eg. "At [key] of object, at [index] of array, at root", etc.
ok, fair enough. i'm not aware of any JSON decoder that does that in OCaml, maybe others can chime in
might be worth a post in the Discuss forum for a wider audience
Huh, ok. I managed to have something passable going when I was parsing by hand, it wasn’t too hard. Maybe I’ll go back to handwriting then, I just like the idea of a ppx. Also would be cool to build that as a general solution, if it really doesn’t exist.