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.
10 #include "jansson_private.h"
21 static void next_token(scanner_t *s)
23 const char *t = s->fmt;
26 /* skip space and ignored chars */
27 while(*t == ' ' || *t == '\t' || *t == '\n' || *t == ',' || *t == ':') {
44 static void set_error(scanner_t *s, const char *fmt, ...)
48 jsonp_error_vset(s->error, s->line, s->column, fmt, ap);
52 static json_t *pack(scanner_t *s, va_list *ap);
54 static json_t *pack_object(scanner_t *s, va_list *ap)
56 json_t *object = json_object();
59 while(s->token != '}') {
64 set_error(s, "Unexpected end of format string");
69 set_error(s, "Expected format 's', got '%c'\n", *s->fmt);
73 key = va_arg(*ap, const char *);
75 set_error(s, "NULL object key");
85 if(json_object_set_new(object, key, value)) {
86 set_error(s, "Unable to add key \"%s\"", key);
100 static json_t *pack_array(scanner_t *s, va_list *ap)
102 json_t *array = json_array();
105 while(s->token != ']') {
109 set_error(s, "Unexpected end of format string");
117 if(json_array_append_new(array, value)) {
118 set_error(s, "Unable to append to array");
131 static json_t *pack(scanner_t *s, va_list *ap)
135 return pack_object(s, ap);
138 return pack_array(s, ap);
140 case 's': /* string */
142 const char *str = va_arg(*ap, const char *);
145 set_error(s, "NULL string");
148 return json_string(str);
154 case 'b': /* boolean */
155 return va_arg(*ap, int) ? json_true() : json_false();
157 case 'i': /* integer */
158 return json_integer(va_arg(*ap, int));
160 case 'f': /* double-precision float */
161 return json_real(va_arg(*ap, double));
163 case 'O': /* a json_t object; increments refcount */
164 return json_incref(va_arg(*ap, json_t *));
166 case 'o': /* a json_t object; doesn't increment refcount */
167 return va_arg(*ap, json_t *);
169 default: /* Whoops! */
170 set_error(s, "Unrecognized format character '%c'", s->token);
175 static int unpack(scanner_t *s, json_t *root, va_list *ap);
177 static int unpack_object(scanner_t *s, json_t *root, va_list *ap)
182 /* Use a set (emulated by a hashtable) to check that all object
183 keys are accessed. Checking that the correct number of keys
184 were accessed is not enough, as the same key can be unpacked
187 hashtable_t *key_set;
189 if(!(s->flags & JSON_UNPACK_ONLY)) {
190 key_set = hashtable_create(jsonp_hash_key, jsonp_key_equal, NULL, NULL);
192 set_error(s, "Out of memory");
197 if(!json_is_object(root)) {
198 set_error(s, "Expected object, got %i", json_typeof(root));
203 while(s->token != '}') {
208 set_error(s, "Expected '}' after '*', got '%c'", s->token);
213 set_error(s, "Unexpected end of format string");
217 if(s->token == '*') {
223 if(s->token != 's') {
224 set_error(s, "Expected format 's', got '%c'\n", *s->fmt);
228 key = va_arg(*ap, const char *);
230 set_error(s, "NULL object key");
236 value = json_object_get(root, key);
237 if(unpack(s, value, ap))
240 if(!(s->flags & JSON_UNPACK_ONLY))
241 hashtable_set(key_set, (void *)key, NULL);
246 if(s->flags & JSON_UNPACK_ONLY)
249 if(!wildcard && key_set->size != json_object_size(root)) {
250 long diff = (long)json_object_size(root) - (long)key_set->size;
251 set_error(s, "%li object items left unpacked", diff);
258 if(!(s->flags & JSON_UNPACK_ONLY))
259 hashtable_destroy(key_set);
264 static int unpack_array(scanner_t *s, json_t *root, va_list *ap)
269 if(!json_is_array(root)) {
270 set_error(s, "Expected array, got %d", json_typeof(root));
275 while(s->token != ']') {
279 set_error(s, "Expected ']' after '*', got '%c'", s->token);
284 set_error(s, "Unexpected end of format string");
288 if(s->token == '*') {
294 value = json_array_get(root, i);
296 set_error(s, "Array index %lu out of range", (unsigned long)i);
300 if(unpack(s, value, ap))
307 if(s->flags & JSON_UNPACK_ONLY)
310 if(!wildcard && i != json_array_size(root)) {
311 long diff = (long)json_array_size(root) - (long)i;
312 set_error(s, "%li array items left upacked", diff);
319 static int unpack(scanner_t *s, json_t *root, va_list *ap)
324 return unpack_object(s, root, ap);
327 return unpack_array(s, root, ap);
330 if(!json_is_string(root))
332 set_error(s, "Type mismatch! Object (%i) wasn't a string.",
337 if(!(s->flags & JSON_VALIDATE_ONLY)) {
340 str = va_arg(*ap, const char **);
342 set_error(s, "Passed a NULL string pointer!");
346 *str = json_string_value(root);
351 if(!json_is_integer(root))
353 set_error(s, "Type mismatch! Object (%i) wasn't an integer.",
358 if(!(s->flags & JSON_VALIDATE_ONLY))
359 *va_arg(*ap, int*) = json_integer_value(root);
364 if(!json_is_boolean(root))
366 set_error(s, "Type mismatch! Object (%i) wasn't a boolean.",
371 if(!(s->flags & JSON_VALIDATE_ONLY))
372 *va_arg(*ap, int*) = json_is_true(root);
377 if(!json_is_number(root))
379 set_error(s, "Type mismatch! Object (%i) wasn't a real.",
384 if(!(s->flags & JSON_VALIDATE_ONLY))
385 *va_arg(*ap, double*) = json_number_value(root);
390 if(!(s->flags & JSON_VALIDATE_ONLY))
395 if(!(s->flags & JSON_VALIDATE_ONLY))
396 *va_arg(*ap, json_t**) = root;
401 /* Never assign, just validate */
402 if(!json_is_null(root))
404 set_error(s, "Type mismatch! Object (%i) wasn't null.",
411 set_error(s, "Unknown format character '%c'", s->token);
416 json_t *json_vpack_ex(json_error_t *error, size_t flags,
417 const char *fmt, va_list ap)
422 jsonp_error_init(error, "");
425 jsonp_error_set(error, 1, 1, "Null or empty format string!");
436 value = pack(&s, &ap);
441 set_error(&s, "Garbage after format string");
448 json_t *json_pack_ex(json_error_t *error, size_t flags, const char *fmt, ...)
454 value = json_vpack_ex(error, flags, fmt, ap);
460 json_t *json_pack(const char *fmt, ...)
466 value = json_vpack_ex(NULL, 0, fmt, ap);
472 int json_vunpack_ex(json_t *root, json_error_t *error, size_t flags,
473 const char *fmt, va_list ap)
477 jsonp_error_init(error, "");
480 jsonp_error_set(error, 1, 1, "Null or empty format string!");
492 if(unpack(&s, root, &ap))
497 set_error(&s, "Garbage after format string");
504 int json_unpack_ex(json_t *root, json_error_t *error, size_t flags, const char *fmt, ...)
510 ret = json_vunpack_ex(root, error, flags, fmt, ap);
516 int json_unpack(json_t *root, const char *fmt, ...)
522 ret = json_vunpack_ex(root, NULL, 0, fmt, ap);