2 * Copyright (c) 2009, 2010 Petri Lehtinen <petri@digip.org>
3 * Copyright (c) 2010 Graeme Smecher <graeme.smecher@mail.mcgill.ca>
5 * Jansson is free software; you can redistribute it and/or modify
6 * it under the terms of the MIT license. See LICENSE for details.
14 #include "jansson_private.h"
16 static json_t *json_vnpack(json_error_t *error, ssize_t size, const char * const fmt, va_list *ap)
18 json_t *root = NULL; /* root object */
21 /* Scanner variables */
22 const char *tok = fmt;
26 char *key = NULL; /* Current key in an object */
32 /* Skip whitespace at the beginning of the string. */
33 while(size && *tok == ' ') {
40 jsonp_error_set(error, 1, 1, "Empty format string!");
44 /* tok must contain either a container type, or a length-1 string for a
52 /* Simple object. Permit trailing spaces, otherwise complain. */
53 if((ssize_t)strspn(tok+1, " ") < size-1)
55 jsonp_error_set(error, 1, 1,
56 "Expected a single object, got %i", size);
62 case 's': /* string */
63 s = va_arg(*ap, char*);
66 jsonp_error_set(error, 1, 1,
67 "Refusing to handle a NULL string");
70 return(json_string(s));
75 case 'b': /* boolean */
76 obj = va_arg(*ap, int) ?
77 json_true() : json_false();
80 case 'i': /* integer */
81 return(json_integer(va_arg(*ap, int)));
83 case 'f': /* double-precision float */
84 return(json_real(va_arg(*ap, double)));
86 case 'O': /* a json_t object; increments refcount */
87 obj = va_arg(*ap, json_t *);
91 case 'o': /* a json_t object; doesn't increment refcount */
92 obj = va_arg(*ap, json_t *);
95 default: /* Whoops! */
96 jsonp_error_set(error, 1, 1,
97 "Didn't understand format character '%c'",
103 /* Move past container opening token */
107 while(tok-fmt < size) {
114 case ' ': /* Whitespace */
117 case ',': /* Element spacer */
120 jsonp_error_set(error, line, column,
121 "Expected KEY, got COMMA!");
127 case ':': /* Key/value separator */
130 jsonp_error_set(error, line, column,
131 "Got key/value separator without "
132 "a key preceding it!");
137 if(!json_is_object(root))
139 jsonp_error_set(error, line, column,
140 "Got a key/value separator "
141 "(':') outside an object!");
148 case ']': /* Close array or object */
151 if(tok-fmt + (ssize_t)strspn(tok+1, " ") != size-1)
153 jsonp_error_set(error, line, column,
154 "Unexpected close-bracket '%c'", *tok);
159 if((*tok == ']' && !json_is_array(root)) ||
160 (*tok == '}' && !json_is_object(root)))
162 jsonp_error_set(error, line, column,
163 "Stray close-array '%c' character", *tok);
172 /* Shortcut so we don't mess up the column count in error
174 if(json_is_object(root) && !key)
177 /* Find corresponding close bracket */
182 if(!*etok || etok-fmt >= size) {
183 jsonp_error_set(error, line, column,
184 "Couldn't find matching close bracket for '%c'",
192 else if(*tok=='[' && *etok==']') {
195 } else if(*tok=='{' && *etok=='}') {
204 obj = json_vnpack(error, etok-tok+1, tok, ap);
206 /* error should already be set */
207 error->column += column-1;
208 error->line += line-1;
217 /* Handle strings specially, since they're used for both keys
219 s = va_arg(*ap, char*);
223 jsonp_error_set(error, line, column,
224 "Refusing to handle a NULL string");
229 if(json_is_object(root) && !key)
236 obj = json_string(s);
240 obj = json_vnpack(error, 1, tok, ap);
247 /* Add to container */
248 if(json_is_object(root)) {
251 jsonp_error_set(error, line, column,
252 "Expected key, got identifier '%c'!", *tok);
257 json_object_set_new(root, key, obj);
262 json_array_append_new(root, obj);
270 /* Whoops -- we didn't match the close bracket! */
271 jsonp_error_set(error, line, column, "Missing close array or object!");
276 static int json_vnunpack(json_t *root, json_error_t *error, ssize_t size, const char *fmt, va_list *ap)
279 int rv=0; /* Return value */
280 int line = 1; /* Line number */
281 int column = 1; /* Column */
283 /* Position markers for arrays or objects */
289 /* Scanner variables */
290 const char *tok = fmt;
296 /* If we're successful, we need to know if the number of arguments
297 * provided matches the number of JSON objects. We can do this by
298 * counting the elements in every array or object we open up, and
299 * decrementing the count as we visit their children. */
302 /* Skip whitespace at the beginning of the string. */
303 while(size && *tok == ' ') {
310 jsonp_error_set(error, 1, 1, "Empty format string!");
314 /* tok must contain either a container type, or a length-1 string for a
316 if(*tok != '[' && *tok != '{')
318 /* Simple object. Permit trailing spaces, otherwise complain. */
319 if((ssize_t)strspn(tok+1, " ") < size-1)
321 jsonp_error_set(error, 1, 1,
322 "Expected a single object, got %i", size);
329 if(!json_is_string(root))
331 jsonp_error_set(error, line, column,
332 "Type mismatch! Object (%i) wasn't a string.",
336 s = va_arg(*ap, const char **);
338 jsonp_error_set(error, line, column, "Passed a NULL string pointer!");
341 *s = json_string_value(root);
345 if(!json_is_integer(root))
347 jsonp_error_set(error, line, column,
348 "Type mismatch! Object (%i) wasn't an integer.",
352 *va_arg(*ap, int*) = json_integer_value(root);
356 if(!json_is_boolean(root))
358 jsonp_error_set(error, line, column,
359 "Type mismatch! Object (%i) wasn't a boolean.",
363 *va_arg(*ap, int*) = json_is_true(root);
367 if(!json_is_number(root))
369 jsonp_error_set(error, line, column,
370 "Type mismatch! Object (%i) wasn't a real.",
374 *va_arg(*ap, double*) = json_number_value(root);
382 *va_arg(*ap, json_t**) = root;
386 /* Don't actually assign anything; we're just happy
387 * the null turned up as promised in the format
392 jsonp_error_set(error, line, column,
393 "Unknown format character '%c'", *tok);
398 /* Move past container opening token */
401 while(tok-fmt < size) {
408 case ' ': /* Whitespace */
411 case ',': /* Element spacer */
414 jsonp_error_set(error, line, column,
415 "Expected KEY, got COMMA!");
420 case ':': /* Key/value separator */
423 jsonp_error_set(error, line, column,
424 "Got key/value separator without "
425 "a key preceding it!");
429 if(!json_is_object(root))
431 jsonp_error_set(error, line, column,
432 "Got a key/value separator "
433 "(':') outside an object!");
439 case ']': /* Close array or object */
442 if(tok-fmt + (ssize_t)strspn(tok+1, " ") != size-1)
444 jsonp_error_set(error, line, column,
445 "Unexpected close-bracket '%c'", *tok);
449 if((*tok == ']' && !json_is_array(root)) ||
450 (*tok == '}' && !json_is_object(root)))
452 jsonp_error_set(error, line, column,
453 "Stray close-array '%c' character", *tok);
461 /* Find corresponding close bracket */
466 if(!*etok || etok-fmt >= size) {
467 jsonp_error_set(error, line, column,
468 "Couldn't find matching close bracket for '%c'",
475 else if(*tok=='[' && *etok==']') {
478 } else if(*tok=='{' && *etok=='}') {
487 if(json_is_array(root)) {
488 rv = json_vnunpack(json_object_get(root, key),
489 error, etok-tok+1, tok, ap);
491 rv = json_vnunpack(json_array_get(root, array_index++),
492 error, etok-tok+1, tok, ap);
496 /* error should already be set */
497 error->column += column-1;
498 error->line += line-1;
508 /* Handle strings specially, since they're used for both keys
511 if(json_is_object(root) && !key)
514 key = va_arg(*ap, char*);
515 printf("Got key '%s'\n", key);
519 jsonp_error_set(error, line, column,
520 "Refusing to handle a NULL key");
530 /* Fetch the element from the JSON container */
531 if(json_is_object(root))
532 obj = json_object_get(root, key);
534 obj = json_array_get(root, array_index++);
537 jsonp_error_set(error, line, column,
538 "Array/object entry didn't exist!");
542 rv = json_vnunpack(obj, error, 1, tok, ap);
552 /* Whoops -- we didn't match the close bracket! */
553 jsonp_error_set(error, line, column, "Missing close array or object!");
557 json_t *json_pack(json_error_t *error, const char *fmt, ...)
562 jsonp_error_init(error, "");
565 jsonp_error_set(error, 1, 1, "Null or empty format string!");
570 obj = json_vnpack(error, strlen(fmt), fmt, &ap);
576 int json_unpack(json_t *root, json_error_t *error, const char *fmt, ...)
581 jsonp_error_init(error, "");
584 jsonp_error_set(error, 1, 1, "Null or empty format string!");
589 rv = json_vnunpack(root, error, strlen(fmt), fmt, &ap);
595 /* vim: ts=4:expandtab:sw=4