diff options
| author | Joey Adams | 2010-08-06 08:06:01 +0000 |
|---|---|---|
| committer | Joey Adams | 2010-08-06 08:06:01 +0000 |
| commit | f9f677a465a5746874dc2f2c86cc444ffa28a020 (patch) | |
| tree | daae58795aaf5c7facc04fa5939a8a5e25884aac /json.h | |
| parent | e87340e2b0c533d1c1d08ddee6471757bc27b74f (diff) | |
Added more comments to json.c / json.h and cleaned it up a bit.
Also made json_delete a little less confusing (but it still uses gotos).
Diffstat (limited to 'json.h')
| -rw-r--r-- | json.h | 50 |
1 files changed, 30 insertions, 20 deletions
@@ -55,6 +55,17 @@ typedef enum typedef struct JSON JSON; +/* + * All strings in JSON nodes are UTF-8-encoded, not server encoded. + * The reason for this is because we need to encode/decode individual + * Unicode codepoints when dealing with escape characters, but there + * are no functions for efficiently converting between Unicode code points + * and any server encoding. + * + * As an exception to the rule, if you only use node factory functions and + * json_encode without the JSONOPT_ESCAPE_UNICODE option, you may operate + * in the server encoding instead. + */ struct JSON { json_type type; @@ -115,6 +126,8 @@ struct JSON }; +/*** Encoding / decoding / validation ***/ + bool json_validate(const char *str); bool json_validate_server_encoded(const char *str); JSON *json_decode(const char *str); @@ -124,19 +137,10 @@ JSON *json_decode(const char *str); #define JSONOPT_NO_TRIM 4 char *json_encode(JSON * node, int options); -/* Determines the type of a JSON string without fully decoding it. - * Expects the given string to be valid JSON string. - * Might return JSON_INVALID if something is wrong with the input. */ json_type json_text_type(const char *str, size_t nbytes); -/* - * Free a JSON node and all its descendants. - * - * Do not use this function if you have performed json_replace_value on - * a descendant, as this function relies on each node's ->parent field - * being trustworthy. - */ -void json_delete(JSON * node); + +/*** Lookup / traversal ***/ #define json_foreach(child, parent) \ for ((child) = json_head(parent); (child) != NULL; (child) = (child)->next) @@ -154,19 +158,15 @@ json_head(JSON * parent) } } -char *json_decode_string(const char **sp, size_t *length, bool strict); -char *json_encode_string(const char *str, size_t length, char quote, bool escape_unicode); -/* Add child to parent, putting it at the end. */ -void json_append(JSON * parent, JSON * child); +/*** Parent/child manipulation ***/ -/* Remove node from its parent. */ +void json_append(JSON * parent, JSON * child); void json_remove(JSON * node); -/* Update the value of a node, preserving position and key information. */ -void json_replace_value(JSON * node, JSON * replacement); -/* Node factory functions */ +/*** Node factory functions ***/ + JSON *json_mknode(json_type type); JSON *json_mkbool(bool v_bool); JSON *json_mkstring(const char *str, size_t length); @@ -182,9 +182,11 @@ json_mkobject(void) return json_mknode(JSON_OBJECT); } + +/*** Value get/set functions ***/ + void json_touch_value(JSON * node); -/* Node value get/set functions. */ static inline bool json_get_bool(JSON * node) { @@ -203,4 +205,12 @@ void json_set_string(JSON * node, const char *str, size_t length); const char *json_get_number(JSON * node); void json_set_number(JSON * node, const char *number, size_t length); +void json_replace_value(JSON * node, JSON * replacement); + + +/*** Miscellaneous utility functions ***/ + +char *json_decode_string(const char **sp, size_t *length, bool strict); +char *json_encode_string(const char *str, size_t length, char quote, bool escape_unicode); + #endif |
