NathanReb / ppx_yojson

OCaml PPX extension for JSON literals and patterns
BSD 2-Clause "Simplified" License
43 stars 5 forks source link

Empty objects disallowed #34

Closed cemerick closed 2 years ago

cemerick commented 2 years ago

This usage:

[%yojson {}]

yields Error: Syntax error. Likewise for any empty object within a %yojson block. Adding any member to the object yields an `Assoc value as expected.

NathanReb commented 2 years ago

OCaml does not allow this syntax (the empty record {}) so we can't support it as the payload of the extension still has to be valid OCaml.

If you need to insert empty objects in otherwise large json values, I would suggest you use antiquotation. Something along those lines:

let empty_object = `Assoc [] in
let json_value = [%yojson {some_field = [%aq empty_object]; some_other_field = ...}] in
...

or simply:

let json_value = [%yojson {some_field = [%aq `Assoc []]; some_other_field = ...}] in
...

Would that work for you?

cemerick commented 2 years ago

Ach, my bad, I should have realized that before filing this. Sorry for the noise :v: