All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
json_missing.h
Go to the documentation of this file.
1 /*
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.
6  *
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.
11  *
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
15  */
16 
17 /**
18  * $Id: 8ad0534768ec94e56f9263c6bb50eb9c24d40bd6 $
19  *
20  * @brief Function prototypes for missing functions in older json-c libraries.
21  * @file json_missing.h
22  *
23  * @author Aaron Hurt <ahurt@anbcs.com>
24  * @copyright 2013-2014 The FreeRADIUS Server Project.
25  */
26 
27 #ifndef _jsonc_missing_h_
28 #define _jsonc_missing_h_
29 
30 RCSIDH(jsonc_missing_h, "$Id: 8ad0534768ec94e56f9263c6bb50eb9c24d40bd6 $")
31 #include "config.h"
32 #ifdef HAVE_JSON
33 # if defined(HAVE_JSONMC_JSON_H)
34 # include <json-c/json.h>
35 # elif defined(HAVE_JSON_JSON_H)
36 # include <json/json.h>
37 # else
38 # error "Need json-c headers"
39 # endif
40 
41 /*
42  * Fixes for idiocy in older versions of json-c
43  */
44 
45 /*
46  * null JSON values are represented as NULL pointers by JSON-C
47  *
48  * Which would be ok, except functions such as json_object_get_type
49  * and json_object_is_type* SEGV when passed a NULL pointer.
50  *
51  * There's a json_type_null in the type enum, but for some reason
52  * it's not used. Newer versions of json-c don't have this issue.
53  * Old versions of json-c SEGV when passed a null JSON object.
54  *
55  */
56 #define fr_json_object_get_type(_obj) ((_obj) ? json_object_get_type(_obj) : json_type_null)
57 #define fr_json_object_is_type(_obj, _type) ((_obj) ? json_object_is_type(_obj, _type) : (_type == json_type_null))
58 
59 #ifndef HAVE_JSON_OBJECT_OBJECT_GET_EX
60 # include <json/json_object_private.h>
61 int json_object_object_get_ex(struct json_object* jso, const char *key, struct json_object **value);
62 #endif
63 
64 #ifndef HAVE_JSON_OBJECT_GET_STRING_LEN
65 int json_object_get_string_len(struct json_object *obj);
66 #endif
67 
68 #ifndef HAVE_JSON_TOKENER_ERROR_DESC
69 const char *json_tokener_error_desc(enum json_tokener_error jerr);
70 #endif
71 
72 #ifndef HAVE_JSON_TOKENER_GET_ERROR
73 enum json_tokener_error json_tokener_get_error(json_tokener *tok);
74 #endif
75 
76 /* correct poor const handling within json-c library */
77 #ifdef json_object_object_foreach
78 # undef json_object_object_foreach
79 #endif
80 
81 /* redefine with correct handling of const pointers */
82 #if defined(__GNUC__) && !defined(__STRICT_ANSI__)
83 # define json_object_object_foreach(obj, key, val) \
84  char *key = NULL; \
85  struct json_object *val = NULL; \
86  union ctn_u {const void *cdata; void *data; } ctn; \
87  for (struct lh_entry *entry = json_object_get_object(obj)->head; \
88  ({ if (entry) { key = (char *)entry->k; ctn.cdata = entry->v; \
89  val = (struct json_object *)ctn.data; }; entry; }); \
90  entry = entry->next)
91 #else /* ANSI C or MSC */
92 # define json_object_object_foreach(obj, key, val) \
93  char *key = NULL; \
94  struct json_object *val = NULL; \
95  struct lh_entry *entry; \
96  union ctn_u {const void *cdata; void *data; } ctn; \
97  for (entry = json_object_get_object(obj)->head; \
98  (entry ? (key = (char *)entry->k, ctn.cdata = entry->v, \
99  val = (struct json_object *)ctn.data, entry) : 0); entry = entry->next)
100 #endif /* defined(__GNUC__) && !defined(__STRICT_ANSI__) */
101 #endif
102 #endif /* _jsonc_missing_h_ */
#define RCSIDH(h, id)
Definition: build.h:136
int json_object_get_string_len(struct json_object *obj)
Definition: json_missing.c:32
int json_object_object_get_ex(struct json_object *jso, const char *key, struct json_object **value)
Definition: json_missing.c:40
const char * json_tokener_error_desc(enum json_tokener_error jerr)
Definition: json_missing.c:68
enum json_tokener_error json_tokener_get_error(json_tokener *tok)
Definition: json_missing.c:62