Browse Source

Merge pull request #283 from commodo/json_pointer

json_pointer: add first revision based on RFC 6901
tags/json-c-0.13-20171207
Eric Haszlakiewicz GitHub 9 years ago
parent
commit
e8ce1db471
17 changed files with 676 additions and 17 deletions
  1. +1
    -0
      .gitignore
  2. +3
    -0
      CMakeLists.txt
  3. +3
    -0
      Makefile.am
  4. +13
    -0
      compat/strdup.h
  5. +9
    -0
      json-c.vcproj
  6. +4
    -1
      json-c.vcxproj
  7. +10
    -1
      json-c.vcxproj.filters
  8. +1
    -0
      json.h
  9. +1
    -7
      json_object.c
  10. +239
    -0
      json_pointer.c
  11. +69
    -0
      json_pointer.h
  12. +1
    -7
      json_tokener.c
  13. +1
    -0
      tests/Makefile.am
  14. +1
    -1
      tests/strerror_override.c
  15. +272
    -0
      tests/test_json_pointer.c
  16. +36
    -0
      tests/test_json_pointer.expected
  17. +12
    -0
      tests/test_json_pointer.test

+ 1
- 0
.gitignore View File

@@ -35,6 +35,7 @@
/tests/test_set_value
/tests/test_util_file
/tests/test_visit
/tests/test_json_pointer
/tests/*.vg.out
/tests/*.log
/tests/*.trs


+ 3
- 0
CMakeLists.txt View File

@@ -23,6 +23,7 @@ endif()
include_directories(${CMAKE_CURRENT_BINARY_DIR}/include)

set(JSON_C_HEADERS
./compat/strdup.h
./json.h
${CMAKE_CURRENT_BINARY_DIR}/include/config.h
./json_config.h
@@ -31,6 +32,7 @@ set(JSON_C_HEADERS
./json_inttypes.h
./json_object.h
./json_object_private.h
./json_pointer.h
./json_tokener.h
./json_util.h
./linkhash.h
@@ -43,6 +45,7 @@ set(JSON_C_SOURCES
./arraylist.c
./debug.c
./json_object.c
./json_pointer.c
./json_tokener.c
./json_util.c
./linkhash.c


+ 3
- 0
Makefile.am View File

@@ -18,6 +18,7 @@ libjson_cincludedir = $(includedir)/json-c
libjson_cinclude_HEADERS = \
arraylist.h \
bits.h \
compat/strdup.h \
debug.h \
json.h \
json_c_version.h \
@@ -26,6 +27,7 @@ libjson_cinclude_HEADERS = \
json_object.h \
json_object_iterator.h \
json_object_private.h \
json_pointer.h \
json_tokener.h \
json_util.h \
json_visit.h \
@@ -42,6 +44,7 @@ libjson_c_la_SOURCES = \
json_c_version.c \
json_object.c \
json_object_iterator.c \
json_pointer.c \
json_tokener.c \
json_util.c \
json_visit.c \


+ 13
- 0
compat/strdup.h View File

@@ -0,0 +1,13 @@
#ifndef __STRDUP_H_COMPAT__
#define __STRDUP_H_COMPAT__

#include "../config.h"

#if !defined(HAVE_STRDUP) && defined(_MSC_VER)
/* MSC has the version as _strdup */
# define strdup _strdup
#elif !defined(HAVE_STRDUP)
# error You do not have strdup on your system.
#endif /* HAVE_STRDUP */

#endif /* __STRDUP_H_COMPAT__ */

+ 9
- 0
json-c.vcproj View File

@@ -108,6 +108,9 @@ copy json_config.h.win32 json_config.h"/>
<File
RelativePath=".\json_object.c">
</File>
<File
RelativePath=".\json_pointer.c">
</File>
<File
RelativePath=".\json_tokener.c">
</File>
@@ -131,6 +134,9 @@ copy json_config.h.win32 json_config.h"/>
<File
RelativePath=".\bits.h">
</File>
<File
RelativePath=".\compat\strdup.h">
</File>
<File
RelativePath=".\debug.h">
</File>
@@ -140,6 +146,9 @@ copy json_config.h.win32 json_config.h"/>
<File
RelativePath=".\json_object_private.h">
</File>
<File
RelativePath=".\json_pointer.h">
</File>
<File
RelativePath=".\json_tokener.h">
</File>


+ 4
- 1
json-c.vcxproj View File

@@ -133,6 +133,7 @@ copy json_config.h.win32 json_config.h
<ClCompile Include="arraylist.c" />
<ClCompile Include="debug.c" />
<ClCompile Include="json_object.c" />
<ClCompile Include="json_pointer.c" />
<ClCompile Include="json_tokener.c" />
<ClCompile Include="json_util.c" />
<ClCompile Include="linkhash.c" />
@@ -141,10 +142,12 @@ copy json_config.h.win32 json_config.h
</ItemGroup>
<ItemGroup>
<ClInclude Include="arraylist.h" />
<ClInclude Include="compat\strdup.h" />
<ClInclude Include="debug.h" />
<ClInclude Include="json_inttypes.h" />
<ClInclude Include="json_object.h" />
<ClInclude Include="json_object_private.h" />
<ClInclude Include="json_pointer.h" />
<ClInclude Include="json_tokener.h" />
<ClInclude Include="json_util.h" />
<ClInclude Include="linkhash.h" />
@@ -162,4 +165,4 @@ copy json_config.h.win32 json_config.h
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
</Project>

+ 10
- 1
json-c.vcxproj.filters View File

@@ -27,6 +27,9 @@
<ClCompile Include="json_object.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="json_pointer.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="json_tokener.c">
<Filter>Source Files</Filter>
</ClCompile>
@@ -47,6 +50,9 @@
<ClInclude Include="arraylist.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="compat\strdup.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="debug.h">
<Filter>Header Files</Filter>
</ClInclude>
@@ -56,6 +62,9 @@
<ClInclude Include="json_object_private.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="json_pointer.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="json_tokener.h">
<Filter>Header Files</Filter>
</ClInclude>
@@ -87,4 +96,4 @@
<None Include="README-WIN32.html" />
<None Include="README.html" />
</ItemGroup>
</Project>
</Project>

+ 1
- 0
json.h View File

@@ -22,6 +22,7 @@ extern "C" {
#include "arraylist.h"
#include "json_util.h"
#include "json_object.h"
#include "json_pointer.h"
#include "json_tokener.h"
#include "json_object_iterator.h"
#include "json_c_version.h"


+ 1
- 7
json_object.c View File

@@ -29,13 +29,7 @@
#include "json_object_private.h"
#include "json_util.h"
#include "math_compat.h"

#if !defined(HAVE_STRDUP) && defined(_MSC_VER)
/* MSC has the version as _strdup */
# define strdup _strdup
#elif !defined(HAVE_STRDUP)
# error You do not have strdup on your system.
#endif /* HAVE_STRDUP */
#include "compat/strdup.h"

#if !defined(HAVE_SNPRINTF) && defined(_MSC_VER)
/* MSC has the version as _snprintf */


+ 239
- 0
json_pointer.c View File

@@ -0,0 +1,239 @@
/*
* Copyright (c) 2016 Alexandru Ardelean.
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See COPYING for details.
*
*/

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <ctype.h>

#include "json_pointer.h"

/**
* JavaScript Object Notation (JSON) Pointer
* RFC 6901 - https://tools.ietf.org/html/rfc6901
*/

static void string_replace_all_occurrences_with_char(char *s, const char *occur, char repl_char)
{
int slen = strlen(s);
int skip = strlen(occur) - 1; /* length of the occurence, minus the char we're replacing */
char *p = s;
while ((p = strstr(p, occur))) {
*p = repl_char;
p++;
slen -= skip;
memcpy(p, (p + skip), slen - (p - s) + 1); /* includes null char too */
}
}

static int is_valid_index(struct json_object *jo, const char *path, int32_t *idx)
{
int i, len = strlen(path);
/* this code-path optimizes a bit, for when we reference the 0-9 index range in a JSON array
and because leading zeros not allowed */
if (len == 1) {
if (isdigit(path[0])) {
*idx = (path[0] - '0');
goto check_oob;
}
errno = EINVAL;
return 0;
}
/* leading zeros not allowed per RFC */
if (path[0] == '0') {
errno = EINVAL;
return 0;
}
/* RFC states base-10 decimals */
for (i = 0; i < len; i++) {
if (!isdigit(path[i])) {
errno = EINVAL;
return 0;
}
}

*idx = strtol(path, NULL, 10);
if (*idx < 0) {
errno = EINVAL;
return 0;
}
check_oob:
len = json_object_array_length(jo);
if (*idx >= len) {
errno = ENOENT;
return 0;
}

return 1;
}

static int json_pointer_get_single_path(struct json_object *obj, char *path, struct json_object **value)
{
if (json_object_is_type(obj, json_type_array)) {
int32_t idx;
if (!is_valid_index(obj, path, &idx))
return -1;
obj = json_object_array_get_idx(obj, idx);
if (obj) {
if (value)
*value = obj;
return 0;
}
/* Entry not found */
errno = ENOENT;
return -1;
}

/* RFC states that we first must eval all ~1 then all ~0 */
string_replace_all_occurrences_with_char(path, "~1", '/');
string_replace_all_occurrences_with_char(path, "~0", '~');

if (!json_object_object_get_ex(obj, path, value)) {
errno = ENOENT;
return -1;
}

return 0;
}

static int json_pointer_set_single_path(
struct json_object *parent,
const char *path,
struct json_object *value)
{
if (json_object_is_type(parent, json_type_array)) {
int32_t idx;
/* RFC (Chapter 4) states that '-' may be used to add new elements to an array */
if (path[0] == '-' && path[1] == '\0')
return json_object_array_add(parent, value);
if (!is_valid_index(parent, path, &idx))
return -1;
return json_object_array_put_idx(parent, idx, value);
}

/* path replacements should have been done in json_pointer_get_single_path(),
and we should still be good here */
if (json_object_is_type(parent, json_type_object))
return json_object_object_add(parent, path, value);

/* Getting here means that we tried to "dereference" a primitive JSON type (like string, int, bool).
i.e. add a sub-object to it */
errno = ENOENT;
return -1;
}

static int json_pointer_get_recursive(
struct json_object *obj,
char *path,
struct json_object **value)
{
char *endp;
int rc;

/* All paths (on each recursion level must have a leading '/' */
if (path[0] != '/') {
errno = EINVAL;
return -1;
}
path++;

endp = strchr(path, '/');
if (endp)
*endp = '\0';

/* If we err-ed here, return here */
if ((rc = json_pointer_get_single_path(obj, path, &obj)))
return rc;

if (endp) {
*endp = '/'; /* Put the slash back, so that the sanity check passes on next recursion level */
return json_pointer_get_recursive(obj, endp, value);
}

/* We should be at the end of the recursion here */
if (value)
*value = obj;

return 0;
}

int json_pointer_get(struct json_object *obj, const char *path, struct json_object **res)
{
char *path_copy = NULL;
int rc;

if (!obj || !path) {
errno = EINVAL;
return -1;
}

if (path[0] == '\0') {
if (res)
*res = obj;
return 0;
}

/* pass a working copy to the recursive call */
if (!(path_copy = strdup(path))) {
errno = ENOMEM;
return -1;
}
rc = json_pointer_get_recursive(obj, path_copy, res);
free(path_copy);

return rc;
}

int json_pointer_set(struct json_object **obj, const char *path, struct json_object *value)
{
const char *endp;
char *path_copy = NULL;
struct json_object *set = NULL;
int rc;

if (!obj || !path) {
errno = EINVAL;
return -1;
}

if (path[0] == '\0') {
json_object_put(*obj);
*obj = value;
return 0;
}

if (path[0] != '/') {
errno = EINVAL;
return -1;
}

/* If there's only 1 level to set, stop here */
if ((endp = strrchr(path, '/')) == path) {
path++;
return json_pointer_set_single_path(*obj, path, value);
}

/* pass a working copy to the recursive call */
if (!(path_copy = strdup(path))) {
errno = ENOMEM;
return -1;
}
path_copy[endp - path] = '\0';
rc = json_pointer_get_recursive(*obj, path_copy, &set);
free(path_copy);

if (rc)
return rc;

endp++;
return json_pointer_set_single_path(set, endp, value);
}


+ 69
- 0
json_pointer.h View File

@@ -0,0 +1,69 @@
/*
* Copyright (c) 2016 Alexadru Ardelean.
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See COPYING for details.
*
*/

#ifndef _json_pointer_h_
#define _json_pointer_h_

#include "json_object.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
* Retrieves a JSON sub-object from inside another JSON object
* using the JSON pointer notation as defined in RFC 6901
* https://tools.ietf.org/html/rfc6901
*
* The returned JSON sub-object is equivalent to parsing manually the
* 'obj' JSON tree ; i.e. it's not a new object that is created, but rather
* a pointer inside the JSON tree.
*
* Internally, this is equivalent to doing a series of 'json_object_object_get()'
* and 'json_object_array_get_idx()' along the given 'path'.
*
* @param obj the json_object instance/tree from where to retrieve sub-objects
* @param path a (RFC6901) string notation for the sub-object to retrieve
* @param res a pointer where to store a reference to the json_object
* associated with the given path
*
* @return negative if an error (or not found), or 0 if succeeded
*/
int json_pointer_get(struct json_object *obj, const char *path, struct json_object **res);

/**
* Sets JSON object 'value' in the 'obj' tree at the location specified
* by the 'path'. 'path' is JSON pointer notation as defined in RFC 6901
* https://tools.ietf.org/html/rfc6901
*
* Note that 'obj' is a double pointer, mostly for the "" (empty string)
* case, where the entire JSON object would be replaced by 'value'.
* In the case of the "" path, the object at '*obj' will have it's refcount
* decremented with 'json_object_put()' and the 'value' object will be assigned to it.
*
* For other cases (JSON sub-objects) ownership of 'value' will be transferred into
* '*obj' via 'json_object_object_add()' & 'json_object_array_put_idx()', so the
* only time the refcount should be decremented for 'value' is when the return value of
* 'json_pointer_set()' is negative (meaning the 'value' object did not get set into '*obj').
*
* That also implies that 'json_pointer_set()' does not do any refcount incrementing.
* (Just that single decrement that was mentioned above).
*
* @param obj the json_object instance/tree to which to add a sub-object
* @param path a (RFC6901) string notation for the sub-object to set in the tree
* @param value object to set at path
*
* @return negative if an error (or not found), or 0 if succeeded
*/
int json_pointer_set(struct json_object **obj, const char *path, struct json_object *value);

#ifdef __cplusplus
}
#endif

