diff options
author | Lloyd Hilaiel <lloyd@hilaiel.com> | 2011-04-26 09:55:20 -0600 |
---|---|---|
committer | Lloyd Hilaiel <lloyd@hilaiel.com> | 2011-04-26 09:55:20 -0600 |
commit | 266dd3e48d4ea4a046ff4a5d4283d104b3cb5973 (patch) | |
tree | 507474029414194f9e00b2dc0499223feeb52d14 | |
parent | 615924b7bf68791c166149a9f76bec740dacefd6 (diff) | |
download | yajl-266dd3e48d4ea4a046ff4a5d4283d104b3cb5973.tar.gz |
add option for escaping the '/' (solidus) character. closes #28
-rw-r--r-- | src/api/yajl_gen.h | 15 | ||||
-rw-r--r-- | src/yajl_encode.c | 20 | ||||
-rw-r--r-- | src/yajl_encode.h | 12 | ||||
-rw-r--r-- | src/yajl_gen.c | 4 |
4 files changed, 25 insertions, 26 deletions
diff --git a/src/api/yajl_gen.h b/src/api/yajl_gen.h index 8f88ef4..52fa99f 100644 --- a/src/api/yajl_gen.h +++ b/src/api/yajl_gen.h @@ -28,7 +28,7 @@ #ifdef __cplusplus extern "C" { -#endif +#endif /** generator status codes */ typedef enum { /** no error */ @@ -91,7 +91,14 @@ extern "C" { * pass to it via yajl_gen_string() are valid UTF8. Enabling * this option will cause it to do so. */ - yajl_gen_validate_utf8 = 0x08 + yajl_gen_validate_utf8 = 0x08, + /** + * the forward solidus (slash or '/' in human) is not required to be + * escaped in json text. By default, YAJL will not escape it in the + * iterest of saving bytes. Setting this flag will cause YAJL to + * always escape '/' in generated JSON strings. + */ + yajl_gen_escape_solidus = 0x10 } yajl_gen_option; /** allow the modification of generator options subsequent to handle @@ -110,7 +117,7 @@ extern "C" { */ YAJL_API yajl_gen yajl_gen_alloc(const yajl_alloc_funcs * allocFuncs); - /** free a generator handle */ + /** free a generator handle */ YAJL_API void yajl_gen_free(yajl_gen handle); YAJL_API yajl_gen_status yajl_gen_integer(yajl_gen hand, long long int number); @@ -125,7 +132,7 @@ extern "C" { const unsigned char * str, size_t len); YAJL_API yajl_gen_status yajl_gen_null(yajl_gen hand); - YAJL_API yajl_gen_status yajl_gen_bool(yajl_gen hand, int boolean); + YAJL_API yajl_gen_status yajl_gen_bool(yajl_gen hand, int boolean); YAJL_API yajl_gen_status yajl_gen_map_open(yajl_gen hand); YAJL_API yajl_gen_status yajl_gen_map_close(yajl_gen hand); YAJL_API yajl_gen_status yajl_gen_array_open(yajl_gen hand); diff --git a/src/yajl_encode.c b/src/yajl_encode.c index 981df5b..9dc9a3e 100644 --- a/src/yajl_encode.c +++ b/src/yajl_encode.c @@ -29,20 +29,14 @@ static void CharToHex(unsigned char c, char * hexBuf) } void -yajl_string_encode(yajl_buf buf, const unsigned char * str, - size_t len) -{ - yajl_string_encode2((const yajl_print_t) &yajl_buf_append, buf, str, len); -} - -void -yajl_string_encode2(const yajl_print_t print, - void * ctx, - const unsigned char * str, - size_t len) +yajl_string_encode(const yajl_print_t print, + void * ctx, + const unsigned char * str, + size_t len, + int escape_solidus) { size_t beg = 0; - size_t end = 0; + size_t end = 0; char hexBuf[7]; hexBuf[0] = '\\'; hexBuf[1] = 'u'; hexBuf[2] = '0'; hexBuf[3] = '0'; hexBuf[6] = 0; @@ -58,7 +52,7 @@ yajl_string_encode2(const yajl_print_t print, * specifically, this production from the grammar: * unescaped = %x20-21 / %x23-5B / %x5D-10FFFF */ - /* case '/': escaped = "\\/"; break; */ + case '/': if (escape_solidus) escaped = "\\/"; break; case '"': escaped = "\\\""; break; case '\f': escaped = "\\f"; break; case '\b': escaped = "\\b"; break; diff --git a/src/yajl_encode.h b/src/yajl_encode.h index 25fbd70..ac7acc6 100644 --- a/src/yajl_encode.h +++ b/src/yajl_encode.h @@ -20,13 +20,11 @@ #include "yajl_buf.h" #include "api/yajl_gen.h" -void yajl_string_encode2(const yajl_print_t printer, - void * ctx, - const unsigned char * str, - size_t length); - -void yajl_string_encode(yajl_buf buf, const unsigned char * str, - size_t length); +void yajl_string_encode(const yajl_print_t printer, + void * ctx, + const unsigned char * str, + size_t length, + int escape_solidus); void yajl_string_decode(yajl_buf buf, const unsigned char * str, size_t length); diff --git a/src/yajl_gen.c b/src/yajl_gen.c index dd693e6..3d42f4d 100644 --- a/src/yajl_gen.c +++ b/src/yajl_gen.c @@ -35,7 +35,7 @@ typedef enum { yajl_gen_error } yajl_gen_state; -struct yajl_gen_t +struct yajl_gen_t { unsigned int flags; unsigned int depth; @@ -252,7 +252,7 @@ yajl_gen_string(yajl_gen g, const unsigned char * str, } ENSURE_VALID_STATE; INSERT_SEP; INSERT_WHITESPACE; g->print(g->ctx, "\"", 1); - yajl_string_encode2(g->print, g->ctx, str, len); + yajl_string_encode(g->print, g->ctx, str, len, g->flags & yajl_gen_escape_solidus); g->print(g->ctx, "\"", 1); APPENDED_ATOM; FINAL_NEWLINE; |