Optional object items for json_unpack() #67

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

Projects

None yet

1 participant

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

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

@akheron akheron closed this Aug 28, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment