Skip to content
This repository

Optional object items for json_unpack() #67

Closed
akheron opened this Issue April 13, 2012 · 1 comment

1 participant

Petri Lehtinen
Petri Lehtinen
Owner

There's currently no way to have optional items in objects. If they were implemented, the best way would prbably be to do it like this:

json_t *root;  /* the JSON value */
char *str;
double *real;
int have_foo, have_bar;

json_unpack(root, "{s: s?, s: f?}", "foo", &str, &have_foo, "bar", &real, &have_bar);

That is, the new ? format character would consume an extra int pointer that would be set as 1 if the optional key was present and 0 if it wasn't.

See also #58.

Petri Lehtinen
Owner

Erm, there IS a way to have optional items in objects using the {s?o} syntax. There's just no way to detect whether a value was unpacked or not reliably. The only way is to set the initial value to something and check if the initial value was changed, but this doesn't work well if the supplied value can be anything.

Closing this for now. Yet another flag in addition to ?, that works like suggested here, can be added later if a need arises.

Petri Lehtinen akheron closed this August 27, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.