2010-10-26 07:36:29 +08:00
|
|
|
/*
|
2011-01-21 03:28:54 +08:00
|
|
|
* Copyright (c) 2009-2011 Petri Lehtinen <petri@digip.org>
|
|
|
|
* Copyright (c) 2010-2011 Graeme Smecher <graeme.smecher@mail.mcgill.ca>
|
2010-10-26 07:36:29 +08:00
|
|
|
*
|
|
|
|
* Jansson is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the MIT license. See LICENSE for details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <jansson.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
json_t *value;
|
|
|
|
int i;
|
2011-01-15 01:18:42 +08:00
|
|
|
json_error_t error;
|
2010-10-26 07:36:29 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Simple, valid json_pack cases
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* true */
|
2011-01-25 15:36:43 +08:00
|
|
|
value = json_pack_ex(&error, 0, "b", 1);
|
2010-10-26 07:36:29 +08:00
|
|
|
if(!json_is_true(value))
|
2011-01-21 03:09:14 +08:00
|
|
|
fail("json_pack boolean failed");
|
2010-10-26 07:36:29 +08:00
|
|
|
if(value->refcount != (ssize_t)-1)
|
2011-01-21 03:09:14 +08:00
|
|
|
fail("json_pack boolean refcount failed");
|
2010-10-26 07:36:29 +08:00
|
|
|
json_decref(value);
|
|
|
|
|
|
|
|
/* false */
|
2011-01-25 15:36:43 +08:00
|
|
|
value = json_pack_ex(&error, 0, "b", 0);
|
2010-10-26 07:36:29 +08:00
|
|
|
if(!json_is_false(value))
|
2011-01-21 03:09:14 +08:00
|
|
|
fail("json_pack boolean failed");
|
2010-10-26 07:36:29 +08:00
|
|
|
if(value->refcount != (ssize_t)-1)
|
2011-01-21 03:09:14 +08:00
|
|
|
fail("json_pack boolean refcount failed");
|
2010-10-26 07:36:29 +08:00
|
|
|
json_decref(value);
|
|
|
|
|
|
|
|
/* null */
|
2011-01-25 15:36:43 +08:00
|
|
|
value = json_pack_ex(&error, 0, "n");
|
2010-10-26 07:36:29 +08:00
|
|
|
if(!json_is_null(value))
|
2011-01-21 03:09:14 +08:00
|
|
|
fail("json_pack null failed");
|
2010-10-26 07:36:29 +08:00
|
|
|
if(value->refcount != (ssize_t)-1)
|
2011-01-21 03:09:14 +08:00
|
|
|
fail("json_pack null refcount failed");
|
2010-10-26 07:36:29 +08:00
|
|
|
json_decref(value);
|
|
|
|
|
|
|
|
/* integer */
|
2011-01-25 15:36:43 +08:00
|
|
|
value = json_pack_ex(&error, 0, "i", 1);
|
2010-10-26 07:36:29 +08:00
|
|
|
if(!json_is_integer(value) || json_integer_value(value) != 1)
|
2011-01-21 03:09:14 +08:00
|
|
|
fail("json_pack integer failed");
|
2010-10-26 07:36:29 +08:00
|
|
|
if(value->refcount != (ssize_t)1)
|
2011-01-21 03:09:14 +08:00
|
|
|
fail("json_pack integer refcount failed");
|
2010-10-26 07:36:29 +08:00
|
|
|
json_decref(value);
|
|
|
|
|
|
|
|
|
|
|
|
/* real */
|
2011-01-25 15:36:43 +08:00
|
|
|
value = json_pack_ex(&error, 0, "f", 1.0);
|
2010-10-26 07:36:29 +08:00
|
|
|
if(!json_is_real(value) || json_real_value(value) != 1.0)
|
2011-01-21 03:09:14 +08:00
|
|
|
fail("json_pack real failed");
|
2010-10-26 07:36:29 +08:00
|
|
|
if(value->refcount != (ssize_t)1)
|
2011-01-21 03:09:14 +08:00
|
|
|
fail("json_pack real refcount failed");
|
2010-10-26 07:36:29 +08:00
|
|
|
json_decref(value);
|
|
|
|
|
|
|
|
/* string */
|
2011-01-25 15:36:43 +08:00
|
|
|
value = json_pack_ex(&error, 0, "s", "test");
|
2010-10-26 07:36:29 +08:00
|
|
|
if(!json_is_string(value) || strcmp("test", json_string_value(value)))
|
2011-01-21 03:09:14 +08:00
|
|
|
fail("json_pack string failed");
|
2010-10-26 07:36:29 +08:00
|
|
|
if(value->refcount != (ssize_t)1)
|
2011-01-21 03:09:14 +08:00
|
|
|
fail("json_pack string refcount failed");
|
2010-10-26 07:36:29 +08:00
|
|
|
json_decref(value);
|
|
|
|
|
|
|
|
/* empty object */
|
2011-01-25 15:36:43 +08:00
|
|
|
value = json_pack_ex(&error, 0, "{}", 1.0);
|
2010-10-26 07:36:29 +08:00
|
|
|
if(!json_is_object(value) || json_object_size(value) != 0)
|
2011-01-21 03:09:14 +08:00
|
|
|
fail("json_pack empty object failed");
|
2010-10-26 07:36:29 +08:00
|
|
|
if(value->refcount != (ssize_t)1)
|
2011-01-21 03:09:14 +08:00
|
|
|
fail("json_pack empty object refcount failed");
|
2010-10-26 07:36:29 +08:00
|
|
|
json_decref(value);
|
|
|
|
|
|
|
|
/* empty list */
|
2011-01-25 15:36:43 +08:00
|
|
|
value = json_pack_ex(&error, 0, "[]", 1.0);
|
2010-10-26 07:36:29 +08:00
|
|
|
if(!json_is_array(value) || json_array_size(value) != 0)
|
2011-01-21 03:09:14 +08:00
|
|
|
fail("json_pack empty list failed");
|
2010-10-26 07:36:29 +08:00
|
|
|
if(value->refcount != (ssize_t)1)
|
2011-01-21 03:09:14 +08:00
|
|
|
fail("json_pack empty list failed");
|
2010-10-26 07:36:29 +08:00
|
|
|
json_decref(value);
|
|
|
|
|
|
|
|
/* non-incref'd object */
|
2011-01-25 15:36:43 +08:00
|
|
|
value = json_pack_ex(&error, 0, "o", json_integer(1));
|
2010-10-26 07:36:29 +08:00
|
|
|
if(!json_is_integer(value) || json_integer_value(value) != 1)
|
2011-01-21 03:09:14 +08:00
|
|
|
fail("json_pack object failed");
|
2010-10-26 07:36:29 +08:00
|
|
|
if(value->refcount != (ssize_t)1)
|
2011-01-21 03:09:14 +08:00
|
|
|
fail("json_pack integer refcount failed");
|
2010-10-26 07:36:29 +08:00
|
|
|
json_decref(value);
|
|
|
|
|
|
|
|
/* incref'd object */
|
2011-01-25 15:36:43 +08:00
|
|
|
value = json_pack_ex(&error, 0, "O", json_integer(1));
|
2010-10-26 07:36:29 +08:00
|
|
|
if(!json_is_integer(value) || json_integer_value(value) != 1)
|
2011-01-21 03:09:14 +08:00
|
|
|
fail("json_pack object failed");
|
2010-10-26 07:36:29 +08:00
|
|
|
if(value->refcount != (ssize_t)2)
|
2011-01-21 03:09:14 +08:00
|
|
|
fail("json_pack integer refcount failed");
|
2010-10-26 07:36:29 +08:00
|
|
|
json_decref(value);
|
|
|
|
json_decref(value);
|
|
|
|
|
|
|
|
/* simple object */
|
2011-01-25 15:36:43 +08:00
|
|
|
value = json_pack_ex(&error, 0, "{s:[]}", "foo");
|
2010-10-26 07:36:29 +08:00
|
|
|
if(!json_is_object(value) || json_object_size(value) != 1)
|
2011-01-21 03:09:14 +08:00
|
|
|
fail("json_pack array failed");
|
2010-10-26 07:36:29 +08:00
|
|
|
if(!json_is_array(json_object_get(value, "foo")))
|
2011-01-21 03:09:14 +08:00
|
|
|
fail("json_pack array failed");
|
2010-10-26 07:36:29 +08:00
|
|
|
if(json_object_get(value, "foo")->refcount != (ssize_t)1)
|
2011-01-21 03:09:14 +08:00
|
|
|
fail("json_pack object refcount failed");
|
2010-10-26 07:36:29 +08:00
|
|
|
json_decref(value);
|
|
|
|
|
|
|
|
/* simple array */
|
2011-01-25 15:36:43 +08:00
|
|
|
value = json_pack_ex(&error, 0, "[i,i,i]", 0, 1, 2);
|
2010-10-26 07:36:29 +08:00
|
|
|
if(!json_is_array(value) || json_array_size(value) != 3)
|
2011-01-21 03:09:14 +08:00
|
|
|
fail("json_pack object failed");
|
2010-10-26 07:36:29 +08:00
|
|
|
for(i=0; i<3; i++)
|
|
|
|
{
|
|
|
|
if(!json_is_integer(json_array_get(value, i)) ||
|
|
|
|
json_integer_value(json_array_get(value, i)) != i)
|
|
|
|
|
|
|
|
fail("json_pack integer array failed");
|
|
|
|
}
|
|
|
|
json_decref(value);
|
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
/* Whitespace; regular string */
|
2011-01-25 15:36:43 +08:00
|
|
|
value = json_pack_ex(&error, 0, " s ", "test");
|
2011-01-15 01:18:42 +08:00
|
|
|
if(!json_is_string(value) || strcmp("test", json_string_value(value)))
|
2011-01-21 03:09:14 +08:00
|
|
|
fail("json_pack string (with whitespace) failed");
|
2011-01-15 01:18:42 +08:00
|
|
|
json_decref(value);
|
|
|
|
|
|
|
|
/* Whitespace; empty array */
|
2011-01-25 15:36:43 +08:00
|
|
|
value = json_pack_ex(&error, 0, "[ ]");
|
2011-01-15 01:18:42 +08:00
|
|
|
if(!json_is_array(value) || json_array_size(value) != 0)
|
2011-01-21 03:09:14 +08:00
|
|
|
fail("json_pack empty array (with whitespace) failed");
|
2011-01-15 01:18:42 +08:00
|
|
|
json_decref(value);
|
|
|
|
|
|
|
|
/* Whitespace; array */
|
2011-01-25 15:36:43 +08:00
|
|
|
value = json_pack_ex(&error, 0, "[ i , i, i ] ", 1, 2, 3);
|
2011-01-15 01:18:42 +08:00
|
|
|
if(!json_is_array(value) || json_array_size(value) != 3)
|
2011-01-21 03:09:14 +08:00
|
|
|
fail("json_pack array (with whitespace) failed");
|
2011-01-15 01:18:42 +08:00
|
|
|
json_decref(value);
|
|
|
|
|
2010-10-26 07:36:29 +08:00
|
|
|
/*
|
|
|
|
* Invalid cases
|
|
|
|
*/
|
2011-01-15 01:18:42 +08:00
|
|
|
|
2010-10-26 07:36:29 +08:00
|
|
|
/* mismatched open/close array/object */
|
2011-01-25 15:36:43 +08:00
|
|
|
if(json_pack_ex(&error, 0, "[}"))
|
2010-10-26 07:36:29 +08:00
|
|
|
fail("json_pack failed to catch mismatched '}'");
|
2011-01-15 01:18:42 +08:00
|
|
|
if(error.line != 1 || error.column != 2)
|
|
|
|
fail("json_pack didn't get the error coordinates right!");
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-25 15:36:43 +08:00
|
|
|
if(json_pack_ex(&error, 0, "{]"))
|
2010-10-26 07:36:29 +08:00
|
|
|
fail("json_pack failed to catch mismatched ']'");
|
2011-01-15 01:18:42 +08:00
|
|
|
if(error.line != 1 || error.column != 2)
|
|
|
|
fail("json_pack didn't get the error coordinates right!");
|
2010-10-26 07:36:29 +08:00
|
|
|
|
|
|
|
/* missing close array */
|
2011-01-25 15:36:43 +08:00
|
|
|
if(json_pack_ex(&error, 0, "["))
|
2010-10-26 07:36:29 +08:00
|
|
|
fail("json_pack failed to catch missing ']'");
|
2011-01-15 01:18:42 +08:00
|
|
|
if(error.line != 1 || error.column != 2)
|
|
|
|
fail("json_pack didn't get the error coordinates right!");
|
2010-10-26 07:36:29 +08:00
|
|
|
|
|
|
|
/* missing close object */
|
2011-01-25 15:36:43 +08:00
|
|
|
if(json_pack_ex(&error, 0, "{"))
|
2010-10-26 07:36:29 +08:00
|
|
|
fail("json_pack failed to catch missing '}'");
|
2011-01-15 01:18:42 +08:00
|
|
|
if(error.line != 1 || error.column != 2)
|
|
|
|
fail("json_pack didn't get the error coordinates right!");
|
2010-10-26 07:36:29 +08:00
|
|
|
|
|
|
|
/* NULL string */
|
2011-01-25 15:36:43 +08:00
|
|
|
if(json_pack_ex(&error, 0, "s", NULL))
|
2011-01-15 01:18:42 +08:00
|
|
|
fail("json_pack failed to catch null argument string");
|
|
|
|
if(error.line != 1 || error.column != 1)
|
|
|
|
fail("json_pack didn't get the error coordinates right!");
|
|
|
|
|
|
|
|
/* NULL format */
|
2011-01-25 15:36:43 +08:00
|
|
|
if(json_pack_ex(&error, 0, NULL))
|
2011-01-15 01:18:42 +08:00
|
|
|
fail("json_pack failed to catch NULL format string");
|
2011-02-22 16:47:02 +08:00
|
|
|
if(error.line != -1 || error.column != -1)
|
2011-01-15 01:18:42 +08:00
|
|
|
fail("json_pack didn't get the error coordinates right!");
|
|
|
|
|
|
|
|
/* More complicated checks for row/columns */
|
2011-01-25 15:36:43 +08:00
|
|
|
if(json_pack_ex(&error, 0, "{ {}: s }", "foo"))
|
2011-01-15 01:18:42 +08:00
|
|
|
fail("json_pack failed to catch object as key");
|
|
|
|
if(error.line != 1 || error.column != 3)
|
|
|
|
fail("json_pack didn't get the error coordinates right!");
|
|
|
|
|
2011-01-25 15:36:43 +08:00
|
|
|
if(json_pack_ex(&error, 0, "{ s: {}, s:[ii{} }", "foo", "bar", 12, 13))
|
2011-01-15 01:18:42 +08:00
|
|
|
fail("json_pack failed to catch missing ]");
|
2011-01-24 03:14:19 +08:00
|
|
|
if(error.line != 1 || error.column != 19)
|
2011-01-15 01:18:42 +08:00
|
|
|
fail("json_pack didn't get the error coordinates right!");
|
|
|
|
|
2011-01-25 15:36:43 +08:00
|
|
|
if(json_pack_ex(&error, 0, "[[[[[ [[[[[ [[[[ }]]]] ]]]] ]]]]]"))
|
2011-01-21 03:09:14 +08:00
|
|
|
fail("json_pack failed to catch extra }");
|
2011-01-15 01:18:42 +08:00
|
|
|
if(error.line != 1 || error.column != 21)
|
|
|
|
fail("json_pack didn't get the error coordinates right!");
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-21 03:09:14 +08:00
|
|
|
return 0;
|
2010-10-26 07:36:29 +08:00
|
|
|
}
|