diff options
| author | Joey Adams | 2010-08-10 05:10:19 +0000 |
|---|---|---|
| committer | Joey Adams | 2010-08-10 05:10:19 +0000 |
| commit | 75b17eeca0394e27759acf2f6b039851a5a28f98 (patch) | |
| tree | 9c156bf2f70a49ed781a56ebfe89caed4edbc6a0 /json.c | |
| parent | f9f677a465a5746874dc2f2c86cc444ffa28a020 (diff) | |
Added explicit NULL and '\0' checks, and documented more functions/structures.
Diffstat (limited to 'json.c')
| -rw-r--r-- | json.c | 99 |
1 files changed, 53 insertions, 46 deletions
@@ -35,8 +35,8 @@ skip_whitespace(const char **sp) static char end_parenthesis(JSON * node) { - if (!node) - return 0; + Assert(node != NULL); + switch (node->type) { case JSON_ARRAY: @@ -44,7 +44,8 @@ end_parenthesis(JSON * node) case JSON_OBJECT: return '}'; default: - return 0; + Assert(false); + return '\0'; } } @@ -145,7 +146,7 @@ json_mknumber(const char *number, size_t length) * Indicate that the node's value has changed, * marking ancestors as necessary. * - * Call json_touch_value so that json_encode(, JSONOPT_ORIG) + * Call json_touch_value so that json_encode(..., JSONOPT_ORIG) * will encode the new value rather than using original text. */ void @@ -208,7 +209,7 @@ json_remove(JSON * node) { JSON *parent = node->parent; - if (!parent) + if (parent == NULL) return; Assert(parent->type == JSON_ARRAY || parent->type == JSON_OBJECT); Assert(parent->v.children.count > 0); @@ -333,7 +334,7 @@ json_delete(JSON * node) JSON *parent, *next; - if (!node) + if (node == NULL) return; /* Remove node from parent (if it has one). */ @@ -349,14 +350,14 @@ advance: free_node(node); node = next; - if (node) + if (node != NULL) { goto descend; } else { node = parent; - if (node) + if (node != NULL) goto advance; else return; @@ -368,11 +369,13 @@ advance: static JSON *decode_leaf(const char **sp); static JSON *decode_number(const char **sp); -char *json_decode_string(const char **sp, size_t *length, bool strict); -/* json_decode_string has a different signature than its friends - because it's also used to parse object member keys. - It's also useful outside of json.c, such as in jsonpath.c . */ +/* + * json_decode_string has a different signature than its friends + * because it's also used to parse object member keys. + * It's also useful outside of json.c, such as in jsonpath.c . + */ +char *json_decode_string(const char **sp, size_t *length, bool strict); /* * json_validate @@ -387,7 +390,7 @@ json_validate(const char *str) { JSON *node = json_decode(str); - if (!node) + if (node == NULL) return false; json_delete(node); return true; @@ -431,7 +434,7 @@ json_decode(const char *str) struct json_orig orig; bool expect_endp; - if (!str) + if (str == NULL) return NULL; Assert(utf8_validate(str, strlen(str))); @@ -456,14 +459,14 @@ item: /* Expect a value (set expect_endp before goto goto endp; } - if (parent && parent->type == JSON_OBJECT) + if (parent != NULL && parent->type == JSON_OBJECT) { /* Parse member key string. */ orig.key_left_space.end = s; orig.key.start = s; key = json_decode_string(&s, &key_length, true); - if (!key) + if (key == NULL) goto failed; orig.key.end = s; @@ -492,7 +495,7 @@ item: /* Expect a value (set expect_endp before goto orig.value.start = s; node = decode_leaf(&s); - if (!node) + if (node == NULL) { if (*s == '[') node = json_mknode(JSON_ARRAY); @@ -528,7 +531,7 @@ item: /* Expect a value (set expect_endp before goto node->orig = orig; - if (parent) + if (parent != NULL) json_append_notouch(parent, node); else root = node; @@ -545,7 +548,7 @@ item: /* Expect a value (set expect_endp before goto goto item; } - if (parent) + if (parent != NULL) goto comma_endp; else goto end; @@ -583,18 +586,18 @@ endp: /* Handle an end bracket/brace */ node->orig.right_space.end = s; - if (parent) + if (parent != NULL) goto comma_endp; else goto end; end: /* Expect end of text */ - if (*s) + if (*s != '\0') goto failed; return node; failed: /* Handle failure */ - if (key) + if (key != NULL) pfree(key); json_delete(root); return NULL; @@ -616,7 +619,7 @@ decode_leaf(const char **sp) size_t length; char *str = json_decode_string(sp, &length, true); - if (str) + if (str != NULL) { JSON *node = json_mknode(JSON_STRING); @@ -629,17 +632,17 @@ decode_leaf(const char **sp) } if ((c >= '0' && c <= '9') || c == '-') return decode_number(sp); - if (!strncmp(*sp, "true", 4)) + if (strncmp(*sp, "true", 4) == 0) { (*sp) += 4; return json_mkbool(true); } - if (!strncmp(*sp, "false", 5)) + if (strncmp(*sp, "false", 5) == 0) { (*sp) += 5; return json_mkbool(false); } - if (!strncmp(*sp, "null", 4)) + if (strncmp(*sp, "null", 4) == 0) { (*sp) += 4; return json_mknode(JSON_NULL); @@ -769,7 +772,7 @@ json_decode_string(const char **sp, size_t *length, bool strict) return NULL; } - while (*s && *s != quote) + while (*s != '\0' && *s != quote) { unsigned char c = *s++; unsigned int uc; @@ -849,7 +852,7 @@ json_decode_string(const char **sp, size_t *length, bool strict) appendStringInfoChar(&ret, c); } - if (!*s++) + if (*s++ != quote) goto failed; *length = ret.len; @@ -906,6 +909,10 @@ json_text_type(const char *str, size_t nbytes) /****************************** Encoding *****************************/ +/* + * encode_string + * Variant of json_encode_string that writes its output to a StringInfo. + */ static void encode_string(StringInfo out, const char *string, size_t length, char quote, bool escape_unicode) @@ -922,33 +929,33 @@ encode_string(StringInfo out, const char *string, size_t length, char quote, while (s < e) { unsigned char c = *s++; - unsigned char e; + unsigned char endchar; switch (c) { case '\\': - e = '\\'; + endchar = '\\'; break; case '\b': - e = 'b'; + endchar = 'b'; break; case '\f': - e = 'f'; + endchar = 'f'; break; case '\n': - e = 'n'; + endchar = 'n'; break; case '\r': - e = 'r'; + endchar = 'r'; break; case '\t': - e = 't'; + endchar = 't'; break; default: { if (c == quote) { - e = quote; + endchar = quote; break; } if (c < 0x1F || (c >= 0x80 && escape_unicode)) @@ -985,13 +992,13 @@ encode_string(StringInfo out, const char *string, size_t length, char quote, appendStringInfoString(out, txt); continue; /* Skip backslash-encoding code below. */ } - e = 0; + endchar = '\0'; } } - appendStringInfoChar(out, e ? '\\' : c); - if (e) - appendStringInfoChar(out, e); + appendStringInfoChar(out, endchar ? '\\' : c); + if (endchar != '\0') + appendStringInfoChar(out, endchar); } appendStringInfoChar(out, quote); @@ -1004,7 +1011,7 @@ encode_number(StringInfo out, const char *string) const char *start, *end; - if (!string) + if (string == NULL) return false; /* Validate number, trimming whitespace. */ @@ -1038,7 +1045,7 @@ static bool json_encode_recurse(JSON * node, json_encode_ctx * ctx); * Encode a JSON node. * * The JSONOPT_ESCAPE_UNICODE option may only be used - * if the strings in the JSON tree are UTF-8-encoded. + * if the strings in the JSON tree are UTF-8-encoded. */ char * json_encode(JSON * node, int options) @@ -1112,7 +1119,7 @@ json_encode_recurse(JSON * node, json_encode_ctx * ctx) json_foreach(child, node) { json_encode_recurse(child, ctx); - if (child->next) + if (child->next != NULL) appendStringInfoChar(&ctx->str, ','); } @@ -1146,7 +1153,7 @@ json_encode_recurse(JSON * node, json_encode_ctx * ctx) json_encode_recurse(node, ctx); - if (node->next) + if (node->next != NULL) appendStringInfoChar(&ctx->str, ','); } @@ -1156,7 +1163,7 @@ json_encode_recurse(JSON * node, json_encode_ctx * ctx) return false; } - if (txt) + if (txt != NULL) appendStringInfoString(&ctx->str, txt); } @@ -1178,7 +1185,7 @@ json_encode_recurse(JSON * node, json_encode_ctx * ctx) * in invalid JSON. * * If escape_unicode is true, str must be valid UTF-8. - * In any case, str may contain null characters (hence the length argument). + * In any case, str may contain null characters (hence the length argument). * * quote must not be a backslash. */ |