#endif

+ 1
- 7
json_tokener.c View File

@@ -31,6 +31,7 @@
#include "json_object.h"
#include "json_tokener.h"
#include "json_util.h"
#include "compat/strdup.h"

#ifdef HAVE_LOCALE_H
#include <locale.h>
@@ -41,13 +42,6 @@

#define jt_hexdigit(x) (((x) <= '9') ? (x) - '0' : ((x) & 7) + 9)

#if !HAVE_STRDUP && defined(_MSC_VER)
/* MSC has the version as _strdup */
# define strdup _strdup
#elif !HAVE_STRDUP
# error You do not have strdup on your system.
#endif /* HAVE_STRDUP */

#if !HAVE_STRNCASECMP && defined(_MSC_VER)
/* MSC has the version as _strnicmp */
# define strncasecmp _strnicmp


+ 1
- 0
tests/Makefile.am View File

@@ -23,6 +23,7 @@ TESTS+= test_set_serializer.test
TESTS+= test_compare.test
TESTS+= test_set_value.test
TESTS+= test_visit.test
TESTS+= test_json_pointer.test

check_PROGRAMS=
check_PROGRAMS += $(TESTS:.test=)


+ 1
- 1
tests/strerror_override.c View File

@@ -50,7 +50,7 @@ static struct {

#define PREFIX "ERRNO="
static char errno_buf[128] = PREFIX;
const char *strerror(int errno_in)
char *strerror(int errno_in)
{
int start_idx;
char digbuf[20];


+ 272
- 0
tests/test_json_pointer.c View File

@@ -0,0 +1,272 @@
#include <errno.h>
#include <assert.h>
#include <stdio.h>
#include <string.h>

#include "json.h"

static void test_example_int(struct json_object *jo1, const char *json_pointer, int expected_int)
{
struct json_object *jo2 = NULL;
assert(0 == json_pointer_get(jo1, json_pointer, NULL));
assert(0 == json_pointer_get(jo1, json_pointer, &jo2));
assert(json_object_is_type(jo2, json_type_int));
assert(expected_int == json_object_get_int(jo2));
printf("PASSED - GET - %s == %d\n", json_pointer, expected_int);
}

static const char *input_json_str = "{ "
"'foo': ['bar', 'baz'], "
"'': 0, "
"'a/b': 1, "
"'c\%d': 2, "
"'e^f': 3, "
"'g|h': 4, "
"'i\\\\j': 5, "
"'k\\\"l': 6, "
"' ': 7, "
"'m~n': 8 "
"}";


static const char *rec_input_json_str = "{"
"'arr' : ["
"{"
"'obj': ["
"{},{},"
"{"
"'obj1': 0,"
"'obj2': \"1\""
"}"
"]"
"}"
"],"
"'obj' : {"
"'obj': {"
"'obj': ["
"{"
"'obj1': 0,"
"'obj2': \"1\""
"}"
"]"
"}"
"}"
"}";

/* Example from RFC */
static void test_example_get()
{
int i;
struct json_object *jo1, *jo2, *jo3;
struct json_pointer_map_s_i {
const char *s;
int i;
};
/* Create a map to iterate over for the ints */
struct json_pointer_map_s_i json_pointers[] = {
{ "/", 0 },
{ "/a~1b", 1 },
{"/c%d", 2 },
{"/e^f", 3 },
{ "/g|h", 4 },
{ "/i\\j", 5 },
{ "/k\"l", 6 },
{ "/ ", 7 },
{ "/m~0n", 8 },
{ NULL, 0}
};

jo1 = json_tokener_parse(input_json_str);
assert(NULL != jo1);
printf("PASSED - GET - LOADED TEST JSON\n");
printf("%s\n", json_object_get_string(jo1));

/* Test empty string returns entire object */
jo2 = NULL;
/* For each test, we're trying to see that NULL **value works (does no segfault) */
assert(0 == json_pointer_get(jo1, "", NULL));
assert(0 == json_pointer_get(jo1, "", &jo2));
assert(json_object_equal(jo2, jo1));
printf("PASSED - GET - ENTIRE OBJECT WORKED\n");

/* Test /foo == ['bar', 'baz'] */
jo3 = json_object_new_array();
json_object_array_add(jo3, json_object_new_string("bar"));
json_object_array_add(jo3, json_object_new_string("baz"));

jo2 = NULL;
assert(0 == json_pointer_get(jo1, "/foo", NULL));
assert(0 == json_pointer_get(jo1, "/foo", &jo2));
assert(NULL != jo2);
assert(json_object_equal(jo2, jo3));
json_object_put(jo3);
printf("PASSED - GET - /foo == ['bar', 'baz']\n");

/* Test /foo/0 == 'bar' */
jo2 = NULL;
assert(0 == json_pointer_get(jo1, "/foo/0", NULL));
assert(0 == json_pointer_get(jo1, "/foo/0", &jo2));
assert(NULL != jo2);
assert(0 == strcmp("bar", json_object_get_string(jo2)));
printf("PASSED - GET - /foo/0 == 'bar'\n");

for (i = 0 ; json_pointers[i].s; i++)
test_example_int(jo1, json_pointers[i].s, json_pointers[i].i);

json_object_put(jo1);
}

/* I'm not too happy with the RFC example to test the recusion of the json_pointer_get() function */
static void test_recursion_get()
{
struct json_object *jo2, *jo1 = json_tokener_parse(rec_input_json_str);

jo2 = NULL;
assert(jo1 != NULL);
printf("%s\n", json_object_get_string(jo1));
assert(0 == json_pointer_get(jo1, "/arr/0/obj/2/obj1", &jo2));
assert(json_object_is_type(jo2, json_type_int));
assert(0 == json_object_get_int(jo2));

assert(0 == json_pointer_get(jo1, "/arr/0/obj/2/obj2", &jo2));
assert(json_object_is_type(jo2, json_type_string));
assert(0 == strcmp("1", json_object_get_string(jo2)));

assert(jo1 != NULL);
assert(0 == json_pointer_get(jo1, "/obj/obj/obj/0/obj1", &jo2));
assert(json_object_is_type(jo2, json_type_int));
assert(0 == json_object_get_int(jo2));

assert(0 == json_pointer_get(jo1, "/obj/obj/obj/0/obj2", &jo2));
assert(json_object_is_type(jo2, json_type_string));
assert(0 == strcmp("1", json_object_get_string(jo2)));

printf("PASSED - GET - RECURSION TEST\n");

json_object_put(jo1);
}

static void test_wrong_inputs_get()
{
struct json_object *jo2, *jo1 = json_tokener_parse(input_json_str);

assert(NULL != jo1);
printf("PASSED - GET - LOADED TEST JSON\n");
printf("%s\n", json_object_get_string(jo1));

/* Test leading '/' missing */
jo2 = NULL;
errno = 0;
assert(0 != json_pointer_get(jo1, "foo/bar", NULL));
assert(0 != json_pointer_get(jo1, "foo/bar", &jo2));
assert(errno == EINVAL);
assert(jo2 == NULL);
printf("PASSED - GET - MISSING /\n");

/* Test combinations of NULL params for input json & path */
errno = 0;
assert(0 != json_pointer_get(NULL, "foo/bar", NULL));
assert(errno == EINVAL);
errno = 0;
assert(0 != json_pointer_get(NULL, NULL, NULL));
assert(errno == EINVAL);
errno = 0;
assert(0 != json_pointer_get(jo1, NULL, NULL));
assert(errno == EINVAL);
printf("PASSED - GET - NULL INPUTS\n");

/* Test invalid indexes for array */
errno = 0;
assert(0 != json_pointer_get(jo1, "/foo/a", NULL));
assert(errno == EINVAL);
errno = 0;
assert(0 != json_pointer_get(jo1, "/foo/-", NULL));
assert(errno == EINVAL);
errno = 0;
/* Test optimized array path */
assert(0 != json_pointer_get(jo1, "/foo/4", NULL));
assert(errno == ENOENT);
errno = 0;
/* Test non-optimized array path */
assert(0 != json_pointer_get(jo1, "/foo/22", NULL));
assert(errno == ENOENT);
errno = 0;
assert(0 != json_pointer_get(jo1, "/foo/-1", NULL));
assert(errno == EINVAL);
errno = 0;
assert(0 != json_pointer_get(jo1, "/foo/10", NULL));
assert(errno == ENOENT);
printf("PASSED - GET - INVALID INDEXES\n");

json_object_put(jo1);
}

static void test_example_set()
{
struct json_object *jo2, *jo1 = json_tokener_parse(input_json_str);

assert(jo1 != NULL);
printf("PASSED - SET - LOADED TEST JSON\n");
printf("%s\n", json_object_get_string(jo1));

assert(0 == json_pointer_set(&jo1, "/foo/1", json_object_new_string("cod")));
assert(0 == strcmp("cod", json_object_get_string(json_object_array_get_idx(json_object_object_get(jo1, "foo"), 1))));
printf("PASSED - SET - 'cod' in /foo/1\n");
assert(0 != json_pointer_set(&jo1, "/fud/gaw", (jo2 = json_tokener_parse("[1,2,3]"))));
assert(errno == ENOENT);
printf("PASSED - SET - non-existing /fud/gaw\n");
assert(0 == json_pointer_set(&jo1, "/fud", json_object_new_object()));
printf("PASSED - SET - /fud == {}\n");
assert(0 == json_pointer_set(&jo1, "/fud/gaw", jo2)); /* re-using jo2 from above */
printf("PASSED - SET - /fug/gaw == [1,2,3]\n");
assert(0 == json_pointer_set(&jo1, "/fud/gaw/0", json_object_new_int(0)));
printf("PASSED - SET - /fug/gaw == [0,2,3]\n");
assert(0 == json_pointer_set(&jo1, "/fud/gaw/-", json_object_new_int(4)));
printf("PASSED - SET - /fug/gaw == [0,2,3,4]\n");
assert(0 == json_pointer_set(&jo1, "/", json_object_new_int(9)));
printf("PASSED - SET - / == 9\n");

jo2 = json_tokener_parse("{ 'foo': [ 'bar', 'cod' ], '': 9, 'a/b': 1, 'c\%d': 2, 'e^f': 3, 'g|h': 4, 'i\\\\j': 5, 'k\\\"l': 6, ' ': 7, 'm~n': 8, 'fud': { 'gaw': [ 0, 2, 3, 4 ] } }");
assert(json_object_equal(jo2, jo1));
printf("PASSED - SET - Final JSON is: %s\n", json_object_get_string(jo1));
json_object_put(jo2);

assert(0 == json_pointer_set(&jo1, "", json_object_new_int(10)));
assert(10 == json_object_get_int(jo1));
printf("%s\n", json_object_get_string(jo1));

json_object_put(jo1);
}

static void test_wrong_inputs_set()
{
struct json_object *jo2, *jo1 = json_tokener_parse(input_json_str);

assert(jo1 != NULL);
printf("PASSED - SET - LOADED TEST JSON\n");
printf("%s\n", json_object_get_string(jo1));

assert(0 != json_pointer_set(&jo1, "foo/bar", (jo2 = json_object_new_string("cod"))));
printf("PASSED - SET - failed 'cod' with path 'foo/bar'\n");
json_object_put(jo2);

assert(0 != json_pointer_set(&jo1, "/fud/gaw", (jo2 = json_object_new_string("whatever"))));
assert(0 == json_pointer_set(&jo1, "/fud", json_object_new_object()));
assert(0 == json_pointer_set(&jo1, "/fud/gaw", jo2)); /* re-using jo2 from above */
assert(0 != json_pointer_set(&jo1, "/fud/gaw/0", json_object_new_int(0)));
assert(0 != json_pointer_set(&jo1, "/fud/gaw/", json_object_new_int(0)));
printf("PASSED - SET - failed to set index to non-array\n");

json_object_put(jo1);
json_object_put(jo2);
}

int main(int argc, char **argv)
{
test_example_get();
test_recursion_get();
test_wrong_inputs_get();
test_example_set();
test_wrong_inputs_set();
return 0;
}

+ 36
- 0
tests/test_json_pointer.expected View File

@@ -0,0 +1,36 @@
PASSED - GET - LOADED TEST JSON
{ "foo": [ "bar", "baz" ], "": 0, "a\/b": 1, "c%d": 2, "e^f": 3, "g|h": 4, "i\\j": 5, "k\"l": 6, " ": 7, "m~n": 8 }
PASSED - GET - ENTIRE OBJECT WORKED
PASSED - GET - /foo == ['bar', 'baz']
PASSED - GET - /foo/0 == 'bar'
PASSED - GET - / == 0
PASSED - GET - /a~1b == 1
PASSED - GET - /c%d == 2
PASSED - GET - /e^f == 3
PASSED - GET - /g|h == 4
PASSED - GET - /i\j == 5
PASSED - GET - /k"l == 6
PASSED - GET - / == 7
PASSED - GET - /m~0n == 8
{ "arr": [ { "obj": [ { }, { }, { "obj1": 0, "obj2": "1" } ] } ], "obj": { "obj": { "obj": [ { "obj1": 0, "obj2": "1" } ] } } }
PASSED - GET - RECURSION TEST
PASSED - GET - LOADED TEST JSON
{ "foo": [ "bar", "baz" ], "": 0, "a\/b": 1, "c%d": 2, "e^f": 3, "g|h": 4, "i\\j": 5, "k\"l": 6, " ": 7, "m~n": 8 }
PASSED - GET - MISSING /
PASSED - GET - NULL INPUTS
PASSED - GET - INVALID INDEXES
PASSED - SET - LOADED TEST JSON
{ "foo": [ "bar", "baz" ], "": 0, "a\/b": 1, "c%d": 2, "e^f": 3, "g|h": 4, "i\\j": 5, "k\"l": 6, " ": 7, "m~n": 8 }
PASSED - SET - 'cod' in /foo/1
PASSED - SET - non-existing /fud/gaw
PASSED - SET - /fud == {}
PASSED - SET - /fug/gaw == [1,2,3]
PASSED - SET - /fug/gaw == [0,2,3]
PASSED - SET - /fug/gaw == [0,2,3,4]
PASSED - SET - / == 9
PASSED - SET - Final JSON is: { "foo": [ "bar", "cod" ], "": 9, "a\/b": 1, "c%d": 2, "e^f": 3, "g|h": 4, "i\\j": 5, "k\"l": 6, " ": 7, "m~n": 8, "fud": { "gaw": [ 0, 2, 3, 4 ] } }
10
PASSED - SET - LOADED TEST JSON
{ "foo": [ "bar", "baz" ], "": 0, "a\/b": 1, "c%d": 2, "e^f": 3, "g|h": 4, "i\\j": 5, "k\"l": 6, " ": 7, "m~n": 8 }
PASSED - SET - failed 'cod' with path 'foo/bar'
PASSED - SET - failed to set index to non-array

+ 12
- 0
tests/test_json_pointer.test View File

@@ -0,0 +1,12 @@
#!/bin/sh

# Common definitions
if test -z "$srcdir"; then
srcdir="${0%/*}"
test "$srcdir" = "$0" && srcdir=.
test -z "$srcdir" && srcdir=.
fi
. "$srcdir/test-defs.sh"

run_output_test test_json_pointer
exit $?

Loading…
Cancel
Save