| 1 | /*************************************************************************** |
| 2 | * Copyright (C) 2007 by Rui Maciel * |
| 3 | * [email protected] * |
| 4 | * * |
| 5 | * This program is free software; you can redistribute it and/or modify * |
| 6 | * it under the terms of the GNU Library General Public License as * |
| 7 | * published by the Free Software Foundation; either version 2 of the * |
| 8 | * License, or (at your option) any later version. * |
| 9 | * * |
| 10 | * This program is distributed in the hope that it will be useful, * |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 13 | * GNU General Public License for more details. * |
| 14 | * * |
| 15 | * You should have received a copy of the GNU Library General Public * |
| 16 | * License along with this program; if not, write to the * |
| 17 | * Free Software Foundation, Inc., * |
| 18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
| 19 | ***************************************************************************/ |
| 20 | |
| 21 | /** @file json.h A small library that helps deal with JSON-encoded information |
| 22 | \ingroup JSON |
| 23 | |
| 24 | \note error handling is only in a very rudimentary form. |
| 25 | \author Rui Maciel [email protected] |
| 26 | \author Sven Herzberg |
| 27 | \version v1.5 |
| 28 | */ |
| 29 | |
| 30 | #include <stdint.h> |
| 31 | #include <stdio.h> |
| 32 | #include <limits.h> |
| 33 | |
| 34 | #ifndef JSON_H |
| 35 | #define JSON_H |
| 36 | |
| 37 | #ifdef __cplusplus |
| 38 | extern "C" |
| 39 | { |
| 40 | #endif |
| 41 | |
| 42 | #define JSON_MAX_STRING_LENGTH SIZE_MAX-1 |
| 43 | |
| 44 | /** |
| 45 | The descriptions of the json_value node type |
| 46 | **/ |
| 47 | enum json_value_type |
| 48 | { JSON_STRING = 0, |
| 49 | JSON_NUMBER, |
| 50 | JSON_OBJECT, |
| 51 | JSON_ARRAY, |
| 52 | JSON_TRUE, |
| 53 | JSON_FALSE, |
| 54 | JSON_NULL |
| 55 | }; |
| 56 | |
| 57 | /** |
| 58 | String implementation |
| 59 | **/ |
| 60 | struct rui_cstring |
| 61 | { |
| 62 | char *text; /*<! char c-string */ |
| 63 | size_t length; /*<! put in place to avoid strlen() calls */ |
| 64 | size_t max; /*<! usable memory allocated to text minus the space for the nul character */ |
| 65 | }; |
| 66 | |
| 67 | typedef struct rui_cstring rcstring; |
| 68 | |
| 69 | /** |
| 70 | The error messages produced by the JSON parsers |
| 71 | **/ |
| 72 | enum json_error |
| 73 | { |
| 74 | JSON_OK = 1, /*!< everything went smoothly */ |
| 75 | JSON_INCOMPLETE_DOCUMENT, /*!< the parsed document didn't ended */ |
| 76 | JSON_WAITING_FOR_EOF, /*!< A complete JSON document tree was already finished but needs to get to EOF. Other characters beyond whitespaces produce errors */ |
| 77 | JSON_MALFORMED_DOCUMENT, /* the JSON document which was fed to this parser is malformed */ |
| 78 | JSON_INCOMPATIBLE_TYPE, /*!< the currently parsed type does not belong here */ |
| 79 | JSON_MEMORY, /*!< an error occurred when allocating memory */ |
| 80 | JSON_ILLEGAL_CHARACTER, /*!< the currently parsed character does not belong here */ |
| 81 | JSON_BAD_TREE_STRUCTURE, /*!< the document tree structure is malformed */ |
| 82 | JSON_MAXIMUM_LENGTH, /*!< the parsed string reached the maximum allowed size */ |
| 83 | JSON_UNKNOWN_PROBLEM /*!< some random, unaccounted problem occurred */ |
| 84 | }; |
| 85 | |
| 86 | |
| 87 | /** |
| 88 | The JSON document tree node, which is a basic JSON type |
| 89 | **/ |
| 90 | typedef struct json_value |
| 91 | { |
| 92 | enum json_value_type type; /*!< the type of node */ |
| 93 | char *text; /*!< The text stored by the node. It stores UTF-8 strings and is used exclusively by the JSON_STRING and JSON_NUMBER node types */ |
| 94 | |
| 95 | /* FIFO queue data */ |
| 96 | struct json_value *next; /*!< The pointer pointing to the next element in the FIFO sibling list */ |
| 97 | struct json_value *previous; /*!< The pointer pointing to the previous element in the FIFO sibling list */ |
| 98 | struct json_value *parent; /*!< The pointer pointing to the parent node in the document tree */ |
| 99 | struct json_value *child; /*!< The pointer pointing to the first child node in the document tree */ |
| 100 | struct json_value *child_end; /*!< The pointer pointing to the last child node in the document tree */ |
| 101 | } json_t; |
| 102 | |
| 103 | |
| 104 | /** |
| 105 | The structure holding all information needed to resume parsing |
| 106 | **/ |
| 107 | struct json_parsing_info |
| 108 | { |
| 109 | unsigned int state; /*!< the state where the parsing was left on the last parser run */ |
| 110 | unsigned int lex_state; |
| 111 | rcstring *lex_text; |
| 112 | const char *p; |
| 113 | int string_length_limit_reached; /*!< flag informing if the string limit length defined by JSON_MAX_STRING_LENGTH was reached */ |
| 114 | size_t line; // current document line |
| 115 | json_t *cursor; /*!< pointers to nodes belonging to the document tree which aid the document parsing */ |
| 116 | }; |
| 117 | |
| 118 | |
| 119 | /** |
| 120 | The structure which holds the pointers to the functions that will be called by the saxy parser whenever their evens are triggered |
| 121 | **/ |
| 122 | struct json_saxy_functions |
| 123 | { |
| 124 | int (*open_object) (void); |
| 125 | int (*close_object) (void); |
| 126 | int (*open_array) (void); |
| 127 | int (*close_array) (void); |
| 128 | int (*new_string) (char *text); |
| 129 | int (*new_number) (char *text); |
| 130 | int (*new_true) (void); |
| 131 | int (*new_false) (void); |
| 132 | int (*new_null) (void); |
| 133 | int (*label_value_separator) (void); |
| 134 | int (*sibling_separator) (void); |
| 135 | }; |
| 136 | |
| 137 | |
| 138 | /** |
| 139 | The structure holding the information needed for json_saxy_parse to resume parsing |
| 140 | **/ |
| 141 | struct json_saxy_parser_status |
| 142 | { |
| 143 | unsigned int state; /*!< current parser state */ |
| 144 | int string_length_limit_reached; /*!< flag informing if the string limit length defined by JSON_MAX_STRING_LENGTH was reached */ |
| 145 | rcstring *temp; /*!< temporary string which will be used to build up parsed strings between parser runs. */ |
| 146 | }; |
| 147 | |
| 148 | |
| 149 | /** |
| 150 | Buils a json_t document by parsing an open file stream |
| 151 | @param file a pointer to an object controlling a stream, returned by fopen() |
| 152 | @param document a reference to a json_t pointer, set to NULL, which will store the parsed document |
| 153 | @return a json_error error code according to how the parsing operation went. |
| 154 | **/ |
| 155 | enum json_error json_stream_parse (FILE * file, json_t ** document); |
| 156 | |
| 157 | |
| 158 | /** |
| 159 | Creates a new JSON value and defines it's type |
| 160 | @param type the value's type |
| 161 | @return a pointer to the newly created value structure |
| 162 | **/ |
| 163 | json_t *json_new_value (const enum json_value_type type); |
| 164 | |
| 165 | |
| 166 | /** |
| 167 | Creates a new JSON string and defines it's text |
| 168 | @param text the value's text |
| 169 | @return a pointer to the newly created JSON string value |
| 170 | **/ |
| 171 | json_t *json_new_string (const char *text); |
| 172 | |
| 173 | |
| 174 | /** |
| 175 | Creates a new JSON number and defines it's text. The user is responsible for the number string's correctness |
| 176 | @param text the value's number |
| 177 | @return a pointer to the newly created JSON string value |
| 178 | **/ |
| 179 | json_t *json_new_number (const char *text); |
| 180 | |
| 181 | |
| 182 | /** |
| 183 | Creates a new JSON object |
| 184 | @return a pointer to the newly created JSON object value |
| 185 | **/ |
| 186 | json_t *json_new_object (void); |
| 187 | |
| 188 | |
| 189 | /** |
| 190 | Creates a new JSON array |
| 191 | @return a pointer to the newly created JSON array value |
| 192 | **/ |
| 193 | json_t *json_new_array (void); |
| 194 | |
| 195 | |
| 196 | /** |
| 197 | Creates a new JSON null |
| 198 | @return a pointer to the newly created JSON null value |
| 199 | **/ |
| 200 | json_t *json_new_null (void); |
| 201 | |
| 202 | |
| 203 | /** |
| 204 | Creates a new JSON true |
| 205 | @return a pointer to the newly created JSON true value |
| 206 | **/ |
| 207 | json_t *json_new_true (void); |
| 208 | |
| 209 | |
| 210 | /** |
| 211 | Creates a new JSON false |
| 212 | @return a pointer to the newly created JSON false value |
| 213 | **/ |
| 214 | json_t *json_new_false (void); |
| 215 | |
| 216 | |
| 217 | /** |
| 218 | Creates a new JSON true/false |
| 219 | @return a pointer to the newly created JSON true/false value |
| 220 | **/ |
| 221 | json_t *json_new_bool (int value); |
| 222 | |
| 223 | |
| 224 | /** |
| 225 | Frees the memory appointed to the value fed as the parameter, as well as all the child nodes |
| 226 | @param value the root node of the tree being freed |
| 227 | **/ |
| 228 | void json_free_value (json_t ** value); |
| 229 | |
| 230 | |
| 231 | /** |
| 232 | Inserts a child node into a parent node, as well as performs some document tree integrity checks. |
| 233 | @param parent the parent node |
| 234 | @param child the node being added as a child to parent |
| 235 | @return the error code corresponding to the operation result |
| 236 | **/ |
| 237 | enum json_error json_insert_child (json_t * parent, json_t * child); |
| 238 | |
| 239 | |
| 240 | /** |
| 241 | Inserts a label:value pair into a parent node, as well as performs some document tree integrity checks. |
| 242 | @param parent the parent node |
| 243 | @param text_label a char string which serves as the label in the label:value pair |
| 244 | @param value the value in the label:value pair |
| 245 | @return the error code corresponding to the operation result |
| 246 | **/ |
| 247 | enum json_error json_insert_pair_into_object (json_t * parent, const char *text_label, json_t * value); |
| 248 | |
| 249 | |
| 250 | /** |
| 251 | Produces a JSON markup text document from a document tree |
| 252 | @param root The document's root node |
| 253 | @param text a pointer to a char string that will hold the JSON document text. |
| 254 | @return a json_error code describing how the operation went |
| 255 | **/ |
| 256 | enum json_error json_tree_to_string (json_t * root, char **text); |
| 257 | |
| 258 | |
| 259 | /** |
| 260 | Produces a JSON markup text document from a json_t document tree to a text stream |
| 261 | @param file a opened file stream |
| 262 | @param root The document's root node |
| 263 | @return a json_error code describing how the operation went |
| 264 | **/ |
| 265 | enum json_error json_stream_output (FILE * file, json_t * root); |
| 266 | |
| 267 | |
| 268 | /** |
| 269 | Strips all JSON white spaces from the text string |
| 270 | @param text a char string holding a JSON document or document snippet |
| 271 | **/ |
| 272 | void json_strip_white_spaces (char *text); |
| 273 | |
| 274 | |
| 275 | /** |
| 276 | Formats a JSON markup text contained in the given string |
| 277 | @param text a JSON formatted document |
| 278 | @return a pointer to a char string holding the formated document |
| 279 | **/ |
| 280 | char *json_format_string (const char *text); |
| 281 | |
| 282 | |
| 283 | /** |
| 284 | Outputs a new UTF8 c-string which replaces all characters that must be escaped with their respective escaped versions |
| 285 | @param text an UTF8 char text string |
| 286 | @return an UTF-8 c-string holding the same text string but with escaped characters |
| 287 | **/ |
| 288 | char *json_escape (const char *text); |
| 289 | |
| 290 | /** |
| 291 | * Outputs a new UTF-8 c-string which has all escaped characters replaced by |
| 292 | * their unescaped, UTF-8 encoded variants. |
| 293 | * |
| 294 | * @param test a UTF-8 c-string |
| 295 | * @return a newly allocated UTF-8 c-string; free with free() |
| 296 | */ |
| 297 | char *json_unescape (const char *text); |
| 298 | |
| 299 | |
| 300 | /** |
| 301 | This function takes care of the tedious task of initializing any instance of |
| 302 | struct json_parsing_info |
| 303 | @param jpi a pointer to a struct json_parsing_info instance |
| 304 | **/ |
| 305 | void json_jpi_init (struct json_parsing_info *jpi); |
| 306 | |
| 307 | |
| 308 | /** |
| 309 | Produces a document tree sequentially from a JSON markup text fragment |
| 310 | @param info the information necessary to resume parsing any incomplete document |
| 311 | @param buffer a null-terminated c-string containing a JSON document fragment |
| 312 | @return a code describing how the operation ended up |
| 313 | **/ |
| 314 | enum json_error json_parse_fragment (struct json_parsing_info *info, const char *buffer); |
| 315 | |
| 316 | |
| 317 | /** |
| 318 | Produces a document tree from a JSON markup text string that contains a complete document |
| 319 | @param root a reference to a pointer to a json_t type. The function allocates memory to the passed pointer and sets up the value |
| 320 | @param text a c-string containing a complete JSON text document |
| 321 | @return a pointer to the new document tree or NULL if some error occurred |
| 322 | **/ |
| 323 | enum json_error json_parse_document (json_t ** root, const char *text); |
| 324 | |
| 325 | |
| 326 | /** |
| 327 | Function to perform a SAX-like parsing of any JSON document or document fragment that is passed to it |
| 328 | @param jsps a structure holding the status information of the current parser |
| 329 | @param jsf a structure holding the function pointers to the event functions |
| 330 | @param c the character to be parsed |
| 331 | @return a json_error code informing how the parsing went |
| 332 | **/ |
| 333 | enum json_error json_saxy_parse (struct json_saxy_parser_status *jsps, struct json_saxy_functions *jsf, char c); |
| 334 | |
| 335 | |
| 336 | /** |
| 337 | Searches through the object's children for a label holding the text text_label |
| 338 | @param object a json_value of type JSON_OBJECT |
| 339 | @param text_label the c-string to search for through the object's child labels |
| 340 | @return a pointer to the first label holding a text equal to text_label or NULL if there is no such label or if object has no children |
| 341 | **/ |
| 342 | json_t *json_find_first_label (const json_t * object, const char *text_label); |
| 343 | |
| 344 | |
| 345 | #ifdef __cplusplus |
| 346 | } |
| 347 | #endif |
| 348 | #endif |