JSON Parser  1.0
json-parser.h
Go to the documentation of this file.
1 
115 /* JSON Parser version 1.0 -- Copyright (c) 2014-2022 David Winterburn <info@fwsentry.org> */
116 
117 
118 #ifndef JSON_PARSER_H
119 #define JSON_PARSER_H
120 
121 #ifdef __cplusplus
122 #include <string.h>
123 extern "C" {
124 #endif
125 
126 
127 /* inttypes.h support is broken MS Visual Studio compilers */
128 #if !defined(_WIN32) || !defined(_MSC_VER)
129 #include <inttypes.h>
130 #else
131 #include <basetsd.h>
132 typedef UINT8 uint8_t;
133 typedef INT8 int8_t;
134 typedef UINT16 uint16_t;
135 typedef INT16 int16_t;
136 typedef ULONG32 uint32_t;
137 typedef LONG32 int32_t;
138 typedef UINT64 uint64_t;
139 typedef INT64 int64_t;
140 #endif
141 
142 /* Support for old compilers */
143 #ifdef __STDC_VERSION__
144 # if __STDC_VERSION__ >= 199901L
145  /* "inline" and "restrict" are already builtin keywords */
146 #else
147  /* Stop compiler errors */
148 #define inline
149 #define restrict
150 #ifndef __func__
151 #define __func__ "unknown_function"
152 #endif
153 #endif
154 #endif /* (__STDC_VERSION__) */
155 
156 
157 
158 #if __STDC_VERSION__ >- 199901L
159 #include <stdbool.h>
160 #endif /* __STDC_VERSION__ >- 199901L */
161 
162 #ifdef __bool_true_false_are_defined
163 #define TRUE true
164 #define FALSE false
165 #define boolean _Bool
166 
167 #else
168 
169 /* for environments with no stdbool.h */
170 typedef int bool
171 #define false (bool)0
172 #define true (bool)1
173 
174 #ifndef TRUE
175 #define TRUE (bool)1
176 #endif /* !TRUE */
177 #ifndef FALSE
178 #define FALSE (bool)0
179 #endif /* !FALSE */
180 
181 #endif /* __bool_true_false_are_defined */
182 
849  */
850 
851 /* The available JSON value types for elements in JSON block lists.
852 
853  VERY IMPORTANT: Do not alter the ordering of this value types list
854  It will destroy the semantics of mixed value type
855  evaluations.
856 
857  Note: invalid_empty is never used, it is a place holder for an
858  incomplete element
859  JSON_value is never used in elements, but can be returned
860  by the function parse_json_data_block() to alert
861  the caller that the parsed JSON block returned
862  contains a value element (type described in that
863  element), instead of a JSON array or JSON object. */
864 typedef unsigned int JSON_value_types;
865 #define invalid_empty 0
866 #define JSON_object 1
867 #define JSON_array 2
868 #define JSON_value 3 /* NOT a valid types for list elements */
869 #define JSON_null 4
870 #define JSON_boolean 5
871 #define JSON_integer 6
872 #define JSON_double 7
873 #define JSON_float_str 8
874 #define JSON_string 9
875 
876 typedef int64_t json_integer_t;
877 typedef long double json_double_t;
878 
879 
880 typedef struct {
881  JSON_value_types value_type; /* the value type of this name/value pair (from JSON_value_types) */
882  char *member_name; /* Member name for JSON_object Member elements, NULL for all other value types */
883  union {
884  list_object *object; /* ptr to list_object for a JSON_object, or JSON_array, or NULL */
885  char *string; /* For JSON_string or JSON_float_str if requested */
886  json_integer_t integer; /* For integer (JSON_integer) */
887  json_double_t double_number; /* For non integer (JSON_double) user must deal with the conversion issues */
888  boolean boolean_value; /* For JSON_boolean either TRUE or FALSE */
889  } value;
890  boolean free_string_ptr; /* TRUE, remove function will free() the string and member_name pointer, FALSE it will not */
892 
893 
894 /* This typedef is used to describe JSON Object paths to either find data or
895  express where data is in a JSON hierarchy.
896 
897  So for example to express a JSON Object path of
898  "bakery.product.[0].name"
899  would take an array of 4 json_path_t entries
900  {JSON_object, "bakery"},
901  {JSON_object, "product"},
902  {JSON_array, 0},
903  {JSON_object, "name"},
904  {invalid_empty, NULL} can be used to terminate, or a count can be provided */
905 typedef struct {
906  JSON_value_types value_type; /* the type of the link link in the JSON Object path, JSON_object or JSON_array */
907  union {
908  unsigned int array_index;
909  char *member_name; /* AKA keys in javascript land etc */
910  } link;
911 } json_path_t;
912 
913 /* the json_member_list_t struct is used by various JSON support functions
914  that perform searches of hierarchical JSON data list_objects. If a list
915  of members or array values is returned to a caller, this is the form of
916  each element returned. Additionally the support functions that accept
917  JSON paths also use this structure to transfer data to and from the
918  callers.
919 
920  The pointer to the parent list_object of this element is required for
921  quick access to other Members of the same JSON Object this element came
922  from.
923 
924  The pointer to the element reference is required, in combination with
925  the parent list_object, to access or manage the element using D-List.
926  Without the element reference, you would have to search for the again.
927 
928  The pointer to the element itself is provided so you can access or update
929  the JSON Member data value, or change it. However having the address of
930  the element will not assist you when attempting to access or manage the
931  element in D-List. D-List deliberately abstracts the address of an
932  element away from managing the element. That is why you need the
933  list_object and the element reference. Please refer to the D_list
934  documentation if you need more information about abstract element
935  references and using them to access and manipulate elements.
936 
937  The path and path segment count, replicate the JSON dotted syntax used
938  to describe paths in JSON data blocks. These can also be used as input
939  to the various json_path_xxx functions */
940 typedef struct {
941  list_object *json_owner_object; /* parent list_object of this JSON Member element, use to locate element in D-List */
942  element_reference *reference; /* D-List element reference of this JSON Member element, use to manipulate element in D-List */
943  json_element_t *json_member; /* Pointer to the actual JSON Member Element, use to access the Member data */
944  JSON_value_types value_type; /* the Value type of the JSON Member value */
945  unsigned int path_segments; /* the number of elements in the path array (e.g. 4 in the example below) */
946  json_path_t path[]; /* the JSON path of the Member as recorded when found (e.g. bakery.product.[19]."curry pan") */
948 
949 
950 typedef struct {
951  element_search object_search_function;
952  element_compare object_compare_function;
953  element_search array_search_function;
954  element_compare array_compare_function;
956 
957 
958 /* the environment variables we can use */
959 #define ENVIRONMENT_VAR_DEBUGGING "JSON_PARSER_DEBUG"
960 
961 /* global debugging flags */
962 
963 #if defined( INITIALIZE_DEBUGGING_FLAG )
964  boolean json_parser_debugging = FALSE;
965 #else
966  extern boolean json_parser_debugging;
967 #endif
968 
969 
970 /* ===================================================================
971 
972  JSON Conversion Functions for JSON Parser D-List hierarchical lists.
973 
974  ===================================================================*/
975 
976 
1138 list_object *json_parse_data_block( char *json_data_block,
1139  JSON_value_types *json_data_type,
1140  element_search object_search_function,
1141  element_compare object_compare_function,
1142  element_search array_search_function,
1143  element_compare array_compare_function,
1144  const boolean keep_exponent);
1145 
1146 
1147 
1148 
1200 boolean json_valid_data_block( const char *json_object,
1201  char **object_error,
1202  unsigned long int *line_number,
1203  const boolean suppress_warnings);
1204 
1253 int json_generate_data_block( list_object *json_data,
1254  JSON_value_types json_data_type,
1255  char **json_text_buffer);
1256 
1257 
1258 
1259 
1260 
1261 /* ================================================================
1262 
1263  JSON D_list Functions for JSON Parser D-List hierarchical lists.
1264 
1265  ================================================================*/
1266 
1267 
1268 /* Default D-List user functions for JSON Parser lists.
1269  ====================================================*/
1270 
1306 boolean default_search_function_member_name(const void *element, const void *key);
1307 
1311 boolean default_search_function_partial_member_name(const void *element, const void *key);
1312 
1391 boolean default_search_function_array_values(const void *element, const void *key);
1392 
1403 boolean default_search_function_array_string_value(const void *element, const void *key);
1404 
1415 boolean default_search_function_array_partial_string_value(const void *element, const void *key);
1416 
1427 boolean default_search_function_array_integer_value(const void *element, const void *key);
1428 
1443 boolean default_search_function_array_double_value(const void *element, const void *key);
1444 
1460 boolean default_search_function_precise_array_double_value(const void *element, const void *key);
1461 
1479 boolean default_search_function_sloppy_array_double_value(const void *element, const void *key);
1480 
1481 
1482 
1493 int default_compare_function_member_names(const void *element, const void *key);
1494 
1495 
1545 int default_compare_function_array_values(const void *element, const void *key);
1546 
1547 
1548 
1549 int default_compare_function_array_number_values(const void *element, const void *key);
1550 
1551 
1577 int default_compare_function_array_string_values(const void *element, const void *key);
1578 
1583 int default_compare_function_array_integer_values(const void *element, const void *key);
1584 
1596 int default_compare_function_array_double_values(const void *element, const void *key);
1597 
1598 
1599 
1600 
1601 
1602 
1603 
1604 
1605 
1721 list_object *json_find_members_value_string( list_object *json_data,
1722  const char *member_name,
1723  const boolean partial_name,
1724  const char *string_value,
1725  const boolean partial_value);
1726 
1791 list_object *json_find_members_value_integer( list_object *json_data,
1792  char *member_name,
1793  const boolean partial_name,
1794  const json_integer_t minimum_value,
1795  const json_integer_t maximum_value,
1796  const boolean value_range);
1797 
1862 list_object *json_find_members_value_double( list_object *json_data,
1863  char *member_name,
1864  const boolean partial_name,
1865  const json_double_t minimum_value,
1866  const json_double_t maximum_value,
1867  const boolean value_range);
1868 
1911 list_object *json_find_members_value_type( list_object *json_data,
1912  const char *member_name,
1913  const boolean partial_name,
1914  const JSON_value_types value_type);
1915 
1979  list_object *json_data,
1980  const char *member_name,
1981  const boolean partial_name,
1982  const char *string_value,
1983  const boolean partial_value);
1984 
2028 json_member_list_t *json_path_locate( list_object *json_data,
2029  json_member_list_t *path_to_find,
2030  const boolean strict_path,
2031  const boolean suppress_errors);
2032 
2033 
2051 list_object *json_create_new_list_object( const JSON_value_types list_type,
2052  const functions_globals_t *functions);
2053 
2064 JSON_value_types json_report_list_object_type(list_object *list);
2065 
2088 int json_list_object_member_add_string( list_object *list_to_add,
2089  const char *member_name,
2090  const char *string_value);
2091 
2092 
2127 int json_list_object_member_add_number( list_object *list_to_add,
2128  const char *member_name,
2129  const json_integer_t integer_number,
2130  const json_double_t double_number);
2131 
2132 
2167 int json_list_object_member_add_float_str( list_object *list_to_add,
2168  const char *member_name,
2169  const char *float_str);
2170 
2195 int json_list_object_member_add_boolean( list_object *list_to_add,
2196  const char *member_name,
2197  const boolean boolean_value);
2198 
2222 int json_list_object_member_add_null( list_object *list_to_add,
2223  const char *member_name);
2224 
2256 int json_list_object_member_add_list( list_object *list_to_add,
2257  const char *member_name,
2258  list_object *new_list,
2259  const JSON_value_types list_type);
2260 
2261 
2280 int json_list_object_array_value_add_string( list_object *list_to_add,
2281  const char *string_value);
2282 
2316 int json_list_object_array_value_add_number( list_object *list_to_add,
2317  const json_integer_t *integer_number,
2318  const json_double_t *double_number,
2319  const unsigned int item_count);
2320 
2321 
2354 int json_list_object_array_value_add_float_str( list_object *list_to_add,
2355  const char *float_str);
2356 
2377 int json_list_object_array_value_add_boolean( list_object *list_to_add,
2378  const boolean boolean_value);
2379 
2399 int json_list_object_array_value_add_null( list_object *list_to_add);
2400 
2429 int json_list_object_array_value_add_list( list_object *list_to_add,
2430  list_object *new_list,
2431  const JSON_value_types list_type);
2432 
2433 
2434 
2435 
2436 
2437 
2438 /* ======================================================================
2439 
2440  JSON Support Functions for Reporting hierarchical lists and JSON data.
2441 
2442  create character strings from data for display, convert paths, etc
2443 
2444  =======================================================================*/
2445 
2446 
2447 
2489 list_object *json_path_to_list_object( list_object *json_data,
2490  const char *path);
2491 
2492 
2520 json_member_list_t *json_parse_dotted_path(const char *dotted_path);
2521 
2533  unsigned int count);
2534 
2543  unsigned int count);
2544 
2545 
2552 void display_parsed_json_object( list_object* json_object,
2553  const char *list_name,
2554  const int list_type,
2555  const boolean dig_down);
2556 
2557 
2558 
2559 
2560 #ifdef __cplusplus
2561 }
2562 #endif /* __cplusplus */
2563 
2564 #endif /* JSON_PARSER_H */
JSON_value_types value_type
Definition: json-parser.h:881
boolean boolean_value
Definition: json-parser.h:888
list_object * json_find_members_value_type(list_object *json_data, const char *member_name, const boolean partial_name, const JSON_value_types value_type)
Using the provided search criteria, search the JSON data and locate all object members in the list hi...
void display_parsed_json_object(list_object *json_object, const char *list_name, const int list_type, const boolean dig_down)
This function will either display all the elements in a single list_object, or will walk an entire hi...
element_compare object_compare_function
Definition: json-parser.h:952
char * member_name
Definition: json-parser.h:882
list_object * json_parse_data_block(char *json_data_block, JSON_value_types *json_data_type, element_search object_search_function, element_compare object_compare_function, element_search array_search_function, element_compare array_compare_function, const boolean keep_exponent)
Call the JSON Parser and return a hierarchical list_object containing the parsed contents of the JSON...
char * string
Definition: json-parser.h:885
int json_list_object_member_add_string(list_object *list_to_add, const char *member_name, const char *string_value)
Add a new Member with a string value to a JSON Object, defined by the list_object provided...
int default_compare_function_array_values(const void *element, const void *key)
This the default compare function applied to all JSON Array lists, unless an alternative is provided ...
char * member_name
Definition: json-parser.h:909
boolean default_search_function_array_double_value(const void *element, const void *key)
This search function searches the elements of a specified JSON Array List looking for a match of elem...
int json_list_object_member_add_list(list_object *list_to_add, const char *member_name, list_object *new_list, const JSON_value_types list_type)
Add a new Member with a JSON Array or JSON Object value to an existing JSON Object, defined by the list_object provided.
char * json_generate_path_from_links(json_path_t path[], unsigned int count)
Create a character array describing a set of links in a JSON Data block for reporting or printing pur...
boolean default_search_function_precise_array_double_value(const void *element, const void *key)
This search function searches the elements of a specified JSON Array List looking for a match of elem...
boolean default_search_function_member_name(const void *element, const void *key)
The following is the list of available search and compare functions that are built into JSON Parser...
Definition: json-parser.h:880
boolean default_search_function_array_values(const void *element, const void *key)
This the default search function applied to all JSON Array list_objects, unless an alternative is pro...
boolean default_search_function_partial_member_name(const void *element, const void *key)
This search function searches the elements of a specified JSON Object List looking for a match of obj...
boolean default_search_function_array_partial_string_value(const void *element, const void *key)
This search function searches the elements of a specified JSON Array List looking for a match of elem...
boolean default_search_function_array_string_value(const void *element, const void *key)
This search function searches the elements of a specified JSON Array List looking for an exact match ...
int json_list_object_array_value_add_null(list_object *list_to_add)
Add a new null value to a JSON Array, defined by the list_object provided.
int default_compare_function_array_double_values(const void *element, const void *key)
This compare function evaluates the elements of a specified JSON Array List It can be used on both JS...
int json_list_object_array_value_add_list(list_object *list_to_add, list_object *new_list, const JSON_value_types list_type)
Add a new data value to an existing JSON Array, that has as a value type of either a JSON Array or JS...
list_object * json_owner_object
Definition: json-parser.h:941
*typedef unsigned int JSON_value_types
Definition: json-parser.h:864
long double json_double_t
Definition: json-parser.h:877
element_search object_search_function
Definition: json-parser.h:951
int json_list_object_member_add_float_str(list_object *list_to_add, const char *member_name, const char *float_str)
Add a new Member with a exponential number represented as a string value (instead of a binary floatin...
JSON_value_types json_report_list_object_type(list_object *list)
Given an unknown list_object, provide the JSON value type this list_object represents.
int json_list_object_array_value_add_string(list_object *list_to_add, const char *string_value)
Add a new String Value to a JSON Array, defined by the list_object provided.
json_member_list_t * json_path_locate(list_object *json_data, json_member_list_t *path_to_find, const boolean strict_path, const boolean suppress_errors)
Using the internal representation of a JSON dotted notation path, trace the provided path and return ...
void json_print_path_from_links(json_path_t path[], unsigned int count)
Display to stdout text describing a set of links in a JSON data block.
int json_list_object_member_add_boolean(list_object *list_to_add, const char *member_name, const boolean boolean_value)
Add a new Member with a boolean value to a JSON Object, defined by the list_object provided...
json_integer_t integer
Definition: json-parser.h:886
list_object * json_find_members_value_integer(list_object *json_data, char *member_name, const boolean partial_name, const json_integer_t minimum_value, const json_integer_t maximum_value, const boolean value_range)
Using the provided search criteria, search the JSON data and locate all object members in the list hi...
boolean json_parser_debugging
unsigned int array_index
Definition: json-parser.h:908
int json_list_object_array_value_add_float_str(list_object *list_to_add, const char *float_str)
Add a new value with a exponential number represented as a string, (instead of a binary floating numb...
list_object * json_find_members_value_double(list_object *json_data, char *member_name, const boolean partial_name, const json_double_t minimum_value, const json_double_t maximum_value, const boolean value_range)
Using the provided search criteria, search the JSON data and locate all object members in the list hi...
JSON_value_types value_type
Definition: json-parser.h:944
boolean json_valid_data_block(const char *json_object, char **object_error, unsigned long int *line_number, const boolean suppress_warnings)
Call the JSON syntax validator and return a boolean value based on either successful or failed valida...
int default_compare_function_array_number_values(const void *element, const void *key)
JSON_value_types value_type
Definition: json-parser.h:906
list_object * json_path_to_list_object(list_object *json_data, const char *path)
Convert a JSON dotted notation path to a list_object for that path.
int json_list_object_member_add_null(list_object *list_to_add, const char *member_name)
Add a new Member with a null value to a JSON Object, defined by the list_object provided.
int json_list_object_array_value_add_boolean(list_object *list_to_add, const boolean boolean_value)
Add a new Boolean Value to a JSON Array, defined by the list_object provided.
boolean default_search_function_array_integer_value(const void *element, const void *key)
This search function searches the elements of a specified JSON Array List looking for a match of elem...
int json_find_member_value(json_member_list_t *result, list_object *json_data, const char *member_name, const boolean partial_name, const char *string_value, const boolean partial_value)
Using the provided search criteria, search the JSON data and locate the first JSON object member in t...
int64_t json_integer_t
Definition: json-parser.h:876
int json_list_object_array_value_add_number(list_object *list_to_add, const json_integer_t *integer_number, const json_double_t *double_number, const unsigned int item_count)
Add a new Number Value to a JSON Array, defined by the list_object provided.
#define FALSE
Definition: json-parser.h:178
int json_list_object_member_add_number(list_object *list_to_add, const char *member_name, const json_integer_t integer_number, const json_double_t double_number)
Add a new Member with a number value to a JSON Object, defined by the list_object provided...
Definition: json-parser.h:950
element_compare array_compare_function
Definition: json-parser.h:954
list_object * object
Definition: json-parser.h:884
json_double_t double_number
Definition: json-parser.h:887
list_object * json_find_members_value_string(list_object *json_data, const char *member_name, const boolean partial_name, const char *string_value, const boolean partial_value)
=================================================================
list_object * json_create_new_list_object(const JSON_value_types list_type, const functions_globals_t *functions)
Create a new list_object to be used to represent either a JSON Object or a JSON Array.
boolean free_string_ptr
Definition: json-parser.h:890
element_reference * reference
Definition: json-parser.h:942
int default_compare_function_member_names(const void *element, const void *key)
This the default compare function applied to all JSON Object lists, unless an alternative is provided...
unsigned int path_segments
Definition: json-parser.h:945
int json_generate_data_block(list_object *json_data, JSON_value_types json_data_type, char **json_text_buffer)
Using a JSON Object data tree, represented by a hierarchical D-List list_object structure, create a textural version of that JSON data presented and return a character array with that JSON data in it, in strict JSON Grammar format.
int default_compare_function_array_string_values(const void *element, const void *key)
JSON Array List value specific compare functions.
int default_compare_function_array_integer_values(const void *element, const void *key)
This compare function evaluates the elements of a specified JSON Array List It can be used on both JS...
element_search array_search_function
Definition: json-parser.h:953
json_element_t * json_member
Definition: json-parser.h:943
Definition: json-parser.h:940
boolean default_search_function_sloppy_array_double_value(const void *element, const void *key)
This search function searches the elements of a specified JSON Array List looking for a match of elem...
Definition: json-parser.h:905
json_member_list_t * json_parse_dotted_path(const char *dotted_path)
Create a set of links describing a JSON data path, by parsing a character array with JSON dotted noti...