diff options
Diffstat (limited to 'json.c')
| -rw-r--r-- | json.c | 134 |
1 files changed, 61 insertions, 73 deletions
@@ -1,25 +1,13 @@ -/* - Copyright (C) 2010 Joseph A. Adams (joeyadams3.14159@gmail.com) - All rights reserved. - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included in - all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - THE SOFTWARE. -*/ +/*------------------------------------------------------------------------- + * + * json.c + * Core JSON manipulation routines used by JSON data type support. + * + * Copyright (c) 2010, PostgreSQL Global Development Group + * Written by Joey Adams <joeyadams3.14159@gmail.com>. + * + *------------------------------------------------------------------------- + */ #include "json.h" #include "util.h" @@ -350,11 +338,11 @@ char *json_decode_string(const char **sp, size_t *length, bool strict); /* * json_validate - * Make sure the given UTF-8 string is valid JSON. + * Make sure the given UTF-8 string is valid JSON. * * TODO: Consider making a dedicated function for this so we don't have to - * convert to UTF-8, build a JSON node, then free both - * whenever we need to validate (such as in json_in and json_recv). + * convert to UTF-8, build a JSON node, then free both + * whenever we need to validate (such as in json_in and json_recv). */ bool json_validate(const char *str) @@ -369,29 +357,29 @@ json_validate(const char *str) /* * json_validate_server_encoded - * Variant of json_validate that takes a server-encoded string - * rather than a UTF-8 string. + * Variant of json_validate that takes a server-encoded string + * rather than a UTF-8 string. * - * Note that a dedicated json_validate (described in the TODO above) - * would be able to handle both encodings natively, since both are - * ASCII-compatible. + * Note that a dedicated json_validate (described in the TODO above) + * would be able to handle both encodings natively, since both are + * ASCII-compatible. */ bool json_validate_server_encoded(const char *str) { - char *str_utf8 = server_to_utf8(str, strlen(str)); - bool result = json_validate(str_utf8); - + char *str_utf8 = server_to_utf8(str, strlen(str)); + bool result = json_validate(str_utf8); + if (str_utf8 != str) pfree(str_utf8); - + return result; } /* * json_decode - * Convert a JSON-encoded string to a JSON node. - * @str must be valid UTF-8. + * Convert a JSON-encoded string to a JSON node. + * @str must be valid UTF-8. */ json_node * json_decode(const char *str) @@ -699,33 +687,33 @@ decode_number(const char **sp) /* * json_decode_string - * If you're interested in the decoding JSON in general, see json_decode. + * If you're interested in the decoding JSON in general, see json_decode. * - * Decodes a JSON string literal (e.g. "\"hello\""). + * Decodes a JSON string literal (e.g. "\"hello\""). * - * If strict is true, string must be double-quoted, - * as is required by the JSON RFC. - * Otherwise (e.g. if parsing something JSON-like, such as JSONPath), - * the string may be single- or double-quoted. + * If strict is true, string must be double-quoted, + * as is required by the JSON RFC. + * Otherwise (e.g. if parsing something JSON-like, such as JSONPath), + * the string may be single- or double-quoted. * - * Also, no whitespace skipping is done, so the caller should only - * call this function when it expects **sp to be either " or ' + * Also, no whitespace skipping is done, so the caller should only + * call this function when it expects **sp to be either " or ' * - * On success, returns the decoded string, passes that string's length - * through *length (which must not be NULL), and advances *sp to point - * to the end of string literal (including the quote character). + * On success, returns the decoded string, passes that string's length + * through *length (which must not be NULL), and advances *sp to point + * to the end of string literal (including the quote character). * - * On failure (parse error), returns NULL and - * leaves *length and *sp untouched. + * On failure (parse error), returns NULL and + * leaves *length and *sp untouched. */ char * json_decode_string(const char **sp, size_t *length, bool strict) { - const char *s = *sp; - StringInfoData ret; - char buf[4]; - int len; - char quote; + const char *s = *sp; + StringInfoData ret; + char buf[4]; + int len; + char quote; Assert(length != NULL); @@ -877,7 +865,7 @@ json_text_type(const char *str, size_t nbytes) static void encode_string(StringInfo out, const char *string, size_t length, char quote, - bool escape_unicode) + bool escape_unicode) { const char *s = string; const char *e = s + length; @@ -993,10 +981,10 @@ encode_number(StringInfo out, const char *string) typedef struct { - StringInfoData str; - bool use_orig; - bool escape_unicode; - bool trim; + StringInfoData str; + bool use_orig; + bool escape_unicode; + bool trim; } json_encode_ctx; static bool json_encode_recurse(json_node * node, json_encode_ctx * ctx); @@ -1007,9 +995,9 @@ json_encode(json_node * node, int options) json_encode_ctx ctx; initStringInfo(&ctx.str); - ctx.use_orig = !!(options & JSONOPT_USE_ORIG); - ctx.escape_unicode = !!(options & JSONOPT_ESCAPE_UNICODE); - ctx.trim = !(options & JSONOPT_NO_TRIM); + ctx.use_orig = !!(options & JSONOPT_USE_ORIG); + ctx.escape_unicode = !!(options & JSONOPT_ESCAPE_UNICODE); + ctx.trim = !(options & JSONOPT_NO_TRIM); if (!json_encode_recurse(node, &ctx)) { @@ -1060,8 +1048,8 @@ json_encode_recurse(json_node * node, json_encode_ctx * ctx) break; case JSON_STRING: encode_string(&ctx->str, - node->v.string.str, node->v.string.length, - '"', ctx->escape_unicode); + node->v.string.str, node->v.string.length, + '"', ctx->escape_unicode); break; case JSON_NUMBER: if (!encode_number(&ctx->str, node->v.number)) @@ -1098,7 +1086,7 @@ json_encode_recurse(json_node * node, json_encode_ctx * ctx) push_orig(key); else encode_string(&ctx->str, node->key, node->key_length, - '"', ctx->escape_unicode); + '"', ctx->escape_unicode); if (has_orig(key_right_space)) push_orig(key_right_space); @@ -1132,19 +1120,19 @@ json_encode_recurse(json_node * node, json_encode_ctx * ctx) /* * json_encode_string - * If you're interested in encoding JSON in general, see json_encode . + * If you're interested in encoding JSON in general, see json_encode . * - * Encodes a string literal JSON-style using the given quote character. - * Note that using anything but '"' as the quote character will result - * in invalid JSON. + * Encodes a string literal JSON-style using the given quote character. + * Note that using anything but '"' as the quote character will result + * in invalid JSON. * - * @str must be valid UTF-8, though it may contain null characters - * (hence the length argument). - * @quote must not be a backslash. + * @str must be valid UTF-8, though it may contain null characters + * (hence the length argument). + * @quote must not be a backslash. */ char * json_encode_string(const char *str, size_t length, char quote, - bool escape_unicode) + bool escape_unicode) { StringInfoData ret; |
