diff options
| author | Joey Adams | 2010-08-04 21:44:22 +0000 |
|---|---|---|
| committer | Joey Adams | 2010-08-04 21:44:22 +0000 |
| commit | 2b2fda2b7219004d65c1d121e7fed52ba85a9fb8 (patch) | |
| tree | 30300ad44b786a6d5ed6106f1010c481d49b6e10 /json.c | |
| parent | f475e581b72b8c42cf951f6653610d15e71caeee (diff) | |
Made JSON datatype well-behaved with respect to character sets.
Note that this is currently untested with server encodings other than UTF-8.
The encoding policy used is: JSON nodes and most of the JSON functions still
operate in UTF-8. Strings are converted between server encoding and UTF-8
when they go in and out of varlena (text*), and a set of helper functions
are implemented to make these conversions simple to apply.
It is done this way because converting individual codepoints to/from whatever
the server encoding may be is nontrivial (possibly requires a loaded module).
The JSON code needs to encode/decode codepoints when it deals with escapes.
Although a more clever and efficient solution might be to defer charset
conversions to when they're necessary (e.g. round up all the escapes
and encode them all at once), this is not simple, and it's probably not much
more efficient, either. Conversions to/from server encoding and UTF-8
are no-ops when the server encoding is UTF-8, anyway.
Diffstat (limited to 'json.c')
| -rw-r--r-- | json.c | 25 |
1 files changed, 25 insertions, 0 deletions
@@ -351,6 +351,10 @@ char *json_decode_string(const char **sp, size_t *length, bool strict); /* * json_validate * 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). */ bool json_validate(const char *str) @@ -364,6 +368,27 @@ 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. + * + * 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); + + 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. |
