2 * Copyright (c) 2009-2011 Petri Lehtinen <petri@digip.org>
3 * Copyright (c) 2011 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.
11 #include "jansson_private.h"
24 static const char *type_names[] = {
35 #define type_name(x) type_names[json_typeof(x)]
37 static const char *unpack_value_starters = "{[siIbfFOon";
40 static void scanner_init(scanner_t *s, json_error_t *error,
41 size_t flags, const char *fmt)
45 s->fmt = s->start = fmt;
50 static void next_token(scanner_t *s)
52 const char *t = s->fmt;
55 /* skip space and ignored chars */
56 while(*t == ' ' || *t == '\t' || *t == '\n' || *t == ',' || *t == ':') {
73 static void set_error(scanner_t *s, const char *source, const char *fmt, ...)
79 pos = (size_t)(s->fmt - s->start);
80 jsonp_error_vset(s->error, s->line, s->column, pos, fmt, ap);
82 jsonp_error_set_source(s->error, source);
87 static json_t *pack(scanner_t *s, va_list *ap);
89 static json_t *pack_object(scanner_t *s, va_list *ap)
91 json_t *object = json_object();
94 while(s->token != '}') {
99 set_error(s, "<format>", "Unexpected end of format string");
103 if(s->token != 's') {
104 set_error(s, "<format>", "Expected format 's', got '%c'", s->token);
108 key = va_arg(*ap, const char *);
110 set_error(s, "<args>", "NULL object key");
114 if(!utf8_check_string(key, -1)) {
115 set_error(s, "<args>", "Invalid UTF-8 in object key");
125 if(json_object_set_new_nocheck(object, key, value)) {
126 set_error(s, "<internal>", "Unable to add key \"%s\"", key);
140 static json_t *pack_array(scanner_t *s, va_list *ap)
142 json_t *array = json_array();
145 while(s->token != ']') {
149 set_error(s, "<format>", "Unexpected end of format string");
157 if(json_array_append_new(array, value)) {
158 set_error(s, "<internal>", "Unable to append to array");
171 static json_t *pack(scanner_t *s, va_list *ap)
175 return pack_object(s, ap);
178 return pack_array(s, ap);
180 case 's': /* string */
182 const char *str = va_arg(*ap, const char *);
184 set_error(s, "<args>", "NULL string argument");
187 if(!utf8_check_string(str, -1)) {
188 set_error(s, "<args>", "Invalid UTF-8 string");
191 return json_string_nocheck(str);
197 case 'b': /* boolean */
198 return va_arg(*ap, int) ? json_true() : json_false();
200 case 'i': /* integer from int */
201 return json_integer(va_arg(*ap, int));
203 case 'I': /* integer from json_int_t */
204 return json_integer(va_arg(*ap, json_int_t));
207 return json_real(va_arg(*ap, double));
209 case 'O': /* a json_t object; increments refcount */
210 return json_incref(va_arg(*ap, json_t *));
212 case 'o': /* a json_t object; doesn't increment refcount */
213 return va_arg(*ap, json_t *);
216 set_error(s, "<format>", "Unexpected format character '%c'",
222 static int unpack(scanner_t *s, json_t *root, va_list *ap);
224 static int unpack_object(scanner_t *s, json_t *root, va_list *ap)
229 /* Use a set (emulated by a hashtable) to check that all object
230 keys are accessed. Checking that the correct number of keys
231 were accessed is not enough, as the same key can be unpacked
236 if(hashtable_init(&key_set, jsonp_hash_str, jsonp_str_equal, NULL, NULL)) {
237 set_error(s, "<internal>", "Out of memory");
241 if(!json_is_object(root)) {
242 set_error(s, "<validation>", "Expected object, got %s",
248 while(s->token != '}') {
253 set_error(s, "<format>", "Expected '}' after '%c', got '%c'",
254 (strict == 1 ? '!' : '*'), s->token);
259 set_error(s, "<format>", "Unexpected end of format string");
263 if(s->token == '!' || s->token == '*') {
264 strict = (s->token == '!' ? 1 : -1);
269 if(s->token != 's') {
270 set_error(s, "<format>", "Expected format 's', got '%c'", s->token);
274 key = va_arg(*ap, const char *);
276 set_error(s, "<args>", "NULL object key");
282 value = json_object_get(root, key);
284 set_error(s, "<validation>", "Object item not found: %s", key);
288 if(unpack(s, value, ap))
291 hashtable_set(&key_set, (void *)key, NULL);
295 if(strict == 0 && (s->flags & JSON_STRICT))
298 if(strict == 1 && key_set.size != json_object_size(root)) {
299 long diff = (long)json_object_size(root) - (long)key_set.size;
300 set_error(s, "<validation>", "%li object item(s) left unpacked", diff);
307 hashtable_close(&key_set);
311 static int unpack_array(scanner_t *s, json_t *root, va_list *ap)
316 if(!json_is_array(root)) {
317 set_error(s, "<validation>", "Expected array, got %s", type_name(root));
322 while(s->token != ']') {
326 set_error(s, "<format>", "Expected ']' after '%c', got '%c'",
327 (strict == 1 ? '!' : '*'),
333 set_error(s, "<format>", "Unexpected end of format string");
337 if(s->token == '!' || s->token == '*') {
338 strict = (s->token == '!' ? 1 : -1);
343 if(!strchr(unpack_value_starters, s->token)) {
344 set_error(s, "<format>", "Unexpected format character '%c'",
349 value = json_array_get(root, i);
351 set_error(s, "<validation>", "Array index %lu out of range",
356 if(unpack(s, value, ap))
363 if(strict == 0 && (s->flags & JSON_STRICT))
366 if(strict == 1 && i != json_array_size(root)) {
367 long diff = (long)json_array_size(root) - (long)i;
368 set_error(s, "<validation>", "%li array item(s) left unpacked", diff);
375 static int unpack(scanner_t *s, json_t *root, va_list *ap)
380 return unpack_object(s, root, ap);
383 return unpack_array(s, root, ap);
386 if(!json_is_string(root)) {
387 set_error(s, "<validation>", "Expected string, got %s",
392 if(!(s->flags & JSON_VALIDATE_ONLY)) {
395 str = va_arg(*ap, const char **);
397 set_error(s, "<args>", "NULL string argument");
401 *str = json_string_value(root);
406 if(!json_is_integer(root)) {
407 set_error(s, "<validation>", "Expected integer, got %s",
412 if(!(s->flags & JSON_VALIDATE_ONLY))
413 *va_arg(*ap, int*) = json_integer_value(root);
418 if(!json_is_integer(root)) {
419 set_error(s, "<validation>", "Expected integer, got %s",
424 if(!(s->flags & JSON_VALIDATE_ONLY))
425 *va_arg(*ap, json_int_t*) = json_integer_value(root);
430 if(!json_is_boolean(root)) {
431 set_error(s, "<validation>", "Expected true or false, got %s",
436 if(!(s->flags & JSON_VALIDATE_ONLY))
437 *va_arg(*ap, int*) = json_is_true(root);
442 if(!json_is_real(root)) {
443 set_error(s, "<validation>", "Expected real, got %s",
448 if(!(s->flags & JSON_VALIDATE_ONLY))
449 *va_arg(*ap, double*) = json_real_value(root);
454 if(!json_is_number(root)) {
455 set_error(s, "<validation>", "Expected real or integer, got %s",
460 if(!(s->flags & JSON_VALIDATE_ONLY))
461 *va_arg(*ap, double*) = json_number_value(root);
466 if(!(s->flags & JSON_VALIDATE_ONLY))
471 if(!(s->flags & JSON_VALIDATE_ONLY))
472 *va_arg(*ap, json_t**) = root;
477 /* Never assign, just validate */
478 if(!json_is_null(root)) {
479 set_error(s, "<validation>", "Expected null, got %s",
486 set_error(s, "<format>", "Unexpected format character '%c'",
492 json_t *json_vpack_ex(json_error_t *error, size_t flags,
493 const char *fmt, va_list ap)
500 jsonp_error_init(error, "<format>");
501 jsonp_error_set(error, -1, -1, 0, "NULL or empty format string");
504 jsonp_error_init(error, NULL);
506 scanner_init(&s, error, flags, fmt);
509 va_copy(ap_copy, ap);
510 value = pack(&s, &ap_copy);
519 set_error(&s, "<format>", "Garbage after format string");
526 json_t *json_pack_ex(json_error_t *error, size_t flags, const char *fmt, ...)
532 value = json_vpack_ex(error, flags, fmt, ap);
538 json_t *json_pack(const char *fmt, ...)
544 value = json_vpack_ex(NULL, 0, fmt, ap);
550 int json_vunpack_ex(json_t *root, json_error_t *error, size_t flags,
551 const char *fmt, va_list ap)
557 jsonp_error_init(error, "<root>");
558 jsonp_error_set(error, -1, -1, 0, "NULL root value");
563 jsonp_error_init(error, "<format>");
564 jsonp_error_set(error, -1, -1, 0, "NULL or empty format string");
567 jsonp_error_init(error, NULL);
569 scanner_init(&s, error, flags, fmt);
572 va_copy(ap_copy, ap);
573 if(unpack(&s, root, &ap_copy)) {
581 set_error(&s, "<format>", "Garbage after format string");
588 int json_unpack_ex(json_t *root, json_error_t *error, size_t flags, const char *fmt, ...)
594 ret = json_vunpack_ex(root, error, flags, fmt, ap);
600 int json_unpack(json_t *root, const char *fmt, ...)
606 ret = json_vunpack_ex(root, NULL, 0, fmt, ap);