Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add option yajl_dont_unescape_strings #81

Open
wants to merge 1 commit into from

1 participant

@fdmanana

This options allows applications to receive unescaped strings
in their string callback. For such applications, this avoids
having them to escape the string in the string callback, which
saves some CPU/memory.

@fdmanana fdmanana Add option yajl_dont_unescape_strings
This option allows applications to receive unescaped strings
in their string callback. For such applications, this avoids
having them to escape the string in the string callback, which
saves some CPU/memory.
52b04c1
@t3rm1n4l t3rm1n4l referenced this pull request from a commit in t3rm1n4l/couchdb
@fdmanana fdmanana MB-5794 View parser didn't preserve escaping in strings
This includes a patch submitted upstream that tells YAJL
to not unescape strings before passing them to the user's
string callback function.

Upstream pull request:

lloyd/yajl#81

Change-Id: I9855e104dec2ce5bddaed526284afcca06d62c12
Reviewed-on: http://review.couchbase.org/17979
Reviewed-by: Volker Mische <volker.mische@gmail.com>
Tested-by: Filipe David Borba Manana <fdmanana@gmail.com>
588f33d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 6, 2012
  1. @fdmanana

    Add option yajl_dont_unescape_strings

    fdmanana authored
    This option allows applications to receive unescaped strings
    in their string callback. For such applications, this avoids
    having them to escape the string in the string callback, which
    saves some CPU/memory.
This page is out of date. Refresh to see the latest.
View
9 src/api/yajl_parse.h
@@ -156,7 +156,14 @@ extern "C" {
* yajl will enter an error state (premature EOF). Setting this
* flag suppresses that check and the corresponding error.
*/
- yajl_allow_partial_values = 0x10
+ yajl_allow_partial_values = 0x10,
+ /**
+ By default YAJL unescapes strings before passing them to the
+ string callback function. This options disables that behaviour.
+ Some applications might want the string escaped, and without this
+ option their string callback would have to escape the input string.
+ */
+ yajl_dont_unescape_strings = 0x20
} yajl_option;
/** allow the modification of parser options subsequent to handle
View
1  src/yajl.c
@@ -91,6 +91,7 @@ yajl_config(yajl_handle h, yajl_option opt, ...)
case yajl_allow_trailing_garbage:
case yajl_allow_multiple_values:
case yajl_allow_partial_values:
+ case yajl_dont_unescape_strings:
if (va_arg(ap, int)) h->flags |= opt;
else h->flags &= ~opt;
break;
View
11 src/yajl_parser.c
@@ -246,11 +246,16 @@ yajl_do_parse(yajl_handle hand, const unsigned char * jsonText,
break;
case yajl_tok_string_with_escapes:
if (hand->callbacks && hand->callbacks->yajl_string) {
- yajl_buf_clear(hand->decodeBuf);
- yajl_string_decode(hand->decodeBuf, buf, bufLen);
- _CC_CHK(hand->callbacks->yajl_string(
+ if (hand->flags & yajl_dont_unescape_strings) {
+ _CC_CHK(hand->callbacks->yajl_string(hand->ctx,
+ buf, bufLen));
+ } else {
+ yajl_buf_clear(hand->decodeBuf);
+ yajl_string_decode(hand->decodeBuf, buf, bufLen);
+ _CC_CHK(hand->callbacks->yajl_string(
hand->ctx, yajl_buf_data(hand->decodeBuf),
yajl_buf_len(hand->decodeBuf)));
+ }
}
break;
case yajl_tok_bool:
View
1  test/cases/us_string.json
@@ -0,0 +1 @@
+"my escaped \"string\""
View
2  test/cases/us_string.json.gold
@@ -0,0 +1,2 @@
+string: 'my escaped \"string\"'
+memory leaks: 0
View
7 test/run_tests.sh
@@ -41,6 +41,7 @@ for file in cases/*.json ; do
allowGarbage=""
allowMultiple=""
allowPartials=""
+ passEscapedStrings=""
# if the filename starts with dc_, we disallow comments for this test
case $(basename $file) in
@@ -55,7 +56,9 @@ for file in cases/*.json ; do
;;
ap_*)
allowPartials="-p ";
- ;;
+ ;;
+ us_*)
+ passEscapedStrings="-e ";
esac
fileShort=`basename $file`
testName=`echo $fileShort | sed -e 's/\.json$//'`
@@ -67,7 +70,7 @@ for file in cases/*.json ; do
# ${ECHO} -n "$testBinShort $allowPartials$allowComments$allowGarbage$allowMultiple-b $iter < $fileShort > ${fileShort}.test : "
# parse with a read buffer size ranging from 1-31 to stress stream parsing
while [ $iter -lt 32 ] && [ $success = "SUCCESS" ] ; do
- $testBin $allowPartials $allowComments $allowGarbage $allowMultiple -b $iter < $file > ${file}.test 2>&1
+ $testBin $allowPartials $allowComments $allowGarbage $allowMultiple $passEscapedStrings -b $iter < $file > ${file}.test 2>&1
diff ${DIFF_FLAGS} ${file}.gold ${file}.test > ${file}.out
if [ $? -eq 0 ] ; then
if [ $iter -eq 31 ] ; then : $(( testsSucceeded += 1)) ; fi
View
2  test/yajl_test.c
@@ -219,6 +219,8 @@ main(int argc, char ** argv)
yajl_config(hand, yajl_allow_multiple_values, 1);
} else if (!strcmp("-p", argv[i])) {
yajl_config(hand, yajl_allow_partial_values, 1);
+ } else if (!strcmp("-e", argv[i])) {
+ yajl_config(hand, yajl_dont_unescape_strings, 1);
} else {
fprintf(stderr, "invalid command line option: '%s'\n",
argv[i]);
Something went wrong with that request. Please try again.