ffi-yajl 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +201 -0
- data/README.md +17 -0
- data/Rakefile +62 -0
- data/bin/ffi-yajl-bench +36 -0
- data/ext/ffi_yajl/ext/encoder/encoder.c +240 -0
- data/ext/ffi_yajl/ext/encoder/extconf.rb +29 -0
- data/ext/ffi_yajl/ext/parser/extconf.rb +29 -0
- data/ext/ffi_yajl/ext/parser/parser.c +199 -0
- data/ext/libyajl2/extconf.rb +65 -0
- data/ext/libyajl2/vendored/.gitignore +3 -0
- data/ext/libyajl2/vendored/BUILDING +23 -0
- data/ext/libyajl2/vendored/BUILDING.win32 +27 -0
- data/ext/libyajl2/vendored/CMakeLists.txt +79 -0
- data/ext/libyajl2/vendored/COPYING +13 -0
- data/ext/libyajl2/vendored/ChangeLog +175 -0
- data/ext/libyajl2/vendored/README +74 -0
- data/ext/libyajl2/vendored/TODO +9 -0
- data/ext/libyajl2/vendored/YAJLDoc.cmake +26 -0
- data/ext/libyajl2/vendored/configure +79 -0
- data/ext/libyajl2/vendored/example/CMakeLists.txt +23 -0
- data/ext/libyajl2/vendored/example/README.md +7 -0
- data/ext/libyajl2/vendored/example/parse_config.c +69 -0
- data/ext/libyajl2/vendored/example/sample.config +101 -0
- data/ext/libyajl2/vendored/perf/CMakeLists.txt +23 -0
- data/ext/libyajl2/vendored/perf/documents.c +1418 -0
- data/ext/libyajl2/vendored/perf/documents.h +28 -0
- data/ext/libyajl2/vendored/perf/perftest.c +134 -0
- data/ext/libyajl2/vendored/reformatter/CMakeLists.txt +39 -0
- data/ext/libyajl2/vendored/reformatter/json_reformat.c +194 -0
- data/ext/libyajl2/vendored/src/CMakeLists.txt +86 -0
- data/ext/libyajl2/vendored/src/YAJL.dxy +1258 -0
- data/ext/libyajl2/vendored/src/api/yajl_common.h +75 -0
- data/ext/libyajl2/vendored/src/api/yajl_gen.h +157 -0
- data/ext/libyajl2/vendored/src/api/yajl_parse.h +226 -0
- data/ext/libyajl2/vendored/src/api/yajl_tree.h +185 -0
- data/ext/libyajl2/vendored/src/api/yajl_version.h.cmake +23 -0
- data/ext/libyajl2/vendored/src/yajl +33 -0
- data/ext/libyajl2/vendored/src/yajl.c +175 -0
- data/ext/libyajl2/vendored/src/yajl.pc.cmake +9 -0
- data/ext/libyajl2/vendored/src/yajl_alloc.c +52 -0
- data/ext/libyajl2/vendored/src/yajl_alloc.h +34 -0
- data/ext/libyajl2/vendored/src/yajl_buf.c +103 -0
- data/ext/libyajl2/vendored/src/yajl_buf.h +57 -0
- data/ext/libyajl2/vendored/src/yajl_bytestack.h +69 -0
- data/ext/libyajl2/vendored/src/yajl_encode.c +220 -0
- data/ext/libyajl2/vendored/src/yajl_encode.h +34 -0
- data/ext/libyajl2/vendored/src/yajl_gen.c +354 -0
- data/ext/libyajl2/vendored/src/yajl_lex.c +763 -0
- data/ext/libyajl2/vendored/src/yajl_lex.h +117 -0
- data/ext/libyajl2/vendored/src/yajl_parser.c +498 -0
- data/ext/libyajl2/vendored/src/yajl_parser.h +78 -0
- data/ext/libyajl2/vendored/src/yajl_tree.c +503 -0
- data/ext/libyajl2/vendored/src/yajl_version.c +7 -0
- data/ext/libyajl2/vendored/test/CMakeLists.txt +23 -0
- data/ext/libyajl2/vendored/test/cases/ac_difficult_json_c_test_case_with_comments.json +1 -0
- data/ext/libyajl2/vendored/test/cases/ac_difficult_json_c_test_case_with_comments.json.gold +36 -0
- data/ext/libyajl2/vendored/test/cases/ac_simple_with_comments.json +11 -0
- data/ext/libyajl2/vendored/test/cases/ac_simple_with_comments.json.gold +9 -0
- data/ext/libyajl2/vendored/test/cases/ag_false_then_garbage.json +1 -0
- data/ext/libyajl2/vendored/test/cases/ag_false_then_garbage.json.gold +2 -0
- data/ext/libyajl2/vendored/test/cases/ag_null_then_garbage.json +1 -0
- data/ext/libyajl2/vendored/test/cases/ag_null_then_garbage.json.gold +2 -0
- data/ext/libyajl2/vendored/test/cases/ag_true_then_garbage.json +1 -0
- data/ext/libyajl2/vendored/test/cases/ag_true_then_garbage.json.gold +2 -0
- data/ext/libyajl2/vendored/test/cases/am_eof.json +1 -0
- data/ext/libyajl2/vendored/test/cases/am_eof.json.gold +4 -0
- data/ext/libyajl2/vendored/test/cases/am_integers.json +1 -0
- data/ext/libyajl2/vendored/test/cases/am_integers.json.gold +3 -0
- data/ext/libyajl2/vendored/test/cases/am_multiple.json +3 -0
- data/ext/libyajl2/vendored/test/cases/am_multiple.json.gold +5 -0
- data/ext/libyajl2/vendored/test/cases/am_stuff.json +7 -0
- data/ext/libyajl2/vendored/test/cases/am_stuff.json.gold +14 -0
- data/ext/libyajl2/vendored/test/cases/ap_array_open.json +1 -0
- data/ext/libyajl2/vendored/test/cases/ap_array_open.json.gold +2 -0
- data/ext/libyajl2/vendored/test/cases/ap_eof_str.json +1 -0
- data/ext/libyajl2/vendored/test/cases/ap_eof_str.json.gold +1 -0
- data/ext/libyajl2/vendored/test/cases/ap_map_open.json +1 -0
- data/ext/libyajl2/vendored/test/cases/ap_map_open.json.gold +2 -0
- data/ext/libyajl2/vendored/test/cases/ap_partial_ok.json +1 -0
- data/ext/libyajl2/vendored/test/cases/ap_partial_ok.json.gold +4 -0
- data/ext/libyajl2/vendored/test/cases/array.json +6 -0
- data/ext/libyajl2/vendored/test/cases/array.json.gold +22 -0
- data/ext/libyajl2/vendored/test/cases/array_close.json +1 -0
- data/ext/libyajl2/vendored/test/cases/array_close.json.gold +2 -0
- data/ext/libyajl2/vendored/test/cases/bignums.json +1 -0
- data/ext/libyajl2/vendored/test/cases/bignums.json.gold +5 -0
- data/ext/libyajl2/vendored/test/cases/bogus_char.json +4 -0
- data/ext/libyajl2/vendored/test/cases/bogus_char.json.gold +10 -0
- data/ext/libyajl2/vendored/test/cases/codepoints_from_unicode_org.json +1 -0
- data/ext/libyajl2/vendored/test/cases/codepoints_from_unicode_org.json.gold +2 -0
- data/ext/libyajl2/vendored/test/cases/deep_arrays.json +1 -0
- data/ext/libyajl2/vendored/test/cases/deep_arrays.json.gold +2049 -0
- data/ext/libyajl2/vendored/test/cases/difficult_json_c_test_case.json +1 -0
- data/ext/libyajl2/vendored/test/cases/difficult_json_c_test_case.json.gold +36 -0
- data/ext/libyajl2/vendored/test/cases/doubles.json +1 -0
- data/ext/libyajl2/vendored/test/cases/doubles.json.gold +7 -0
- data/ext/libyajl2/vendored/test/cases/doubles_in_array.json +1 -0
- data/ext/libyajl2/vendored/test/cases/doubles_in_array.json.gold +8 -0
- data/ext/libyajl2/vendored/test/cases/empty_array.json +1 -0
- data/ext/libyajl2/vendored/test/cases/empty_array.json.gold +3 -0
- data/ext/libyajl2/vendored/test/cases/empty_string.json +1 -0
- data/ext/libyajl2/vendored/test/cases/empty_string.json.gold +2 -0
- data/ext/libyajl2/vendored/test/cases/escaped_bulgarian.json +4 -0
- data/ext/libyajl2/vendored/test/cases/escaped_bulgarian.json.gold +7 -0
- data/ext/libyajl2/vendored/test/cases/escaped_foobar.json +1 -0
- data/ext/libyajl2/vendored/test/cases/escaped_foobar.json.gold +2 -0
- data/ext/libyajl2/vendored/test/cases/false.json +1 -0
- data/ext/libyajl2/vendored/test/cases/false.json.gold +2 -0
- data/ext/libyajl2/vendored/test/cases/fg_false_then_garbage.json +1 -0
- data/ext/libyajl2/vendored/test/cases/fg_false_then_garbage.json.gold +3 -0
- data/ext/libyajl2/vendored/test/cases/fg_issue_7.json +1 -0
- data/ext/libyajl2/vendored/test/cases/fg_issue_7.json.gold +3 -0
- data/ext/libyajl2/vendored/test/cases/fg_null_then_garbage.json +1 -0
- data/ext/libyajl2/vendored/test/cases/fg_null_then_garbage.json.gold +3 -0
- data/ext/libyajl2/vendored/test/cases/fg_true_then_garbage.json +1 -0
- data/ext/libyajl2/vendored/test/cases/fg_true_then_garbage.json.gold +3 -0
- data/ext/libyajl2/vendored/test/cases/four_byte_utf8.json +2 -0
- data/ext/libyajl2/vendored/test/cases/four_byte_utf8.json.gold +5 -0
- data/ext/libyajl2/vendored/test/cases/high_overflow.json +1 -0
- data/ext/libyajl2/vendored/test/cases/high_overflow.json.gold +2 -0
- data/ext/libyajl2/vendored/test/cases/integers.json +3 -0
- data/ext/libyajl2/vendored/test/cases/integers.json.gold +14 -0
- data/ext/libyajl2/vendored/test/cases/invalid_utf8.json +1 -0
- data/ext/libyajl2/vendored/test/cases/invalid_utf8.json.gold +3 -0
- data/ext/libyajl2/vendored/test/cases/isolated_surrogate_marker.json +1 -0
- data/ext/libyajl2/vendored/test/cases/isolated_surrogate_marker.json.gold +2 -0
- data/ext/libyajl2/vendored/test/cases/leading_zero_in_number.json +1 -0
- data/ext/libyajl2/vendored/test/cases/leading_zero_in_number.json.gold +5 -0
- data/ext/libyajl2/vendored/test/cases/lonely_minus_sign.json +7 -0
- data/ext/libyajl2/vendored/test/cases/lonely_minus_sign.json.gold +9 -0
- data/ext/libyajl2/vendored/test/cases/lonely_number.json +1 -0
- data/ext/libyajl2/vendored/test/cases/lonely_number.json.gold +2 -0
- data/ext/libyajl2/vendored/test/cases/low_overflow.json +1 -0
- data/ext/libyajl2/vendored/test/cases/low_overflow.json.gold +2 -0
- data/ext/libyajl2/vendored/test/cases/map_close.json +1 -0
- data/ext/libyajl2/vendored/test/cases/map_close.json.gold +2 -0
- data/ext/libyajl2/vendored/test/cases/missing_integer_after_decimal_point.json +1 -0
- data/ext/libyajl2/vendored/test/cases/missing_integer_after_decimal_point.json.gold +2 -0
- data/ext/libyajl2/vendored/test/cases/missing_integer_after_exponent.json +1 -0
- data/ext/libyajl2/vendored/test/cases/missing_integer_after_exponent.json.gold +2 -0
- data/ext/libyajl2/vendored/test/cases/multiple.json +3 -0
- data/ext/libyajl2/vendored/test/cases/multiple.json.gold +4 -0
- data/ext/libyajl2/vendored/test/cases/non_utf8_char_in_string.json +1 -0
- data/ext/libyajl2/vendored/test/cases/non_utf8_char_in_string.json.gold +8 -0
- data/ext/libyajl2/vendored/test/cases/np_partial_bad.json +1 -0
- data/ext/libyajl2/vendored/test/cases/np_partial_bad.json.gold +5 -0
- data/ext/libyajl2/vendored/test/cases/null.json +1 -0
- data/ext/libyajl2/vendored/test/cases/null.json.gold +2 -0
- data/ext/libyajl2/vendored/test/cases/nulls_and_bools.json +5 -0
- data/ext/libyajl2/vendored/test/cases/nulls_and_bools.json.gold +9 -0
- data/ext/libyajl2/vendored/test/cases/simple.json +5 -0
- data/ext/libyajl2/vendored/test/cases/simple.json.gold +9 -0
- data/ext/libyajl2/vendored/test/cases/simple_with_comments.json +11 -0
- data/ext/libyajl2/vendored/test/cases/simple_with_comments.json.gold +5 -0
- data/ext/libyajl2/vendored/test/cases/string_invalid_escape.json +1 -0
- data/ext/libyajl2/vendored/test/cases/string_invalid_escape.json.gold +3 -0
- data/ext/libyajl2/vendored/test/cases/string_invalid_hex_char.json +1 -0
- data/ext/libyajl2/vendored/test/cases/string_invalid_hex_char.json.gold +2 -0
- data/ext/libyajl2/vendored/test/cases/string_with_escapes.json +3 -0
- data/ext/libyajl2/vendored/test/cases/string_with_escapes.json.gold +7 -0
- data/ext/libyajl2/vendored/test/cases/string_with_invalid_newline.json +2 -0
- data/ext/libyajl2/vendored/test/cases/string_with_invalid_newline.json.gold +2 -0
- data/ext/libyajl2/vendored/test/cases/three_byte_utf8.json +1 -0
- data/ext/libyajl2/vendored/test/cases/three_byte_utf8.json.gold +7 -0
- data/ext/libyajl2/vendored/test/cases/true.json +1 -0
- data/ext/libyajl2/vendored/test/cases/true.json.gold +2 -0
- data/ext/libyajl2/vendored/test/cases/unescaped_bulgarian.json +1 -0
- data/ext/libyajl2/vendored/test/cases/unescaped_bulgarian.json.gold +4 -0
- data/ext/libyajl2/vendored/test/cases/zerobyte.json +1 -0
- data/ext/libyajl2/vendored/test/cases/zerobyte.json.gold +0 -0
- data/ext/libyajl2/vendored/test/run_tests.sh +94 -0
- data/ext/libyajl2/vendored/test/yajl_test.c +281 -0
- data/ext/libyajl2/vendored/verify/CMakeLists.txt +39 -0
- data/ext/libyajl2/vendored/verify/json_verify.c +116 -0
- data/lib/ffi_yajl.rb +14 -0
- data/lib/ffi_yajl/benchmark.rb +7 -0
- data/lib/ffi_yajl/benchmark/MIT-LICENSE +20 -0
- data/lib/ffi_yajl/benchmark/encode.rb +135 -0
- data/lib/ffi_yajl/benchmark/encode_json_and_marshal.rb +42 -0
- data/lib/ffi_yajl/benchmark/encode_json_and_yaml.rb +53 -0
- data/lib/ffi_yajl/benchmark/encode_profile.rb +38 -0
- data/lib/ffi_yajl/benchmark/http.rb +32 -0
- data/lib/ffi_yajl/benchmark/parse.rb +133 -0
- data/lib/ffi_yajl/benchmark/parse_json_and_marshal.rb +50 -0
- data/lib/ffi_yajl/benchmark/parse_json_and_yaml.rb +55 -0
- data/lib/ffi_yajl/benchmark/parse_profile.rb +37 -0
- data/lib/ffi_yajl/benchmark/parse_profile_ruby_prof.rb +39 -0
- data/lib/ffi_yajl/benchmark/parse_stream.rb +54 -0
- data/lib/ffi_yajl/benchmark/subjects/item.json +1 -0
- data/lib/ffi_yajl/benchmark/subjects/ohai.json +1216 -0
- data/lib/ffi_yajl/benchmark/subjects/ohai.marshal_dump +0 -0
- data/lib/ffi_yajl/benchmark/subjects/ohai.yml +975 -0
- data/lib/ffi_yajl/benchmark/subjects/twitter_search.json +1 -0
- data/lib/ffi_yajl/benchmark/subjects/twitter_stream.json +430 -0
- data/lib/ffi_yajl/benchmark/subjects/unicode.json +1 -0
- data/lib/ffi_yajl/encoder.rb +53 -0
- data/lib/ffi_yajl/ext.rb +22 -0
- data/lib/ffi_yajl/ext/.keep +0 -0
- data/lib/ffi_yajl/ffi.rb +129 -0
- data/lib/ffi_yajl/ffi/encoder.rb +175 -0
- data/lib/ffi_yajl/ffi/parser.rb +145 -0
- data/lib/ffi_yajl/json_gem.rb +121 -0
- data/lib/ffi_yajl/parser.rb +23 -0
- data/lib/ffi_yajl/version.rb +3 -0
- data/lib/libyajl.so +0 -0
- data/lib/libyajl.so.2 +0 -0
- data/lib/libyajl.so.2.0.5 +0 -0
- data/lib/libyajl_s.a +0 -0
- data/spec/ffi_yajl/encoder_spec.rb +39 -0
- data/spec/ffi_yajl/json_gem_spec.rb +355 -0
- data/spec/ffi_yajl/parser_spec.rb +78 -0
- data/spec/spec_helper.rb +14 -0
- metadata +332 -0
@@ -0,0 +1 @@
|
|
1
|
+
"\u0000"
|
Binary file
|
@@ -0,0 +1,94 @@
|
|
1
|
+
#!/bin/sh
|
2
|
+
|
3
|
+
ECHO=`which echo`
|
4
|
+
|
5
|
+
DIFF_FLAGS="-u"
|
6
|
+
case "$(uname)" in
|
7
|
+
*W32*)
|
8
|
+
DIFF_FLAGS="-wu"
|
9
|
+
;;
|
10
|
+
esac
|
11
|
+
|
12
|
+
if [ -z "$testBin" ]; then
|
13
|
+
testBin="$1"
|
14
|
+
fi
|
15
|
+
|
16
|
+
# find test binary on both platforms. allow the caller to force a
|
17
|
+
# particular test binary (useful for non-cmake build systems).
|
18
|
+
if [ -z "$testBin" ]; then
|
19
|
+
testBin="../build/test/Release/yajl_test.exe"
|
20
|
+
if [ ! -x $testBin ] ; then
|
21
|
+
testBin="../build/test/Debug/yajl_test.exe"
|
22
|
+
if [ ! -x $testBin ] ; then
|
23
|
+
testBin="../build/test/yajl_test"
|
24
|
+
if [ ! -x $testBin ] ; then
|
25
|
+
${ECHO} "cannot execute test binary: '$testBin'"
|
26
|
+
exit 1;
|
27
|
+
fi
|
28
|
+
fi
|
29
|
+
fi
|
30
|
+
fi
|
31
|
+
|
32
|
+
${ECHO} "using test binary: $testBin"
|
33
|
+
|
34
|
+
testBinShort=`basename $testBin`
|
35
|
+
|
36
|
+
testsSucceeded=0
|
37
|
+
testsTotal=0
|
38
|
+
|
39
|
+
for file in cases/*.json ; do
|
40
|
+
allowComments=""
|
41
|
+
allowGarbage=""
|
42
|
+
allowMultiple=""
|
43
|
+
allowPartials=""
|
44
|
+
|
45
|
+
# if the filename starts with dc_, we disallow comments for this test
|
46
|
+
case $(basename $file) in
|
47
|
+
ac_*)
|
48
|
+
allowComments="-c "
|
49
|
+
;;
|
50
|
+
ag_*)
|
51
|
+
allowGarbage="-g "
|
52
|
+
;;
|
53
|
+
am_*)
|
54
|
+
allowMultiple="-m ";
|
55
|
+
;;
|
56
|
+
ap_*)
|
57
|
+
allowPartials="-p ";
|
58
|
+
;;
|
59
|
+
esac
|
60
|
+
fileShort=`basename $file`
|
61
|
+
testName=`echo $fileShort | sed -e 's/\.json$//'`
|
62
|
+
|
63
|
+
${ECHO} -n " test ($testName): "
|
64
|
+
iter=1
|
65
|
+
success="SUCCESS"
|
66
|
+
|
67
|
+
# ${ECHO} -n "$testBinShort $allowPartials$allowComments$allowGarbage$allowMultiple-b $iter < $fileShort > ${fileShort}.test : "
|
68
|
+
# parse with a read buffer size ranging from 1-31 to stress stream parsing
|
69
|
+
while [ $iter -lt 32 ] && [ $success = "SUCCESS" ] ; do
|
70
|
+
$testBin $allowPartials $allowComments $allowGarbage $allowMultiple -b $iter < $file > ${file}.test 2>&1
|
71
|
+
diff ${DIFF_FLAGS} ${file}.gold ${file}.test > ${file}.out
|
72
|
+
if [ $? -eq 0 ] ; then
|
73
|
+
if [ $iter -eq 31 ] ; then testsSucceeded=$(( $testsSucceeded + 1 )) ; fi
|
74
|
+
else
|
75
|
+
success="FAILURE"
|
76
|
+
iter=32
|
77
|
+
${ECHO}
|
78
|
+
cat ${file}.out
|
79
|
+
fi
|
80
|
+
iter=$(( iter + 1 ))
|
81
|
+
rm ${file}.test ${file}.out
|
82
|
+
done
|
83
|
+
|
84
|
+
${ECHO} $success
|
85
|
+
testsTotal=$(( testsTotal + 1 ))
|
86
|
+
done
|
87
|
+
|
88
|
+
${ECHO} $testsSucceeded/$testsTotal tests successful
|
89
|
+
|
90
|
+
if [ $testsSucceeded != $testsTotal ] ; then
|
91
|
+
exit 1
|
92
|
+
fi
|
93
|
+
|
94
|
+
exit 0
|
@@ -0,0 +1,281 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) 2007-2011, Lloyd Hilaiel <lloyd@hilaiel.com>
|
3
|
+
*
|
4
|
+
* Permission to use, copy, modify, and/or distribute this software for any
|
5
|
+
* purpose with or without fee is hereby granted, provided that the above
|
6
|
+
* copyright notice and this permission notice appear in all copies.
|
7
|
+
*
|
8
|
+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
9
|
+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
10
|
+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
11
|
+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
12
|
+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
13
|
+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
14
|
+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
15
|
+
*/
|
16
|
+
|
17
|
+
#include <yajl/yajl_parse.h>
|
18
|
+
#include <yajl/yajl_gen.h>
|
19
|
+
|
20
|
+
#include <stdio.h>
|
21
|
+
#include <stdlib.h>
|
22
|
+
#include <string.h>
|
23
|
+
|
24
|
+
#include <assert.h>
|
25
|
+
|
26
|
+
/* memory debugging routines */
|
27
|
+
typedef struct
|
28
|
+
{
|
29
|
+
unsigned int numFrees;
|
30
|
+
unsigned int numMallocs;
|
31
|
+
/* XXX: we really need a hash table here with per-allocation
|
32
|
+
* information */
|
33
|
+
} yajlTestMemoryContext;
|
34
|
+
|
35
|
+
/* cast void * into context */
|
36
|
+
#define TEST_CTX(vptr) ((yajlTestMemoryContext *) (vptr))
|
37
|
+
|
38
|
+
static void yajlTestFree(void * ctx, void * ptr)
|
39
|
+
{
|
40
|
+
assert(ptr != NULL);
|
41
|
+
TEST_CTX(ctx)->numFrees++;
|
42
|
+
free(ptr);
|
43
|
+
}
|
44
|
+
|
45
|
+
static void * yajlTestMalloc(void * ctx, size_t sz)
|
46
|
+
{
|
47
|
+
assert(sz != 0);
|
48
|
+
TEST_CTX(ctx)->numMallocs++;
|
49
|
+
return malloc(sz);
|
50
|
+
}
|
51
|
+
|
52
|
+
static void * yajlTestRealloc(void * ctx, void * ptr, size_t sz)
|
53
|
+
{
|
54
|
+
if (ptr == NULL) {
|
55
|
+
assert(sz != 0);
|
56
|
+
TEST_CTX(ctx)->numMallocs++;
|
57
|
+
} else if (sz == 0) {
|
58
|
+
TEST_CTX(ctx)->numFrees++;
|
59
|
+
}
|
60
|
+
|
61
|
+
return realloc(ptr, sz);
|
62
|
+
}
|
63
|
+
|
64
|
+
|
65
|
+
/* begin parsing callback routines */
|
66
|
+
#define BUF_SIZE 2048
|
67
|
+
|
68
|
+
static int test_yajl_null(void *ctx)
|
69
|
+
{
|
70
|
+
printf("null\n");
|
71
|
+
return 1;
|
72
|
+
}
|
73
|
+
|
74
|
+
static int test_yajl_boolean(void * ctx, int boolVal)
|
75
|
+
{
|
76
|
+
printf("bool: %s\n", boolVal ? "true" : "false");
|
77
|
+
return 1;
|
78
|
+
}
|
79
|
+
|
80
|
+
static int test_yajl_integer(void *ctx, long long integerVal)
|
81
|
+
{
|
82
|
+
printf("integer: %lld\n", integerVal);
|
83
|
+
return 1;
|
84
|
+
}
|
85
|
+
|
86
|
+
static int test_yajl_double(void *ctx, double doubleVal)
|
87
|
+
{
|
88
|
+
printf("double: %g\n", doubleVal);
|
89
|
+
return 1;
|
90
|
+
}
|
91
|
+
|
92
|
+
static int test_yajl_string(void *ctx, const unsigned char * stringVal,
|
93
|
+
size_t stringLen)
|
94
|
+
{
|
95
|
+
printf("string: '");
|
96
|
+
fwrite(stringVal, 1, stringLen, stdout);
|
97
|
+
printf("'\n");
|
98
|
+
return 1;
|
99
|
+
}
|
100
|
+
|
101
|
+
static int test_yajl_map_key(void *ctx, const unsigned char * stringVal,
|
102
|
+
size_t stringLen)
|
103
|
+
{
|
104
|
+
char * str = (char *) malloc(stringLen + 1);
|
105
|
+
str[stringLen] = 0;
|
106
|
+
memcpy(str, stringVal, stringLen);
|
107
|
+
printf("key: '%s'\n", str);
|
108
|
+
free(str);
|
109
|
+
return 1;
|
110
|
+
}
|
111
|
+
|
112
|
+
static int test_yajl_start_map(void *ctx)
|
113
|
+
{
|
114
|
+
printf("map open '{'\n");
|
115
|
+
return 1;
|
116
|
+
}
|
117
|
+
|
118
|
+
|
119
|
+
static int test_yajl_end_map(void *ctx)
|
120
|
+
{
|
121
|
+
printf("map close '}'\n");
|
122
|
+
return 1;
|
123
|
+
}
|
124
|
+
|
125
|
+
static int test_yajl_start_array(void *ctx)
|
126
|
+
{
|
127
|
+
printf("array open '['\n");
|
128
|
+
return 1;
|
129
|
+
}
|
130
|
+
|
131
|
+
static int test_yajl_end_array(void *ctx)
|
132
|
+
{
|
133
|
+
printf("array close ']'\n");
|
134
|
+
return 1;
|
135
|
+
}
|
136
|
+
|
137
|
+
static yajl_callbacks callbacks = {
|
138
|
+
test_yajl_null,
|
139
|
+
test_yajl_boolean,
|
140
|
+
test_yajl_integer,
|
141
|
+
test_yajl_double,
|
142
|
+
NULL,
|
143
|
+
test_yajl_string,
|
144
|
+
test_yajl_start_map,
|
145
|
+
test_yajl_map_key,
|
146
|
+
test_yajl_end_map,
|
147
|
+
test_yajl_start_array,
|
148
|
+
test_yajl_end_array
|
149
|
+
};
|
150
|
+
|
151
|
+
static void usage(const char * progname)
|
152
|
+
{
|
153
|
+
fprintf(stderr,
|
154
|
+
"usage: %s [options]\n"
|
155
|
+
"Parse input from stdin as JSON and ouput parsing details "
|
156
|
+
"to stdout\n"
|
157
|
+
" -b set the read buffer size\n"
|
158
|
+
" -c allow comments\n"
|
159
|
+
" -g allow *g*arbage after valid JSON text\n"
|
160
|
+
" -m allows the parser to consume multiple JSON values\n"
|
161
|
+
" from a single string separated by whitespace\n"
|
162
|
+
" -p partial JSON documents should not cause errors\n",
|
163
|
+
progname);
|
164
|
+
exit(1);
|
165
|
+
}
|
166
|
+
|
167
|
+
int
|
168
|
+
main(int argc, char ** argv)
|
169
|
+
{
|
170
|
+
yajl_handle hand;
|
171
|
+
const char * fileName;
|
172
|
+
static unsigned char * fileData = NULL;
|
173
|
+
size_t bufSize = BUF_SIZE;
|
174
|
+
yajl_status stat;
|
175
|
+
size_t rd;
|
176
|
+
int i, j;
|
177
|
+
|
178
|
+
/* memory allocation debugging: allocate a structure which collects
|
179
|
+
* statistics */
|
180
|
+
yajlTestMemoryContext memCtx = { 0,0 };
|
181
|
+
|
182
|
+
/* memory allocation debugging: allocate a structure which holds
|
183
|
+
* allocation routines */
|
184
|
+
yajl_alloc_funcs allocFuncs = {
|
185
|
+
yajlTestMalloc,
|
186
|
+
yajlTestRealloc,
|
187
|
+
yajlTestFree,
|
188
|
+
(void *) NULL
|
189
|
+
};
|
190
|
+
|
191
|
+
allocFuncs.ctx = (void *) &memCtx;
|
192
|
+
|
193
|
+
/* allocate the parser */
|
194
|
+
hand = yajl_alloc(&callbacks, &allocFuncs, NULL);
|
195
|
+
|
196
|
+
/* check arguments. We expect exactly one! */
|
197
|
+
for (i=1;i<argc;i++) {
|
198
|
+
if (!strcmp("-c", argv[i])) {
|
199
|
+
yajl_config(hand, yajl_allow_comments, 1);
|
200
|
+
} else if (!strcmp("-b", argv[i])) {
|
201
|
+
if (++i >= argc) usage(argv[0]);
|
202
|
+
|
203
|
+
/* validate integer */
|
204
|
+
for (j=0;j<(int)strlen(argv[i]);j++) {
|
205
|
+
if (argv[i][j] <= '9' && argv[i][j] >= '0') continue;
|
206
|
+
fprintf(stderr, "-b requires an integer argument. '%s' "
|
207
|
+
"is invalid\n", argv[i]);
|
208
|
+
usage(argv[0]);
|
209
|
+
}
|
210
|
+
|
211
|
+
bufSize = atoi(argv[i]);
|
212
|
+
if (!bufSize) {
|
213
|
+
fprintf(stderr, "%zu is an invalid buffer size\n",
|
214
|
+
bufSize);
|
215
|
+
}
|
216
|
+
} else if (!strcmp("-g", argv[i])) {
|
217
|
+
yajl_config(hand, yajl_allow_trailing_garbage, 1);
|
218
|
+
} else if (!strcmp("-m", argv[i])) {
|
219
|
+
yajl_config(hand, yajl_allow_multiple_values, 1);
|
220
|
+
} else if (!strcmp("-p", argv[i])) {
|
221
|
+
yajl_config(hand, yajl_allow_partial_values, 1);
|
222
|
+
} else {
|
223
|
+
fprintf(stderr, "invalid command line option: '%s'\n",
|
224
|
+
argv[i]);
|
225
|
+
usage(argv[0]);
|
226
|
+
}
|
227
|
+
}
|
228
|
+
|
229
|
+
fileData = (unsigned char *) malloc(bufSize);
|
230
|
+
|
231
|
+
if (fileData == NULL) {
|
232
|
+
fprintf(stderr,
|
233
|
+
"failed to allocate read buffer of %zu bytes, exiting.",
|
234
|
+
bufSize);
|
235
|
+
yajl_free(hand);
|
236
|
+
exit(2);
|
237
|
+
}
|
238
|
+
|
239
|
+
fileName = argv[argc-1];
|
240
|
+
|
241
|
+
for (;;) {
|
242
|
+
rd = fread((void *) fileData, 1, bufSize, stdin);
|
243
|
+
|
244
|
+
if (rd == 0) {
|
245
|
+
if (!feof(stdin)) {
|
246
|
+
fprintf(stderr, "error reading from '%s'\n", fileName);
|
247
|
+
}
|
248
|
+
break;
|
249
|
+
}
|
250
|
+
/* read file data, now pass to parser */
|
251
|
+
stat = yajl_parse(hand, fileData, rd);
|
252
|
+
|
253
|
+
if (stat != yajl_status_ok) break;
|
254
|
+
}
|
255
|
+
|
256
|
+
stat = yajl_complete_parse(hand);
|
257
|
+
if (stat != yajl_status_ok)
|
258
|
+
{
|
259
|
+
unsigned char * str = yajl_get_error(hand, 0, fileData, rd);
|
260
|
+
fflush(stdout);
|
261
|
+
fprintf(stderr, "%s", (char *) str);
|
262
|
+
yajl_free_error(hand, str);
|
263
|
+
}
|
264
|
+
|
265
|
+
yajl_free(hand);
|
266
|
+
free(fileData);
|
267
|
+
|
268
|
+
/* finally, print out some memory statistics */
|
269
|
+
|
270
|
+
/* (lth) only print leaks here, as allocations and frees may vary depending
|
271
|
+
* on read buffer size, causing false failures.
|
272
|
+
*
|
273
|
+
* printf("allocations:\t%u\n", memCtx.numMallocs);
|
274
|
+
* printf("frees:\t\t%u\n", memCtx.numFrees);
|
275
|
+
*/
|
276
|
+
fflush(stderr);
|
277
|
+
fflush(stdout);
|
278
|
+
printf("memory leaks:\t%u\n", memCtx.numMallocs - memCtx.numFrees);
|
279
|
+
|
280
|
+
return 0;
|
281
|
+
}
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# Copyright (c) 2007-2011, Lloyd Hilaiel <lloyd@hilaiel.com>
|
2
|
+
#
|
3
|
+
# Permission to use, copy, modify, and/or distribute this software for any
|
4
|
+
# purpose with or without fee is hereby granted, provided that the above
|
5
|
+
# copyright notice and this permission notice appear in all copies.
|
6
|
+
#
|
7
|
+
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
8
|
+
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
9
|
+
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
10
|
+
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
11
|
+
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
12
|
+
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
13
|
+
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
14
|
+
|
15
|
+
# set up some paths
|
16
|
+
SET (binDir ${CMAKE_CURRENT_BINARY_DIR}/../${YAJL_DIST_NAME}/bin)
|
17
|
+
|
18
|
+
# create some directories
|
19
|
+
FILE(MAKE_DIRECTORY ${binDir})
|
20
|
+
|
21
|
+
SET (SRCS json_verify.c)
|
22
|
+
|
23
|
+
# use the library we build, duh.
|
24
|
+
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR}/../${YAJL_DIST_NAME}/include)
|
25
|
+
LINK_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR}/../${YAJL_DIST_NAME}/lib)
|
26
|
+
|
27
|
+
ADD_EXECUTABLE(json_verify ${SRCS})
|
28
|
+
|
29
|
+
TARGET_LINK_LIBRARIES(json_verify yajl_s)
|
30
|
+
|
31
|
+
# copy in the binary
|
32
|
+
GET_TARGET_PROPERTY(binPath json_verify LOCATION)
|
33
|
+
|
34
|
+
ADD_CUSTOM_COMMAND(TARGET json_verify POST_BUILD
|
35
|
+
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${binPath} ${binDir})
|
36
|
+
|
37
|
+
IF (NOT WIN32)
|
38
|
+
INSTALL(TARGETS json_verify RUNTIME DESTINATION bin)
|
39
|
+
ENDIF ()
|
@@ -0,0 +1,116 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) 2007-2011, Lloyd Hilaiel <lloyd@hilaiel.com>
|
3
|
+
*
|
4
|
+
* Permission to use, copy, modify, and/or distribute this software for any
|
5
|
+
* purpose with or without fee is hereby granted, provided that the above
|
6
|
+
* copyright notice and this permission notice appear in all copies.
|
7
|
+
*
|
8
|
+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
9
|
+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
10
|
+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
11
|
+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
12
|
+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
13
|
+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
14
|
+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
15
|
+
*/
|
16
|
+
|
17
|
+
#include <yajl/yajl_parse.h>
|
18
|
+
|
19
|
+
#include <stdio.h>
|
20
|
+
#include <stdlib.h>
|
21
|
+
#include <string.h>
|
22
|
+
|
23
|
+
static void
|
24
|
+
usage(const char * progname)
|
25
|
+
{
|
26
|
+
fprintf(stderr, "%s: validate json from stdin\n"
|
27
|
+
"usage: json_verify [options]\n"
|
28
|
+
" -q quiet mode\n"
|
29
|
+
" -c allow comments\n"
|
30
|
+
" -u allow invalid utf8 inside strings\n",
|
31
|
+
progname);
|
32
|
+
exit(1);
|
33
|
+
}
|
34
|
+
|
35
|
+
int
|
36
|
+
main(int argc, char ** argv)
|
37
|
+
{
|
38
|
+
yajl_status stat;
|
39
|
+
size_t rd;
|
40
|
+
yajl_handle hand;
|
41
|
+
static unsigned char fileData[65536];
|
42
|
+
int quiet = 0;
|
43
|
+
int retval = 0;
|
44
|
+
int a = 1;
|
45
|
+
|
46
|
+
/* allocate a parser */
|
47
|
+
hand = yajl_alloc(NULL, NULL, NULL);
|
48
|
+
|
49
|
+
/* check arguments.*/
|
50
|
+
while ((a < argc) && (argv[a][0] == '-') && (strlen(argv[a]) > 1)) {
|
51
|
+
unsigned int i;
|
52
|
+
for ( i=1; i < strlen(argv[a]); i++) {
|
53
|
+
switch (argv[a][i]) {
|
54
|
+
case 'q':
|
55
|
+
quiet = 1;
|
56
|
+
break;
|
57
|
+
case 'c':
|
58
|
+
yajl_config(hand, yajl_allow_comments, 1);
|
59
|
+
break;
|
60
|
+
case 'u':
|
61
|
+
yajl_config(hand, yajl_dont_validate_strings, 1);
|
62
|
+
break;
|
63
|
+
default:
|
64
|
+
fprintf(stderr, "unrecognized option: '%c'\n\n", argv[a][i]);
|
65
|
+
usage(argv[0]);
|
66
|
+
}
|
67
|
+
}
|
68
|
+
++a;
|
69
|
+
}
|
70
|
+
if (a < argc) {
|
71
|
+
usage(argv[0]);
|
72
|
+
}
|
73
|
+
|
74
|
+
for (;;) {
|
75
|
+
rd = fread((void *) fileData, 1, sizeof(fileData) - 1, stdin);
|
76
|
+
|
77
|
+
retval = 0;
|
78
|
+
|
79
|
+
if (rd == 0) {
|
80
|
+
if (!feof(stdin)) {
|
81
|
+
if (!quiet) {
|
82
|
+
fprintf(stderr, "error encountered on file read\n");
|
83
|
+
}
|
84
|
+
retval = 1;
|
85
|
+
}
|
86
|
+
break;
|
87
|
+
}
|
88
|
+
fileData[rd] = 0;
|
89
|
+
|
90
|
+
/* read file data, pass to parser */
|
91
|
+
stat = yajl_parse(hand, fileData, rd);
|
92
|
+
|
93
|
+
if (stat != yajl_status_ok) break;
|
94
|
+
}
|
95
|
+
|
96
|
+
/* parse any remaining buffered data */
|
97
|
+
stat = yajl_complete_parse(hand);
|
98
|
+
|
99
|
+
if (stat != yajl_status_ok)
|
100
|
+
{
|
101
|
+
if (!quiet) {
|
102
|
+
unsigned char * str = yajl_get_error(hand, 1, fileData, rd);
|
103
|
+
fprintf(stderr, "%s", (const char *) str);
|
104
|
+
yajl_free_error(hand, str);
|
105
|
+
}
|
106
|
+
retval = 1;
|
107
|
+
}
|
108
|
+
|
109
|
+
yajl_free(hand);
|
110
|
+
|
111
|
+
if (!quiet) {
|
112
|
+
printf("JSON is %s\n", retval ? "invalid" : "valid");
|
113
|
+
}
|
114
|
+
|
115
|
+
return retval;
|
116
|
+
}
|