]> granicus.if.org Git - json-c/commitdiff
json_pointer: add first revision
authorAlexandru Ardelean <ardeleanalex@gmail.com>
Tue, 25 Oct 2016 14:39:16 +0000 (17:39 +0300)
committerAlexandru Ardelean <ardeleanalex@gmail.com>
Fri, 4 Nov 2016 07:51:36 +0000 (09:51 +0200)
Signed-off-by: Alexandru Ardelean <ardeleanalex@gmail.com>
CMakeLists.txt
Makefile.am
json-c.vcproj
json-c.vcxproj
json-c.vcxproj.filters
json.h
json_pointer.c [new file with mode: 0644]
json_pointer.h [new file with mode: 0644]

index 39ffe2474aabf76cbfa11e815c5d4d70c242cc74..77a9558b4f80cb77d13344682bb1ff039f11bb00 100644 (file)
@@ -32,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
@@ -44,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
index 8f7af3850285ae39189f84c264332d3a262a99c7..58452fc2b1d27ed310326f84794fab4128592337 100644 (file)
@@ -27,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 \
@@ -43,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 \
index 184aa838442e39edd44a8a19ec429a04a1a53d7a..796856ea31fa7c533985239f39f20cbffcf043c4 100644 (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>
@@ -143,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>
index d0f1e98148652d6e83d614403d0df5d535c0b9c7..02e0c2de48c525467d6b36d9c9bc33609a8f01f7 100644 (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" />
@@ -146,6 +147,7 @@ copy json_config.h.win32 json_config.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" />
index 786106492cd1718577d0f47858636910ec3671de..cc133558823502de19ba15f9c6e0fb3ac859225c 100644 (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>
@@ -59,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>
diff --git a/json.h b/json.h
index e198f5da0692c51be9ba5e2321a6c4d539adbe4d..dca0df5ca1a2975a10ccccf5e77c26410a624808 100644 (file)
--- a/json.h
+++ b/json.h
@@ -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"
diff --git a/json_pointer.c b/json_pointer.c
new file mode 100644 (file)
index 0000000..f06aed0
--- /dev/null
@@ -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);
+}
+
diff --git a/json_pointer.h b/json_pointer.h
new file mode 100644 (file)
index 0000000..d661b7b
--- /dev/null
@@ -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