2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * @brief Workarounds for missing functions in older json-c libraries.
21 * @file json_missing.c
23 * @copyright 2013-2014 Aaron Hurt <ahurt@anbcs.com>
30 #include "jsonc_missing.h"
32 #ifndef HAVE_JSON_OBJECT_GET_STRING_LEN
33 int json_object_get_string_len(json_object *obj) {
34 if (json_object_get_type(obj) != json_type_string)
36 return (int)strlen(json_object_to_json_string(obj));
40 #ifndef HAVE_JSON_OBJECT_OBJECT_GET_EX
41 int json_object_object_get_ex(struct json_object *jso, const char *key, struct json_object **value) {
42 struct json_object *jobj;
44 if ((jso == NULL) || (key == NULL)) return 0;
45 if (value != NULL) *value = NULL;
47 switch (json_object_get_type(jso)) {
48 case json_type_object:
49 jobj = json_object_object_get(jso, key);
50 if (jobj == NULL) return 0;
52 if (value != NULL) *value = jobj;
56 if (value != NULL) *value = NULL;
62 #ifndef HAVE_JSON_TOKENER_PARSE_VERBOSE
63 struct json_object* json_tokener_parse_verbose(const char *str, enum json_tokener_error *error) {
64 struct json_tokener* tok;
65 struct json_object* obj;
67 tok = json_tokener_new();
70 obj = json_tokener_parse_ex(tok, str, -1);
72 if(tok->err != json_tokener_success) {
78 json_tokener_free(tok);
83 #ifndef HAVE_JSON_TOKENER_GET_ERROR
84 enum json_tokener_error json_tokener_get_error(json_tokener *tok) {
89 #ifndef HAVE_JSON_TOKENER_ERROR_DESC
90 const char *json_tokener_error_desc(enum json_tokener_error jerr) {
91 int jerr_int = (int)jerr;
92 if (json_tokener_errors[jerr_int] == NULL)
93 return "Unknown error, invalid json_tokener_error value passed to json_tokener_error_desc()";
94 return json_tokener_errors[jerr_int];