#JSON codec ppx with better parse errors

1 messages · Page 1 of 1 (latest)

neat basin
#

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)

oak gale
pulsar talon
#

are you aware of jsont and its associated ppx that was released recently?

neat basin
#

Nope! I’m very new to OCaml, basically going off AI guidance in the first instance

neat basin
oak gale
#

I hear you, that should be doable

still minnow
#

it can be used with ppx_deriving_yojson, because parsing and decoding are two different functions

neat basin
#

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

still minnow
#

yeah the parse step is the string->AST step

neat basin
#

Aha

still minnow
#

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...

neat basin
#

For me location info would be eg. "At [key] of object, at [index] of array, at root", etc.

still minnow
#

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

neat basin
#

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.