Merge branch 'master' of /home/elanthis/Source/janssonxx
authorSean Middleditch <sean@middleditch.us>
Tue, 19 Jan 2010 05:26:10 +0000 (21:26 -0800)
committerSean Middleditch <sean@middleditch.us>
Tue, 19 Jan 2010 05:26:10 +0000 (21:26 -0800)
Conflicts:
.gitignore

436 files changed:
.gitignore
CHANGES [new file with mode: 0644]
LICENSE [moved from README with 69% similarity]
Makefile [deleted file]
Makefile.am [new file with mode: 0644]
README.rst [new file with mode: 0644]
configure.ac [new file with mode: 0644]
doc/.gitignore [new file with mode: 0644]
doc/Makefile.am [new file with mode: 0644]
doc/README [new file with mode: 0644]
doc/apiref.rst [new file with mode: 0644]
doc/conf.py [new file with mode: 0644]
doc/ext/refcounting.py [new file with mode: 0644]
doc/gettingstarted.rst [new file with mode: 0644]
doc/github_commits.c [new file with mode: 0644]
doc/index.rst [new file with mode: 0644]
doc/tutorial.rst [new file with mode: 0644]
driver.cpp [deleted file]
src/Makefile.am [new file with mode: 0644]
src/dump.c [new file with mode: 0644]
src/hashtable.c [new file with mode: 0644]
src/hashtable.h [new file with mode: 0644]
src/jansson-impl.hpp [moved from jansson-impl.hpp with 100% similarity]
src/jansson.h [new file with mode: 0644]
src/jansson.hpp [moved from jansson.hpp with 100% similarity]
src/jansson_private.h [new file with mode: 0644]
src/load.c [new file with mode: 0644]
src/strbuffer.c [new file with mode: 0644]
src/strbuffer.h [new file with mode: 0644]
src/utf.c [new file with mode: 0644]
src/utf.h [new file with mode: 0644]
src/util.h [new file with mode: 0644]
src/value.c [new file with mode: 0644]
test/.gitignore [new file with mode: 0644]
test/Makefile.am [new file with mode: 0644]
test/bin/Makefile.am [new file with mode: 0644]
test/bin/json_process.c [new file with mode: 0644]
test/run-suites [new file with mode: 0755]
test/scripts/run-tests.sh [new file with mode: 0644]
test/scripts/valgrind.sh [new file with mode: 0644]
test/suites/Makefile.am [new file with mode: 0644]
test/suites/api/Makefile.am [new file with mode: 0644]
test/suites/api/check-exports [new file with mode: 0755]
test/suites/api/run [new file with mode: 0755]
test/suites/api/test.json [moved from test.json with 100% similarity]
test/suites/api/test_array.c [new file with mode: 0644]
test/suites/api/test_copy.c [new file with mode: 0644]
test/suites/api/test_cpp.cpp [moved from test.cpp with 100% similarity]
test/suites/api/test_equal.c [new file with mode: 0644]
test/suites/api/test_load.c [new file with mode: 0644]
test/suites/api/test_number.c [new file with mode: 0644]
test/suites/api/test_object.c [new file with mode: 0644]
test/suites/api/test_simple.c [new file with mode: 0644]
test/suites/api/util.h [new file with mode: 0644]
test/suites/encoding-flags/array/input [new file with mode: 0644]
test/suites/encoding-flags/array/output [new file with mode: 0644]
test/suites/encoding-flags/compact-array/env [new file with mode: 0644]
test/suites/encoding-flags/compact-array/input [new file with mode: 0644]
test/suites/encoding-flags/compact-array/output [new file with mode: 0644]
test/suites/encoding-flags/compact-object/env [new file with mode: 0644]
test/suites/encoding-flags/compact-object/input [new file with mode: 0644]
test/suites/encoding-flags/compact-object/output [new file with mode: 0644]
test/suites/encoding-flags/ensure-ascii/env [new file with mode: 0644]
test/suites/encoding-flags/ensure-ascii/input [new file with mode: 0644]
test/suites/encoding-flags/ensure-ascii/output [new file with mode: 0644]
test/suites/encoding-flags/indent-array/env [new file with mode: 0644]
test/suites/encoding-flags/indent-array/input [new file with mode: 0644]
test/suites/encoding-flags/indent-array/output [new file with mode: 0644]
test/suites/encoding-flags/indent-compact-array/env [new file with mode: 0644]
test/suites/encoding-flags/indent-compact-array/input [new file with mode: 0644]
test/suites/encoding-flags/indent-compact-array/output [new file with mode: 0644]
test/suites/encoding-flags/indent-compact-object/env [new file with mode: 0644]
test/suites/encoding-flags/indent-compact-object/input [new file with mode: 0644]
test/suites/encoding-flags/indent-compact-object/output [new file with mode: 0644]
test/suites/encoding-flags/indent-object/env [new file with mode: 0644]
test/suites/encoding-flags/indent-object/input [new file with mode: 0644]
test/suites/encoding-flags/indent-object/output [new file with mode: 0644]
test/suites/encoding-flags/object/input [new file with mode: 0644]
test/suites/encoding-flags/object/output [new file with mode: 0644]
test/suites/encoding-flags/run [new file with mode: 0755]
test/suites/encoding-flags/sort-keys/env [new file with mode: 0644]
test/suites/encoding-flags/sort-keys/input [new file with mode: 0644]
test/suites/encoding-flags/sort-keys/output [new file with mode: 0644]
test/suites/invalid-strip/apostrophe/error [new file with mode: 0644]
test/suites/invalid-strip/apostrophe/input [new file with mode: 0644]
test/suites/invalid-strip/ascii-unicode-identifier/error [new file with mode: 0644]
test/suites/invalid-strip/ascii-unicode-identifier/input [new file with mode: 0644]
test/suites/invalid-strip/brace-comma/error [new file with mode: 0644]
test/suites/invalid-strip/brace-comma/input [new file with mode: 0644]
test/suites/invalid-strip/bracket-comma/error [new file with mode: 0644]
test/suites/invalid-strip/bracket-comma/input [new file with mode: 0644]
test/suites/invalid-strip/bracket-one-comma/error [new file with mode: 0644]
test/suites/invalid-strip/bracket-one-comma/input [new file with mode: 0644]
test/suites/invalid-strip/empty/error [new file with mode: 0644]
test/suites/invalid-strip/empty/input [new file with mode: 0644]
test/suites/invalid-strip/extra-comma-in-array/error [new file with mode: 0644]
test/suites/invalid-strip/extra-comma-in-array/input [new file with mode: 0644]
test/suites/invalid-strip/extra-command-in-multiline-array/error [new file with mode: 0644]
test/suites/invalid-strip/extra-command-in-multiline-array/input [new file with mode: 0644]
test/suites/invalid-strip/garbage-after-newline/error [new file with mode: 0644]
test/suites/invalid-strip/garbage-after-newline/input [new file with mode: 0644]
test/suites/invalid-strip/garbage-at-the-end/error [new file with mode: 0644]
test/suites/invalid-strip/garbage-at-the-end/input [new file with mode: 0644]
test/suites/invalid-strip/integer-starting-with-zero/error [new file with mode: 0644]
test/suites/invalid-strip/integer-starting-with-zero/input [new file with mode: 0644]
test/suites/invalid-strip/invalid-escape/error [new file with mode: 0644]
test/suites/invalid-strip/invalid-escape/input [new file with mode: 0644]
test/suites/invalid-strip/invalid-identifier/error [new file with mode: 0644]
test/suites/invalid-strip/invalid-identifier/input [new file with mode: 0644]
test/suites/invalid-strip/invalid-negative-integerr/error [new file with mode: 0644]
test/suites/invalid-strip/invalid-negative-integerr/input [new file with mode: 0644]
test/suites/invalid-strip/invalid-negative-real/error [new file with mode: 0644]
test/suites/invalid-strip/invalid-negative-real/input [new file with mode: 0644]
test/suites/invalid-strip/invalid-second-surrogate/error [new file with mode: 0644]
test/suites/invalid-strip/invalid-second-surrogate/input [new file with mode: 0644]
test/suites/invalid-strip/lone-open-brace/error [new file with mode: 0644]
test/suites/invalid-strip/lone-open-brace/input [new file with mode: 0644]
test/suites/invalid-strip/lone-open-bracket/error [new file with mode: 0644]
test/suites/invalid-strip/lone-open-bracket/input [new file with mode: 0644]
test/suites/invalid-strip/lone-second-surrogate/error [new file with mode: 0644]
test/suites/invalid-strip/lone-second-surrogate/input [new file with mode: 0644]
test/suites/invalid-strip/minus-sign-without-number/error [new file with mode: 0644]
test/suites/invalid-strip/minus-sign-without-number/input [new file with mode: 0644]
test/suites/invalid-strip/negative-integer-starting-with-zero/error [new file with mode: 0644]
test/suites/invalid-strip/negative-integer-starting-with-zero/input [new file with mode: 0644]
test/suites/invalid-strip/null-byte-in-string/error [new file with mode: 0644]
test/suites/invalid-strip/null-byte-in-string/input [new file with mode: 0644]
test/suites/invalid-strip/null/error [new file with mode: 0644]
test/suites/invalid-strip/null/input [new file with mode: 0644]
test/suites/invalid-strip/object-apostrophes/error [new file with mode: 0644]
test/suites/invalid-strip/object-apostrophes/input [new file with mode: 0644]
test/suites/invalid-strip/object-garbage-at-end/error [new file with mode: 0644]
test/suites/invalid-strip/object-garbage-at-end/input [new file with mode: 0644]
test/suites/invalid-strip/object-in-unterminated-array/error [new file with mode: 0644]
test/suites/invalid-strip/object-in-unterminated-array/input [new file with mode: 0644]
test/suites/invalid-strip/object-no-colon/error [new file with mode: 0644]
test/suites/invalid-strip/object-no-colon/input [new file with mode: 0644]
test/suites/invalid-strip/object-no-value/error [new file with mode: 0644]
test/suites/invalid-strip/object-no-value/input [new file with mode: 0644]
test/suites/invalid-strip/object-unterminated-value/error [new file with mode: 0644]
test/suites/invalid-strip/object-unterminated-value/input [new file with mode: 0644]
test/suites/invalid-strip/real-garbage-after-e/error [new file with mode: 0644]
test/suites/invalid-strip/real-garbage-after-e/input [new file with mode: 0644]
test/suites/invalid-strip/real-negative-overflow/error [new file with mode: 0644]
test/suites/invalid-strip/real-negative-overflow/input [new file with mode: 0644]
test/suites/invalid-strip/real-positive-overflow/error [new file with mode: 0644]
test/suites/invalid-strip/real-positive-overflow/input [new file with mode: 0644]
test/suites/invalid-strip/real-truncated-at-e/error [new file with mode: 0644]
test/suites/invalid-strip/real-truncated-at-e/input [new file with mode: 0644]
test/suites/invalid-strip/real-truncated-at-point/error [new file with mode: 0644]
test/suites/invalid-strip/real-truncated-at-point/input [new file with mode: 0644]
test/suites/invalid-strip/real-underflow/error [new file with mode: 0644]
test/suites/invalid-strip/real-underflow/input [new file with mode: 0644]
test/suites/invalid-strip/run [new file with mode: 0755]
test/suites/invalid-strip/tab-character-in-string/error [new file with mode: 0644]
test/suites/invalid-strip/tab-character-in-string/input [new file with mode: 0644]
test/suites/invalid-strip/too-big-negative-integer/error [new file with mode: 0644]
test/suites/invalid-strip/too-big-negative-integer/input [new file with mode: 0644]
test/suites/invalid-strip/too-big-positive-integer/error [new file with mode: 0644]
test/suites/invalid-strip/too-big-positive-integer/input [new file with mode: 0644]
test/suites/invalid-strip/truncated-unicode-surrogate/error [new file with mode: 0644]
test/suites/invalid-strip/truncated-unicode-surrogate/input [new file with mode: 0644]
test/suites/invalid-strip/unicode-identifier/error [new file with mode: 0644]
test/suites/invalid-strip/unicode-identifier/input [new file with mode: 0644]
test/suites/invalid-strip/unterminated-array-and-object/error [new file with mode: 0644]
test/suites/invalid-strip/unterminated-array-and-object/input [new file with mode: 0644]
test/suites/invalid-strip/unterminated-array/error [new file with mode: 0644]
test/suites/invalid-strip/unterminated-array/input [new file with mode: 0644]
test/suites/invalid-strip/unterminated-empty-key/error [new file with mode: 0644]
test/suites/invalid-strip/unterminated-empty-key/input [new file with mode: 0644]
test/suites/invalid-strip/unterminated-key/error [new file with mode: 0644]
test/suites/invalid-strip/unterminated-key/input [new file with mode: 0644]
test/suites/invalid-strip/unterminated-object-and-array/error [new file with mode: 0644]
test/suites/invalid-strip/unterminated-object-and-array/input [new file with mode: 0644]
test/suites/invalid-strip/unterminated-string/error [new file with mode: 0644]
test/suites/invalid-strip/unterminated-string/input [new file with mode: 0644]
test/suites/invalid-unicode/encoded-surrogate-half/error [new file with mode: 0644]
test/suites/invalid-unicode/encoded-surrogate-half/input [new file with mode: 0644]
test/suites/invalid-unicode/invalid-utf-8-after-backslash/error [new file with mode: 0644]
test/suites/invalid-unicode/invalid-utf-8-after-backslash/input [new file with mode: 0644]
test/suites/invalid-unicode/invalid-utf-8-in-array/error [new file with mode: 0644]
test/suites/invalid-unicode/invalid-utf-8-in-array/input [new file with mode: 0644]
test/suites/invalid-unicode/invalid-utf-8-in-bigger-int/error [new file with mode: 0644]
test/suites/invalid-unicode/invalid-utf-8-in-bigger-int/input [new file with mode: 0644]
test/suites/invalid-unicode/invalid-utf-8-in-escape/error [new file with mode: 0644]
test/suites/invalid-unicode/invalid-utf-8-in-escape/input [new file with mode: 0644]
test/suites/invalid-unicode/invalid-utf-8-in-exponent/error [new file with mode: 0644]
test/suites/invalid-unicode/invalid-utf-8-in-exponent/input [new file with mode: 0644]
test/suites/invalid-unicode/invalid-utf-8-in-identifier/error [new file with mode: 0644]
test/suites/invalid-unicode/invalid-utf-8-in-identifier/input [new file with mode: 0644]
test/suites/invalid-unicode/invalid-utf-8-in-int/error [new file with mode: 0644]
test/suites/invalid-unicode/invalid-utf-8-in-int/input [new file with mode: 0644]
test/suites/invalid-unicode/invalid-utf-8-in-real-after-e/error [new file with mode: 0644]
test/suites/invalid-unicode/invalid-utf-8-in-real-after-e/input [new file with mode: 0644]
test/suites/invalid-unicode/invalid-utf-8-in-string/error [new file with mode: 0644]
test/suites/invalid-unicode/invalid-utf-8-in-string/input [new file with mode: 0644]
test/suites/invalid-unicode/lone-invalid-utf-8/error [new file with mode: 0644]
test/suites/invalid-unicode/lone-invalid-utf-8/input [new file with mode: 0644]
test/suites/invalid-unicode/lone-utf-8-continuation-byte/error [new file with mode: 0644]
test/suites/invalid-unicode/lone-utf-8-continuation-byte/input [new file with mode: 0644]
test/suites/invalid-unicode/not-in-unicode-range/error [new file with mode: 0644]
test/suites/invalid-unicode/not-in-unicode-range/input [new file with mode: 0644]
test/suites/invalid-unicode/overlong-3-byte-encoding/error [new file with mode: 0644]
test/suites/invalid-unicode/overlong-3-byte-encoding/input [new file with mode: 0644]
test/suites/invalid-unicode/overlong-4-byte-encoding/error [new file with mode: 0644]
test/suites/invalid-unicode/overlong-4-byte-encoding/input [new file with mode: 0644]
test/suites/invalid-unicode/overlong-ascii-encoding/error [new file with mode: 0644]
test/suites/invalid-unicode/overlong-ascii-encoding/input [new file with mode: 0644]
test/suites/invalid-unicode/restricted-utf-8/error [new file with mode: 0644]
test/suites/invalid-unicode/restricted-utf-8/input [new file with mode: 0644]
test/suites/invalid-unicode/run [new file with mode: 0755]
test/suites/invalid-unicode/truncated-utf-8/error [new file with mode: 0644]
test/suites/invalid-unicode/truncated-utf-8/input [new file with mode: 0644]
test/suites/invalid/apostrophe/error [new file with mode: 0644]
test/suites/invalid/apostrophe/input [new file with mode: 0644]
test/suites/invalid/ascii-unicode-identifier/error [new file with mode: 0644]
test/suites/invalid/ascii-unicode-identifier/input [new file with mode: 0644]
test/suites/invalid/brace-comma/error [new file with mode: 0644]
test/suites/invalid/brace-comma/input [new file with mode: 0644]
test/suites/invalid/bracket-comma/error [new file with mode: 0644]
test/suites/invalid/bracket-comma/input [new file with mode: 0644]
test/suites/invalid/bracket-one-comma/error [new file with mode: 0644]
test/suites/invalid/bracket-one-comma/input [new file with mode: 0644]
test/suites/invalid/empty/error [new file with mode: 0644]
test/suites/invalid/empty/input [new file with mode: 0644]
test/suites/invalid/extra-comma-in-array/error [new file with mode: 0644]
test/suites/invalid/extra-comma-in-array/input [new file with mode: 0644]
test/suites/invalid/extra-command-in-multiline-array/error [new file with mode: 0644]
test/suites/invalid/extra-command-in-multiline-array/input [new file with mode: 0644]
test/suites/invalid/garbage-after-newline/error [new file with mode: 0644]
test/suites/invalid/garbage-after-newline/input [new file with mode: 0644]
test/suites/invalid/garbage-at-the-end/error [new file with mode: 0644]
test/suites/invalid/garbage-at-the-end/input [new file with mode: 0644]
test/suites/invalid/integer-starting-with-zero/error [new file with mode: 0644]
test/suites/invalid/integer-starting-with-zero/input [new file with mode: 0644]
test/suites/invalid/invalid-escape/error [new file with mode: 0644]
test/suites/invalid/invalid-escape/input [new file with mode: 0644]
test/suites/invalid/invalid-identifier/error [new file with mode: 0644]
test/suites/invalid/invalid-identifier/input [new file with mode: 0644]
test/suites/invalid/invalid-negative-integerr/error [new file with mode: 0644]
test/suites/invalid/invalid-negative-integerr/input [new file with mode: 0644]
test/suites/invalid/invalid-negative-real/error [new file with mode: 0644]
test/suites/invalid/invalid-negative-real/input [new file with mode: 0644]
test/suites/invalid/invalid-second-surrogate/error [new file with mode: 0644]
test/suites/invalid/invalid-second-surrogate/input [new file with mode: 0644]
test/suites/invalid/lone-open-brace/error [new file with mode: 0644]
test/suites/invalid/lone-open-brace/input [new file with mode: 0644]
test/suites/invalid/lone-open-bracket/error [new file with mode: 0644]
test/suites/invalid/lone-open-bracket/input [new file with mode: 0644]
test/suites/invalid/lone-second-surrogate/error [new file with mode: 0644]
test/suites/invalid/lone-second-surrogate/input [new file with mode: 0644]
test/suites/invalid/minus-sign-without-number/error [new file with mode: 0644]
test/suites/invalid/minus-sign-without-number/input [new file with mode: 0644]
test/suites/invalid/negative-integer-starting-with-zero/error [new file with mode: 0644]
test/suites/invalid/negative-integer-starting-with-zero/input [new file with mode: 0644]
test/suites/invalid/null-byte-in-string/error [new file with mode: 0644]
test/suites/invalid/null-byte-in-string/input [new file with mode: 0644]
test/suites/invalid/null/error [new file with mode: 0644]
test/suites/invalid/null/input [new file with mode: 0644]
test/suites/invalid/object-apostrophes/error [new file with mode: 0644]
test/suites/invalid/object-apostrophes/input [new file with mode: 0644]
test/suites/invalid/object-garbage-at-end/error [new file with mode: 0644]
test/suites/invalid/object-garbage-at-end/input [new file with mode: 0644]
test/suites/invalid/object-in-unterminated-array/error [new file with mode: 0644]
test/suites/invalid/object-in-unterminated-array/input [new file with mode: 0644]
test/suites/invalid/object-no-colon/error [new file with mode: 0644]
test/suites/invalid/object-no-colon/input [new file with mode: 0644]
test/suites/invalid/object-no-value/error [new file with mode: 0644]
test/suites/invalid/object-no-value/input [new file with mode: 0644]
test/suites/invalid/object-unterminated-value/error [new file with mode: 0644]
test/suites/invalid/object-unterminated-value/input [new file with mode: 0644]
test/suites/invalid/real-garbage-after-e/error [new file with mode: 0644]
test/suites/invalid/real-garbage-after-e/input [new file with mode: 0644]
test/suites/invalid/real-negative-overflow/error [new file with mode: 0644]
test/suites/invalid/real-negative-overflow/input [new file with mode: 0644]
test/suites/invalid/real-positive-overflow/error [new file with mode: 0644]
test/suites/invalid/real-positive-overflow/input [new file with mode: 0644]
test/suites/invalid/real-truncated-at-e/error [new file with mode: 0644]
test/suites/invalid/real-truncated-at-e/input [new file with mode: 0644]
test/suites/invalid/real-truncated-at-point/error [new file with mode: 0644]
test/suites/invalid/real-truncated-at-point/input [new file with mode: 0644]
test/suites/invalid/real-underflow/error [new file with mode: 0644]
test/suites/invalid/real-underflow/input [new file with mode: 0644]
test/suites/invalid/run [new file with mode: 0755]
test/suites/invalid/tab-character-in-string/error [new file with mode: 0644]
test/suites/invalid/tab-character-in-string/input [new file with mode: 0644]
test/suites/invalid/too-big-negative-integer/error [new file with mode: 0644]
test/suites/invalid/too-big-negative-integer/input [new file with mode: 0644]
test/suites/invalid/too-big-positive-integer/error [new file with mode: 0644]
test/suites/invalid/too-big-positive-integer/input [new file with mode: 0644]
test/suites/invalid/truncated-unicode-surrogate/error [new file with mode: 0644]
test/suites/invalid/truncated-unicode-surrogate/input [new file with mode: 0644]
test/suites/invalid/unicode-identifier/error [new file with mode: 0644]
test/suites/invalid/unicode-identifier/input [new file with mode: 0644]
test/suites/invalid/unterminated-array-and-object/error [new file with mode: 0644]
test/suites/invalid/unterminated-array-and-object/input [new file with mode: 0644]
test/suites/invalid/unterminated-array/error [new file with mode: 0644]
test/suites/invalid/unterminated-array/input [new file with mode: 0644]
test/suites/invalid/unterminated-empty-key/error [new file with mode: 0644]
test/suites/invalid/unterminated-empty-key/input [new file with mode: 0644]
test/suites/invalid/unterminated-key/error [new file with mode: 0644]
test/suites/invalid/unterminated-key/input [new file with mode: 0644]
test/suites/invalid/unterminated-object-and-array/error [new file with mode: 0644]
test/suites/invalid/unterminated-object-and-array/input [new file with mode: 0644]
test/suites/invalid/unterminated-string/error [new file with mode: 0644]
test/suites/invalid/unterminated-string/input [new file with mode: 0644]
test/suites/valid-strip/complex-array/input [new file with mode: 0644]
test/suites/valid-strip/complex-array/output [new file with mode: 0644]
test/suites/valid-strip/empty-array/input [new file with mode: 0644]
test/suites/valid-strip/empty-array/output [new file with mode: 0644]
test/suites/valid-strip/empty-object-in-array/input [new file with mode: 0644]
test/suites/valid-strip/empty-object-in-array/output [new file with mode: 0644]
test/suites/valid-strip/empty-object/input [new file with mode: 0644]
test/suites/valid-strip/empty-object/output [new file with mode: 0644]
test/suites/valid-strip/empty-string/input [new file with mode: 0644]
test/suites/valid-strip/empty-string/output [new file with mode: 0644]
test/suites/valid-strip/escaped-utf-control-char/input [new file with mode: 0644]
test/suites/valid-strip/escaped-utf-control-char/output [new file with mode: 0644]
test/suites/valid-strip/false/input [new file with mode: 0644]
test/suites/valid-strip/false/output [new file with mode: 0644]
test/suites/valid-strip/negative-int/input [new file with mode: 0644]
test/suites/valid-strip/negative-int/output [new file with mode: 0644]
test/suites/valid-strip/negative-one/input [new file with mode: 0644]
test/suites/valid-strip/negative-one/output [new file with mode: 0644]
test/suites/valid-strip/negative-zero/input [new file with mode: 0644]
test/suites/valid-strip/negative-zero/output [new file with mode: 0644]
test/suites/valid-strip/null/input [new file with mode: 0644]
test/suites/valid-strip/null/output [new file with mode: 0644]
test/suites/valid-strip/one-byte-utf-8/input [new file with mode: 0644]
test/suites/valid-strip/one-byte-utf-8/output [new file with mode: 0644]
test/suites/valid-strip/real-capital-e-negative-exponent/input [new file with mode: 0644]
test/suites/valid-strip/real-capital-e-negative-exponent/output [new file with mode: 0644]
test/suites/valid-strip/real-capital-e-positive-exponent/input [new file with mode: 0644]
test/suites/valid-strip/real-capital-e-positive-exponent/output [new file with mode: 0644]
test/suites/valid-strip/real-capital-e/input [new file with mode: 0644]
test/suites/valid-strip/real-capital-e/output [new file with mode: 0644]
test/suites/valid-strip/real-exponent/input [new file with mode: 0644]
test/suites/valid-strip/real-exponent/output [new file with mode: 0644]
test/suites/valid-strip/real-fraction-exponent/input [new file with mode: 0644]
test/suites/valid-strip/real-fraction-exponent/output [new file with mode: 0644]
test/suites/valid-strip/real-negative-exponent/input [new file with mode: 0644]
test/suites/valid-strip/real-negative-exponent/output [new file with mode: 0644]
test/suites/valid-strip/real-positive-exponent/input [new file with mode: 0644]
test/suites/valid-strip/real-positive-exponent/output [new file with mode: 0644]
test/suites/valid-strip/run [new file with mode: 0755]
test/suites/valid-strip/short-string/input [new file with mode: 0644]
test/suites/valid-strip/short-string/output [new file with mode: 0644]
test/suites/valid-strip/simple-ascii-string/input [new file with mode: 0644]
test/suites/valid-strip/simple-ascii-string/output [new file with mode: 0644]
test/suites/valid-strip/simple-int-0/input [new file with mode: 0644]
test/suites/valid-strip/simple-int-0/output [new file with mode: 0644]
test/suites/valid-strip/simple-int-1/input [new file with mode: 0644]
test/suites/valid-strip/simple-int-1/output [new file with mode: 0644]
test/suites/valid-strip/simple-int-123/input [new file with mode: 0644]
test/suites/valid-strip/simple-int-123/output [new file with mode: 0644]
test/suites/valid-strip/simple-object/input [new file with mode: 0644]
test/suites/valid-strip/simple-object/output [new file with mode: 0644]
test/suites/valid-strip/simple-real/input [new file with mode: 0644]
test/suites/valid-strip/simple-real/output [new file with mode: 0644]
test/suites/valid-strip/string-escapes/input [new file with mode: 0644]
test/suites/valid-strip/string-escapes/output [new file with mode: 0644]
test/suites/valid-strip/three-byte-utf-8/input [new file with mode: 0644]
test/suites/valid-strip/three-byte-utf-8/output [new file with mode: 0644]
test/suites/valid-strip/true/input [new file with mode: 0644]
test/suites/valid-strip/true/output [new file with mode: 0644]
test/suites/valid-strip/two-byte-utf-8/input [new file with mode: 0644]
test/suites/valid-strip/two-byte-utf-8/output [new file with mode: 0644]
test/suites/valid-strip/utf-8-string/input [new file with mode: 0644]
test/suites/valid-strip/utf-8-string/output [new file with mode: 0644]
test/suites/valid-strip/utf-surrogate-four-byte-encoding/input [new file with mode: 0644]
test/suites/valid-strip/utf-surrogate-four-byte-encoding/output [new file with mode: 0644]
test/suites/valid/complex-array/input [new file with mode: 0644]
test/suites/valid/complex-array/output [new file with mode: 0644]
test/suites/valid/empty-array/input [new file with mode: 0644]
test/suites/valid/empty-array/output [new file with mode: 0644]
test/suites/valid/empty-object-in-array/input [new file with mode: 0644]
test/suites/valid/empty-object-in-array/output [new file with mode: 0644]
test/suites/valid/empty-object/input [new file with mode: 0644]
test/suites/valid/empty-object/output [new file with mode: 0644]
test/suites/valid/empty-string/input [new file with mode: 0644]
test/suites/valid/empty-string/output [new file with mode: 0644]
test/suites/valid/escaped-utf-control-char/input [new file with mode: 0644]
test/suites/valid/escaped-utf-control-char/output [new file with mode: 0644]
test/suites/valid/false/input [new file with mode: 0644]
test/suites/valid/false/output [new file with mode: 0644]
test/suites/valid/negative-int/input [new file with mode: 0644]
test/suites/valid/negative-int/output [new file with mode: 0644]
test/suites/valid/negative-one/input [new file with mode: 0644]
test/suites/valid/negative-one/output [new file with mode: 0644]
test/suites/valid/negative-zero/input [new file with mode: 0644]
test/suites/valid/negative-zero/output [new file with mode: 0644]
test/suites/valid/null/input [new file with mode: 0644]
test/suites/valid/null/output [new file with mode: 0644]
test/suites/valid/one-byte-utf-8/input [new file with mode: 0644]
test/suites/valid/one-byte-utf-8/output [new file with mode: 0644]
test/suites/valid/real-capital-e-negative-exponent/input [new file with mode: 0644]
test/suites/valid/real-capital-e-negative-exponent/output [new file with mode: 0644]
test/suites/valid/real-capital-e-positive-exponent/input [new file with mode: 0644]
test/suites/valid/real-capital-e-positive-exponent/output [new file with mode: 0644]
test/suites/valid/real-capital-e/input [new file with mode: 0644]
test/suites/valid/real-capital-e/output [new file with mode: 0644]
test/suites/valid/real-exponent/input [new file with mode: 0644]
test/suites/valid/real-exponent/output [new file with mode: 0644]
test/suites/valid/real-fraction-exponent/input [new file with mode: 0644]
test/suites/valid/real-fraction-exponent/output [new file with mode: 0644]
test/suites/valid/real-negative-exponent/input [new file with mode: 0644]
test/suites/valid/real-negative-exponent/output [new file with mode: 0644]
test/suites/valid/real-positive-exponent/input [new file with mode: 0644]
test/suites/valid/real-positive-exponent/output [new file with mode: 0644]
test/suites/valid/run [new file with mode: 0755]
test/suites/valid/short-string/input [new file with mode: 0644]
test/suites/valid/short-string/output [new file with mode: 0644]
test/suites/valid/simple-ascii-string/input [new file with mode: 0644]
test/suites/valid/simple-ascii-string/output [new file with mode: 0644]
test/suites/valid/simple-int-0/input [new file with mode: 0644]
test/suites/valid/simple-int-0/output [new file with mode: 0644]
test/suites/valid/simple-int-1/input [new file with mode: 0644]
test/suites/valid/simple-int-1/output [new file with mode: 0644]
test/suites/valid/simple-int-123/input [new file with mode: 0644]
test/suites/valid/simple-int-123/output [new file with mode: 0644]
test/suites/valid/simple-object/input [new file with mode: 0644]
test/suites/valid/simple-object/output [new file with mode: 0644]
test/suites/valid/simple-real/input [new file with mode: 0644]
test/suites/valid/simple-real/output [new file with mode: 0644]
test/suites/valid/string-escapes/input [new file with mode: 0644]
test/suites/valid/string-escapes/output [new file with mode: 0644]
test/suites/valid/three-byte-utf-8/input [new file with mode: 0644]
test/suites/valid/three-byte-utf-8/output [new file with mode: 0644]
test/suites/valid/true/input [new file with mode: 0644]
test/suites/valid/true/output [new file with mode: 0644]
test/suites/valid/two-byte-utf-8/input [new file with mode: 0644]
test/suites/valid/two-byte-utf-8/output [new file with mode: 0644]
test/suites/valid/utf-8-string/input [new file with mode: 0644]
test/suites/valid/utf-8-string/output [new file with mode: 0644]
test/suites/valid/utf-surrogate-four-byte-encoding/input [new file with mode: 0644]
test/suites/valid/utf-surrogate-four-byte-encoding/output [new file with mode: 0644]

index 5880b2a..a2baa14 100644 (file)
@@ -1 +1,24 @@
-test-bin
+*.o
+*.a
+.libs
+.deps
+Makefile
+Makefile.in
+aclocal.m4
+autom4te.cache
+config.guess
+config.h
+config.h.in
+config.log
+config.status
+config.sub
+configure
+depcomp
+install-sh
+libtool
+ltmain.sh
+missing
+*.lo
+*.la
+stamp-h1
+*.pyc
diff --git a/CHANGES b/CHANGES
new file mode 100644 (file)
index 0000000..abe0062
--- /dev/null
+++ b/CHANGES
@@ -0,0 +1,71 @@
+Version 1.1.3, released 2009-12-18
+
+* Encode reals correctly, so that first encoding and then decoding a
+  real always produces the same value
+* Don't export private symbols in libjansson.so
+
+
+Version 1.1.2, released 2009-11-08
+
+* Fix a bug where an error message was not produced if the input file
+  could not be opened in json_load_file()
+* Fix an assertion failure in decoder caused by a minus sign without a
+  digit after it
+* Remove an unneeded include for stdint.h in jansson.h
+
+
+Version 1.1.1, released 2009-10-26
+
+* All documentation files were not distributed with v1.1; build
+  documentation in make distcheck to prevent this in the future
+* Fix v1.1 release date in CHANGES
+
+
+Version 1.1, released 2009-10-20
+
+* API additions and improvements:
+  - Extend array and object APIs
+  - Add functions to modify integer, real and string values
+  - Improve argument validation
+  - Use unsigned int instead of uint32_t for encoding flags
+* Enhance documentation
+  - Add getting started guide and tutorial
+  - Fix some typos
+  - General clarifications and cleanup
+* Check for integer and real overflows and underflows in decoder
+* Make singleton values thread-safe (true, false and null)
+* Enhance circular reference handling
+* Don't define -std=c99 in AM_CFLAGS
+* Add C++ guards to jansson.h
+* Minor performance and portability improvements
+* Expand test coverage
+
+
+Version 1.0.4, released 2009-10-11
+
+* Relax Autoconf version requirement to 2.59
+* Make Jansson compile on platforms where plain char is unsigned
+* Fix API tests for object
+
+
+Version 1.0.3, released 2009-09-14
+
+* Check for integer and real overflows and underflows in decoder
+* Use the Python json module for tests, or simplejson if the json
+  module is not found
+* Distribute changelog (this file)
+
+
+Version 1.0.2, released 2009-09-08
+
+* Handle EOF correctly in decoder
+
+
+Version 1.0.1, released 2009-09-04
+
+* Fixed broken json_is_boolean()
+
+
+Version 1.0, released 2009-08-25
+
+* Initial release
diff --git a/README b/LICENSE
similarity index 69%
rename from README
rename to LICENSE
index fc5648d..6c6dbed 100644 (file)
--- a/README
+++ b/LICENSE
@@ -1,15 +1,4 @@
-janssonxx is a simple C++ wrapper around the Jansson library.
-
-       http://digip.org/jansson/
-
-It does not purport to be as flexible or efficient as what a competent
-developer can achieve using the Jansson C API, but it does offer a
-safe and easy to use API that should cover most users' needs.
-
-janssonxx is developed by Sean Middleditch <sean@middleditch.us> and
-is released under the MIT/X license, same as Jansson:
-
-Copyright (c) 2010 Sean Middleditch
+Copyright (c) 2009 Petri Lehtinen <petri@digip.org>
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
diff --git a/Makefile b/Makefile
deleted file mode 100644 (file)
index 7d4931c..0000000
--- a/Makefile
+++ /dev/null
@@ -1,13 +0,0 @@
-JANSSON_CFLAGS := $(shell pkg-config --cflags jansson)
-JANSSON_LIBS := $(shell pkg-config --libs jansson)
-
-all: test
-
-test-bin: driver.cpp test.cpp jansson.hpp jansson-impl.hpp Makefile
-       $(CXX) -o $@ -g -O0 -Wall $(JANSSON_CFLAGS) driver.cpp test.cpp $(JANSSON_LIBS)
-
-test: test-bin
-       ./test-bin
-
-clean:
-       rm -f test-bin
diff --git a/Makefile.am b/Makefile.am
new file mode 100644 (file)
index 0000000..185eaab
--- /dev/null
@@ -0,0 +1,4 @@
+EXTRA_DIST = CHANGES LICENSE README.rst
+SUBDIRS = doc src test
+
+check-local: html
diff --git a/README.rst b/README.rst
new file mode 100644 (file)
index 0000000..27706dd
--- /dev/null
@@ -0,0 +1,59 @@
+Jansson README
+==============
+
+Jansson_ is a C library for encoding, decoding and manipulating JSON
+data. Its main features and design principles are:
+
+- Simple and intuitive API and data model
+
+- Comprehensive documentation
+
+- No dependencies on other libraries
+
+- Full Unicode support (UTF-8)
+
+- Extensive test suite
+
+Jansson is licensed under the `MIT license`_; see LICENSE in the
+source distribution for details.
+
+
+Compilation and Installation
+----------------------------
+
+If you obtained a source tarball, just use the standard autotools
+commands::
+
+   $ ./configure && make && make install
+
+If the source has been checked out from a Git repository, the
+./configure script has to be generated fist. The easiest way is to use
+autoreconf::
+
+   $ autoreconf -i
+
+To run the test suite, invoke::
+
+   $ make check
+
+
+Documentation
+-------------
+
+Documentation is in the ``doc/`` subdirectory. It's written in
+reStructuredText_ with Sphinx_ annotations, so reading it in plain may
+be inconvenient. For this reason, prebuilt HTML documentation is
+available at http://www.digip.org/jansson/doc/.
+
+To generate HTML documentation yourself, invoke::
+
+   make html
+
+and point your browser to ``doc/_build/html/index.html``. Sphinx_ is
+required to generate the documentation.
+
+
+.. _Jansson: http://www.digip.org/jansson/
+.. _`MIT license`: http://www.opensource.org/licenses/mit-license.php
+.. _reStructuredText: http://docutils.sourceforge.net/rst.html
+.. _Sphinx: http://sphinx.pocoo.org/
diff --git a/configure.ac b/configure.ac
new file mode 100644 (file)
index 0000000..d28a5e4
--- /dev/null
@@ -0,0 +1,30 @@
+AC_PREREQ([2.59])
+AC_INIT([jansson], [1.1.3+], [petri@digip.org])
+
+AM_INIT_AUTOMAKE([1.10 foreign])
+
+AC_CONFIG_SRCDIR([src/value.c])
+AC_CONFIG_HEADERS([config.h])
+
+# Checks for programs.
+AC_PROG_CC
+AC_PROG_LIBTOOL
+
+# Checks for libraries.
+
+# Checks for header files.
+
+# Checks for typedefs, structures, and compiler characteristics.
+
+# Checks for library functions.
+
+AC_CONFIG_FILES([
+        Makefile
+        doc/Makefile
+        src/Makefile
+        test/Makefile
+        test/bin/Makefile
+        test/suites/Makefile
+        test/suites/api/Makefile
+])
+AC_OUTPUT
diff --git a/doc/.gitignore b/doc/.gitignore
new file mode 100644 (file)
index 0000000..69fa449
--- /dev/null
@@ -0,0 +1 @@
+_build/
diff --git a/doc/Makefile.am b/doc/Makefile.am
new file mode 100644 (file)
index 0000000..cb3723f
--- /dev/null
@@ -0,0 +1,20 @@
+EXTRA_DIST = \
+       conf.py apiref.rst gettingstarted.rst github_commits.c index.rst \
+       tutorial.rst ext/refcounting.py
+
+SPHINXBUILD = sphinx-build
+SPHINXOPTS = -d _build/doctrees -W
+
+html-local:
+       $(SPHINXBUILD) -b html $(SPHINXOPTS) $(srcdir) _build/html
+
+install-html-local: html
+       mkdir -p $(DESTDIR)$(htmldir)
+       cp -r _build/html $(DESTDIR)$(htmldir)
+
+uninstall-local:
+       rm -rf $(DESTDIR)$(htmldir)
+
+clean-local:
+       rm -rf _build
+       rm -f ext/refcounting.pyc
diff --git a/doc/README b/doc/README
new file mode 100644 (file)
index 0000000..930b3bf
--- /dev/null
@@ -0,0 +1,5 @@
+To build the documentation, invoke
+
+    make html
+
+Then point your browser to _build/html/index.html.
diff --git a/doc/apiref.rst b/doc/apiref.rst
new file mode 100644 (file)
index 0000000..a2a0794
--- /dev/null
@@ -0,0 +1,747 @@
+.. _apiref:
+
+*************
+API Reference
+*************
+
+.. highlight:: c
+
+Preliminaries
+=============
+
+All declarations are in :file:`jansson.h`, so it's enough to
+
+::
+
+   #include <jansson.h>
+
+in each source file.
+
+All constants are prefixed ``JSON_`` and other identifiers with
+``json_``. Type names are suffixed with ``_t`` and ``typedef``\ 'd so
+that the ``struct`` keyword need not be used.
+
+
+Value Representation
+====================
+
+The JSON specification (:rfc:`4627`) defines the following data types:
+*object*, *array*, *string*, *number*, *boolean*, and *null*. JSON
+types are used dynamically; arrays and objects can hold any other data
+type, including themselves. For this reason, Jansson's type system is
+also dynamic in nature. There's one C type to represent all JSON
+values, and this structure knows the type of the JSON value it holds.
+
+.. ctype:: json_t
+
+  This data structure is used throughout the library to represent all
+  JSON values. It always contains the type of the JSON value it holds
+  and the value's reference count. The rest depends on the type of the
+  value.
+
+Objects of :ctype:`json_t` are always used through a pointer. There
+are APIs for querying the type, manipulating the reference count, and
+for constructing and manipulating values of different types.
+
+Unless noted otherwise, all API functions return an error value if an
+error occurs. Depending on the function's signature, the error value
+is either *NULL* or -1. Invalid arguments or invalid input are
+apparent sources for errors. Memory allocation and I/O operations may
+also cause errors.
+
+
+Type
+----
+
+The type of a JSON value is queried and tested using the following
+functions:
+
+.. ctype:: enum json_type
+
+   The type of a JSON value. The following members are defined:
+
+   +-------------------------+
+   | :const:`JSON_OBJECT`    |
+   +-------------------------+
+   | :const:`JSON_ARRAY`     |
+   +-------------------------+
+   | :const:`JSON_STRING`    |
+   +-------------------------+
+   | :const:`JSON_INTEGER`   |
+   +-------------------------+
+   | :const:`JSON_REAL`      |
+   +-------------------------+
+   | :const:`JSON_TRUE`      |
+   +-------------------------+
+   | :const:`JSON_FALSE`     |
+   +-------------------------+
+   | :const:`JSON_NULL`      |
+   +-------------------------+
+
+   These correspond to JSON object, array, string, number, boolean and
+   null. A number is represented by either a value of the type
+   :const:`JSON_INTEGER` or of the type :const:`JSON_REAL`. A true
+   boolean value is represented by a value of the type
+   :const:`JSON_TRUE` and false by a value of the type
+   :const:`JSON_FALSE`.
+
+.. cfunction:: int json_typeof(const json_t *json)
+
+   Return the type of the JSON value (a :ctype:`json_type` cast to
+   :ctype:`int`). *json* MUST NOT be *NULL*. This function is actually
+   implemented as a macro for speed.
+
+.. cfunction:: json_is_object(const json_t *json)
+               json_is_array(const json_t *json)
+               json_is_string(const json_t *json)
+               json_is_integer(const json_t *json)
+               json_is_real(const json_t *json)
+               json_is_true(const json_t *json)
+               json_is_false(const json_t *json)
+               json_is_null(const json_t *json)
+
+   These functions (actually macros) return true (non-zero) for values
+   of the given type, and false (zero) for values of other types and
+   for *NULL*.
+
+.. cfunction:: json_is_number(const json_t *json)
+
+   Returns true for values of types :const:`JSON_INTEGER` and
+   :const:`JSON_REAL`, and false for other types and for *NULL*.
+
+.. cfunction:: json_is_boolean(const json_t *json)
+
+   Returns true for types :const:`JSON_TRUE` and :const:`JSON_FALSE`,
+   and false for values of other types and for *NULL*.
+
+
+.. _apiref-reference-count:
+
+Reference Count
+---------------
+
+The reference count is used to track whether a value is still in use
+or not. When a value is created, it's reference count is set to 1. If
+a reference to a value is kept (e.g. a value is stored somewhere for
+later use), its reference count is incremented, and when the value is
+no longer needed, the reference count is decremented. When the
+reference count drops to zero, there are no references left, and the
+value can be destroyed.
+
+The following functions are used to manipulate the reference count.
+
+.. cfunction:: json_t *json_incref(json_t *json)
+
+   Increment the reference count of *json* if it's not non-*NULL*.
+   Returns *json*.
+
+.. cfunction:: void json_decref(json_t *json)
+
+   Decrement the reference count of *json*. As soon as a call to
+   :cfunc:`json_decref()` drops the reference count to zero, the value
+   is destroyed and it can no longer be used.
+
+Functions creating new JSON values set the reference count to 1. These
+functions are said to return a **new reference**. Other functions
+returning (existing) JSON values do not normally increase the
+reference count. These functions are said to return a **borrowed
+reference**. So, if the user will hold a reference to a value returned
+as a borrowed reference, he must call :cfunc:`json_incref`. As soon as
+the value is no longer needed, :cfunc:`json_decref` should be called
+to release the reference.
+
+Normally, all functions accepting a JSON value as an argument will
+manage the reference, i.e. increase and decrease the reference count
+as needed. However, some functions **steal** the reference, i.e. they
+have the same result as if the user called :cfunc:`json_decref()` on
+the argument right after calling the function. These are usually
+convenience functions for adding new references to containers and not
+to worry about the reference count.
+
+In the following sections it is clearly documented whether a function
+will return a new or borrowed reference or steal a reference to its
+argument.
+
+
+Circular References
+-------------------
+
+A circular reference is created when an object or an array is,
+directly or indirectly, inserted inside itself. The direct case is
+simple::
+
+  json_t *obj = json_object();
+  json_object_set(obj, "foo", obj);
+
+Jansson will refuse to do this, and :cfunc:`json_object_set()` (and
+all the other such functions for objects and arrays) will return with
+an error status. The indirect case is the dangerous one::
+
+  json_t *arr1 = json_array(), *arr2 = json_array();
+  json_array_append(arr1, arr2);
+  json_array_append(arr2, arr1);
+
+In this example, the array ``arr2`` is contained in the array
+``arr1``, and vice versa. Jansson cannot check for this kind of
+indirect circular references without a performance hit, so it's up to
+the user to avoid them.
+
+If a circular reference is created, the memory consumed by the values
+cannot be freed by :cfunc:`json_decref()`. The reference counts never
+drops to zero because the values are keeping the circular reference to
+themselves. Moreover, trying to encode the values with any of the
+encoding functions will fail. The encoder detects circular references
+and returns an error status.
+
+
+True, False and Null
+====================
+
+These values are implemented as singletons, so each of these functions
+returns the same value each time.
+
+.. cfunction:: json_t *json_true(void)
+
+   .. refcounting:: new
+
+   Returns the JSON true value.
+
+.. cfunction:: json_t *json_false(void)
+
+   .. refcounting:: new
+
+   Returns the JSON false value.
+
+.. cfunction:: json_t *json_null(void)
+
+   .. refcounting:: new
+
+   Returns the JSON null value.
+
+
+String
+======
+
+.. cfunction:: json_t *json_string(const char *value)
+
+   .. refcounting:: new
+
+   Returns a new JSON string, or *NULL* on error. *value* must be a
+   valid UTF-8 encoded Unicode string.
+
+.. cfunction:: json_t *json_string_nocheck(const char *value)
+
+   .. refcounting:: new
+
+   Like :cfunc:`json_string`, but doesn't check that *value* is valid
+   UTF-8. Use this function only if you are certain that this really
+   is the case (e.g. you have already checked it by other means).
+
+   .. versionadded:: 1.2
+
+.. cfunction:: const char *json_string_value(const json_t *string)
+
+   Returns the associated value of *string* as a null terminated UTF-8
+   encoded string, or *NULL* if *string* is not a JSON string.
+
+.. cfunction:: int json_string_set(const json_t *string, const char *value)
+
+   Sets the associated value of *string* to *value*. *value* must be a
+   valid UTF-8 encoded Unicode string. Returns 0 on success and -1 on
+   error.
+
+   .. versionadded:: 1.1
+
+.. cfunction:: int json_string_set_nocheck(const json_t *string, const char *value)
+
+   Like :cfunc:`json_string_set`, but doesn't check that *value* is
+   valid UTF-8. Use this function only if you are certain that this
+   really is the case (e.g. you have already checked it by other
+   means).
+
+   .. versionadded:: 1.2
+
+
+Number
+======
+
+.. cfunction:: json_t *json_integer(int value)
+
+   .. refcounting:: new
+
+   Returns a new JSON integer, or *NULL* on error.
+
+.. cfunction:: int json_integer_value(const json_t *integer)
+
+   Returns the associated value of *integer*, or 0 if *json* is not a
+   JSON integer.
+
+.. cfunction:: int json_integer_set(const json_t *integer, int value)
+
+   Sets the associated value of *integer* to *value*. Returns 0 on
+   success and -1 if *integer* is not a JSON integer.
+
+   .. versionadded:: 1.1
+
+.. cfunction:: json_t *json_real(double value)
+
+   .. refcounting:: new
+
+   Returns a new JSON real, or *NULL* on error.
+
+.. cfunction:: double json_real_value(const json_t *real)
+
+   Returns the associated value of *real*, or 0.0 if *real* is not a
+   JSON real.
+
+.. cfunction:: int json_real_set(const json_t *real, double value)
+
+   Sets the associated value of *real* to *value*. Returns 0 on
+   success and -1 if *real* is not a JSON real.
+
+   .. versionadded:: 1.1
+
+In addition to the functions above, there's a common query function
+for integers and reals:
+
+.. cfunction:: double json_number_value(const json_t *json)
+
+   Returns the associated value of the JSON integer or JSON real
+   *json*, cast to double regardless of the actual type. If *json* is
+   neither JSON real nor JSON integer, 0.0 is returned.
+
+
+Array
+=====
+
+A JSON array is an ordered collection of other JSON values.
+
+.. cfunction:: json_t *json_array(void)
+
+   .. refcounting:: new
+
+   Returns a new JSON array, or *NULL* on error. Initially, the array
+   is empty.
+
+.. cfunction:: unsigned int json_array_size(const json_t *array)
+
+   Returns the number of elements in *array*, or 0 if *array* is NULL
+   or not a JSON array.
+
+.. cfunction:: json_t *json_array_get(const json_t *array, unsigned int index)
+
+   .. refcounting:: borrow
+
+   Returns the element in *array* at position *index*. The valid range
+   for *index* is from 0 to the return value of
+   :cfunc:`json_array_size()` minus 1. If *array* is not a JSON array,
+   if *array* is *NULL*, or if *index* is out of range, *NULL* is
+   returned.
+
+.. cfunction:: int json_array_set(json_t *array, unsigned int index, json_t *value)
+
+   Replaces the element in *array* at position *index* with *value*.
+   The valid range for *index* is from 0 to the return value of
+   :cfunc:`json_array_size()` minus 1. Returns 0 on success and -1 on
+   error.
+
+.. cfunction:: int json_array_set_new(json_t *array, unsigned int index, json_t *value)
+
+   Like :cfunc:`json_array_set()` but steals the reference to *value*.
+   This is useful when *value* is newly created and not used after
+   the call.
+
+   .. versionadded:: 1.1
+
+.. cfunction:: int json_array_append(json_t *array, json_t *value)
+
+   Appends *value* to the end of *array*, growing the size of *array*
+   by 1. Returns 0 on success and -1 on error.
+
+.. cfunction:: int json_array_append_new(json_t *array, json_t *value)
+
+   Like :cfunc:`json_array_append()` but steals the reference to
+   *value*. This is useful when *value* is newly created and not used
+   after the call.
+
+   .. versionadded:: 1.1
+
+.. cfunction:: int json_array_insert(json_t *array, unsigned int index, json_t *value)
+
+   Inserts *value* to *array* at position *index*, shifting the
+   elements at *index* and after it one position towards the end of
+   the array. Returns 0 on success and -1 on error.
+
+   .. versionadded:: 1.1
+
+.. cfunction:: int json_array_insert_new(json_t *array, unsigned int index, json_t *value)
+
+   Like :cfunc:`json_array_insert()` but steals the reference to
+   *value*. This is useful when *value* is newly created and not used
+   after the call.
+
+   .. versionadded:: 1.1
+
+.. cfunction:: int json_array_remove(json_t *array, unsigned int index)
+
+   Removes the element in *array* at position *index*, shifting the
+   elements after *index* one position towards the start of the array.
+   Returns 0 on success and -1 on error.
+
+   .. versionadded:: 1.1
+
+.. cfunction:: int json_array_clear(json_t *array)
+
+   Removes all elements from *array*. Returns 0 on sucess and -1 on
+   error.
+
+   .. versionadded:: 1.1
+
+.. cfunction:: int json_array_extend(json_t *array, json_t *other_array)
+
+   Appends all elements in *other_array* to the end of *array*.
+   Returns 0 on success and -1 on error.
+
+   .. versionadded:: 1.1
+
+
+Object
+======
+
+A JSON object is a dictionary of key-value pairs, where the key is a
+Unicode string and the value is any JSON value.
+
+.. cfunction:: json_t *json_object(void)
+
+   .. refcounting:: new
+
+   Returns a new JSON object, or *NULL* on error. Initially, the
+   object is empty.
+
+.. cfunction:: unsigned int json_object_size(const json_t *object)
+
+   Returns the number of elements in *object*, or 0 if *object* is not
+   a JSON object.
+
+   .. versionadded:: 1.1
+
+.. cfunction:: json_t *json_object_get(const json_t *object, const char *key)
+
+   .. refcounting:: borrow
+
+   Get a value corresponding to *key* from *object*. Returns *NULL* if
+   *key* is not found and on error.
+
+.. cfunction:: int json_object_set(json_t *object, const char *key, json_t *value)
+
+   Set the value of *key* to *value* in *object*. *key* must be a
+   valid null terminated UTF-8 encoded Unicode string. If there
+   already is a value for *key*, it is replaced by the new value.
+   Returns 0 on success and -1 on error.
+
+.. cfunction:: int json_object_set_nocheck(json_t *object, const char *key, json_t *value)
+
+   Like :cfunc:`json_object_set`, but doesn't check that *key* is
+   valid UTF-8. Use this function only if you are certain that this
+   really is the case (e.g. you have already checked it by other
+   means).
+
+   .. versionadded:: 1.2
+
+.. cfunction:: int json_object_set_new(json_t *object, const char *key, json_t *value)
+
+   Like :cfunc:`json_object_set()` but steals the reference to
+   *value*. This is useful when *value* is newly created and not used
+   after the call.
+
+   .. versionadded:: 1.1
+
+.. cfunction:: int json_object_set_new_nocheck(json_t *object, const char *key, json_t *value)
+
+   Like :cfunc:`json_object_set_new`, but doesn't check that *key* is
+   valid UTF-8. Use this function only if you are certain that this
+   really is the case (e.g. you have already checked it by other
+   means).
+
+   .. versionadded:: 1.2
+
+.. cfunction:: int json_object_del(json_t *object, const char *key)
+
+   Delete *key* from *object* if it exists. Returns 0 on success, or
+   -1 if *key* was not found.
+
+
+.. cfunction:: int json_object_clear(json_t *object)
+
+   Remove all elements from *object*. Returns 0 on success and -1 if
+   *object* is not a JSON object.
+
+   .. versionadded:: 1.1
+
+.. cfunction:: int json_object_update(json_t *object, json_t *other)
+
+   Update *object* with the key-value pairs from *other*, overwriting
+   existing keys. Returns 0 on success or -1 on error.
+
+   .. versionadded:: 1.1
+
+
+The following functions implement an iteration protocol for objects:
+
+.. cfunction:: void *json_object_iter(json_t *object)
+
+   Returns an opaque iterator which can be used to iterate over all
+   key-value pairs in *object*, or *NULL* if *object* is empty.
+
+.. cfunction:: void *json_object_iter_next(json_t *object, void *iter)
+
+   Returns an iterator pointing to the next key-value pair in *object*
+   after *iter*, or *NULL* if the whole object has been iterated
+   through.
+
+.. cfunction:: const char *json_object_iter_key(void *iter)
+
+   Extract the associated key from *iter*.
+
+.. cfunction:: json_t *json_object_iter_value(void *iter)
+
+   .. refcounting:: borrow
+
+   Extract the associated value from *iter*.
+
+The iteration protocol can be used for example as follows::
+
+   /* obj is a JSON object */
+   const char *key;
+   json_t *value;
+   void *iter = json_object_iter(obj);
+   while(iter)
+   {
+       key = json_object_iter_key(iter);
+       value = json_object_iter_value(iter);
+       /* use key and value ... */
+       iter = json_object_iter_next(obj, iter);
+   }
+
+
+Encoding
+========
+
+This sections describes the functions that can be used to encode
+values to JSON. Only objects and arrays can be encoded, since they are
+the only valid "root" values of a JSON text.
+
+By default, the output has no newlines, and spaces are used between
+array and object elements for a readable output. This behavior can be
+altered by using the ``JSON_INDENT`` and ``JSON_COMPACT`` flags
+described below. A newline is never appended to the end of the encoded
+JSON data.
+
+Each function takes a *flags* parameter that controls some aspects of
+how the data is encoded. Its default value is 0. The following macros
+can be ORed together to obtain *flags*.
+
+``JSON_INDENT(n)``
+   Pretty-print the result, using newlines between array and object
+   items, and indenting with *n* spaces. The valid range for *n* is
+   between 0 and 255, other values result in an undefined output. If
+   ``JSON_INDENT`` is not used or *n* is 0, no newlines are inserted
+   between array and object items.
+
+``JSON_COMPACT``
+   This flag enables a compact representation, i.e. sets the separator
+   between array and object items to ``","`` and between object keys
+   and values to ``":"``. Without this flag, the corresponding
+   separators are ``", "`` and ``": "`` for more readable output.
+
+   .. versionadded:: 1.2
+
+``JSON_ENSURE_ASCII``
+   If this flag is used, the output is guaranteed to consist only of
+   ASCII characters. This is achived by escaping all Unicode
+   characters outside the ASCII range.
+
+   .. versionadded:: 1.2
+
+``JSON_SORT_KEYS``
+   If this flag is used, all the objects in output are sorted by key.
+   This is useful e.g. if two JSON texts are diffed or visually
+   compared.
+
+   .. versionadded:: 1.2
+
+The following functions perform the actual JSON encoding. The result
+is in UTF-8.
+
+.. cfunction:: char *json_dumps(const json_t *root, unsigned long flags)
+
+   Returns the JSON representation of *root* as a string, or *NULL* on
+   error. *flags* is described above. The return value must be freed
+   by the caller using :cfunc:`free()`.
+
+.. cfunction:: int json_dumpf(const json_t *root, FILE *output, unsigned long flags)
+
+   Write the JSON representation of *root* to the stream *output*.
+   *flags* is described above. Returns 0 on success and -1 on error.
+   If an error occurs, something may have already been written to
+   *output*. In this case, the output is undefined and most likely not
+   valid JSON.
+
+.. cfunction:: int json_dump_file(const json_t *json, const char *path, unsigned long flags)
+
+   Write the JSON representation of *root* to the file *path*. If
+   *path* already exists, it is overwritten. *flags* is described
+   above. Returns 0 on success and -1 on error.
+
+
+Decoding
+========
+
+This sections describes the functions that can be used to decode JSON
+text to the Jansson representation of JSON data. The JSON
+specification requires that a JSON text is either a serialized array
+or object, and this requirement is also enforced with the following
+functions.
+
+The only supported character encoding is UTF-8 (which ASCII is a
+subset of).
+
+.. ctype:: json_error_t
+
+   This data structure is used to return information on decoding
+   errors from the decoding functions. Its definition is repeated
+   here::
+
+      #define JSON_ERROR_TEXT_LENGTH  160
+
+      typedef struct {
+          char text[JSON_ERROR_TEXT_LENGTH];
+          int line;
+      } json_error_t;
+
+   *line* is the line number on which the error occurred, or -1 if
+   this information is not available. *text* contains the error
+   message (in UTF-8), or an empty string if a message is not
+   available.
+
+   The normal usef of :ctype:`json_error_t` is to allocate it normally
+   on the stack, and pass a pointer to a decoding function. Example::
+
+      int main() {
+          json_t *json;
+          json_error_t error;
+
+          json = json_load_file("/path/to/file.json", &error);
+          if(!json) {
+              /* the error variable contains error information */
+          }
+          ...
+      }
+
+   Also note that if the decoding succeeded (``json != NULL`` in the
+   above example), the contents of ``error`` are unspecified.
+
+   All decoding functions also accept *NULL* as the
+   :ctype:`json_error_t` pointer, in which case no error information
+   is returned to the caller.
+
+The following functions perform the actual JSON decoding.
+
+.. cfunction:: json_t *json_loads(const char *input, json_error_t *error)
+
+   .. refcounting:: new
+
+   Decodes the JSON string *input* and returns the array or object it
+   contains, or *NULL* on error, in which case *error* is filled with
+   information about the error. See above for discussion on the
+   *error* parameter.
+
+.. cfunction:: json_t *json_loadf(FILE *input, json_error_t *error)
+
+   .. refcounting:: new
+
+   Decodes the JSON text in stream *input* and returns the array or
+   object it contains, or *NULL* on error, in which case *error* is
+   filled with information about the error. See above for discussion
+   on the *error* parameter.
+
+.. cfunction:: json_t *json_load_file(const char *path, json_error_t *error)
+
+   .. refcounting:: new
+
+   Decodes the JSON text in file *path* and returns the array or
+   object it contains, or *NULL* on error, in which case *error* is
+   filled with information about the error. See above for discussion
+   on the *error* parameter.
+
+
+Equality
+========
+
+Testing for equality of two JSON values cannot, in general, be
+achieved using the ``==`` operator. Equality in the terms of the
+``==`` operator states that the two :ctype:`json_t` pointers point to
+exactly the same JSON value. However, two JSON values can be equal not
+only if they are exactly the same value, but also if they have equal
+"contents":
+
+* Two integer or real values are equal if their contained numeric
+  values are equal. An integer value is never equal to a real value,
+  though.
+
+* Two strings are equal if their contained UTF-8 strings are equal.
+
+* Two arrays are equal if they have the same number of elements and
+  each element in the first array is equal to the corresponding
+  element in the second array.
+
+* Two objects are equal if they have exactly the same keys and the
+  value for each key in the first object is equal to the value of the
+  corresponding key in the second object.
+
+* Two true, false or null values have no "contents", so they are equal
+  if their types are equal. (Because these values are singletons,
+  their equality can actually be tested with ``==``.)
+
+The following function can be used to test whether two JSON values are
+equal.
+
+.. cfunction:: int json_equal(json_t *value1, json_t *value2)
+
+   Returns 1 if *value1* and *value2* are equal, as defined above.
+   Returns 0 if they are inequal or one or both of the pointers are
+   *NULL*.
+
+   .. versionadded:: 1.2
+
+
+Copying
+=======
+
+Because of reference counting, passing JSON values around doesn't
+require copying them. But sometimes a fresh copy of a JSON value is
+needed. For example, if you need to modify an array, but still want to
+use the original afterwards, you should take a copy of it first.
+
+Jansson supports two kinds of copying: shallow and deep. There is a
+difference between these methods only for arrays and objects. Shallow
+copying only copies the first level value (array or object) and uses
+the same child values in the copied value. Deep copying makes a fresh
+copy of the child values, too. Moreover, all the child values are deep
+copied in a recursive fashion.
+
+.. cfunction:: json_t *json_copy(json_t *value)
+
+   .. refcounting:: new
+
+   Returns a shallow copy of *value*, or *NULL* on error.
+
+   .. versionadded:: 1.2
+
+.. cfunction:: json_t *json_deep_copy(json_t *value)
+
+   .. refcounting:: new
+
+   Returns a deep copy of *value*, or *NULL* on error.
+
+   .. versionadded:: 1.2
diff --git a/doc/conf.py b/doc/conf.py
new file mode 100644 (file)
index 0000000..10f444d
--- /dev/null
@@ -0,0 +1,191 @@
+# -*- coding: utf-8 -*-
+#
+# Jansson documentation build configuration file, created by
+# sphinx-quickstart on Thu Jul 30 11:35:32 2009.
+#
+# This file is execfile()d with the current directory set to its containing dir.
+#
+# The contents of this file are pickled, so don't put values in the namespace
+# that aren't pickleable (module imports are okay, they're removed automatically).
+#
+# Note that not all possible configuration values are present in this
+# autogenerated file.
+#
+# All configuration values have a default; values that are commented out
+# serve to show the default.
+
+import sys, os
+sys.path.insert(0, os.path.abspath('ext'))
+
+# If your extensions (or modules documented by autodoc) are in another directory,
+# add these directories to sys.path here. If the directory is relative to the
+# documentation root, use os.path.abspath to make it absolute, like shown here.
+#sys.path.append(os.path.abspath('.'))
+
+# General configuration
+# ---------------------
+
+# Add any Sphinx extension module names here, as strings. They can be extensions
+# coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
+extensions = ['refcounting']
+
+# Add any paths that contain templates here, relative to this directory.
+templates_path = []
+
+# The suffix of source filenames.
+source_suffix = '.rst'
+
+# The encoding of source files.
+#source_encoding = 'utf-8'
+
+# The master toctree document.
+master_doc = 'index'
+
+# General information about the project.
+project = u'Jansson'
+copyright = u'2009, Petri Lehtinen'
+
+# The version info for the project you're documenting, acts as replacement for
+# |version| and |release|, also used in various other places throughout the
+# built documents.
+#
+# The short X.Y version.
+version = '1.1'
+# The full version, including alpha/beta/rc tags.
+release = '1.1.3+'
+
+# The language for content autogenerated by Sphinx. Refer to documentation
+# for a list of supported languages.
+#language = None
+
+# There are two options for replacing |today|: either, you set today to some
+# non-false value, then it is used:
+#today = ''
+# Else, today_fmt is used as the format for a strftime call.
+#today_fmt = '%B %d, %Y'
+
+# List of documents that shouldn't be included in the build.
+#unused_docs = []
+
+# List of directories, relative to source directory, that shouldn't be searched
+# for source files.
+exclude_trees = ['_build']
+
+# The reST default role (used for this markup: `text`) to use for all documents.
+#default_role = None
+
+# If true, '()' will be appended to :func: etc. cross-reference text.
+#add_function_parentheses = True
+
+# If true, the current module name will be prepended to all description
+# unit titles (such as .. function::).
+#add_module_names = True
+
+# If true, sectionauthor and moduleauthor directives will be shown in the
+# output. They are ignored by default.
+#show_authors = False
+
+# The name of the Pygments (syntax highlighting) style to use.
+pygments_style = 'sphinx'
+
+
+# Options for HTML output
+# -----------------------
+
+# The style sheet to use for HTML and HTML Help pages. A file of that name
+# must exist either in Sphinx' static/ path, or in one of the custom paths
+# given in html_static_path.
+html_style = 'default.css'
+
+# The name for this set of Sphinx documents.  If None, it defaults to
+# "<project> v<release> documentation".
+#html_title = None
+
+# A shorter title for the navigation bar.  Default is the same as html_title.
+#html_short_title = None
+
+# The name of an image file (relative to this directory) to place at the top
+# of the sidebar.
+#html_logo = None
+
+# The name of an image file (within the static path) to use as favicon of the
+# docs.  This file should be a Windows icon file (.ico) being 16x16 or 32x32
+# pixels large.
+#html_favicon = None
+
+# Add any paths that contain custom static files (such as style sheets) here,
+# relative to this directory. They are copied after the builtin static files,
+# so a file named "default.css" will overwrite the builtin "default.css".
+html_static_path = []
+
+# If not '', a 'Last updated on:' timestamp is inserted at every page bottom,
+# using the given strftime format.
+#html_last_updated_fmt = '%b %d, %Y'
+
+# If true, SmartyPants will be used to convert quotes and dashes to
+# typographically correct entities.
+#html_use_smartypants = True
+
+# Custom sidebar templates, maps document names to template names.
+#html_sidebars = {}
+
+# Additional templates that should be rendered to pages, maps page names to
+# template names.
+#html_additional_pages = {}
+
+# If false, no module index is generated.
+#html_use_modindex = True
+
+# If false, no index is generated.
+#html_use_index = True
+
+# If true, the index is split into individual pages for each letter.
+#html_split_index = False
+
+# If true, the reST sources are included in the HTML build as _sources/<name>.
+#html_copy_source = True
+
+# If true, an OpenSearch description file will be output, and all pages will
+# contain a <link> tag referring to it.  The value of this option must be the
+# base URL from which the finished HTML is served.
+#html_use_opensearch = ''
+
+# If nonempty, this is the file name suffix for HTML files (e.g. ".xhtml").
+#html_file_suffix = ''
+
+# Output file base name for HTML help builder.
+htmlhelp_basename = 'Janssondoc'
+
+
+# Options for LaTeX output
+# ------------------------
+
+# The paper size ('letter' or 'a4').
+#latex_paper_size = 'letter'
+
+# The font size ('10pt', '11pt' or '12pt').
+#latex_font_size = '10pt'
+
+# Grouping the document tree into LaTeX files. List of tuples
+# (source start file, target name, title, author, document class [howto/manual]).
+latex_documents = [
+  ('index', 'Jansson.tex', ur'Jansson Documentation',
+   ur'Petri Lehtinen', 'manual'),
+]
+
+# The name of an image file (relative to this directory) to place at the top of
+# the title page.
+#latex_logo = None
+
+# For "manual" documents, if this is true, then toplevel headings are parts,
+# not chapters.
+#latex_use_parts = False
+
+# Additional stuff for the LaTeX preamble.
+#latex_preamble = ''
+
+# Documents to append as an appendix to all manuals.
+#latex_appendices = []
+
+# If false, no module index is generated.
+#latex_use_modindex = True
diff --git a/doc/ext/refcounting.py b/doc/ext/refcounting.py
new file mode 100644 (file)
index 0000000..506b2ee
--- /dev/null
@@ -0,0 +1,59 @@
+"""
+    refcounting
+    ~~~~~~~~~~~
+
+    Reference count annotations for C API functions. Has the same
+    result as the sphinx.ext.refcounting extension but works for all
+    functions regardless of the signature, and the reference counting
+    information is written inline with the documentation instead of a
+    separate file.
+
+    Adds a new directive "refcounting". The directive has no content
+    and one required positional parameter:: "new" or "borrow".
+
+    Example:
+
+    .. cfunction:: json_t *json_object(void)
+
+       .. refcounting:: new
+
+       <description of the json_object function>
+
+    :copyright: Copyright 2009 Petri Lehtinen <petri@digip.org>
+    :license: MIT, see LICENSE for details.
+"""
+
+from docutils import nodes
+
+class refcounting(nodes.emphasis): pass
+
+def visit(self, node):
+    self.visit_emphasis(node)
+
+def depart(self, node):
+    self.depart_emphasis(node)
+
+def html_visit(self, node):
+    self.body.append(self.starttag(node, 'em', '', CLASS='refcount'))
+
+def html_depart(self, node):
+    self.body.append('</em>')
+
+
+def refcounting_directive(name, arguments, options, content, lineno,
+                   content_offset, block_text, state, state_machine):
+    if arguments[0] == 'borrow':
+        text = 'Return value: Borrowed reference.'
+    elif arguments[0] == 'new':
+        text = 'Return value: New reference.'
+    else:
+        raise Error('Valid arguments: new, borrow')
+
+    return [refcounting(text, text)]
+
+def setup(app):
+    app.add_node(refcounting,
+                 html=(html_visit, html_depart),
+                 latex=(visit, depart),
+                 text=(visit, depart))
+    app.add_directive('refcounting', refcounting_directive, 0, (1, 0, 0))
diff --git a/doc/gettingstarted.rst b/doc/gettingstarted.rst
new file mode 100644 (file)
index 0000000..34d2ab9
--- /dev/null
@@ -0,0 +1,108 @@
+***************
+Getting Started
+***************
+
+.. highlight:: c
+
+Compiling and Installing Jansson
+================================
+
+The Jansson source is available at
+http://www.digip.org/jansson/releases/.
+
+Unpack the source tarball and change to the source directory:
+
+.. parsed-literal::
+
+    bunzip2 -c jansson-|release|.tar.bz2 | tar xf -
+    cd jansson-|release|
+
+The source uses GNU Autotools (autoconf_, automake_, libtool_), so
+compiling and installing is extremely simple::
+
+    ./configure
+    make
+    make check
+    make install
+
+To change the destination directory (``/usr/local`` by default), use
+the ``--prefix=DIR`` argument to ``./configure``. See ``./configure
+--help`` for the list of all possible installation options. (There are
+no options to customize the resulting Jansson binary.)
+
+The command ``make check`` runs the test suite distributed with
+Jansson. This step is not strictly necessary, but it may find possible
+problems that Jansson has on your platform. If any problems are found,
+please report them.
+
+If you obtained the source from a Git repository (or any other source
+control system), there's no ``./configure`` script as it's not kept in
+version control. To create the script, Autotools needs to be
+bootstrapped. There are many ways to do this, but the easiest one is
+to use ``autoreconf``::
+
+    autoreconf -vi
+
+This command creates the ``./configure`` script, which can then be
+used as described above.
+
+.. _autoconf: http://www.gnu.org/software/autoconf/
+.. _automake: http://www.gnu.org/software/automake/
+.. _libtool: http://www.gnu.org/software/libtool/
+
+
+Installing Prebuilt Binary Packages
+-----------------------------------
+
+Binary ``.deb`` packages for Ubuntu are available in `this PPA`_ at
+Launchpad_. Follow the instructions in the PPA ("Technical details
+about this PPA" link) to take the PPA into use. Then install the -dev
+package::
+
+  sudo apt-get install libjansson-dev
+
+.. _this PPA: http://launchpad.net/~petri/+archive/ppa
+.. _Launchpad: http://launchpad.net/
+
+
+Building the Documentation
+--------------------------
+
+(This subsection describes how to build the HTML documentation you are
+currently reading, so it can be safely skipped.)
+
+Documentation is in the ``doc/`` subdirectory. It's written in
+reStructuredText_ with Sphinx_ annotations. To generate the HTML
+documentation, invoke::
+
+   make html
+
+and point your browser to ``doc/_build/html/index.html``. Sphinx_ is
+required to generate the documentation.
+
+.. _reStructuredText: http://docutils.sourceforge.net/rst.html
+.. _Sphinx: http://sphinx.pocoo.org/
+
+
+Compiling Programs That Use Jansson
+===================================
+
+Jansson involves one C header file, :file:`jansson.h`, so it's enough
+to put the line
+
+::
+
+    #include <jansson.h>
+
+in the beginning of every source file that uses Jansson.
+
+There's also just one library to link with, ``libjansson``. Compile and
+link the program as follows::
+
+    cc -o prog prog.c -ljansson
+
+Starting from version 1.2, there's also support for pkg-config_::
+
+    cc -o prog prog.c `pkg-config --cflags --libs jansson`
+
+.. _pkg-config: http://pkg-config.freedesktop.org/
diff --git a/doc/github_commits.c b/doc/github_commits.c
new file mode 100644 (file)
index 0000000..75b5d34
--- /dev/null
@@ -0,0 +1,171 @@
+/*
+ * Copyright (c) 2009 Petri Lehtinen <petri@digip.org>
+ *
+ * Jansson is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include <stdlib.h>
+#include <string.h>
+
+#include <jansson.h>
+#include <curl/curl.h>
+
+#define BUFFER_SIZE  (256 * 1024)  /* 256 KB */
+
+#define URL_FORMAT   "http://github.com/api/v2/json/commits/list/%s/%s/master"
+#define URL_SIZE     256
+
+/* Return the offset of the first newline in text or the length of
+   text if there's no newline */
+static int newline_offset(const char *text)
+{
+    const char *newline = strchr(text, '\n');
+    if(!newline)
+        return strlen(text);
+    else
+        return (int)(newline - text);
+}
+
+struct write_result
+{
+    char *data;
+    int pos;
+};
+
+static size_t write_response(void *ptr, size_t size, size_t nmemb, void *stream)
+{
+    struct write_result *result = (struct write_result *)stream;
+
+    if(result->pos + size * nmemb >= BUFFER_SIZE - 1)
+    {
+        fprintf(stderr, "error: too small buffer\n");
+        return 0;
+    }
+
+    memcpy(result->data + result->pos, ptr, size * nmemb);
+    result->pos += size * nmemb;
+
+    return size * nmemb;
+}
+
+static char *request(const char *url)
+{
+    CURL *curl;
+    CURLcode status;
+    char *data;
+    long code;
+
+    curl = curl_easy_init();
+    data = malloc(BUFFER_SIZE);
+    if(!curl || !data)
+        return NULL;
+
+    struct write_result write_result = {
+        .data = data,
+        .pos = 0
+    };
+
+    curl_easy_setopt(curl, CURLOPT_URL, url);
+    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_response);
+    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &write_result);
+
+    status = curl_easy_perform(curl);
+    if(status != 0)
+    {
+        fprintf(stderr, "error: unable to request data from %s:\n", url);
+        fprintf(stderr, "%s\n", curl_easy_strerror(status));
+        return NULL;
+    }
+
+    curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
+    if(code != 200)
+    {
+        fprintf(stderr, "error: server responded with code %ld\n", code);
+        return NULL;
+    }
+
+    curl_easy_cleanup(curl);
+    curl_global_cleanup();
+
+    /* zero-terminate the result */
+    data[write_result.pos] = '\0';
+
+    return data;
+}
+
+int main(int argc, char *argv[])
+{
+    unsigned int i;
+    char *text;
+    char url[URL_SIZE];
+
+    json_t *root;
+    json_error_t error;
+    json_t *commits;
+
+    if(argc != 3)
+    {
+        fprintf(stderr, "usage: %s USER REPOSITORY\n\n", argv[0]);
+        fprintf(stderr, "List commits at USER's REPOSITORY.\n\n");
+        return 2;
+    }
+
+    snprintf(url, URL_SIZE, URL_FORMAT, argv[1], argv[2]);
+
+    text = request(url);
+    if(!text)
+        return 1;
+
+    root = json_loads(text, &error);
+    free(text);
+
+    if(!root)
+    {
+        fprintf(stderr, "error: on line %d: %s\n", error.line, error.text);
+        return 1;
+    }
+
+    commits = json_object_get(root, "commits");
+    if(!json_is_array(commits))
+    {
+        fprintf(stderr, "error: commits is not an array\n");
+        return 1;
+    }
+
+    for(i = 0; i < json_array_size(commits); i++)
+    {
+        json_t *commit, *id, *message;
+        const char *message_text;
+
+        commit = json_array_get(commits, i);
+        if(!json_is_object(commit))
+        {
+            fprintf(stderr, "error: commit %d is not an object\n", i + 1);
+            return 1;
+        }
+
+        id = json_object_get(commit, "id");
+        if(!json_is_string(id))
+        {
+            fprintf(stderr, "error: commit %d: id is not a string\n", i + 1);
+            return 1;
+        }
+
+        message = json_object_get(commit, "message");
+        if(!json_is_string(message))
+        {
+            fprintf(stderr, "error: commit %d: message is not a string\n", i + 1);
+            return 1;
+        }
+
+        message_text = json_string_value(message);
+        printf("%.8s %.*s\n",
+               json_string_value(id),
+               newline_offset(message_text),
+               message_text);
+    }
+
+    json_decref(root);
+    return 0;
+}
diff --git a/doc/index.rst b/doc/index.rst
new file mode 100644 (file)
index 0000000..1ae9bf3
--- /dev/null
@@ -0,0 +1,44 @@
+Jansson Documentation
+=====================
+
+This is the documentation for Jansson_ |release|, last updated |today|.
+
+Introduction
+------------
+
+Jansson_ is a C library for encoding, decoding and manipulating JSON
+data. Its main features and design principles are:
+
+- Simple and intuitive API and data model
+
+- Comprehensive documentation
+
+- No dependencies on other libraries
+
+- Full Unicode support (UTF-8)
+
+- Extensive test suite
+
+Jansson is licensed under the `MIT license`_; see LICENSE in the
+source distribution for details.
+
+
+.. _`MIT license`: http://www.opensource.org/licenses/mit-license.php
+.. _Jansson: http://www.digip.org/jansson/
+
+Contents
+--------
+
+.. toctree::
+   :maxdepth: 2
+
+   gettingstarted
+   tutorial
+   apiref
+
+
+Indices and Tables
+==================
+
+* :ref:`genindex`
+* :ref:`search`
diff --git a/doc/tutorial.rst b/doc/tutorial.rst
new file mode 100644 (file)
index 0000000..aaeb554
--- /dev/null
@@ -0,0 +1,275 @@
+.. _tutorial:
+
+********
+Tutorial
+********
+
+.. highlight:: c
+
+In this tutorial, we create a program that fetches the latest commits
+of a repository in GitHub_ over the web. One of the response formats
+supported by `GitHub API`_ is JSON, so the result can be parsed using
+Jansson.
+
+To stick to the the scope of this tutorial, we will only cover the the
+parts of the program related to handling JSON data. For the best user
+experience, the full source code is available:
+:download:`github_commits.c`. To compile it (on Unix-like systems with
+gcc), use the following command::
+
+    gcc -o github_commits github_commits.c -ljansson -lcurl
+
+libcurl_ is used to communicate over the web, so it is required to
+compile the program.
+
+The command line syntax is::
+
+    github_commits USER REPOSITORY
+
+``USER`` is a GitHub user ID and ``REPOSITORY`` is the repository
+name. Please note that the GitHub API is rate limited, so if you run
+the program too many times within a short period of time, the sever
+starts to respond with an error.
+
+.. _GitHub: http://github.com/
+.. _GitHub API: http://develop.github.com/
+.. _libcurl: http://curl.haxx.se/
+
+
+.. _tutorial-github-commits-api:
+
+The GitHub Commits API
+======================
+
+The `GitHub commits API`_ is used by sending HTTP requests to URLs
+starting with ``http://github.com/api/v2/json/commits/``. Our program
+only lists the latest commits, so the rest of the URL is
+``list/USER/REPOSITORY/BRANCH``, where ``USER``, ``REPOSITORY`` and
+``BRANCH`` are the GitHub user ID, the name of the repository, and the
+name of the branch whose commits are to be listed, respectively.
+
+GitHub responds with a JSON object of the following form:
+
+.. code-block:: none
+
+    {
+        "commits": [
+            {
+                "id": "<the commit ID>",
+                "message": "<the commit message>",
+                <more fields, not important to this tutorial>
+            },
+            {
+                "id": "<the commit ID>",
+                "message": "<the commit message>",
+                <more fields, not important to this tutorial>
+            },
+            <more commits...>
+        ]
+    }
+
+In our program, the HTTP request is sent using the following
+function::
+
+    static char *request(const char *url);
+
+It takes the URL as a parameter, preforms a HTTP GET request, and
+returns a newly allocated string that contains the response body. If
+the request fails, an error message is printed to stderr and the
+return value is *NULL*. For full details, refer to :download:`the code
+<github_commits.c>`, as the actual implementation is not important
+here.
+
+.. _GitHub commits API: http://develop.github.com/p/commits.html
+
+.. _tutorial-the-program:
+
+The Program
+===========
+
+First the includes::
+
+    #include <string.h>
+    #include <jansson.h>
+
+Like all the programs using Jansson, we need to include
+:file:`jansson.h`.
+
+The following definitions are used to build the GitHub commits API
+request URL::
+
+   #define URL_FORMAT   "http://github.com/api/v2/json/commits/list/%s/%s/master"
+   #define URL_SIZE     256
+
+The following function is used when formatting the result to find the
+first newline in the commit message::
+
+    /* Return the offset of the first newline in text or the length of
+       text if there's no newline */
+    static int newline_offset(const char *text)
+    {
+        const char *newline = strchr(text, '\n');
+        if(!newline)
+            return strlen(text);
+        else
+            return (int)(newline - text);
+    }
+
+The main function follows. In the beginning, we first declare a bunch
+of variables and check the command line parameters::
+
+    unsigned int i;
+    char *text;
+    char url[URL_SIZE];
+
+    json_t *root;
+    json_error_t error;
+    json_t *commits;
+
+    if(argc != 3)
+    {
+        fprintf(stderr, "usage: %s USER REPOSITORY\n\n", argv[0]);
+        fprintf(stderr, "List commits at USER's REPOSITORY.\n\n");
+        return 2;
+    }
+
+Then we build the request URL using the user and repository names
+given as command line parameters::
+
+    snprintf(url, URL_SIZE, URL_FORMAT, argv[1], argv[2]);
+
+This uses the ``URL_SIZE`` and ``URL_FORMAT`` constants defined above.
+Now we're ready to actually request the JSON data over the web::
+
+    text = request(url);
+    if(!text)
+        return 1;
+
+If an error occurs, our function ``request`` prints the error and
+returns *NULL*, so it's enough to just return 1 from the main
+function.
+
+Next we'll call :cfunc:`json_loads()` to decode the JSON text we got
+as a response::
+
+    root = json_loads(text, &error);
+    free(text);
+
+    if(!root)
+    {
+        fprintf(stderr, "error: on line %d: %s\n", error.line, error.text);
+        return 1;
+    }
+
+We don't need the JSON text anymore, so we can free the ``text``
+variable right after decoding it. If :cfunc:`json_loads()` fails, it
+returns *NULL* and sets error information to the :ctype:`json_error_t`
+structure given as the second parameter. In this case, our program
+prints the error information out and returns 1 from the main function.
+
+Now we're ready to extract the data out of the decoded JSON response.
+The structure of the response JSON was explained in section
+:ref:`tutorial-github-commits-api`.
+
+First, we'll extract the ``commits`` array from the JSON response::
+
+    commits = json_object_get(root, "commits");
+    if(!json_is_array(commits))
+    {
+        fprintf(stderr, "error: commits is not an array\n");
+        return 1;
+    }
+
+This is the array that contains objects describing latest commits in
+the repository. We check that the returned value really is an array.
+If the key ``commits`` doesn't exist, :cfunc:`json_object_get()`
+returns *NULL*, but :cfunc:`json_is_array()` handles this case, too.
+
+Then we proceed to loop over all the commits in the array::
+
+    for(i = 0; i < json_array_size(commits); i++)
+    {
+        json_t *commit, *id, *message;
+        const char *message_text;
+
+        commit = json_array_get(commits, i);
+        if(!json_is_object(commit))
+        {
+            fprintf(stderr, "error: commit %d is not an object\n", i + 1);
+            return 1;
+        }
+    ...
+
+The function :cfunc:`json_array_size()` returns the size of a JSON
+array. First, we again declare some variables and then extract the
+i'th element of the ``commits`` array using :cfunc:`json_array_get()`.
+We also check that the resulting value is a JSON object.
+
+Next we'll extract the commit ID and commit message, and check that
+they both are JSON strings::
+
+        id = json_object_get(commit, "id");
+        if(!json_is_string(id))
+        {
+            fprintf(stderr, "error: commit %d: id is not a string\n", i + 1);
+            return 1;
+        }
+
+        message = json_object_get(commit, "message");
+        if(!json_is_string(message))
+        {
+            fprintf(stderr, "error: commit %d: message is not a string\n", i + 1);
+            return 1;
+        }
+    ...
+
+And finally, we'll print the first 8 characters of the commit ID and
+the first line of the commit message. A C-style string is extracted
+from a JSON string using :cfunc:`json_string_value()`::
+
+        message_text = json_string_value(message);
+        printf("%.8s %.*s\n",
+               json_string_value(id),
+               newline_offset(message_text),
+               message_text);
+    }
+
+After sending the HTTP request, we decoded the JSON text using
+:cfunc:`json_loads()`, remember? It returns a *new reference* to the
+JSON value it decodes. When we're finished with the value, we'll need
+to decrease the reference count using :cfunc:`json_decref()`. This way
+Jansson can release the resources::
+
+    json_decref(root);
+    return 0;
+
+For a detailed explanation of reference counting in Jansson, see
+:ref:`apiref-reference-count` in :ref:`apiref`.
+
+The program's ready, let's test it and view the latest commits in
+Jansson's repository::
+
+    $ ./github_commits akheron jansson
+    86dc1d62 Fix indentation
+    b67e130f json_dumpf: Document the output shortage on error
+    4cd77771 Enhance handling of circular references
+    79009e62 json_dumps: Close the strbuffer if dumping fails
+    76999799 doc: Fix a small typo in apiref
+    22af193a doc/Makefile.am: Remove *.pyc in clean
+    951d091f Make integer, real and string mutable
+    185e107d Don't use non-portable asprintf()
+    ca7703fb Merge branch '1.0'
+    12cd4e8c jansson 1.0.4
+    <etc...>
+
+
+Conclusion
+==========
+
+In this tutorial, we implemented a program that fetches the latest
+commits of a GitHub repository using the GitHub commits API. Jansson
+was used to decode the JSON response and to extract the commit data.
+
+This tutorial only covered a small part of Jansson. For example, we
+did not create or manipulate JSON values at all. Proceed to
+:ref:`apiref` to explore all features of Jansson.
diff --git a/driver.cpp b/driver.cpp
deleted file mode 100644 (file)
index f42831e..0000000
+++ /dev/null
@@ -1,7 +0,0 @@
-#include "jansson.hpp"
-
-extern int json_cpp_tests();
-
-int main() {
-       return json_cpp_tests();
-}
diff --git a/src/Makefile.am b/src/Makefile.am
new file mode 100644 (file)
index 0000000..87123a0
--- /dev/null
@@ -0,0 +1,20 @@
+include_HEADERS = jansson.h
+
+lib_LTLIBRARIES = libjansson.la
+libjansson_la_SOURCES = \
+       dump.c \
+       hashtable.c \
+       hashtable.h \
+       jansson_private.h \
+       load.c \
+       strbuffer.c \
+       strbuffer.h \
+       utf.c \
+       utf.h \
+       util.h \
+       value.c
+libjansson_la_LDFLAGS = \
+       -export-symbols-regex '^json_' \
+       -version-info 1:2:1
+
+AM_CFLAGS = -Wall -Wextra -Werror
diff --git a/src/dump.c b/src/dump.c
new file mode 100644 (file)
index 0000000..ba70f8d
--- /dev/null
@@ -0,0 +1,438 @@
+/*
+ * Copyright (c) 2009 Petri Lehtinen <petri@digip.org>
+ *
+ * Jansson is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h>
+#include <assert.h>
+
+#include <jansson.h>
+#include "jansson_private.h"
+#include "strbuffer.h"
+#include "utf.h"
+
+#define MAX_INTEGER_STR_LENGTH  100
+#define MAX_REAL_STR_LENGTH     100
+
+typedef int (*dump_func)(const char *buffer, int size, void *data);
+
+struct string
+{
+    char *buffer;
+    int length;
+    int size;
+};
+
+static int dump_to_strbuffer(const char *buffer, int size, void *data)
+{
+    return strbuffer_append_bytes((strbuffer_t *)data, buffer, size);
+}
+
+static int dump_to_file(const char *buffer, int size, void *data)
+{
+    FILE *dest = (FILE *)data;
+    if(fwrite(buffer, size, 1, dest) != 1)
+        return -1;
+    return 0;
+}
+
+/* 256 spaces (the maximum indentation size) */
+static char whitespace[] = "                                                                                                                                                                                                                                                                ";
+
+static int dump_indent(unsigned long flags, int depth, int space, dump_func dump, void *data)
+{
+    if(JSON_INDENT(flags) > 0)
+    {
+        int i, ws_count = JSON_INDENT(flags);
+
+        if(dump("\n", 1, data))
+            return -1;
+
+        for(i = 0; i < depth; i++)
+        {
+            if(dump(whitespace, ws_count, data))
+                return -1;
+        }
+    }
+    else if(space && !(flags & JSON_COMPACT))
+    {
+        return dump(" ", 1, data);
+    }
+    return 0;
+}
+
+static int dump_string(const char *str, int ascii, dump_func dump, void *data)
+{
+    const char *pos, *end;
+    int32_t codepoint;
+
+    if(dump("\"", 1, data))
+        return -1;
+
+    end = pos = str;
+    while(1)
+    {
+        const char *text;
+        char seq[13];
+        int length;
+
+        while(*end)
+        {
+            end = utf8_iterate(pos, &codepoint);
+            if(!end)
+                return -1;
+
+            /* mandatory escape or control char */
+            if(codepoint == '\\' || codepoint == '"' || codepoint < 0x20)
+                break;
+
+            /* non-ASCII */
+            if(ascii && codepoint > 0x7F)
+                break;
+
+            pos = end;
+        }
+
+        if(pos != str) {
+            if(dump(str, pos - str, data))
+                return -1;
+        }
+
+        if(end == pos)
+            break;
+
+        /* handle \, ", and control codes */
+        length = 2;
+        switch(codepoint)
+        {
+            case '\\': text = "\\\\"; break;
+            case '\"': text = "\\\""; break;
+            case '\b': text = "\\b"; break;
+            case '\f': text = "\\f"; break;
+            case '\n': text = "\\n"; break;
+            case '\r': text = "\\r"; break;
+            case '\t': text = "\\t"; break;
+            default:
+            {
+                /* codepoint is in BMP */
+                if(codepoint < 0x10000)
+                {
+                    sprintf(seq, "\\u%04x", codepoint);
+                    length = 6;
+                }
+
+                /* not in BMP -> construct a UTF-16 surrogate pair */
+                else
+                {
+                    int32_t first, last;
+
+                    codepoint -= 0x10000;
+                    first = 0xD800 | ((codepoint & 0xffc00) >> 10);
+                    last = 0xDC00 | (codepoint & 0x003ff);
+
+                    sprintf(seq, "\\u%04x\\u%04x", first, last);
+                    length = 12;
+                }
+
+                text = seq;
+                break;
+            }
+        }
+
+        if(dump(text, length, data))
+            return -1;
+
+        str = pos = end;
+    }
+
+    return dump("\"", 1, data);
+}
+
+static int object_key_cmp(const void *key1, const void *key2)
+{
+    return strcmp(*(const char **)key1, *(const char **)key2);
+}
+
+static int do_dump(const json_t *json, unsigned long flags, int depth,
+                   dump_func dump, void *data)
+{
+    int ascii = flags & JSON_ENSURE_ASCII ? 1 : 0;
+
+    switch(json_typeof(json)) {
+        case JSON_NULL:
+            return dump("null", 4, data);
+
+        case JSON_TRUE:
+            return dump("true", 4, data);
+
+        case JSON_FALSE:
+            return dump("false", 5, data);
+
+        case JSON_INTEGER:
+        {
+            char buffer[MAX_INTEGER_STR_LENGTH];
+            int size;
+
+            size = snprintf(buffer, MAX_INTEGER_STR_LENGTH, "%d", json_integer_value(json));
+            if(size >= MAX_INTEGER_STR_LENGTH)
+                return -1;
+
+            return dump(buffer, size, data);
+        }
+
+        case JSON_REAL:
+        {
+            char buffer[MAX_REAL_STR_LENGTH];
+            int size;
+
+            size = snprintf(buffer, MAX_REAL_STR_LENGTH, "%.17g",
+                            json_real_value(json));
+            if(size >= MAX_REAL_STR_LENGTH)
+                return -1;
+
+            /* Make sure there's a dot or 'e' in the output. Otherwise
+               a real is converted to an integer when decoding */
+            if(strchr(buffer, '.') == NULL &&
+               strchr(buffer, 'e') == NULL)
+            {
+                if(size + 2 >= MAX_REAL_STR_LENGTH) {
+                    /* No space to append ".0" */
+                    return -1;
+                }
+                buffer[size] = '.';
+                buffer[size + 1] = '0';
+                size += 2;
+            }
+
+            return dump(buffer, size, data);
+        }
+
+        case JSON_STRING:
+            return dump_string(json_string_value(json), ascii, dump, data);
+
+        case JSON_ARRAY:
+        {
+            int i;
+            int n;
+            json_array_t *array;
+
+            /* detect circular references */
+            array = json_to_array(json);
+            if(array->visited)
+                return -1;
+            array->visited = 1;
+
+            n = json_array_size(json);
+
+            if(dump("[", 1, data))
+                return -1;
+            if(n == 0)
+                return dump("]", 1, data);
+            if(dump_indent(flags, depth + 1, 0, dump, data))
+                return -1;
+
+            for(i = 0; i < n; ++i) {
+                if(do_dump(json_array_get(json, i), flags, depth + 1,
+                           dump, data))
+                    return -1;
+
+                if(i < n - 1)
+                {
+                    if(dump(",", 1, data) ||
+                       dump_indent(flags, depth + 1, 1, dump, data))
+                        return -1;
+                }
+                else
+                {
+                    if(dump_indent(flags, depth, 0, dump, data))
+                        return -1;
+                }
+            }
+
+            array->visited = 0;
+            return dump("]", 1, data);
+        }
+
+        case JSON_OBJECT:
+        {
+            json_object_t *object;
+            void *iter;
+            const char *separator;
+            int separator_length;
+
+            if(flags & JSON_COMPACT) {
+                separator = ":";
+                separator_length = 1;
+            }
+            else {
+                separator = ": ";
+                separator_length = 2;
+            }
+
+            /* detect circular references */
+            object = json_to_object(json);
+            if(object->visited)
+                return -1;
+            object->visited = 1;
+
+            iter = json_object_iter((json_t *)json);
+
+            if(dump("{", 1, data))
+                return -1;
+            if(!iter)
+                return dump("}", 1, data);
+            if(dump_indent(flags, depth + 1, 0, dump, data))
+                return -1;
+
+            if(flags & JSON_SORT_KEYS)
+            {
+                /* Sort keys */
+
+                const char **keys;
+                unsigned int size;
+                unsigned int i;
+
+                size = json_object_size(json);
+                keys = malloc(size * sizeof(const char *));
+                if(!keys)
+                    return -1;
+
+                i = 0;
+                while(iter)
+                {
+                    keys[i] = json_object_iter_key(iter);
+                    iter = json_object_iter_next((json_t *)json, iter);
+                    i++;
+                }
+                assert(i == size);
+
+                qsort(keys, size, sizeof(const char *), object_key_cmp);
+
+                for(i = 0; i < size; i++)
+                {
+                    const char *key;
+                    json_t *value;
+
+                    key = keys[i];
+                    value = json_object_get(json, key);
+                    assert(value);
+
+                    dump_string(key, ascii, dump, data);
+                    if(dump(separator, separator_length, data) ||
+                       do_dump(value, flags, depth + 1, dump, data))
+                    {
+                        free(keys);
+                        return -1;
+                    }
+
+                    if(i < size - 1)
+                    {
+                        if(dump(",", 1, data) ||
+                           dump_indent(flags, depth + 1, 1, dump, data))
+                        {
+                            free(keys);
+                            return -1;
+                        }
+                    }
+                    else
+                    {
+                        if(dump_indent(flags, depth, 0, dump, data))
+                        {
+                            free(keys);
+                            return -1;
+                        }
+                    }
+                }
+
+                free(keys);
+            }
+            else
+            {
+                /* Don't sort keys */
+
+                while(iter)
+                {
+                    void *next = json_object_iter_next((json_t *)json, iter);
+
+                    dump_string(json_object_iter_key(iter), ascii, dump, data);
+                    if(dump(separator, separator_length, data) ||
+                       do_dump(json_object_iter_value(iter), flags, depth + 1,
+                               dump, data))
+                        return -1;
+
+                    if(next)
+                    {
+                        if(dump(",", 1, data) ||
+                           dump_indent(flags, depth + 1, 1, dump, data))
+                            return -1;
+                    }
+                    else
+                    {
+                        if(dump_indent(flags, depth, 0, dump, data))
+                            return -1;
+                    }
+
+                    iter = next;
+                }
+            }
+
+            object->visited = 0;
+            return dump("}", 1, data);
+        }
+
+        default:
+            /* not reached */
+            return -1;
+    }
+}
+
+
+char *json_dumps(const json_t *json, unsigned long flags)
+{
+    strbuffer_t strbuff;
+    char *result;
+
+    if(!json_is_array(json) && !json_is_object(json))
+        return NULL;
+
+    if(strbuffer_init(&strbuff))
+        return NULL;
+
+    if(do_dump(json, flags, 0, dump_to_strbuffer, (void *)&strbuff)) {
+        strbuffer_close(&strbuff);
+        return NULL;
+    }
+
+    result = strdup(strbuffer_value(&strbuff));
+    strbuffer_close(&strbuff);
+
+    return result;
+}
+
+int json_dumpf(const json_t *json, FILE *output, unsigned long flags)
+{
+    if(!json_is_array(json) && !json_is_object(json))
+        return -1;
+
+    return do_dump(json, flags, 0, dump_to_file, (void *)output);
+}
+
+int json_dump_file(const json_t *json, const char *path, unsigned long flags)
+{
+    int result;
+
+    FILE *output = fopen(path, "w");
+    if(!output)
+        return -1;
+
+    result = json_dumpf(json, output, flags);
+
+    fclose(output);
+    return result;
+}
diff --git a/src/hashtable.c b/src/hashtable.c
new file mode 100644 (file)
index 0000000..05dc167
--- /dev/null
@@ -0,0 +1,339 @@
+/*
+ * Copyright (c) 2009 Petri Lehtinen <petri@digip.org>
+ *
+ * This library is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include <stdlib.h>
+#include "hashtable.h"
+
+typedef struct hashtable_list list_t;
+typedef struct hashtable_pair pair_t;
+typedef struct hashtable_bucket bucket_t;
+
+#define container_of(ptr_, type_, member_)                      \
+    ((type_ *)((char *)ptr_ - (size_t)&((type_ *)0)->member_))
+
+#define list_to_pair(list_)  container_of(list_, pair_t, list)
+
+static inline void list_init(list_t *list)
+{
+    list->next = list;
+    list->prev = list;
+}
+
+static inline void list_insert(list_t *list, list_t *node)
+{
+    node->next = list;
+    node->prev = list->prev;
+    list->prev->next = node;
+    list->prev = node;
+}
+
+static inline void list_remove(list_t *list)
+{
+    list->prev->next = list->next;
+    list->next->prev = list->prev;
+}
+
+static inline int bucket_is_empty(hashtable_t *hashtable, bucket_t *bucket)
+{
+    return bucket->first == &hashtable->list && bucket->first == bucket->last;
+}
+
+static void insert_to_bucket(hashtable_t *hashtable, bucket_t *bucket,
+                             list_t *list)
+{
+    if(bucket_is_empty(hashtable, bucket))
+    {
+        list_insert(&hashtable->list, list);
+        bucket->first = bucket->last = list;
+    }
+    else
+    {
+        list_insert(bucket->first, list);
+        bucket->first = list;
+    }
+}
+
+static unsigned int primes[] = {
+    5, 13, 23, 53, 97, 193, 389, 769, 1543, 3079, 6151, 12289, 24593,
+    49157, 98317, 196613, 393241, 786433, 1572869, 3145739, 6291469,
+    12582917, 25165843, 50331653, 100663319, 201326611, 402653189,
+    805306457, 1610612741
+};
+static const unsigned int num_primes = sizeof(primes) / sizeof(unsigned int);
+
+static inline unsigned int num_buckets(hashtable_t *hashtable)
+{
+    return primes[hashtable->num_buckets];
+}
+
+
+static pair_t *hashtable_find_pair(hashtable_t *hashtable, bucket_t *bucket,
+                                   const void *key, unsigned int hash)
+{
+    list_t *list;
+    pair_t *pair;
+
+    if(bucket_is_empty(hashtable, bucket))
+        return NULL;
+
+    list = bucket->first;
+    while(1)
+    {
+        pair = list_to_pair(list);
+        if(pair->hash == hash && hashtable->cmp_keys(pair->key, key))
+            return pair;
+
+        if(list == bucket->last)
+            break;
+
+        list = list->next;
+    }
+
+    return NULL;
+}
+
+/* returns 0 on success, -1 if key was not found */
+static int hashtable_do_del(hashtable_t *hashtable,
+                            const void *key, unsigned int hash)
+{
+    pair_t *pair;
+    bucket_t *bucket;
+    unsigned int index;
+
+    index = hash % num_buckets(hashtable);
+    bucket = &hashtable->buckets[index];
+
+    pair = hashtable_find_pair(hashtable, bucket, key, hash);
+    if(!pair)
+        return -1;
+
+    if(&pair->list == bucket->first && &pair->list == bucket->last)
+        bucket->first = bucket->last = &hashtable->list;
+
+    else if(&pair->list == bucket->first)
+        bucket->first = pair->list.next;
+
+    else if(&pair->list == bucket->last)
+        bucket->last = pair->list.prev;
+
+    list_remove(&pair->list);
+
+    if(hashtable->free_key)
+        hashtable->free_key(pair->key);
+    if(hashtable->free_value)
+        hashtable->free_value(pair->value);
+
+    free(pair);
+    hashtable->size--;
+
+    return 0;
+}
+
+static void hashtable_do_clear(hashtable_t *hashtable)
+{
+    list_t *list, *next;
+    pair_t *pair;
+
+    for(list = hashtable->list.next; list != &hashtable->list; list = next)
+    {
+        next = list->next;
+        pair = list_to_pair(list);
+        if(hashtable->free_key)
+            hashtable->free_key(pair->key);
+        if(hashtable->free_value)
+            hashtable->free_value(pair->value);
+        free(pair);
+    }
+}
+
+static int hashtable_do_rehash(hashtable_t *hashtable)
+{
+    list_t *list, *next;
+    pair_t *pair;
+    unsigned int i, index, new_size;
+
+    free(hashtable->buckets);
+
+    hashtable->num_buckets++;
+    new_size = num_buckets(hashtable);
+
+    hashtable->buckets = malloc(new_size * sizeof(bucket_t));
+    if(!hashtable->buckets)
+        return -1;
+
+    for(i = 0; i < num_buckets(hashtable); i++)
+    {
+        hashtable->buckets[i].first = hashtable->buckets[i].last =
+            &hashtable->list;
+    }
+
+    list = hashtable->list.next;
+    list_init(&hashtable->list);
+
+    for(; list != &hashtable->list; list = next) {
+        next = list->next;
+        pair = list_to_pair(list);
+        index = pair->hash % new_size;
+        insert_to_bucket(hashtable, &hashtable->buckets[index], &pair->list);
+    }
+
+    return 0;
+}
+
+
+hashtable_t *hashtable_create(key_hash_fn hash_key, key_cmp_fn cmp_keys,
+                              free_fn free_key, free_fn free_value)
+{
+    hashtable_t *hashtable = malloc(sizeof(hashtable_t));
+    if(!hashtable)
+        return NULL;
+
+    if(hashtable_init(hashtable, hash_key, cmp_keys, free_key, free_value))
+    {
+        free(hashtable);
+        return NULL;
+    }
+
+    return hashtable;
+}
+
+void hashtable_destroy(hashtable_t *hashtable)
+{
+    hashtable_close(hashtable);
+    free(hashtable);
+}
+
+int hashtable_init(hashtable_t *hashtable,
+                   key_hash_fn hash_key, key_cmp_fn cmp_keys,
+                   free_fn free_key, free_fn free_value)
+{
+    unsigned int i;
+
+    hashtable->size = 0;
+    hashtable->num_buckets = 0;  /* index to primes[] */
+    hashtable->buckets = malloc(num_buckets(hashtable) * sizeof(bucket_t));
+    if(!hashtable->buckets)
+        return -1;
+
+    list_init(&hashtable->list);
+
+    hashtable->hash_key = hash_key;
+    hashtable->cmp_keys = cmp_keys;
+    hashtable->free_key = free_key;
+    hashtable->free_value = free_value;
+
+    for(i = 0; i < num_buckets(hashtable); i++)
+    {
+        hashtable->buckets[i].first = hashtable->buckets[i].last =
+            &hashtable->list;
+    }
+
+    return 0;
+}
+
+void hashtable_close(hashtable_t *hashtable)
+{
+    hashtable_do_clear(hashtable);
+    free(hashtable->buckets);
+}
+
+int hashtable_set(hashtable_t *hashtable, void *key, void *value)
+{
+    pair_t *pair;
+    bucket_t *bucket;
+    unsigned int hash, index;
+
+    hash = hashtable->hash_key(key);
+
+    /* if the key already exists, delete it */
+    hashtable_do_del(hashtable, key, hash);
+
+    /* rehash if the load ratio exceeds 1 */
+    if(hashtable->size >= num_buckets(hashtable))
+        if(hashtable_do_rehash(hashtable))
+            return -1;
+
+    pair = malloc(sizeof(pair_t));
+    if(!pair)
+        return -1;
+
+    pair->key = key;
+    pair->value = value;
+    pair->hash = hash;
+    list_init(&pair->list);
+
+    index = hash % num_buckets(hashtable);
+    bucket = &hashtable->buckets[index];
+
+    insert_to_bucket(hashtable, bucket, &pair->list);
+
+    hashtable->size++;
+    return 0;
+}
+
+void *hashtable_get(hashtable_t *hashtable, const void *key)
+{
+    pair_t *pair;
+    unsigned int hash;
+    bucket_t *bucket;
+
+    hash = hashtable->hash_key(key);
+    bucket = &hashtable->buckets[hash % num_buckets(hashtable)];
+
+    pair = hashtable_find_pair(hashtable, bucket, key, hash);
+    if(!pair)
+        return NULL;
+
+    return pair->value;
+}
+
+int hashtable_del(hashtable_t *hashtable, const void *key)
+{
+    unsigned int hash = hashtable->hash_key(key);
+    return hashtable_do_del(hashtable, key, hash);
+}
+
+void hashtable_clear(hashtable_t *hashtable)
+{
+    unsigned int i;
+
+    hashtable_do_clear(hashtable);
+
+    for(i = 0; i < num_buckets(hashtable); i++)
+    {
+        hashtable->buckets[i].first = hashtable->buckets[i].last =
+            &hashtable->list;
+    }
+
+    list_init(&hashtable->list);
+    hashtable->size = 0;
+}
+
+void *hashtable_iter(hashtable_t *hashtable)
+{
+    return hashtable_iter_next(hashtable, &hashtable->list);
+}
+
+void *hashtable_iter_next(hashtable_t *hashtable, void *iter)
+{
+    list_t *list = (list_t *)iter;
+    if(list->next == &hashtable->list)
+        return NULL;
+    return list->next;
+}
+
+void *hashtable_iter_key(void *iter)
+{
+    pair_t *pair = list_to_pair((list_t *)iter);
+    return pair->key;
+}
+
+void *hashtable_iter_value(void *iter)
+{
+    pair_t *pair = list_to_pair((list_t *)iter);
+    return pair->value;
+}
diff --git a/src/hashtable.h b/src/hashtable.h
new file mode 100644 (file)
index 0000000..81a0af5
--- /dev/null
@@ -0,0 +1,188 @@
+/*
+ * Copyright (c) 2009 Petri Lehtinen <petri@digip.org>
+ *
+ * This library is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#ifndef HASHTABLE_H
+#define HASHTABLE_H
+
+typedef unsigned int (*key_hash_fn)(const void *key);
+typedef int (*key_cmp_fn)(const void *key1, const void *key2);
+typedef void (*free_fn)(void *key);
+
+struct hashtable_list {
+    struct hashtable_list *prev;
+    struct hashtable_list *next;
+};
+
+struct hashtable_pair {
+    void *key;
+    void *value;
+    unsigned int hash;
+    struct hashtable_list list;
+};
+
+struct hashtable_bucket {
+    struct hashtable_list *first;
+    struct hashtable_list *last;
+};
+
+typedef struct hashtable {
+    unsigned int size;
+    struct hashtable_bucket *buckets;
+    unsigned int num_buckets;  /* index to primes[] */
+    struct hashtable_list list;
+
+    key_hash_fn hash_key;
+    key_cmp_fn cmp_keys;  /* returns non-zero for equal keys */
+    free_fn free_key;
+    free_fn free_value;
+} hashtable_t;
+
+/**
+ * hashtable_create - Create a hashtable object
+ *
+ * @hash_key: The key hashing function
+ * @cmp_keys: The key compare function. Returns non-zero for equal and
+ *     zero for unequal unequal keys
+ * @free_key: If non-NULL, called for a key that is no longer referenced.
+ * @free_value: If non-NULL, called for a value that is no longer referenced.
+ *
+ * Returns a new hashtable object that should be freed with
+ * hashtable_destroy when it's no longer used, or NULL on failure (out
+ * of memory).
+ */
+hashtable_t *hashtable_create(key_hash_fn hash_key, key_cmp_fn cmp_keys,
+                              free_fn free_key, free_fn free_value);
+
+/**
+ * hashtable_destroy - Destroy a hashtable object
+ *
+ * @hashtable: The hashtable
+ *
+ * Destroys a hashtable created with hashtable_create().
+ */
+void hashtable_destroy(hashtable_t *hashtable);
+
+/**
+ * hashtable_init - Initialize a hashtable object
+ *
+ * @hashtable: The (statically allocated) hashtable object
+ * @hash_key: The key hashing function
+ * @cmp_keys: The key compare function. Returns non-zero for equal and
+ *     zero for unequal unequal keys
+ * @free_key: If non-NULL, called for a key that is no longer referenced.
+ * @free_value: If non-NULL, called for a value that is no longer referenced.
+ *
+ * Initializes a statically allocated hashtable object. The object
+ * should be cleared with hashtable_close when it's no longer used.
+ *
+ * Returns 0 on success, -1 on error (out of memory).
+ */
+int hashtable_init(hashtable_t *hashtable,
+                   key_hash_fn hash_key, key_cmp_fn cmp_keys,
+                   free_fn free_key, free_fn free_value);
+
+/**
+ * hashtable_close - Release all resources used by a hashtable object
+ *
+ * @hashtable: The hashtable
+ *
+ * Destroys a statically allocated hashtable object.
+ */
+void hashtable_close(hashtable_t *hashtable);
+
+/**
+ * hashtable_set - Add/modify value in hashtable
+ *
+ * @hashtable: The hashtable object
+ * @key: The key
+ * @value: The value
+ *
+ * If a value with the given key already exists, its value is replaced
+ * with the new value.
+ *
+ * Key and value are "stealed" in the sense that hashtable frees them
+ * automatically when they are no longer used. The freeing is
+ * accomplished by calling free_key and free_value functions that were
+ * supplied to hashtable_new. In case one or both of the free
+ * functions is NULL, the corresponding item is not "stealed".
+ *
+ * Returns 0 on success, -1 on failure (out of memory).
+ */
+int hashtable_set(hashtable_t *hashtable, void *key, void *value);
+
+/**
+ * hashtable_get - Get a value associated with a key
+ *
+ * @hashtable: The hashtable object
+ * @key: The key
+ *
+ * Returns value if it is found, or NULL otherwise.
+ */
+void *hashtable_get(hashtable_t *hashtable, const void *key);
+
+/**
+ * hashtable_del - Remove a value from the hashtable
+ *
+ * @hashtable: The hashtable object
+ * @key: The key
+ *
+ * Returns 0 on success, or -1 if the key was not found.
+ */
+int hashtable_del(hashtable_t *hashtable, const void *key);
+
+/**
+ * hashtable_clear - Clear hashtable
+ *
+ * @hashtable: The hashtable object
+ *
+ * Removes all items from the hashtable.
+ */
+void hashtable_clear(hashtable_t *hashtable);
+
+/**
+ * hashtable_iter - Iterate over hashtable
+ *
+ * @hashtable: The hashtable object
+ *
+ * Returns an opaque iterator to the first element in the hashtable.
+ * The iterator should be passed to hashtable_iter_* functions.
+ * The hashtable items are not iterated over in any particular order.
+ *
+ * There's no need to free the iterator in any way. The iterator is
+ * valid as long as the item that is referenced by the iterator is not
+ * deleted. Other values may be added or deleted. In particular,
+ * hashtable_iter_next() may be called on an iterator, and after that
+ * the key/value pair pointed by the old iterator may be deleted.
+ */
+void *hashtable_iter(hashtable_t *hashtable);
+
+/**
+ * hashtable_iter_next - Advance an iterator
+ *
+ * @hashtable: The hashtable object
+ * @iter: The iterator
+ *
+ * Returns a new iterator pointing to the next element in the
+ * hashtable or NULL if the whole hastable has been iterated over.
+ */
+void *hashtable_iter_next(hashtable_t *hashtable, void *iter);
+
+/**
+ * hashtable_iter_key - Retrieve the key pointed by an iterator
+ *
+ * @iter: The iterator
+ */
+void *hashtable_iter_key(void *iter);
+
+/**
+ * hashtable_iter_value - Retrieve the value pointed by an iterator
+ *
+ * @iter: The iterator
+ */
+void *hashtable_iter_value(void *iter);
+
+#endif
similarity index 100%
rename from jansson-impl.hpp
rename to src/jansson-impl.hpp
diff --git a/src/jansson.h b/src/jansson.h
new file mode 100644 (file)
index 0000000..73f6ce0
--- /dev/null
@@ -0,0 +1,177 @@
+/*
+ * Copyright (c) 2009 Petri Lehtinen <petri@digip.org>
+ *
+ * Jansson is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#ifndef JANSSON_H
+#define JANSSON_H
+
+#include <stdio.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* types */
+
+typedef enum {
+    JSON_OBJECT,
+    JSON_ARRAY,
+    JSON_STRING,
+    JSON_INTEGER,
+    JSON_REAL,
+    JSON_TRUE,
+    JSON_FALSE,
+    JSON_NULL
+} json_type;
+
+typedef struct {
+    json_type type;
+    unsigned long refcount;
+} json_t;
+
+#define json_typeof(json)      ((json)->type)
+#define json_is_object(json)   (json && json_typeof(json) == JSON_OBJECT)
+#define json_is_array(json)    (json && json_typeof(json) == JSON_ARRAY)
+#define json_is_string(json)   (json && json_typeof(json) == JSON_STRING)
+#define json_is_integer(json)  (json && json_typeof(json) == JSON_INTEGER)
+#define json_is_real(json)     (json && json_typeof(json) == JSON_REAL)
+#define json_is_number(json)   (json_is_integer(json) || json_is_real(json))
+#define json_is_true(json)     (json && json_typeof(json) == JSON_TRUE)
+#define json_is_false(json)    (json && json_typeof(json) == JSON_FALSE)
+#define json_is_boolean(json)  (json_is_true(json) || json_is_false(json))
+#define json_is_null(json)     (json && json_typeof(json) == JSON_NULL)
+
+/* construction, destruction, reference counting */
+
+json_t *json_object(void);
+json_t *json_array(void);
+json_t *json_string(const char *value);
+json_t *json_string_nocheck(const char *value);
+json_t *json_integer(int value);
+json_t *json_real(double value);
+json_t *json_true(void);
+json_t *json_false(void);
+json_t *json_null(void);
+
+static inline json_t *json_incref(json_t *json)
+{
+    if(json && json->refcount != (unsigned int)-1)
+        ++json->refcount;
+    return json;
+}
+
+/* do not call json_delete directly */
+void json_delete(json_t *json);
+
+static inline void json_decref(json_t *json)
+{
+    if(json && json->refcount != (unsigned int)-1 && --json->refcount == 0)
+        json_delete(json);
+}
+
+
+/* getters, setters, manipulation */
+
+unsigned int json_object_size(const json_t *object);
+json_t *json_object_get(const json_t *object, const char *key);
+int json_object_set_new(json_t *object, const char *key, json_t *value);
+int json_object_set_new_nocheck(json_t *object, const char *key, json_t *value);
+int json_object_del(json_t *object, const char *key);
+int json_object_clear(json_t *object);
+int json_object_update(json_t *object, json_t *other);
+void *json_object_iter(json_t *object);
+void *json_object_iter_next(json_t *object, void *iter);
+const char *json_object_iter_key(void *iter);
+json_t *json_object_iter_value(void *iter);
+
+static inline
+int json_object_set(json_t *object, const char *key, json_t *value)
+{
+    return json_object_set_new(object, key, json_incref(value));
+}
+
+static inline
+int json_object_set_nocheck(json_t *object, const char *key, json_t *value)
+{
+    return json_object_set_new_nocheck(object, key, json_incref(value));
+}
+
+unsigned int json_array_size(const json_t *array);
+json_t *json_array_get(const json_t *array, unsigned int index);
+int json_array_set_new(json_t *array, unsigned int index, json_t *value);
+int json_array_append_new(json_t *array, json_t *value);
+int json_array_insert_new(json_t *array, unsigned int index, json_t *value);
+int json_array_remove(json_t *array, unsigned int index);
+int json_array_clear(json_t *array);
+int json_array_extend(json_t *array, json_t *other);
+
+static inline
+int json_array_set(json_t *array, unsigned int index, json_t *value)
+{
+    return json_array_set_new(array, index, json_incref(value));
+}
+
+static inline
+int json_array_append(json_t *array, json_t *value)
+{
+    return json_array_append_new(array, json_incref(value));
+}
+
+static inline
+int json_array_insert(json_t *array, unsigned int index, json_t *value)
+{
+    return json_array_insert_new(array, index, json_incref(value));
+}
+
+const char *json_string_value(const json_t *string);
+int json_integer_value(const json_t *integer);
+double json_real_value(const json_t *real);
+double json_number_value(const json_t *json);
+
+int json_string_set(json_t *string, const char *value);
+int json_string_set_nocheck(json_t *string, const char *value);
+int json_integer_set(json_t *integer, int value);
+int json_real_set(json_t *real, double value);
+
+
+/* equality */
+
+int json_equal(json_t *value1, json_t *value2);
+
+
+/* copying */
+
+json_t *json_copy(json_t *value);
+json_t *json_deep_copy(json_t *value);
+
+
+/* loading, printing */
+
+#define JSON_ERROR_TEXT_LENGTH  160
+
+typedef struct {
+    char text[JSON_ERROR_TEXT_LENGTH];
+    int line;
+} json_error_t;
+
+json_t *json_loads(const char *input, json_error_t *error);
+json_t *json_loadf(FILE *input, json_error_t *error);
+json_t *json_load_file(const char *path, json_error_t *error);
+
+#define JSON_INDENT(n)      (n & 0xFF)
+#define JSON_COMPACT        0x100
+#define JSON_ENSURE_ASCII   0x200
+#define JSON_SORT_KEYS      0x400
+
+char *json_dumps(const json_t *json, unsigned long flags);
+int json_dumpf(const json_t *json, FILE *output, unsigned long flags);
+int json_dump_file(const json_t *json, const char *path, unsigned long flags);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
similarity index 100%
rename from jansson.hpp
rename to src/jansson.hpp
diff --git a/src/jansson_private.h b/src/jansson_private.h
new file mode 100644 (file)
index 0000000..3e9bb7f
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2009 Petri Lehtinen <petri@digip.org>
+ *
+ * Jansson is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#ifndef JANSSON_PRIVATE_H
+#define JANSSON_PRIVATE_H
+
+#include "jansson.h"
+#include "hashtable.h"
+
+#define container_of(ptr_, type_, member_)  \
+    ((type_ *)((char *)ptr_ - (size_t)&((type_ *)0)->member_))
+
+typedef struct {
+    json_t json;
+    hashtable_t hashtable;
+    int visited;
+} json_object_t;
+
+typedef struct {
+    json_t json;
+    unsigned int size;
+    unsigned int entries;
+    json_t **table;
+    int visited;
+} json_array_t;
+
+typedef struct {
+    json_t json;
+    char *value;
+} json_string_t;
+
+typedef struct {
+    json_t json;
+    double value;
+} json_real_t;
+
+typedef struct {
+    json_t json;
+    int value;
+} json_integer_t;
+
+#define json_to_object(json_)  container_of(json_, json_object_t, json)
+#define json_to_array(json_)   container_of(json_, json_array_t, json)
+#define json_to_string(json_)  container_of(json_, json_string_t, json)
+#define json_to_real(json_)   container_of(json_, json_real_t, json)
+#define json_to_integer(json_) container_of(json_, json_integer_t, json)
+
+#endif
diff --git a/src/load.c b/src/load.c
new file mode 100644 (file)
index 0000000..4d08139
--- /dev/null
@@ -0,0 +1,886 @@
+/*
+ * Copyright (c) 2009 Petri Lehtinen <petri@digip.org>
+ *
+ * Jansson is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#define _GNU_SOURCE
+#include <ctype.h>
+#include <errno.h>
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdarg.h>
+#include <assert.h>
+#include <stdint.h>
+
+#include <jansson.h>
+#include "jansson_private.h"
+#include "strbuffer.h"
+#include "utf.h"
+
+#define TOKEN_INVALID         -1
+#define TOKEN_EOF              0
+#define TOKEN_STRING         256
+#define TOKEN_INTEGER        257
+#define TOKEN_REAL           258
+#define TOKEN_TRUE           259
+#define TOKEN_FALSE          260
+#define TOKEN_NULL           261
+
+/* read one byte from stream, return EOF on end of file */
+typedef int (*get_func)(void *data);
+
+/* return non-zero if end of file has been reached */
+typedef int (*eof_func)(void *data);
+
+typedef struct {
+    get_func get;
+    eof_func eof;
+    void *data;
+    int stream_pos;
+    char buffer[5];
+    int buffer_pos;
+} stream_t;
+
+
+typedef struct {
+    stream_t stream;
+    strbuffer_t saved_text;
+    int token;
+    int line, column;
+    union {
+        char *string;
+        int integer;
+        double real;
+    } value;
+} lex_t;
+
+
+/*** error reporting ***/
+
+static void error_init(json_error_t *error)
+{
+    if(error)
+    {
+        error->text[0] = '\0';
+        error->line = -1;
+    }
+}
+
+static void error_set(json_error_t *error, const lex_t *lex,
+                      const char *msg, ...)
+{
+    va_list ap;
+    char text[JSON_ERROR_TEXT_LENGTH];
+
+    if(!error || error->text[0] != '\0') {
+        /* error already set */
+        return;
+    }
+
+    va_start(ap, msg);
+    vsnprintf(text, JSON_ERROR_TEXT_LENGTH, msg, ap);
+    va_end(ap);
+
+    if(lex)
+    {
+        const char *saved_text = strbuffer_value(&lex->saved_text);
+        error->line = lex->line;
+        if(saved_text && saved_text[0])
+        {
+            if(lex->saved_text.length <= 20) {
+                snprintf(error->text, JSON_ERROR_TEXT_LENGTH,
+                         "%s near '%s'", text, saved_text);
+            }
+            else
+                snprintf(error->text, JSON_ERROR_TEXT_LENGTH, "%s", text);
+        }
+        else
+        {
+            snprintf(error->text, JSON_ERROR_TEXT_LENGTH,
+                     "%s near end of file", text);
+        }
+    }
+    else
+    {
+        error->line = -1;
+        snprintf(error->text, JSON_ERROR_TEXT_LENGTH, "%s", text);
+    }
+}
+
+
+/*** lexical analyzer ***/
+
+void stream_init(stream_t *stream, get_func get, eof_func eof, void *data)
+{
+    stream->get = get;
+    stream->eof = eof;
+    stream->data = data;
+    stream->stream_pos = 0;
+    stream->buffer[0] = '\0';
+    stream->buffer_pos = 0;
+}
+
+static char stream_get(stream_t *stream, json_error_t *error)
+{
+    char c;
+
+    if(!stream->buffer[stream->buffer_pos])
+    {
+        stream->buffer[0] = stream->get(stream->data);
+        stream->buffer_pos = 0;
+
+        c = stream->buffer[0];
+
+        if((unsigned char)c >= 0x80 && c != (char)EOF)
+        {
+            /* multi-byte UTF-8 sequence */
+            int i, count;
+
+            count = utf8_check_first(c);
+            if(!count)
+                goto out;
+
+            assert(count >= 2);
+
+            for(i = 1; i < count; i++)
+                stream->buffer[i] = stream->get(stream->data);
+
+            if(!utf8_check_full(stream->buffer, count, NULL))
+                goto out;
+
+            stream->stream_pos += count;
+            stream->buffer[count] = '\0';
+        }
+        else {
+            stream->buffer[1] = '\0';
+            stream->stream_pos++;
+        }
+    }
+
+    return stream->buffer[stream->buffer_pos++];
+
+out:
+    error_set(error, NULL, "unable to decode byte 0x%x at position %d",
+              (unsigned char)c, stream->stream_pos);
+
+    stream->buffer[0] = EOF;
+    stream->buffer[1] = '\0';
+    stream->buffer_pos = 1;
+
+    return EOF;
+}
+
+static void stream_unget(stream_t *stream, char c)
+{
+    assert(stream->buffer_pos > 0);
+    stream->buffer_pos--;
+    assert(stream->buffer[stream->buffer_pos] == c);
+}
+
+
+static int lex_get(lex_t *lex, json_error_t *error)
+{
+    return stream_get(&lex->stream, error);
+}
+
+static int lex_eof(lex_t *lex)
+{
+    return lex->stream.eof(lex->stream.data);
+}
+
+static void lex_save(lex_t *lex, char c)
+{
+    strbuffer_append_byte(&lex->saved_text, c);
+}
+
+static int lex_get_save(lex_t *lex, json_error_t *error)
+{
+    char c = stream_get(&lex->stream, error);
+    lex_save(lex, c);
+    return c;
+}
+
+static void lex_unget_unsave(lex_t *lex, char c)
+{
+    char d;
+    stream_unget(&lex->stream, c);
+    d = strbuffer_pop(&lex->saved_text);
+    assert(c == d);
+}
+
+static void lex_save_cached(lex_t *lex)
+{
+    while(lex->stream.buffer[lex->stream.buffer_pos] != '\0')
+    {
+        lex_save(lex, lex->stream.buffer[lex->stream.buffer_pos]);
+        lex->stream.buffer_pos++;
+    }
+}
+
+/* assumes that str points to 'u' plus at least 4 valid hex digits */
+static int32_t decode_unicode_escape(const char *str)
+{
+    int i;
+    int32_t value = 0;
+
+    assert(str[0] == 'u');
+
+    for(i = 1; i <= 4; i++) {
+        char c = str[i];
+        value <<= 4;
+        if(isdigit(c))
+            value += c - '0';
+        else if(islower(c))
+            value += c - 'a' + 10;
+        else if(isupper(c))
+            value += c - 'A' + 10;
+        else
+            assert(0);
+    }
+
+    return value;
+}
+
+static void lex_scan_string(lex_t *lex, json_error_t *error)
+{
+    char c;
+    const char *p;
+    char *t;
+    int i;
+
+    lex->value.string = NULL;
+    lex->token = TOKEN_INVALID;
+
+    c = lex_get_save(lex, error);
+
+    while(c != '"') {
+        if(c == (char)EOF) {
+            lex_unget_unsave(lex, c);
+            if(lex_eof(lex))
+                error_set(error, lex, "premature end of input");
+            goto out;
+        }
+
+        else if((unsigned char)c <= 0x1F) {
+            /* control character */
+            lex_unget_unsave(lex, c);
+            if(c == '\n')
+                error_set(error, lex, "unexpected newline", c);
+            else
+                error_set(error, lex, "control character 0x%x", c);
+            goto out;
+        }
+
+        else if(c == '\\') {
+            c = lex_get_save(lex, error);
+            if(c == 'u') {
+                c = lex_get_save(lex, error);
+                for(i = 0; i < 4; i++) {
+                    if(!isxdigit(c)) {
+                        lex_unget_unsave(lex, c);
+                        error_set(error, lex, "invalid escape");
+                        goto out;
+                    }
+                    c = lex_get_save(lex, error);
+                }
+            }
+            else if(c == '"' || c == '\\' || c == '/' || c == 'b' ||
+                    c == 'f' || c == 'n' || c == 'r' || c == 't')
+                c = lex_get_save(lex, error);
+            else {
+                lex_unget_unsave(lex, c);
+                error_set(error, lex, "invalid escape");
+                goto out;
+            }
+        }
+        else
+            c = lex_get_save(lex, error);
+    }
+
+    /* the actual value is at most of the same length as the source
+       string, because:
+         - shortcut escapes (e.g. "\t") (length 2) are converted to 1 byte
+         - a single \uXXXX escape (length 6) is converted to at most 3 bytes
+         - two \uXXXX escapes (length 12) forming an UTF-16 surrogate pair
+           are converted to 4 bytes
+    */
+    lex->value.string = malloc(lex->saved_text.length + 1);
+    if(!lex->value.string) {
+        /* this is not very nice, since TOKEN_INVALID is returned */
+        goto out;
+    }
+
+    /* the target */
+    t = lex->value.string;
+
+    /* + 1 to skip the " */
+    p = strbuffer_value(&lex->saved_text) + 1;
+
+    while(*p != '"') {
+        if(*p == '\\') {
+            p++;
+            if(*p == 'u') {
+                char buffer[4];
+                int length;
+                int32_t value;
+
+                value = decode_unicode_escape(p);
+                p += 5;
+
+                if(0xD800 <= value && value <= 0xDBFF) {
+                    /* surrogate pair */
+                    if(*p == '\\' && *(p + 1) == 'u') {
+                        int32_t value2 = decode_unicode_escape(++p);
+                        p += 5;
+
+                        if(0xDC00 <= value2 && value2 <= 0xDFFF) {
+                            /* valid second surrogate */
+                            value =
+                                ((value - 0xD800) << 10) +
+                                (value2 - 0xDC00) +
+                                0x10000;
+                        }
+                        else {
+                            /* invalid second surrogate */
+                            error_set(error, lex,
+                                      "invalid Unicode '\\u%04X\\u%04X'",
+                                      value, value2);
+                            goto out;
+                        }
+                    }
+                    else {
+                        /* no second surrogate */
+                        error_set(error, lex, "invalid Unicode '\\u%04X'",
+                                  value);
+                        goto out;
+                    }
+                }
+                else if(0xDC00 <= value && value <= 0xDFFF) {
+                    error_set(error, lex, "invalid Unicode '\\u%04X'", value);
+                    goto out;
+                }
+                else if(value == 0)
+                {
+                    error_set(error, lex, "\\u0000 is not allowed");
+                    goto out;
+                }
+
+                if(utf8_encode(value, buffer, &length))
+                    assert(0);
+
+                memcpy(t, buffer, length);
+                t += length;
+            }
+            else {
+                switch(*p) {
+                    case '"': case '\\': case '/':
+                        *t = *p; break;
+                    case 'b': *t = '\b'; break;
+                    case 'f': *t = '\f'; break;
+                    case 'n': *t = '\n'; break;
+                    case 'r': *t = '\r'; break;
+                    case 't': *t = '\t'; break;
+                    default: assert(0);
+                }
+                t++;
+                p++;
+            }
+        }
+        else
+            *(t++) = *(p++);
+    }
+    *t = '\0';
+    lex->token = TOKEN_STRING;
+    return;
+
+out:
+    free(lex->value.string);
+}
+
+static int lex_scan_number(lex_t *lex, char c, json_error_t *error)
+{
+    const char *saved_text;
+    char *end;
+    double value;
+
+    lex->token = TOKEN_INVALID;
+
+    if(c == '-')
+        c = lex_get_save(lex, error);
+
+    if(c == '0') {
+        c = lex_get_save(lex, error);
+        if(isdigit(c)) {
+            lex_unget_unsave(lex, c);
+            goto out;
+        }
+    }
+    else if(isdigit(c)) {
+        c = lex_get_save(lex, error);
+        while(isdigit(c))
+            c = lex_get_save(lex, error);
+    }
+    else {
+      lex_unget_unsave(lex, c);
+      goto out;
+    }
+
+    if(c != '.' && c != 'E' && c != 'e') {
+        long value;
+
+        lex_unget_unsave(lex, c);
+
+        saved_text = strbuffer_value(&lex->saved_text);
+        value = strtol(saved_text, &end, 10);
+        assert(end == saved_text + lex->saved_text.length);
+
+        if((value == LONG_MAX && errno == ERANGE) || value > INT_MAX) {
+            error_set(error, lex, "too big integer");
+            goto out;
+        }
+        else if((value == LONG_MIN && errno == ERANGE) || value < INT_MIN) {
+            error_set(error, lex, "too big negative integer");
+            goto out;
+        }
+
+        lex->token = TOKEN_INTEGER;
+        lex->value.integer = (int)value;
+        return 0;
+    }
+
+    if(c == '.') {
+        c = lex_get(lex, error);
+        if(!isdigit(c))
+            goto out;
+        lex_save(lex, c);
+
+        c = lex_get_save(lex, error);
+        while(isdigit(c))
+            c = lex_get_save(lex, error);
+    }
+
+    if(c == 'E' || c == 'e') {
+        c = lex_get_save(lex, error);
+        if(c == '+' || c == '-')
+            c = lex_get_save(lex, error);
+
+        if(!isdigit(c)) {
+            lex_unget_unsave(lex, c);
+            goto out;
+        }
+
+        c = lex_get_save(lex, error);
+        while(isdigit(c))
+            c = lex_get_save(lex, error);
+    }
+
+    lex_unget_unsave(lex, c);
+
+    saved_text = strbuffer_value(&lex->saved_text);
+    value = strtod(saved_text, &end);
+    assert(end == saved_text + lex->saved_text.length);
+
+    if(value == 0 && errno == ERANGE) {
+        error_set(error, lex, "real number underflow");
+        goto out;
+    }
+
+    /* Cannot test for +/-HUGE_VAL because the HUGE_VAL constant is
+       only defined in C99 mode. So let's trust in sole errno. */
+    else if(errno == ERANGE) {
+        error_set(error, lex, "real number overflow");
+        goto out;
+    }
+
+    lex->token = TOKEN_REAL;
+    lex->value.real = value;
+    return 0;
+
+out:
+    return -1;
+}
+
+static int lex_scan(lex_t *lex, json_error_t *error)
+{
+    char c;
+
+    strbuffer_clear(&lex->saved_text);
+
+    if(lex->token == TOKEN_STRING) {
+        free(lex->value.string);
+        lex->value.string = NULL;
+    }
+
+    c = lex_get(lex, error);
+    while(c == ' ' || c == '\t' || c == '\n' || c == '\r')
+    {
+        if(c == '\n')
+            lex->line++;
+
+        c = lex_get(lex, error);
+    }
+
+    if(c == (char)EOF) {
+        if(lex_eof(lex))
+            lex->token = TOKEN_EOF;
+        else
+            lex->token = TOKEN_INVALID;
+        goto out;
+    }
+
+    lex_save(lex, c);
+
+    if(c == '{' || c == '}' || c == '[' || c == ']' || c == ':' || c == ',')
+        lex->token = c;
+
+    else if(c == '"')
+        lex_scan_string(lex, error);
+
+    else if(isdigit(c) || c == '-') {
+        if(lex_scan_number(lex, c, error))
+            goto out;
+    }
+
+    else if(isupper(c) || islower(c)) {
+        /* eat up the whole identifier for clearer error messages */
+        const char *saved_text;
+
+        c = lex_get_save(lex, error);
+        while(isupper(c) || islower(c))
+            c = lex_get_save(lex, error);
+        lex_unget_unsave(lex, c);
+
+        saved_text = strbuffer_value(&lex->saved_text);
+
+        if(strcmp(saved_text, "true") == 0)
+            lex->token = TOKEN_TRUE;
+        else if(strcmp(saved_text, "false") == 0)
+            lex->token = TOKEN_FALSE;
+        else if(strcmp(saved_text, "null") == 0)
+            lex->token = TOKEN_NULL;
+        else
+            lex->token = TOKEN_INVALID;
+    }
+
+    else {
+        /* save the rest of the input UTF-8 sequence to get an error
+           message of valid UTF-8 */
+        lex_save_cached(lex);
+        lex->token = TOKEN_INVALID;
+    }
+
+out:
+    return lex->token;
+}
+
+static char *lex_steal_string(lex_t *lex)
+{
+    char *result = NULL;
+    if(lex->token == TOKEN_STRING)
+    {
+        result = lex->value.string;
+        lex->value.string = NULL;
+    }
+    return result;
+}
+
+static int lex_init(lex_t *lex, get_func get, eof_func eof, void *data)
+{
+    stream_init(&lex->stream, get, eof, data);
+    if(strbuffer_init(&lex->saved_text))
+        return -1;
+
+    lex->token = TOKEN_INVALID;
+    lex->line = 1;
+
+    return 0;
+}
+
+static void lex_close(lex_t *lex)
+{
+    if(lex->token == TOKEN_STRING)
+        free(lex->value.string);
+    strbuffer_close(&lex->saved_text);
+}
+
+
+/*** parser ***/
+
+static json_t *parse_value(lex_t *lex, json_error_t *error);
+
+static json_t *parse_object(lex_t *lex, json_error_t *error)
+{
+    json_t *object = json_object();
+    if(!object)
+        return NULL;
+
+    lex_scan(lex, error);
+    if(lex->token == '}')
+        return object;
+
+    while(1) {
+        char *key;
+        json_t *value;
+
+        if(lex->token != TOKEN_STRING) {
+            error_set(error, lex, "string or '}' expected");
+            goto error;
+        }
+
+        key = lex_steal_string(lex);
+        if(!key)
+            return NULL;
+
+        lex_scan(lex, error);
+        if(lex->token != ':') {
+            free(key);
+            error_set(error, lex, "':' expected");
+            goto error;
+        }
+
+        lex_scan(lex, error);
+        value = parse_value(lex, error);
+        if(!value) {
+            free(key);
+            goto error;
+        }
+
+        if(json_object_set_nocheck(object, key, value)) {
+            free(key);
+            json_decref(value);
+            goto error;
+        }
+
+        json_decref(value);
+        free(key);
+
+        lex_scan(lex, error);
+        if(lex->token != ',')
+            break;
+
+        lex_scan(lex, error);
+    }
+
+    if(lex->token != '}') {
+        error_set(error, lex, "'}' expected");
+        goto error;
+    }
+
+    return object;
+
+error:
+    json_decref(object);
+    return NULL;
+}
+
+static json_t *parse_array(lex_t *lex, json_error_t *error)
+{
+    json_t *array = json_array();
+    if(!array)
+        return NULL;
+
+    lex_scan(lex, error);
+    if(lex->token == ']')
+        return array;
+
+    while(lex->token) {
+        json_t *elem = parse_value(lex, error);
+        if(!elem)
+            goto error;
+
+        if(json_array_append(array, elem)) {
+            json_decref(elem);
+            goto error;
+        }
+        json_decref(elem);
+
+        lex_scan(lex, error);
+        if(lex->token != ',')
+            break;
+
+        lex_scan(lex, error);
+    }
+
+    if(lex->token != ']') {
+        error_set(error, lex, "']' expected");
+        goto error;
+    }
+
+    return array;
+
+error:
+    json_decref(array);
+    return NULL;
+}
+
+static json_t *parse_value(lex_t *lex, json_error_t *error)
+{
+    json_t *json;
+
+    switch(lex->token) {
+        case TOKEN_STRING: {
+            json = json_string_nocheck(lex->value.string);
+            break;
+        }
+
+        case TOKEN_INTEGER: {
+            json = json_integer(lex->value.integer);
+            break;
+        }
+
+        case TOKEN_REAL: {
+            json = json_real(lex->value.real);
+            break;
+        }
+
+        case TOKEN_TRUE:
+            json = json_true();
+            break;
+
+        case TOKEN_FALSE:
+            json = json_false();
+            break;
+
+        case TOKEN_NULL:
+            json = json_null();
+            break;
+
+        case '{':
+            json = parse_object(lex, error);
+            break;
+
+        case '[':
+            json = parse_array(lex, error);
+            break;
+
+        case TOKEN_INVALID:
+            error_set(error, lex, "invalid token");
+            return NULL;
+
+        default:
+            error_set(error, lex, "unexpected token");
+            return NULL;
+    }
+
+    if(!json)
+        return NULL;
+
+    return json;
+}
+
+static json_t *parse_json(lex_t *lex, json_error_t *error)
+{
+    error_init(error);
+
+    lex_scan(lex, error);
+    if(lex->token != '[' && lex->token != '{') {
+        error_set(error, lex, "'[' or '{' expected");
+        return NULL;
+    }
+
+    return parse_value(lex, error);
+}
+
+typedef struct
+{
+    const char *data;
+    int pos;
+} string_data_t;
+
+static int string_get(void *data)
+{
+    char c;
+    string_data_t *stream = (string_data_t *)data;
+    c = stream->data[stream->pos];
+    if(c == '\0')
+        return EOF;
+    else
+    {
+        stream->pos++;
+        return c;
+    }
+}
+
+static int string_eof(void *data)
+{
+    string_data_t *stream = (string_data_t *)data;
+    return (stream->data[stream->pos] == '\0');
+}
+
+json_t *json_loads(const char *string, json_error_t *error)
+{
+    lex_t lex;
+    json_t *result;
+
+    string_data_t stream_data = {
+        .data = string,
+        .pos = 0
+    };
+
+    if(lex_init(&lex, string_get, string_eof, (void *)&stream_data))
+        return NULL;
+
+    result = parse_json(&lex, error);
+    if(!result)
+        goto out;
+
+    lex_scan(&lex, error);
+    if(lex.token != TOKEN_EOF) {
+        error_set(error, &lex, "end of file expected");
+        json_decref(result);
+        result = NULL;
+    }
+
+out:
+    lex_close(&lex);
+    return result;
+}
+
+json_t *json_loadf(FILE *input, json_error_t *error)
+{
+    lex_t lex;
+    json_t *result;
+
+    if(lex_init(&lex, (get_func)fgetc, (eof_func)feof, input))
+        return NULL;
+
+    result = parse_json(&lex, error);
+    if(!result)
+        goto out;
+
+    lex_scan(&lex, error);
+    if(lex.token != TOKEN_EOF) {
+        error_set(error, &lex, "end of file expected");
+        json_decref(result);
+        result = NULL;
+    }
+
+out:
+    lex_close(&lex);
+    return result;
+}
+
+json_t *json_load_file(const char *path, json_error_t *error)
+{
+    json_t *result;
+    FILE *fp;
+
+    error_init(error);
+
+    fp = fopen(path, "r");
+    if(!fp)
+    {
+        error_set(error, NULL, "unable to open %s: %s",
+                  path, strerror(errno));
+        return NULL;
+    }
+
+    result = json_loadf(fp, error);
+
+    fclose(fp);
+    return result;
+}
diff --git a/src/strbuffer.c b/src/strbuffer.c
new file mode 100644 (file)
index 0000000..1a71a53
--- /dev/null
@@ -0,0 +1,95 @@
+/*
+ * Copyright (c) 2009 Petri Lehtinen <petri@digip.org>
+ *
+ * Jansson is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#define _GNU_SOURCE
+#include <stdlib.h>
+#include <string.h>
+#include "strbuffer.h"
+#include "util.h"
+
+#define STRBUFFER_MIN_SIZE  16
+#define STRBUFFER_FACTOR    2
+
+int strbuffer_init(strbuffer_t *strbuff)
+{
+    strbuff->size = STRBUFFER_MIN_SIZE;
+    strbuff->length = 0;
+
+    strbuff->value = malloc(strbuff->size);
+    if(!strbuff->value)
+        return -1;
+
+    /* initialize to empty */
+    strbuff->value[0] = '\0';
+    return 0;
+}
+
+void strbuffer_close(strbuffer_t *strbuff)
+{
+    free(strbuff->value);
+    strbuff->size = 0;
+    strbuff->length = 0;
+    strbuff->value = NULL;
+}
+
+void strbuffer_clear(strbuffer_t *strbuff)
+{
+    strbuff->length = 0;
+    strbuff->value[0] = '\0';
+}
+
+const char *strbuffer_value(const strbuffer_t *strbuff)
+{
+    return strbuff->value;
+}
+
+char *strbuffer_steal_value(strbuffer_t *strbuff)
+{
+    char *result = strbuff->value;
+    strbuffer_init(strbuff);
+    return result;
+}
+
+int strbuffer_append(strbuffer_t *strbuff, const char *string)
+{
+    return strbuffer_append_bytes(strbuff, string, strlen(string));
+}
+
+int strbuffer_append_byte(strbuffer_t *strbuff, char byte)
+{
+    return strbuffer_append_bytes(strbuff, &byte, 1);
+}
+
+int strbuffer_append_bytes(strbuffer_t *strbuff, const char *data, int size)
+{
+    if(strbuff->length + size >= strbuff->size)
+    {
+        strbuff->size = max(strbuff->size * STRBUFFER_FACTOR,
+                            strbuff->length + size + 1);
+
+        strbuff->value = realloc(strbuff->value, strbuff->size);
+        if(!strbuff->value)
+            return -1;
+    }
+
+    memcpy(strbuff->value + strbuff->length, data, size);
+    strbuff->length += size;
+    strbuff->value[strbuff->length] = '\0';
+
+    return 0;
+}
+
+char strbuffer_pop(strbuffer_t *strbuff)
+{
+    if(strbuff->length > 0) {
+        char c = strbuff->value[--strbuff->length];
+        strbuff->value[strbuff->length] = '\0';
+        return c;
+    }
+    else
+        return '\0';
+}
diff --git a/src/strbuffer.h b/src/strbuffer.h
new file mode 100644 (file)
index 0000000..4afefa9
--- /dev/null
@@ -0,0 +1,31 @@
+/*
+ * Copyright (c) 2009 Petri Lehtinen <petri@digip.org>
+ *
+ * Jansson is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#ifndef STRBUFFER_H
+#define STRBUFFER_H
+
+typedef struct {
+    char *value;
+    int length;   /* bytes used */
+    int size;     /* bytes allocated */
+} strbuffer_t;
+
+int strbuffer_init(strbuffer_t *strbuff);
+void strbuffer_close(strbuffer_t *strbuff);
+
+void strbuffer_clear(strbuffer_t *strbuff);
+
+const char *strbuffer_value(const strbuffer_t *strbuff);
+char *strbuffer_steal_value(strbuffer_t *strbuff);
+
+int strbuffer_append(strbuffer_t *strbuff, const char *string);
+int strbuffer_append_byte(strbuffer_t *strbuff, char byte);
+int strbuffer_append_bytes(strbuffer_t *strbuff, const char *data, int size);
+
+char strbuffer_pop(strbuffer_t *strbuff);
+
+#endif
diff --git a/src/utf.c b/src/utf.c
new file mode 100644 (file)
index 0000000..dda80f0
--- /dev/null
+++ b/src/utf.c
@@ -0,0 +1,190 @@
+/*
+ * Copyright (c) 2009 Petri Lehtinen <petri@digip.org>
+ *
+ * Jansson is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include <string.h>
+#include <stdint.h>
+
+int utf8_encode(int32_t codepoint, char *buffer, int *size)
+{
+    if(codepoint < 0)
+        return -1;
+    else if(codepoint < 0x80)
+    {
+        buffer[0] = (char)codepoint;
+        *size = 1;
+    }
+    else if(codepoint < 0x800)
+    {
+        buffer[0] = 0xC0 + ((codepoint & 0x7C0) >> 6);
+        buffer[1] = 0x80 + ((codepoint & 0x03F));
+        *size = 2;
+    }
+    else if(codepoint < 0x10000)
+    {
+        buffer[0] = 0xE0 + ((codepoint & 0xF000) >> 12);
+        buffer[1] = 0x80 + ((codepoint & 0x0FC0) >> 6);
+        buffer[2] = 0x80 + ((codepoint & 0x003F));
+        *size = 3;
+    }
+    else if(codepoint <= 0x10FFFF)
+    {
+        buffer[0] = 0xF0 + ((codepoint & 0x1C0000) >> 18);
+        buffer[1] = 0x80 + ((codepoint & 0x03F000) >> 12);
+        buffer[2] = 0x80 + ((codepoint & 0x000FC0) >> 6);
+        buffer[3] = 0x80 + ((codepoint & 0x00003F));
+        *size = 4;
+    }
+    else
+        return -1;
+
+    return 0;
+}
+
+int utf8_check_first(char byte)
+{
+    unsigned char u = (unsigned char)byte;
+
+    if(u < 0x80)
+        return 1;
+
+    if(0x80 <= u && u <= 0xBF) {
+        /* second, third or fourth byte of a multi-byte
+           sequence, i.e. a "continuation byte" */
+        return 0;
+    }
+    else if(u == 0xC0 || u == 0xC1) {
+        /* overlong encoding of an ASCII byte */
+        return 0;
+    }
+    else if(0xC2 <= u && u <= 0xDF) {
+        /* 2-byte sequence */
+        return 2;
+    }
+
+    else if(0xE0 <= u && u <= 0xEF) {
+        /* 3-byte sequence */
+        return 3;
+    }
+    else if(0xF0 <= u && u <= 0xF4) {
+        /* 4-byte sequence */
+        return 4;
+    }
+    else { /* u >= 0xF5 */
+        /* Restricted (start of 4-, 5- or 6-byte sequence) or invalid
+           UTF-8 */
+        return 0;
+    }
+}
+
+int utf8_check_full(const char *buffer, int size, int32_t *codepoint)
+{
+    int i;
+    int32_t value = 0;
+    unsigned char u = (unsigned char)buffer[0];
+
+    if(size == 2)
+    {
+        value = u & 0x1F;
+    }
+    else if(size == 3)
+    {
+        value = u & 0xF;
+    }
+    else if(size == 4)
+    {
+        value = u & 0x7;
+    }
+    else
+        return 0;
+
+    for(i = 1; i < size; i++)
+    {
+        u = (unsigned char)buffer[i];
+
+        if(u < 0x80 || u > 0xBF) {
+            /* not a continuation byte */
+            return 0;
+        }
+
+        value = (value << 6) + (u & 0x3F);
+    }
+
+    if(value > 0x10FFFF) {
+        /* not in Unicode range */
+        return 0;
+    }
+
+    else if(0xD800 <= value && value <= 0xDFFF) {
+        /* invalid code point (UTF-16 surrogate halves) */
+        return 0;
+    }
+
+    else if((size == 2 && value < 0x80) ||
+            (size == 3 && value < 0x800) ||
+            (size == 4 && value < 0x10000)) {
+        /* overlong encoding */
+        return 0;
+    }
+
+    if(codepoint)
+        *codepoint = value;
+
+    return 1;
+}
+
+const char *utf8_iterate(const char *buffer, int32_t *codepoint)
+{
+    int count;
+    int32_t value;
+
+    if(!*buffer)
+        return buffer;
+
+    count = utf8_check_first(buffer[0]);
+    if(count <= 0)
+        return NULL;
+
+    if(count == 1)
+        value = (unsigned char)buffer[0];
+    else
+    {
+        if(!utf8_check_full(buffer, count, &value))
+            return NULL;
+    }
+
+    if(codepoint)
+        *codepoint = value;
+
+    return buffer + count;
+}
+
+int utf8_check_string(const char *string, int length)
+{
+    int i;
+
+    if(length == -1)
+        length = strlen(string);
+
+    for(i = 0; i < length; i++)
+    {
+        int count = utf8_check_first(string[i]);
+        if(count == 0)
+            return 0;
+        else if(count > 1)
+        {
+            if(i + count > length)
+                return 0;
+
+            if(!utf8_check_full(&string[i], count, NULL))
+                return 0;
+
+            i += count - 1;
+        }
+    }
+
+    return 1;
+}
diff --git a/src/utf.h b/src/utf.h
new file mode 100644 (file)
index 0000000..03fba69
--- /dev/null
+++ b/src/utf.h
@@ -0,0 +1,19 @@
+/*
+ * Copyright (c) 2009 Petri Lehtinen <petri@digip.org>
+ *
+ * Jansson is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#ifndef UTF_H
+#define UTF_H
+
+int utf8_encode(int codepoint, char *buffer, int *size);
+
+int utf8_check_first(char byte);
+int utf8_check_full(const char *buffer, int size, int32_t *codepoint);
+const char *utf8_iterate(const char *buffer, int32_t *codepoint);
+
+int utf8_check_string(const char *string, int length);
+
+#endif
diff --git a/src/util.h b/src/util.h
new file mode 100644 (file)
index 0000000..66066c5
--- /dev/null
@@ -0,0 +1,13 @@
+/*
+ * Copyright (c) 2009 Petri Lehtinen <petri@digip.org>
+ *
+ * Jansson is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#ifndef UTIL_H
+#define UTIL_H
+
+#define max(a, b)  ((a) > (b) ? (a) : (b))
+
+#endif
diff --git a/src/value.c b/src/value.c
new file mode 100644 (file)
index 0000000..01e180e
--- /dev/null
@@ -0,0 +1,924 @@
+/*
+ * Copyright (c) 2009 Petri Lehtinen <petri@digip.org>
+ *
+ * Jansson is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#define _GNU_SOURCE
+#include <stdlib.h>
+#include <string.h>
+
+#include <jansson.h>
+#include "hashtable.h"
+#include "jansson_private.h"
+#include "utf.h"
+#include "util.h"
+
+
+static inline void json_init(json_t *json, json_type type)
+{
+    json->type = type;
+    json->refcount = 1;
+}
+
+
+/*** object ***/
+
+static unsigned int hash_string(const void *key)
+{
+    const char *str = (const char *)key;
+    unsigned int hash = 5381;
+    unsigned int c;
+
+    while((c = (unsigned int)*str))
+    {
+        hash = ((hash << 5) + hash) + c;
+        str++;
+    }
+
+    return hash;
+}
+
+static int string_equal(const void *key1, const void *key2)
+{
+    return strcmp((const char *)key1, (const char *)key2) == 0;
+}
+
+static void value_decref(void *value)
+{
+    json_decref((json_t *)value);
+}
+
+json_t *json_object(void)
+{
+    json_object_t *object = malloc(sizeof(json_object_t));
+    if(!object)
+        return NULL;
+    json_init(&object->json, JSON_OBJECT);
+
+    if(hashtable_init(&object->hashtable, hash_string, string_equal,
+                      free, value_decref))
+    {
+        free(object);
+        return NULL;
+    }
+
+    object->visited = 0;
+
+    return &object->json;
+}
+
+static void json_delete_object(json_object_t *object)
+{
+    hashtable_close(&object->hashtable);
+    free(object);
+}
+
+unsigned int json_object_size(const json_t *json)
+{
+    json_object_t *object;
+
+    if(!json_is_object(json))
+        return -1;
+
+    object = json_to_object(json);
+    return object->hashtable.size;
+}
+
+json_t *json_object_get(const json_t *json, const char *key)
+{
+    json_object_t *object;
+
+    if(!json_is_object(json))
+        return NULL;
+
+    object = json_to_object(json);
+    return hashtable_get(&object->hashtable, key);
+}
+
+int json_object_set_new_nocheck(json_t *json, const char *key, json_t *value)
+{
+    json_object_t *object;
+
+    if(!key || !value)
+        return -1;
+
+    if(!json_is_object(json) || json == value)
+    {
+        json_decref(value);
+        return -1;
+    }
+    object = json_to_object(json);
+
+    if(hashtable_set(&object->hashtable, strdup(key), value))
+    {
+        json_decref(value);
+        return -1;
+    }
+
+    return 0;
+}
+
+int json_object_set_new(json_t *json, const char *key, json_t *value)
+{
+    if(!key || !utf8_check_string(key, -1))
+    {
+        json_decref(value);
+        return -1;
+    }
+
+    return json_object_set_new_nocheck(json, key, value);
+}
+
+int json_object_del(json_t *json, const char *key)
+{
+    json_object_t *object;
+
+    if(!json_is_object(json))
+        return -1;
+
+    object = json_to_object(json);
+    return hashtable_del(&object->hashtable, key);
+}
+
+int json_object_clear(json_t *json)
+{
+    json_object_t *object;
+
+    if(!json_is_object(json))
+        return -1;
+
+    object = json_to_object(json);
+    hashtable_clear(&object->hashtable);
+
+    return 0;
+}
+
+int json_object_update(json_t *object, json_t *other)
+{
+    void *iter;
+
+    if(!json_is_object(object) || !json_is_object(other))
+        return -1;
+
+    iter = json_object_iter(other);
+    while(iter) {
+        const char *key;
+        json_t *value;
+
+        key = json_object_iter_key(iter);
+        value = json_object_iter_value(iter);
+
+        if(json_object_set_nocheck(object, key, value))
+            return -1;
+
+        iter = json_object_iter_next(other, iter);
+    }
+
+    return 0;
+}
+
+void *json_object_iter(json_t *json)
+{
+    json_object_t *object;
+
+    if(!json_is_object(json))
+        return NULL;
+
+    object = json_to_object(json);
+    return hashtable_iter(&object->hashtable);
+}
+
+void *json_object_iter_next(json_t *json, void *iter)
+{
+    json_object_t *object;
+
+    if(!json_is_object(json) || iter == NULL)
+        return NULL;
+
+    object = json_to_object(json);
+    return hashtable_iter_next(&object->hashtable, iter);
+}
+
+const char *json_object_iter_key(void *iter)
+{
+    if(!iter)
+        return NULL;
+
+    return (const char *)hashtable_iter_key(iter);
+}
+
+json_t *json_object_iter_value(void *iter)
+{
+    if(!iter)
+        return NULL;
+
+    return (json_t *)hashtable_iter_value(iter);
+}
+
+static int json_object_equal(json_t *object1, json_t *object2)
+{
+    void *iter;
+
+    if(json_object_size(object1) != json_object_size(object2))
+        return 0;
+
+    iter = json_object_iter(object1);
+    while(iter)
+    {
+        const char *key;
+        json_t *value1, *value2;
+
+        key = json_object_iter_key(iter);
+        value1 = json_object_iter_value(iter);
+        value2 = json_object_get(object2, key);
+
+        if(!json_equal(value1, value2))
+            return 0;
+
+        iter = json_object_iter_next(object1, iter);
+    }
+
+    return 1;
+}
+
+static json_t *json_object_copy(json_t *object)
+{
+    json_t *result;
+    void *iter;
+
+    result = json_object();
+    if(!result)
+        return NULL;
+
+    iter = json_object_iter(object);
+    while(iter)
+    {
+        const char *key;
+        json_t *value;
+
+        key = json_object_iter_key(iter);
+        value = json_object_iter_value(iter);
+        json_object_set_nocheck(result, key, value);
+
+        iter = json_object_iter_next(object, iter);
+    }
+
+    return result;
+}
+
+static json_t *json_object_deep_copy(json_t *object)
+{
+    json_t *result;
+    void *iter;
+
+    result = json_object();
+    if(!result)
+        return NULL;
+
+    iter = json_object_iter(object);
+    while(iter)
+    {
+        const char *key;
+        json_t *value;
+
+        key = json_object_iter_key(iter);
+        value = json_object_iter_value(iter);
+        json_object_set_new_nocheck(result, key, json_deep_copy(value));
+
+        iter = json_object_iter_next(object, iter);
+    }
+
+    return result;
+}
+
+
+/*** array ***/
+
+json_t *json_array(void)
+{
+    json_array_t *array = malloc(sizeof(json_array_t));
+    if(!array)
+        return NULL;
+    json_init(&array->json, JSON_ARRAY);
+
+    array->entries = 0;
+    array->size = 8;
+
+    array->table = malloc(array->size * sizeof(json_t *));
+    if(!array->table) {
+        free(array);
+        return NULL;
+    }
+
+    array->visited = 0;
+
+    return &array->json;
+}
+
+static void json_delete_array(json_array_t *array)
+{
+    unsigned int i;
+
+    for(i = 0; i < array->entries; i++)
+        json_decref(array->table[i]);
+
+    free(array->table);
+    free(array);
+}
+
+unsigned int json_array_size(const json_t *json)
+{
+    if(!json_is_array(json))
+        return 0;
+
+    return json_to_array(json)->entries;
+}
+
+json_t *json_array_get(const json_t *json, unsigned int index)
+{
+    json_array_t *array;
+    if(!json_is_array(json))
+        return NULL;
+    array = json_to_array(json);
+
+    if(index >= array->entries)
+        return NULL;
+
+    return array->table[index];
+}
+
+int json_array_set_new(json_t *json, unsigned int index, json_t *value)
+{
+    json_array_t *array;
+
+    if(!value)
+        return -1;
+
+    if(!json_is_array(json) || json == value)
+    {
+        json_decref(value);
+        return -1;
+    }
+    array = json_to_array(json);
+
+    if(index >= array->entries)
+    {
+        json_decref(value);
+        return -1;
+    }
+
+    json_decref(array->table[index]);
+    array->table[index] = value;
+
+    return 0;
+}
+
+static void array_move(json_array_t *array, unsigned int dest,
+                       unsigned int src, unsigned int count)
+{
+    memmove(&array->table[dest], &array->table[src], count * sizeof(json_t *));
+}
+
+static void array_copy(json_t **dest, unsigned int dpos,
+                       json_t **src, unsigned int spos,
+                       unsigned int count)
+{
+    memcpy(&dest[dpos], &src[spos], count * sizeof(json_t *));
+}
+
+static json_t **json_array_grow(json_array_t *array,
+                                unsigned int amount,
+                                int copy)
+{
+    unsigned int new_size;
+    json_t **old_table, **new_table;
+
+    if(array->entries + amount <= array->size)
+        return array->table;
+
+    old_table = array->table;
+
+    new_size = max(array->size + amount, array->size * 2);
+    new_table = malloc(new_size * sizeof(json_t *));
+    if(!new_table)
+        return NULL;
+
+    array->size = new_size;
+    array->table = new_table;
+
+    if(copy) {
+        array_copy(array->table, 0, old_table, 0, array->entries);
+        free(old_table);
+        return array->table;
+    }
+
+    return old_table;
+}
+
+int json_array_append_new(json_t *json, json_t *value)
+{
+    json_array_t *array;
+
+    if(!value)
+        return -1;
+
+    if(!json_is_array(json) || json == value)
+    {
+        json_decref(value);
+        return -1;
+    }
+    array = json_to_array(json);
+
+    if(!json_array_grow(array, 1, 1)) {
+        json_decref(value);
+        return -1;
+    }
+
+    array->table[array->entries] = value;
+    array->entries++;
+
+    return 0;
+}
+
+int json_array_insert_new(json_t *json, unsigned int index, json_t *value)
+{
+    json_array_t *array;
+    json_t **old_table;
+
+    if(!value)
+        return -1;
+
+    if(!json_is_array(json) || json == value) {
+        json_decref(value);
+        return -1;
+    }
+    array = json_to_array(json);
+
+    if(index > array->entries) {
+        json_decref(value);
+        return -1;
+    }
+
+    old_table = json_array_grow(array, 1, 0);
+    if(!old_table) {
+        json_decref(value);
+        return -1;
+    }
+
+    if(old_table != array->table) {
+        array_copy(array->table, 0, old_table, 0, index);
+        array_copy(array->table, index + 1, old_table, index,
+                   array->entries - index);
+        free(old_table);
+    }
+    else
+        array_move(array, index + 1, index, array->entries - index);
+
+    array->table[index] = value;
+    array->entries++;
+
+    return 0;
+}
+
+int json_array_remove(json_t *json, unsigned int index)
+{
+    json_array_t *array;
+
+    if(!json_is_array(json))
+        return -1;
+    array = json_to_array(json);
+
+    if(index >= array->entries)
+        return -1;
+
+    json_decref(array->table[index]);
+
+    array_move(array, index, index + 1, array->entries - index);
+    array->entries--;
+
+    return 0;
+}
+
+int json_array_clear(json_t *json)
+{
+    json_array_t *array;
+    unsigned int i;
+
+    if(!json_is_array(json))
+        return -1;
+    array = json_to_array(json);
+
+    for(i = 0; i < array->entries; i++)
+        json_decref(array->table[i]);
+
+    array->entries = 0;
+    return 0;
+}
+
+int json_array_extend(json_t *json, json_t *other_json)
+{
+    json_array_t *array, *other;
+    unsigned int i;
+
+    if(!json_is_array(json) || !json_is_array(other_json))
+        return -1;
+    array = json_to_array(json);
+    other = json_to_array(other_json);
+
+    if(!json_array_grow(array, other->entries, 1))
+        return -1;
+
+    for(i = 0; i < other->entries; i++)
+        json_incref(other->table[i]);
+
+    array_copy(array->table, array->entries, other->table, 0, other->entries);
+
+    array->entries += other->entries;
+    return 0;
+}
+
+static int json_array_equal(json_t *array1, json_t *array2)
+{
+    unsigned int i, size;
+
+    size = json_array_size(array1);
+    if(size != json_array_size(array2))
+        return 0;
+
+    for(i = 0; i < size; i++)
+    {
+        json_t *value1, *value2;
+
+        value1 = json_array_get(array1, i);
+        value2 = json_array_get(array2, i);
+
+        if(!json_equal(value1, value2))
+            return 0;
+    }
+
+    return 1;
+}
+
+static json_t *json_array_copy(json_t *array)
+{
+    json_t *result;
+    unsigned int i;
+
+    result = json_array();
+    if(!result)
+        return NULL;
+
+    for(i = 0; i < json_array_size(array); i++)
+        json_array_append(result, json_array_get(array, i));
+
+    return result;
+}
+
+static json_t *json_array_deep_copy(json_t *array)
+{
+    json_t *result;
+    unsigned int i;
+
+    result = json_array();
+    if(!result)
+        return NULL;
+
+    for(i = 0; i < json_array_size(array); i++)
+        json_array_append_new(result, json_deep_copy(json_array_get(array, i)));
+
+    return result;
+}
+
+/*** string ***/
+
+json_t *json_string_nocheck(const char *value)
+{
+    json_string_t *string;
+
+    if(!value)
+        return NULL;
+
+    string = malloc(sizeof(json_string_t));
+    if(!string)
+        return NULL;
+    json_init(&string->json, JSON_STRING);
+
+    string->value = strdup(value);
+    if(!string->value) {
+        free(string);
+        return NULL;
+    }
+
+    return &string->json;
+}
+
+json_t *json_string(const char *value)
+{
+    if(!value || !utf8_check_string(value, -1))
+        return NULL;
+
+    return json_string_nocheck(value);
+}
+
+const char *json_string_value(const json_t *json)
+{
+    if(!json_is_string(json))
+        return NULL;
+
+    return json_to_string(json)->value;
+}
+
+int json_string_set_nocheck(json_t *json, const char *value)
+{
+    char *dup;
+    json_string_t *string;
+
+    dup = strdup(value);
+    if(!dup)
+        return -1;
+
+    string = json_to_string(json);
+    free(string->value);
+    string->value = dup;
+
+    return 0;
+}
+
+int json_string_set(json_t *json, const char *value)
+{
+    if(!value || !utf8_check_string(value, -1))
+        return -1;
+
+    return json_string_set_nocheck(json, value);
+}
+
+static void json_delete_string(json_string_t *string)
+{
+    free(string->value);
+    free(string);
+}
+
+static int json_string_equal(json_t *string1, json_t *string2)
+{
+    return strcmp(json_string_value(string1), json_string_value(string2)) == 0;
+}
+
+static json_t *json_string_copy(json_t *string)
+{
+    return json_string_nocheck(json_string_value(string));
+}
+
+
+/*** integer ***/
+
+json_t *json_integer(int value)
+{
+    json_integer_t *integer = malloc(sizeof(json_integer_t));
+    if(!integer)
+        return NULL;
+    json_init(&integer->json, JSON_INTEGER);
+
+    integer->value = value;
+    return &integer->json;
+}
+
+int json_integer_value(const json_t *json)
+{
+    if(!json_is_integer(json))
+        return 0;
+
+    return json_to_integer(json)->value;
+}
+
+int json_integer_set(json_t *json, int value)
+{
+    if(!json_is_integer(json))
+        return -1;
+
+    json_to_integer(json)->value = value;
+
+    return 0;
+}
+
+static void json_delete_integer(json_integer_t *integer)
+{
+    free(integer);
+}
+
+static int json_integer_equal(json_t *integer1, json_t *integer2)
+{
+    return json_integer_value(integer1) == json_integer_value(integer2);
+}
+
+static json_t *json_integer_copy(json_t *integer)
+{
+    return json_integer(json_integer_value(integer));
+}
+
+
+/*** real ***/
+
+json_t *json_real(double value)
+{
+    json_real_t *real = malloc(sizeof(json_real_t));
+    if(!real)
+        return NULL;
+    json_init(&real->json, JSON_REAL);
+
+    real->value = value;
+    return &real->json;
+}
+
+double json_real_value(const json_t *json)
+{
+    if(!json_is_real(json))
+        return 0;
+
+    return json_to_real(json)->value;
+}
+
+int json_real_set(json_t *json, double value)
+{
+    if(!json_is_real(json))
+        return 0;
+
+    json_to_real(json)->value = value;
+
+    return 0;
+}
+
+static void json_delete_real(json_real_t *real)
+{
+    free(real);
+}
+
+static int json_real_equal(json_t *real1, json_t *real2)
+{
+    return json_real_value(real1) == json_real_value(real2);
+}
+
+static json_t *json_real_copy(json_t *real)
+{
+    return json_real(json_real_value(real));
+}
+
+
+/*** number ***/
+
+double json_number_value(const json_t *json)
+{
+    if(json_is_integer(json))
+        return json_integer_value(json);
+    else if(json_is_real(json))
+        return json_real_value(json);
+    else
+        return 0.0;
+}
+
+
+/*** simple values ***/
+
+json_t *json_true(void)
+{
+    static json_t the_true = {
+        .type = JSON_TRUE,
+        .refcount = (unsigned int)1
+    };
+    return &the_true;
+}
+
+
+json_t *json_false(void)
+{
+    static json_t the_false = {
+        .type = JSON_FALSE,
+        .refcount = (unsigned int)1
+    };
+    return &the_false;
+}
+
+
+json_t *json_null(void)
+{
+    static json_t the_null = {
+        .type = JSON_NULL,
+        .refcount = (unsigned int)1
+    };
+    return &the_null;
+}
+
+
+/*** deletion ***/
+
+void json_delete(json_t *json)
+{
+    if(json_is_object(json))
+        json_delete_object(json_to_object(json));
+
+    else if(json_is_array(json))
+        json_delete_array(json_to_array(json));
+
+    else if(json_is_string(json))
+        json_delete_string(json_to_string(json));
+
+    else if(json_is_integer(json))
+        json_delete_integer(json_to_integer(json));
+
+    else if(json_is_real(json))
+        json_delete_real(json_to_real(json));
+
+    /* json_delete is not called for true, false or null */
+}
+
+
+/*** equality ***/
+
+int json_equal(json_t *json1, json_t *json2)
+{
+    if(!json1 || !json2)
+        return 0;
+
+    if(json_typeof(json1) != json_typeof(json2))
+        return 0;
+
+    /* this covers true, false and null as they are singletons */
+    if(json1 == json2)
+        return 1;
+
+    if(json_is_object(json1))
+        return json_object_equal(json1, json2);
+
+    if(json_is_array(json1))
+        return json_array_equal(json1, json2);
+
+    if(json_is_string(json1))
+        return json_string_equal(json1, json2);
+
+    if(json_is_integer(json1))
+        return json_integer_equal(json1, json2);
+
+    if(json_is_real(json1))
+        return json_real_equal(json1, json2);
+
+    return 0;
+}
+
+
+/*** copying ***/
+
+json_t *json_copy(json_t *json)
+{
+    if(!json)
+        return NULL;
+
+    if(json_is_object(json))
+        return json_object_copy(json);
+
+    if(json_is_array(json))
+        return json_array_copy(json);
+
+    if(json_is_string(json))
+        return json_string_copy(json);
+
+    if(json_is_integer(json))
+        return json_integer_copy(json);
+
+    if(json_is_real(json))
+        return json_real_copy(json);
+
+    if(json_is_true(json) || json_is_false(json) || json_is_null(json))
+        return json;
+
+    return NULL;
+}
+
+json_t *json_deep_copy(json_t *json)
+{
+    if(!json)
+        return NULL;
+
+    if(json_is_object(json))
+        return json_object_deep_copy(json);
+
+    if(json_is_array(json))
+        return json_array_deep_copy(json);
+
+    /* for the rest of the types, deep copying doesn't differ from
+       shallow copying */
+
+    if(json_is_string(json))
+        return json_string_copy(json);
+
+    if(json_is_integer(json))
+        return json_integer_copy(json);
+
+    if(json_is_real(json))
+        return json_real_copy(json);
+
+    if(json_is_true(json) || json_is_false(json) || json_is_null(json))
+        return json;
+
+    return NULL;
+}
diff --git a/test/.gitignore b/test/.gitignore
new file mode 100644 (file)
index 0000000..a960c50
--- /dev/null
@@ -0,0 +1,9 @@
+logs
+bin/json_process
+suites/api/test_array
+suites/api/test_equal
+suites/api/test_copy
+suites/api/test_load
+suites/api/test_number
+suites/api/test_object
+suites/api/test_simple
diff --git a/test/Makefile.am b/test/Makefile.am
new file mode 100644 (file)
index 0000000..e468d22
--- /dev/null
@@ -0,0 +1,10 @@
+SUBDIRS = bin suites
+EXTRA_DIST = scripts
+
+TESTS = run-suites
+TESTS_ENVIRONMENT = \
+       top_srcdir=$(top_srcdir) \
+       top_builddir=$(top_builddir)
+
+clean-local:
+       rm -rf logs
diff --git a/test/bin/Makefile.am b/test/bin/Makefile.am
new file mode 100644 (file)
index 0000000..346db5d
--- /dev/null
@@ -0,0 +1,6 @@
+check_PROGRAMS = json_process
+
+AM_CPPFLAGS = -I$(top_srcdir)/src
+AM_CFLAGS = -Wall -Werror
+LDFLAGS = -static  # for speed and Valgrind
+LDADD = $(top_builddir)/src/libjansson.la
diff --git a/test/bin/json_process.c b/test/bin/json_process.c
new file mode 100644 (file)
index 0000000..794e307
--- /dev/null
@@ -0,0 +1,69 @@
+/*
+ * Copyright (c) 2009 Petri Lehtinen <petri@digip.org>
+ *
+ * Jansson is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <jansson.h>
+
+static int getenv_int(const char *name)
+{
+    char *value, *end;
+    long result;
+
+    value = getenv(name);
+    if(!value)
+        return 0;
+
+    result = strtol(value, &end, 10);
+    if(*end != '\0')
+        return 0;
+
+    return (int)result;
+}
+
+int main(int argc, char *argv[])
+{
+    int indent = 0;
+    unsigned int flags = 0;
+
+    json_t *json;
+    json_error_t error;
+
+    if(argc != 1) {
+        fprintf(stderr, "usage: %s\n", argv[0]);
+        return 2;
+    }
+
+    indent = getenv_int("JSON_INDENT");
+    if(indent < 0 || indent > 255) {
+        fprintf(stderr, "invalid value for JSON_INDENT: %d\n", indent);
+        return 2;
+    }
+
+    if(indent > 0)
+        flags |= JSON_INDENT(indent);
+
+    if(getenv_int("JSON_COMPACT") > 0)
+        flags |= JSON_COMPACT;
+
+    if(getenv_int("JSON_ENSURE_ASCII"))
+        flags |= JSON_ENSURE_ASCII;
+
+    if(getenv_int("JSON_SORT_KEYS"))
+        flags |= JSON_SORT_KEYS;
+
+    json = json_loadf(stdin, &error);
+    if(!json) {
+        fprintf(stderr, "%d\n%s\n", error.line, error.text);
+        return 1;
+    }
+
+    json_dumpf(json, stdout, flags);
+    json_decref(json);
+
+    return 0;
+}
diff --git a/test/run-suites b/test/run-suites
new file mode 100755 (executable)
index 0000000..e736004
--- /dev/null
@@ -0,0 +1,46 @@
+#!/bin/sh
+
+while [ -n "$1" ]; do
+    suite=$1
+    if [ -x $top_srcdir/test/suites/$suite/run ]; then
+        SUITES="$SUITES $suite"
+    else
+        echo "No such suite: $suite"
+        exit 1
+    fi
+    shift
+done
+
+if [ -z "$SUITES" ]; then
+    suitedirs=$top_srcdir/test/suites/*
+    for suitedir in $suitedirs; do
+        if [ -x $suitedir/run ]; then
+            SUITES="$SUITES `basename $suitedir`"
+        fi
+    done
+fi
+
+export suites_srcdir=$top_srcdir/test/suites
+export suites_builddir=suites
+export scriptdir=$top_srcdir/test/scripts
+export logdir=logs
+export bindir=bin
+
+passed=0
+failed=0
+for suite in $SUITES; do
+    echo "Suite: $suite"
+    if $suites_srcdir/$suite/run $suite; then
+        passed=$(($passed+1))
+    else
+        failed=$(($failed+1))
+    fi
+done
+
+if [ $failed -gt 0 ]; then
+    echo "$failed of $((passed+failed)) test suites failed"
+    exit 1
+else
+    echo "$passed test suites passed"
+    rm -rf $logdir
+fi
diff --git a/test/scripts/run-tests.sh b/test/scripts/run-tests.sh
new file mode 100644 (file)
index 0000000..2559766
--- /dev/null
@@ -0,0 +1,71 @@
+# Copyright (c) 2009 Petri Lehtinen <petri@digip.org>
+#
+# Jansson is free software; you can redistribute it and/or modify
+# it under the terms of the MIT license. See LICENSE for details.
+
+json_process=$bindir/json_process
+
+suite_name=$1
+suite_srcdir=$suites_srcdir/$suite_name
+suite_builddir=$suites_builddir/$suite_name
+suite_log=$logdir/$suite_name
+
+
+[ -z "$VERBOSE" ] && VERBOSE=0
+
+. $scriptdir/valgrind.sh
+
+rm -rf $suite_log
+mkdir -p $suite_log
+
+for test_path in $suite_srcdir/*; do
+    test_name=$(basename $test_path)
+    test_builddir=$suite_builddir/$test_name
+    test_log=$suite_log/$test_name
+
+    [ "$test_name" = "run" ] && continue
+    is_test || continue
+
+    rm -rf $test_log
+    mkdir -p $test_log
+    if [ $VERBOSE -eq 1 ]; then
+        echo -n "$name... "
+    fi
+
+    if run_test; then
+        # Success
+        if [ $VERBOSE -eq 1 ]; then
+            echo "ok"
+        else
+            echo -n "."
+        fi
+        rm -rf $test_log
+    else
+        # Failure
+        if [ $VERBOSE -eq 1 ]; then
+            echo "FAILED"
+        else
+            echo -n "F"
+        fi
+    fi
+done
+
+if [ $VERBOSE -eq 0 ]; then
+    echo
+fi
+
+if [ -n "$(ls -A $suite_log)" ]; then
+    for test_log in $suite_log/*; do
+        test_name=$(basename $test_log)
+        test_path=$suite_srcdir/$test_name
+        echo "================================================================="
+        echo "$suite_name/$test_name"
+        echo "================================================================="
+        show_error
+        echo
+    done
+    echo "================================================================="
+    exit 1
+else
+    rm -rf $suite_log
+fi
diff --git a/test/scripts/valgrind.sh b/test/scripts/valgrind.sh
new file mode 100644 (file)
index 0000000..39cda0d
--- /dev/null
@@ -0,0 +1,35 @@
+# Copyright (c) 2009 Petri Lehtinen <petri@digip.org>
+#
+# Jansson is free software; you can redistribute it and/or modify
+# it under the terms of the MIT license. See LICENSE for details.
+
+[ -z "$VALGRIND" ] && VALGRIND=0
+
+VALGRIND_CMDLINE="valgrind --leak-check=full --show-reachable=yes --track-origins=yes -q"
+
+if [ $VALGRIND -eq 1 ]; then
+    test_runner="$VALGRIND_CMDLINE"
+    json_process="$VALGRIND_CMDLINE $json_process"
+else
+    test_runner=""
+fi
+
+valgrind_check() {
+    if [ $VALGRIND -eq 1 ]; then
+        # Check for Valgrind error output. The valgrind option
+        # --error-exitcode is not enough because Valgrind doesn't
+        # think unfreed allocs are errors.
+        if grep -E -q '^==[0-9]+== ' $1; then
+            touch $test_log/valgrind_error
+            return 1
+        fi
+    fi
+}
+
+valgrind_show_error() {
+    if [ $VALGRIND -eq 1 -a -f $test_log/valgrind_error ]; then
+        echo "valgrind detected an error"
+        return 0
+    fi
+    return 1
+}
diff --git a/test/suites/Makefile.am b/test/suites/Makefile.am
new file mode 100644 (file)
index 0000000..db3443c
--- /dev/null
@@ -0,0 +1,2 @@
+SUBDIRS = api
+EXTRA_DIST = invalid invalid-strip invalid-unicode valid valid-strip
diff --git a/test/suites/api/Makefile.am b/test/suites/api/Makefile.am
new file mode 100644 (file)
index 0000000..772f75a
--- /dev/null
@@ -0,0 +1,20 @@
+check_PROGRAMS = \
+       test_array \
+       test_equal \
+       test_copy \
+       test_load \
+       test_simple \
+       test_number \
+       test_object
+
+test_array_SOURCES = test_array.c util.h
+test_copy_SOURCES = test_copy.c util.h
+test_load_SOURCES = test_load.c util.h
+test_simple_SOURCES = test_simple.c util.h
+test_number_SOURCES = test_number.c util.h
+test_object_SOURCES = test_object.c util.h
+
+AM_CPPFLAGS = -I$(top_srcdir)/src
+AM_CFLAGS = -Wall -Werror
+LDFLAGS = -static  # for speed and Valgrind
+LDADD = $(top_builddir)/src/libjansson.la
diff --git a/test/suites/api/check-exports b/test/suites/api/check-exports
new file mode 100755 (executable)
index 0000000..178abeb
--- /dev/null
@@ -0,0 +1,88 @@
+#!/bin/sh
+
+# This tests checks that the libjansson.so exports the correct
+# symbols.
+
+# The list of symbols that the shared object should export
+sort >$test_log/exports <<EOF
+json_delete
+json_true
+json_false
+json_null
+json_string
+json_string_nocheck
+json_string_value
+json_string_set
+json_string_set_nocheck
+json_integer
+json_integer_value
+json_integer_set
+json_real
+json_real_value
+json_real_set
+json_number_value
+json_array
+json_array_size
+json_array_get
+json_array_set_new
+json_array_append_new
+json_array_insert_new
+json_array_remove
+json_array_clear
+json_array_extend
+json_object
+json_object_size
+json_object_get
+json_object_set_new
+json_object_set_new_nocheck
+json_object_del
+json_object_clear
+json_object_update
+json_object_iter
+json_object_iter_next
+json_object_iter_key
+json_object_iter_value
+json_dumps
+json_dumpf
+json_dump_file
+json_loads
+json_loadf
+json_load_file
+json_equal
+json_copy
+json_deep_copy
+EOF
+
+# The list of functions are not exported in the library because they
+# are macros or static inline functions. This is only the make the
+# list complete, there are not used by the test.
+sort >$test_log/macros_or_inline <<EOF
+json_typeof
+json_incref
+json_decref
+json_is_object
+json_is_object
+json_is_array
+json_is_string
+json_is_integer
+json_is_real
+json_is_true
+json_is_false
+json_is_null
+json_is_number
+json_is_boolean
+json_array_set
+json_array_append
+json_array_insert
+json_object_set
+json_object_set_nocheck
+EOF
+
+SOFILE="../src/.libs/libjansson.so"
+
+nm -D $SOFILE | grep ' T ' | cut -d' ' -f3 | sort >$test_log/output
+
+if ! cmp -s $test_log/exports $test_log/output; then
+    diff -u $test_log/exports $test_log/output >&2
+    exit 1
+fi
diff --git a/test/suites/api/run b/test/suites/api/run
new file mode 100755 (executable)
index 0000000..02e92b0
--- /dev/null
@@ -0,0 +1,30 @@
+#!/bin/sh
+#
+# Copyright (c) 2009 Petri Lehtinen <petri@digip.org>
+#
+# Jansson is free software; you can redistribute it and/or modify
+# it under the terms of the MIT license. See LICENSE for details.
+
+is_test() {
+    [ "${test_name%.c}" != "$test_name" ] && return 0
+    [ -x $test_path -a ! -f $test_path.c ] && return 0
+    return 1
+}
+
+run_test() {
+    if [ -x $test_path ]; then
+        test_log=$test_log $test_path >$test_log/stdout 2>$test_log/stderr
+    else
+        $test_runner $suite_builddir/${test_name%.c} \
+            >$test_log/stdout \
+            2>$test_log/stderr
+        valgrind_check $test_log/stderr || return 1
+    fi
+}
+
+show_error() {
+    valgrind_show_error && return
+    cat $test_log/stderr
+}
+
+. $top_srcdir/test/scripts/run-tests.sh
similarity index 100%
rename from test.json
rename to test/suites/api/test.json
diff --git a/test/suites/api/test_array.c b/test/suites/api/test_array.c
new file mode 100644 (file)
index 0000000..45bfefd
--- /dev/null
@@ -0,0 +1,400 @@
+/*
+ * Copyright (c) 2009 Petri Lehtinen <petri@digip.org>
+ *
+ * Jansson is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include <jansson.h>
+#include "util.h"
+
+static void test_misc(void)
+{
+    json_t *array, *five, *seven, *value;
+    int i;
+
+    array = json_array();
+    five = json_integer(5);
+    seven = json_integer(7);
+
+    if(!array)
+        fail("unable to create array");
+    if(!five || !seven)
+        fail("unable to create integer");
+
+    if(json_array_size(array) != 0)
+        fail("empty array has nonzero size");
+
+    if(!json_array_append(array, NULL))
+        fail("able to append NULL");
+
+    if(json_array_append(array, five))
+        fail("unable to append");
+
+    if(json_array_size(array) != 1)
+        fail("wrong array size");
+
+    value = json_array_get(array, 0);
+    if(!value)
+        fail("unable to get item");
+    if(value != five)
+        fail("got wrong value");
+
+    if(json_array_append(array, seven))
+        fail("unable to append value");
+
+    if(json_array_size(array) != 2)
+        fail("wrong array size");
+
+    value = json_array_get(array, 1);
+    if(!value)
+        fail("unable to get item");
+    if(value != seven)
+        fail("got wrong value");
+
+    if(json_array_set(array, 0, seven))
+        fail("unable to set value");
+
+    if(!json_array_set(array, 0, NULL))
+        fail("able to set NULL");
+
+    if(json_array_size(array) != 2)
+        fail("wrong array size");
+
+    value = json_array_get(array, 0);
+    if(!value)
+        fail("unable to get item");
+    if(value != seven)
+        fail("got wrong value");
+
+    if(json_array_get(array, 2) != NULL)
+        fail("able to get value out of bounds");
+
+    if(!json_array_set(array, 2, seven))
+        fail("able to set value out of bounds");
+
+    for(i = 2; i < 30; i++) {
+        if(json_array_append(array, seven))
+            fail("unable to append value");
+
+        if(json_array_size(array) != i + 1)
+            fail("wrong array size");
+    }
+
+    for(i = 0; i < 30; i++) {
+        value = json_array_get(array, i);
+        if(!value)
+            fail("unable to get item");
+        if(value != seven)
+            fail("got wrong value");
+    }
+
+    if(json_array_set_new(array, 15, json_integer(123)))
+        fail("unable to set new value");
+
+    value = json_array_get(array, 15);
+    if(!json_is_integer(value) || json_integer_value(value) != 123)
+        fail("json_array_set_new works incorrectly");
+
+    if(!json_array_set_new(array, 15, NULL))
+        fail("able to set_new NULL value");
+
+    if(json_array_append_new(array, json_integer(321)))
+        fail("unable to append new value");
+
+    value = json_array_get(array, json_array_size(array) - 1);
+    if(!json_is_integer(value) || json_integer_value(value) != 321)
+        fail("json_array_append_new works incorrectly");
+
+    if(!json_array_append_new(array, NULL))
+        fail("able to append_new NULL value");
+
+    json_decref(five);
+    json_decref(seven);
+    json_decref(array);
+}
+
+static void test_insert(void)
+{
+    json_t *array, *five, *seven, *eleven, *value;
+    int i;
+
+    array = json_array();
+    five = json_integer(5);
+    seven = json_integer(7);
+    eleven = json_integer(11);
+
+    if(!array)
+        fail("unable to create array");
+    if(!five || !seven || !eleven)
+        fail("unable to create integer");
+
+
+    if(!json_array_insert(array, 1, five))
+        fail("able to insert value out of bounds");
+
+
+    if(json_array_insert(array, 0, five))
+        fail("unable to insert value in an empty array");
+
+    if(json_array_get(array, 0) != five)
+        fail("json_array_insert works incorrectly");
+
+    if(json_array_size(array) != 1)
+        fail("array size is invalid after insertion");
+
+
+    if(json_array_insert(array, 1, seven))
+        fail("unable to insert value at the end of an array");
+
+    if(json_array_get(array, 0) != five)
+        fail("json_array_insert works incorrectly");
+
+    if(json_array_get(array, 1) != seven)
+        fail("json_array_insert works incorrectly");
+
+    if(json_array_size(array) != 2)
+        fail("array size is invalid after insertion");
+
+
+    if(json_array_insert(array, 1, eleven))
+        fail("unable to insert value in the middle of an array");
+
+    if(json_array_get(array, 0) != five)
+        fail("json_array_insert works incorrectly");
+
+    if(json_array_get(array, 1) != eleven)
+        fail("json_array_insert works incorrectly");
+
+    if(json_array_get(array, 2) != seven)
+        fail("json_array_insert works incorrectly");
+
+    if(json_array_size(array) != 3)
+        fail("array size is invalid after insertion");
+
+
+    if(json_array_insert_new(array, 2, json_integer(123)))
+        fail("unable to insert value in the middle of an array");
+
+    value = json_array_get(array, 2);
+    if(!json_is_integer(value) || json_integer_value(value) != 123)
+        fail("json_array_insert_new works incorrectly");
+
+    if(json_array_size(array) != 4)
+        fail("array size is invalid after insertion");
+
+
+    for(i = 0; i < 20; i++) {
+        if(json_array_insert(array, 0, seven))
+            fail("unable to insert value at the begining of an array");
+    }
+
+    for(i = 0; i < 20; i++) {
+        if(json_array_get(array, i) != seven)
+            fail("json_aray_insert works incorrectly");
+    }
+
+    if(json_array_size(array) != 24)
+        fail("array size is invalid after loop insertion");
+
+    json_decref(five);
+    json_decref(seven);
+    json_decref(eleven);
+    json_decref(array);
+}
+
+static void test_remove(void)
+{
+    json_t *array, *five, *seven;
+
+    array = json_array();
+    five = json_integer(5);
+    seven = json_integer(7);
+
+    if(!array)
+        fail("unable to create array");
+    if(!five)
+        fail("unable to create integer");
+    if(!seven)
+        fail("unable to create integer");
+
+
+    if(!json_array_remove(array, 0))
+        fail("able to remove an unexisting index");
+
+
+    if(json_array_append(array, five))
+        fail("unable to append");
+
+    if(!json_array_remove(array, 1))
+        fail("able to remove an unexisting index");
+
+    if(json_array_remove(array, 0))
+        fail("unable to remove");
+
+    if(json_array_size(array) != 0)
+        fail("array size is invalid after removing");
+
+
+    if(json_array_append(array, five) ||
+       json_array_append(array, seven) ||
+       json_array_append(array, five) ||
+       json_array_append(array, seven))
+        fail("unable to append");
+
+    if(json_array_remove(array, 2))
+        fail("unable to remove");
+
+    if(json_array_size(array) != 3)
+        fail("array size is invalid after removing");
+
+    if(json_array_get(array, 0) != five ||
+       json_array_get(array, 1) != seven ||
+       json_array_get(array, 2) != seven)
+        fail("remove works incorrectly");
+
+    json_decref(five);
+    json_decref(seven);
+    json_decref(array);
+}
+
+static void test_clear(void)
+{
+    json_t *array, *five, *seven;
+    int i;
+
+    array = json_array();
+    five = json_integer(5);
+    seven = json_integer(7);
+
+    if(!array)
+        fail("unable to create array");
+    if(!five || !seven)
+        fail("unable to create integer");
+
+    for(i = 0; i < 10; i++) {
+        if(json_array_append(array, five))
+            fail("unable to append");
+    }
+    for(i = 0; i < 10; i++) {
+        if(json_array_append(array, seven))
+            fail("unable to append");
+    }
+
+    if(json_array_size(array) != 20)
+        fail("array size is invalid after appending");
+
+    if(json_array_clear(array))
+        fail("unable to clear");
+
+    if(json_array_size(array) != 0)
+        fail("array size is invalid after clearing");
+
+    json_decref(five);
+    json_decref(seven);
+    json_decref(array);
+}
+
+static void test_extend(void)
+{
+    json_t *array1, *array2, *five, *seven;
+    int i;
+
+    array1 = json_array();
+    array2 = json_array();
+    five = json_integer(5);
+    seven = json_integer(7);
+
+    if(!array1 || !array2)
+        fail("unable to create array");
+    if(!five || !seven)
+        fail("unable to create integer");
+
+    for(i = 0; i < 10; i++) {
+        if(json_array_append(array1, five))
+            fail("unable to append");
+    }
+    for(i = 0; i < 10; i++) {
+        if(json_array_append(array2, seven))
+            fail("unable to append");
+    }
+
+    if(json_array_size(array1) != 10 || json_array_size(array2) != 10)
+        fail("array size is invalid after appending");
+
+    if(json_array_extend(array1, array2))
+        fail("unable to extend");
+
+    for(i = 0; i < 10; i++) {
+        if(json_array_get(array1, i) != five)
+            fail("invalid array contents after extending");
+    }
+    for(i = 10; i < 20; i++) {
+        if(json_array_get(array1, i) != seven)
+            fail("invalid array contents after extending");
+    }
+
+    json_decref(five);
+    json_decref(seven);
+    json_decref(array1);
+    json_decref(array2);
+}
+
+static void test_circular()
+{
+    json_t *array1, *array2;
+
+    /* the simple cases are checked */
+
+    array1 = json_array();
+    if(!array1)
+        fail("unable to create array");
+
+    if(json_array_append(array1, array1) == 0)
+        fail("able to append self");
+
+    if(json_array_insert(array1, 0, array1) == 0)
+        fail("able to insert self");
+
+    if(json_array_append_new(array1, json_true()))
+        fail("failed to append true");
+
+    if(json_array_set(array1, 0, array1) == 0)
+        fail("able to set self");
+
+    json_decref(array1);
+
+
+    /* create circular references */
+
+    array1 = json_array();
+    array2 = json_array();
+    if(!array1 || !array2)
+        fail("unable to create array");
+
+    if(json_array_append(array1, array2) ||
+       json_array_append(array2, array1))
+        fail("unable to append");
+
+    /* circularity is detected when dumping */
+    if(json_dumps(array1, 0) != NULL)
+        fail("able to dump circulars");
+
+    /* decref twice to deal with the circular references */
+    json_decref(array1);
+    json_decref(array2);
+    json_decref(array1);
+}
+
+
+int main()
+{
+    test_misc();
+    test_insert();
+    test_remove();
+    test_clear();
+    test_extend();
+    test_circular();
+
+    return 0;
+}
diff --git a/test/suites/api/test_copy.c b/test/suites/api/test_copy.c
new file mode 100644 (file)
index 0000000..9452183
--- /dev/null
@@ -0,0 +1,319 @@
+/*
+ * Copyright (c) 2009 Petri Lehtinen <petri@digip.org>
+ *
+ * Jansson is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include <string.h>
+#include <jansson.h>
+#include "util.h"
+
+static void test_copy_simple(void)
+{
+    json_t *value, *copy;
+
+    if(json_copy(NULL))
+        fail("copying NULL doesn't return NULL");
+
+    /* true */
+    value = json_true();
+    copy = json_copy(value);
+    if(value != copy)
+        fail("copying true failed");
+    json_decref(value);
+    json_decref(copy);
+
+    /* false */
+    value = json_false();
+    copy = json_copy(value);
+    if(value != copy)
+        fail("copying false failed");
+    json_decref(value);
+    json_decref(copy);
+
+    /* null */
+    value = json_null();
+    copy = json_copy(value);
+    if(value != copy)
+        fail("copying null failed");
+    json_decref(value);
+    json_decref(copy);
+
+    /* string */
+    value = json_string("foo");
+    if(!value)
+        fail("unable to create a string");
+    copy = json_copy(value);
+    if(!copy)
+        fail("unable to copy a string");
+    if(copy == value)
+        fail("copying a string doesn't copy");
+    if(!json_equal(copy, value))
+        fail("copying a string produces an inequal copy");
+    if(value->refcount != 1 || copy->refcount != 1)
+        fail("invalid refcounts");
+    json_decref(value);
+    json_decref(copy);
+
+    /* integer */
+    value = json_integer(543);
+    if(!value)
+        fail("unable to create an integer");
+    copy = json_copy(value);
+    if(!copy)
+        fail("unable to copy an integer");
+    if(copy == value)
+        fail("copying an integer doesn't copy");
+    if(!json_equal(copy, value))
+        fail("copying an integer produces an inequal copy");
+    if(value->refcount != 1 || copy->refcount != 1)
+        fail("invalid refcounts");
+    json_decref(value);
+    json_decref(copy);
+
+    /* real */
+    value = json_real(123e9);
+    if(!value)
+        fail("unable to create a real");
+    copy = json_copy(value);
+    if(!copy)
+        fail("unable to copy a real");
+    if(copy == value)
+        fail("copying a real doesn't copy");
+    if(!json_equal(copy, value))
+        fail("copying a real produces an inequal copy");
+    if(value->refcount != 1 || copy->refcount != 1)
+        fail("invalid refcounts");
+    json_decref(value);
+    json_decref(copy);
+}
+
+static void test_deep_copy_simple(void)
+{
+    json_t *value, *copy;
+
+    if(json_deep_copy(NULL))
+        fail("deep copying NULL doesn't return NULL");
+
+    /* true */
+    value = json_true();
+    copy = json_deep_copy(value);
+    if(value != copy)
+        fail("deep copying true failed");
+    json_decref(value);
+    json_decref(copy);
+
+    /* false */
+    value = json_false();
+    copy = json_deep_copy(value);
+    if(value != copy)
+        fail("deep copying false failed");
+    json_decref(value);
+    json_decref(copy);
+
+    /* null */
+    value = json_null();
+    copy = json_deep_copy(value);
+    if(value != copy)
+        fail("deep copying null failed");
+    json_decref(value);
+    json_decref(copy);
+
+    /* string */
+    value = json_string("foo");
+    if(!value)
+        fail("unable to create a string");
+    copy = json_deep_copy(value);
+    if(!copy)
+        fail("unable to deep copy a string");
+    if(copy == value)
+        fail("deep copying a string doesn't copy");
+    if(!json_equal(copy, value))
+        fail("deep copying a string produces an inequal copy");
+    if(value->refcount != 1 || copy->refcount != 1)
+        fail("invalid refcounts");
+    json_decref(value);
+    json_decref(copy);
+
+    /* integer */
+    value = json_integer(543);
+    if(!value)
+        fail("unable to create an integer");
+    copy = json_deep_copy(value);
+    if(!copy)
+        fail("unable to deep copy an integer");
+    if(copy == value)
+        fail("deep copying an integer doesn't copy");
+    if(!json_equal(copy, value))
+        fail("deep copying an integer produces an inequal copy");
+    if(value->refcount != 1 || copy->refcount != 1)
+        fail("invalid refcounts");
+    json_decref(value);
+    json_decref(copy);
+
+    /* real */
+    value = json_real(123e9);
+    if(!value)
+        fail("unable to create a real");
+    copy = json_deep_copy(value);
+    if(!copy)
+        fail("unable to deep copy a real");
+    if(copy == value)
+        fail("deep copying a real doesn't copy");
+    if(!json_equal(copy, value))
+        fail("deep copying a real produces an inequal copy");
+    if(value->refcount != 1 || copy->refcount != 1)
+        fail("invalid refcounts");
+    json_decref(value);
+    json_decref(copy);
+}
+
+static void test_copy_array(void)
+{
+    const char *json_array_text = "[1, \"foo\", 3.141592, {\"foo\": \"bar\"}]";
+
+    json_t *array, *copy;
+    unsigned int i;
+
+    array = json_loads(json_array_text, NULL);
+    if(!array)
+        fail("unable to parse an array");
+
+    copy = json_copy(array);
+    if(!copy)
+        fail("unable to copy an array");
+    if(copy == array)
+        fail("copying an array doesn't copy");
+    if(!json_equal(copy, array))
+        fail("copying an array produces an inequal copy");
+
+    for(i = 0; i < json_array_size(copy); i++)
+    {
+        if(json_array_get(array, i) != json_array_get(copy, i))
+            fail("copying an array modifies its elements");
+    }
+
+    json_decref(array);
+    json_decref(copy);
+}
+
+static void test_deep_copy_array(void)
+{
+    const char *json_array_text = "[1, \"foo\", 3.141592, {\"foo\": \"bar\"}]";
+
+    json_t *array, *copy;
+    unsigned int i;
+
+    array = json_loads(json_array_text, NULL);
+    if(!array)
+        fail("unable to parse an array");
+
+    copy = json_deep_copy(array);
+    if(!copy)
+        fail("unable to deep copy an array");
+    if(copy == array)
+        fail("deep copying an array doesn't copy");
+    if(!json_equal(copy, array))
+        fail("deep copying an array produces an inequal copy");
+
+    for(i = 0; i < json_array_size(copy); i++)
+    {
+        if(json_array_get(array, i) == json_array_get(copy, i))
+            fail("deep copying an array doesn't copy its elements");
+    }
+
+    json_decref(array);
+    json_decref(copy);
+}
+
+static void test_copy_object(void)
+{
+    const char *json_object_text =
+        "{\"foo\": \"bar\", \"a\": 1, \"b\": 3.141592, \"c\": [1,2,3,4]}";
+
+    json_t *object, *copy;
+    void *iter;
+
+    object = json_loads(json_object_text, NULL);
+    if(!object)
+        fail("unable to parse an object");
+
+    copy = json_copy(object);
+    if(!copy)
+        fail("unable to copy an object");
+    if(copy == object)
+        fail("copying an object doesn't copy");
+    if(!json_equal(copy, object))
+        fail("copying an object produces an inequal copy");
+
+    iter = json_object_iter(object);
+    while(iter)
+    {
+        const char *key;
+        json_t *value1, *value2;
+
+        key = json_object_iter_key(iter);
+        value1 = json_object_iter_value(iter);
+        value2 = json_object_get(copy, key);
+
+        if(value1 != value2)
+            fail("deep copying an object modifies its items");
+
+        iter = json_object_iter_next(object, iter);
+    }
+
+    json_decref(object);
+    json_decref(copy);
+}
+
+static void test_deep_copy_object(void)
+{
+    const char *json_object_text =
+        "{\"foo\": \"bar\", \"a\": 1, \"b\": 3.141592, \"c\": [1,2,3,4]}";
+
+    json_t *object, *copy;
+    void *iter;
+
+    object = json_loads(json_object_text, NULL);
+    if(!object)
+        fail("unable to parse an object");
+
+    copy = json_deep_copy(object);
+    if(!copy)
+        fail("unable to deep copy an object");
+    if(copy == object)
+        fail("deep copying an object doesn't copy");
+    if(!json_equal(copy, object))
+        fail("deep copying an object produces an inequal copy");
+
+    iter = json_object_iter(object);
+    while(iter)
+    {
+        const char *key;
+        json_t *value1, *value2;
+
+        key = json_object_iter_key(iter);
+        value1 = json_object_iter_value(iter);
+        value2 = json_object_get(copy, key);
+
+        if(value1 == value2)
+            fail("deep copying an object doesn't copy its items");
+
+        iter = json_object_iter_next(object, iter);
+    }
+
+    json_decref(object);
+    json_decref(copy);
+}
+
+int main()
+{
+    test_copy_simple();
+    test_deep_copy_simple();
+    test_copy_array();
+    test_deep_copy_array();
+    test_copy_object();
+    test_deep_copy_object();
+    return 0;
+}
similarity index 100%
rename from test.cpp
rename to test/suites/api/test_cpp.cpp
diff --git a/test/suites/api/test_equal.c b/test/suites/api/test_equal.c
new file mode 100644 (file)
index 0000000..ab2edcd
--- /dev/null
@@ -0,0 +1,190 @@
+/*
+ * Copyright (c) 2009 Petri Lehtinen <petri@digip.org>
+ *
+ * Jansson is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include <jansson.h>
+#include "util.h"
+
+static void test_equal_simple()
+{
+    json_t *value1, *value2;
+
+    if(json_equal(NULL, NULL))
+        fail("json_equal fails for two NULLs");
+
+    value1 = json_true();
+    if(json_equal(value1, NULL) || json_equal(NULL, value1))
+        fail("json_equal fails for NULL");
+
+    /* this covers true, false and null as they are singletons */
+    if(!json_equal(value1, value1))
+        fail("identical objects are not equal");
+    json_decref(value1);
+
+    /* integer */
+    value1 = json_integer(1);
+    value2 = json_integer(1);
+    if(!value1 || !value2)
+        fail("unable to create integers");
+    if(!json_equal(value1, value2))
+        fail("json_equal fails for two equal integers");
+    json_decref(value2);
+
+    value2 = json_integer(2);
+    if(!value2)
+        fail("unable to create an integer");
+    if(json_equal(value1, value2))
+        fail("json_equal fails for two inequal integers");
+
+    json_decref(value1);
+    json_decref(value2);
+
+    /* real */
+    value1 = json_real(1.2);
+    value2 = json_real(1.2);
+    if(!value1 || !value2)
+        fail("unable to create reals");
+    if(!json_equal(value1, value2))
+        fail("json_equal fails for two equal reals");
+    json_decref(value2);
+
+    value2 = json_real(3.141592);
+    if(!value2)
+        fail("unable to create an real");
+    if(json_equal(value1, value2))
+        fail("json_equal fails for two inequal reals");
+
+    json_decref(value1);
+    json_decref(value2);
+
+    /* string */
+    value1 = json_string("foo");
+    value2 = json_string("foo");
+    if(!value1 || !value2)
+        fail("unable to create strings");
+    if(!json_equal(value1, value2))
+        fail("json_equal fails for two equal strings");
+    json_decref(value2);
+
+    value2 = json_string("bar");
+    if(!value2)
+        fail("unable to create an string");
+    if(json_equal(value1, value2))
+        fail("json_equal fails for two inequal strings");
+
+    json_decref(value1);
+    json_decref(value2);
+}
+
+static void test_equal_array()
+{
+    json_t *array1, *array2;
+
+    array1 = json_array();
+    array2 = json_array();
+    if(!array1 || !array2)
+        fail("unable to create arrays");
+
+    if(!json_equal(array1, array2))
+        fail("json_equal fails for two empty arrays");
+
+    json_array_append_new(array1, json_integer(1));
+    json_array_append_new(array2, json_integer(1));
+    json_array_append_new(array1, json_string("foo"));
+    json_array_append_new(array2, json_string("foo"));
+    json_array_append_new(array1, json_integer(2));
+    json_array_append_new(array2, json_integer(2));
+    if(!json_equal(array1, array2))
+        fail("json_equal fails for two equal arrays");
+
+    json_array_remove(array2, 2);
+    if(json_equal(array1, array2))
+        fail("json_equal fails for two inequal arrays");
+
+    json_array_append_new(array2, json_integer(3));
+    if(json_equal(array1, array2))
+        fail("json_equal fails for two inequal arrays");
+
+    json_decref(array1);
+    json_decref(array2);
+}
+
+static void test_equal_object()
+{
+    json_t *object1, *object2;
+
+    object1 = json_object();
+    object2 = json_object();
+    if(!object1 || !object2)
+        fail("unable to create objects");
+
+    if(!json_equal(object1, object2))
+        fail("json_equal fails for two empty objects");
+
+    json_object_set_new(object1, "a", json_integer(1));
+    json_object_set_new(object2, "a", json_integer(1));
+    json_object_set_new(object1, "b", json_string("foo"));
+    json_object_set_new(object2, "b", json_string("foo"));
+    json_object_set_new(object1, "c", json_integer(2));
+    json_object_set_new(object2, "c", json_integer(2));
+    if(!json_equal(object1, object2))
+        fail("json_equal fails for two equal objects");
+
+    json_object_del(object2, "c");
+    if(json_equal(object1, object2))
+        fail("json_equal fails for two inequal objects");
+
+    json_object_set_new(object2, "c", json_integer(3));
+    if(json_equal(object1, object2))
+        fail("json_equal fails for two inequal objects");
+
+    json_object_del(object2, "c");
+    json_object_set_new(object2, "d", json_integer(2));
+    if(json_equal(object1, object2))
+        fail("json_equal fails for two inequal objects");
+
+    json_decref(object1);
+    json_decref(object2);
+}
+
+static void test_equal_complex()
+{
+    json_t *value1, *value2;
+
+    const char *complex_json =
+"{"
+"    \"integer\": 1, "
+"    \"real\": 3.141592, "
+"    \"string\": \"foobar\", "
+"    \"true\": true, "
+"    \"object\": {"
+"        \"array-in-object\": [1,true,\"foo\",{}],"
+"        \"object-in-object\": {\"foo\": \"bar\"}"
+"    },"
+"    \"array\": [\"foo\", false, null, 1.234]"
+"}";
+
+    value1 = json_loads(complex_json, NULL);
+    value2 = json_loads(complex_json, NULL);
+    if(!value1 || !value2)
+        fail("unable to parse JSON");
+    if(!json_equal(value1, value2))
+        fail("json_equal fails for two inequal strings");
+
+    json_decref(value1);
+    json_decref(value2);
+
+    /* TODO: There's no negative test case here */
+}
+
+int main()
+{
+    test_equal_simple();
+    test_equal_array();
+    test_equal_object();
+    test_equal_complex();
+    return 0;
+}
diff --git a/test/suites/api/test_load.c b/test/suites/api/test_load.c
new file mode 100644 (file)
index 0000000..4d8fa88
--- /dev/null
@@ -0,0 +1,24 @@
+/*
+ * Copyright (c) 2009 Petri Lehtinen <petri@digip.org>
+ *
+ * Jansson is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include <jansson.h>
+#include <string.h>
+#include "util.h"
+
+int main()
+{
+    json_t *json;
+    json_error_t error;
+
+    json = json_load_file("/path/to/nonexistent/file.json", &error);
+    if(error.line != -1)
+        fail("json_load_file returned an invalid line number");
+    if(strcmp(error.text, "unable to open /path/to/nonexistent/file.json: No such file or directory") != 0)
+        fail("json_load_file returned an invalid error message");
+
+    return 0;
+}
diff --git a/test/suites/api/test_number.c b/test/suites/api/test_number.c
new file mode 100644 (file)
index 0000000..a634125
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2009 Petri Lehtinen <petri@digip.org>
+ *
+ * Jansson is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include <jansson.h>
+#include "util.h"
+
+int main()
+{
+    json_t *integer, *real;
+    int i;
+    double d;
+
+    integer = json_integer(5);
+    real = json_real(100.1);
+
+    if(!integer)
+        fail("unable to create integer");
+    if(!real)
+        fail("unable to create real");
+
+    i = json_integer_value(integer);
+    if(i != 5)
+        fail("wrong integer value");
+
+    d = json_real_value(real);
+    if(d != 100.1)
+        fail("wrong real value");
+
+    d = json_number_value(integer);
+    if(d != 5.0)
+        fail("wrong number value");
+    d = json_number_value(real);
+    if(d != 100.1)
+        fail("wrong number value");
+
+    json_decref(integer);
+    json_decref(real);
+
+    return 0;
+}
diff --git a/test/suites/api/test_object.c b/test/suites/api/test_object.c
new file mode 100644 (file)
index 0000000..7e9ada8
--- /dev/null
@@ -0,0 +1,337 @@
+/*
+ * Copyright (c) 2009 Petri Lehtinen <petri@digip.org>
+ *
+ * Jansson is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include <jansson.h>
+#include <string.h>
+#include "util.h"
+
+static void test_clear()
+{
+    json_t *object, *ten;
+
+    object = json_object();
+    ten = json_integer(10);
+
+    if(!object)
+        fail("unable to create object");
+    if(!ten)
+        fail("unable to create integer");
+
+    if(json_object_set(object, "a", ten) ||
+       json_object_set(object, "b", ten) ||
+       json_object_set(object, "c", ten) ||
+       json_object_set(object, "d", ten) ||
+       json_object_set(object, "e", ten))
+        fail("unable to set value");
+
+    if(json_object_size(object) != 5)
+        fail("invalid size");
+
+    json_object_clear(object);
+
+    if(json_object_size(object) != 0)
+        fail("invalid size after clear");
+
+    json_decref(ten);
+    json_decref(object);
+}
+
+static void test_update()
+{
+    json_t *object, *other, *nine, *ten;
+
+    object = json_object();
+    other = json_object();
+
+    nine = json_integer(9);
+    ten = json_integer(10);
+
+    if(!object || !other)
+        fail("unable to create object");
+    if(!nine || !ten)
+        fail("unable to create integer");
+
+
+    /* update an empty object with an empty object */
+
+    if(json_object_update(object, other))
+        fail("unable to update an emtpy object with an empty object");
+
+    if(json_object_size(object) != 0)
+        fail("invalid size after update");
+
+    if(json_object_size(other) != 0)
+        fail("invalid size for updater after update");
+
+
+    /* update an empty object with a nonempty object */
+
+    if(json_object_set(other, "a", ten) ||
+       json_object_set(other, "b", ten) ||
+       json_object_set(other, "c", ten) ||
+       json_object_set(other, "d", ten) ||
+       json_object_set(other, "e", ten))
+        fail("unable to set value");
+
+    if(json_object_update(object, other))
+        fail("unable to update an empty object");
+
+    if(json_object_size(object) != 5)
+        fail("invalid size after update");
+
+    if(json_object_get(object, "a") != ten ||
+       json_object_get(object, "b") != ten ||
+       json_object_get(object, "c") != ten ||
+       json_object_get(object, "d") != ten ||
+       json_object_get(object, "e") != ten)
+        fail("update works incorrectly");
+
+
+    /* perform the same update again */
+
+    if(json_object_update(object, other))
+        fail("unable to update an empty object");
+
+    if(json_object_size(object) != 5)
+        fail("invalid size after update");
+
+    if(json_object_get(object, "a") != ten ||
+       json_object_get(object, "b") != ten ||
+       json_object_get(object, "c") != ten ||
+       json_object_get(object, "d") != ten ||
+       json_object_get(object, "e") != ten)
+        fail("update works incorrectly");
+
+
+    /* update a nonempty object with a nonempty object with both old
+       and new keys */
+
+    if(json_object_clear(other))
+        fail("clear failed");
+
+    if(json_object_set(other, "a", nine) ||
+       json_object_set(other, "b", nine) ||
+       json_object_set(other, "f", nine) ||
+       json_object_set(other, "g", nine) ||
+       json_object_set(other, "h", nine))
+        fail("unable to set value");
+
+    if(json_object_update(object, other))
+        fail("unable to update a nonempty object");
+
+    if(json_object_size(object) != 8)
+        fail("invalid size after update");
+
+    if(json_object_get(object, "a") != nine ||
+       json_object_get(object, "b") != nine ||
+       json_object_get(object, "f") != nine ||
+       json_object_get(object, "g") != nine ||
+       json_object_get(object, "h") != nine)
+        fail("update works incorrectly");
+
+    json_decref(nine);
+    json_decref(ten);
+    json_decref(other);
+    json_decref(object);
+}
+
+static void test_circular()
+{
+    json_t *object1, *object2;
+
+    object1 = json_object();
+    object2 = json_object();
+    if(!object1 || !object2)
+        fail("unable to create object");
+
+    /* the simple case is checked */
+    if(json_object_set(object1, "a", object1) == 0)
+        fail("able to set self");
+
+    /* create circular references */
+    if(json_object_set(object1, "a", object2) ||
+       json_object_set(object2, "a", object1))
+        fail("unable to set value");
+
+    /* circularity is detected when dumping */
+    if(json_dumps(object1, 0) != NULL)
+        fail("able to dump circulars");
+
+    /* decref twice to deal with the circular references */
+    json_decref(object1);
+    json_decref(object2);
+    json_decref(object1);
+}
+
+static void test_set_nocheck()
+{
+    json_t *object, *string;
+
+    object = json_object();
+    string = json_string("bar");
+
+    if(!object)
+        fail("unable to create object");
+    if(!string)
+        fail("unable to create string");
+
+    if(json_object_set_nocheck(object, "foo", string))
+        fail("json_object_set_nocheck failed");
+    if(json_object_get(object, "foo") != string)
+        fail("json_object_get after json_object_set_nocheck failed");
+
+    /* invalid UTF-8 in key */
+    if(json_object_set_nocheck(object, "a\xefz", string))
+        fail("json_object_set_nocheck failed for invalid UTF-8");
+    if(json_object_get(object, "a\xefz") != string)
+        fail("json_object_get after json_object_set_nocheck failed");
+
+    if(json_object_set_new_nocheck(object, "bax", json_integer(123)))
+        fail("json_object_set_new_nocheck failed");
+    if(json_integer_value(json_object_get(object, "bax")) != 123)
+        fail("json_object_get after json_object_set_new_nocheck failed");
+
+    /* invalid UTF-8 in key */
+    if(json_object_set_new_nocheck(object, "asdf\xfe", json_integer(321)))
+        fail("json_object_set_new_nocheck failed for invalid UTF-8");
+    if(json_integer_value(json_object_get(object, "asdf\xfe")) != 321)
+        fail("json_object_get after json_object_set_new_nocheck failed");
+
+    json_decref(string);
+    json_decref(object);
+}
+
+static void test_misc()
+{
+    json_t *object, *string, *other_string, *value;
+    void *iter;
+
+    object = json_object();
+    string = json_string("test");
+    other_string = json_string("other");
+
+    if(!object)
+        fail("unable to create object");
+    if(!string || !other_string)
+        fail("unable to create string");
+
+    if(json_object_get(object, "a"))
+        fail("value for nonexisting key");
+
+    if(json_object_set(object, "a", string))
+        fail("unable to set value");
+
+    if(!json_object_set(object, NULL, string))
+        fail("able to set NULL key");
+
+    if(!json_object_set(object, "a", NULL))
+        fail("able to set NULL value");
+
+    iter = json_object_iter(object);
+    if(!iter)
+        fail("unable to get iterator");
+
+    if(strcmp(json_object_iter_key(iter), "a"))
+        fail("iterating failed: wrong key");
+    if(json_object_iter_value(iter) != string)
+        fail("iterating failed: wrong value");
+    if(json_object_iter_next(object, iter) != NULL)
+        fail("able to iterate over the end");
+
+    /* invalid UTF-8 in key */
+    if(!json_object_set(object, "a\xefz", string))
+        fail("able to set invalid unicode key");
+
+    value = json_object_get(object, "a");
+    if(!value)
+        fail("no value for existing key");
+    if(value != string)
+        fail("got different value than what was added");
+
+    /* "a", "lp" and "px" collide in a five-bucket hashtable */
+    if(json_object_set(object, "b", string) ||
+       json_object_set(object, "lp", string) ||
+       json_object_set(object, "px", string))
+        fail("unable to set value");
+
+    value = json_object_get(object, "a");
+    if(!value)
+        fail("no value for existing key");
+    if(value != string)
+        fail("got different value than what was added");
+
+    if(json_object_set(object, "a", other_string))
+        fail("unable to replace an existing key");
+
+    value = json_object_get(object, "a");
+    if(!value)
+        fail("no value for existing key");
+    if(value != other_string)
+        fail("got different value than what was set");
+
+    if(!json_object_del(object, "nonexisting"))
+        fail("able to delete a nonexisting key");
+
+    if(json_object_del(object, "px"))
+        fail("unable to delete an existing key");
+
+    if(json_object_del(object, "a"))
+        fail("unable to delete an existing key");
+
+    if(json_object_del(object, "lp"))
+        fail("unable to delete an existing key");
+
+
+    /* add many keys to initiate rehashing */
+
+    if(json_object_set(object, "a", string))
+        fail("unable to set value");
+
+    if(json_object_set(object, "lp", string))
+        fail("unable to set value");
+
+    if(json_object_set(object, "px", string))
+        fail("unable to set value");
+
+    if(json_object_set(object, "c", string))
+        fail("unable to set value");
+
+    if(json_object_set(object, "d", string))
+        fail("unable to set value");
+
+    if(json_object_set(object, "e", string))
+        fail("unable to set value");
+
+
+    if(json_object_set_new(object, "foo", json_integer(123)))
+        fail("unable to set new value");
+
+    value = json_object_get(object, "foo");
+    if(!json_is_integer(value) || json_integer_value(value) != 123)
+        fail("json_object_set_new works incorrectly");
+
+    if(!json_object_set_new(object, NULL, json_integer(432)))
+        fail("able to set_new NULL key");
+
+    if(!json_object_set_new(object, "foo", NULL))
+        fail("able to set_new NULL value");
+
+    json_decref(string);
+    json_decref(other_string);
+    json_decref(object);
+}
+
+int main()
+{
+    test_misc();
+    test_clear();
+    test_update();
+    test_circular();
+    test_set_nocheck();
+
+    return 0;
+}
diff --git a/test/suites/api/test_simple.c b/test/suites/api/test_simple.c
new file mode 100644 (file)
index 0000000..0284879
--- /dev/null
@@ -0,0 +1,154 @@
+/*
+ * Copyright (c) 2009 Petri Lehtinen <petri@digip.org>
+ *
+ * Jansson is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include <string.h>
+#include <jansson.h>
+#include "util.h"
+
+/* Call the simple functions not covered by other tests of the public API */
+int main()
+{
+    json_t *value;
+
+    value = json_integer(1);
+    if(json_typeof(value) != JSON_INTEGER)
+        fail("json_typeof failed");
+
+    if(json_is_object(value))
+        fail("json_is_object failed");
+
+    if(json_is_array(value))
+        fail("json_is_array failed");
+
+    if(json_is_string(value))
+        fail("json_is_string failed");
+
+    if(!json_is_integer(value))
+        fail("json_is_integer failed");
+
+    if(json_is_real(value))
+        fail("json_is_real failed");
+
+    if(!json_is_number(value))
+        fail("json_is_number failed");
+
+    if(json_is_true(value))
+        fail("json_is_true failed");
+
+    if(json_is_false(value))
+        fail("json_is_false failed");
+
+    if(json_is_boolean(value))
+        fail("json_is_boolean failed");
+
+    if(json_is_null(value))
+        fail("json_is_null failed");
+
+    json_decref(value);
+
+
+    value = json_string("foo");
+    if(!value)
+        fail("json_string failed");
+    if(strcmp(json_string_value(value), "foo"))
+        fail("invalid string value");
+
+    if(json_string_set(value, "bar"))
+        fail("json_string_set failed");
+    if(strcmp(json_string_value(value), "bar"))
+        fail("invalid string value");
+
+    json_decref(value);
+
+    value = json_string(NULL);
+    if(value)
+        fail("json_string(NULL) failed");
+
+    /* invalid UTF-8  */
+    value = json_string("a\xefz");
+    if(value)
+        fail("json_string(<invalid utf-8>) failed");
+
+    value = json_string_nocheck("foo");
+    if(!value)
+        fail("json_string_nocheck failed");
+    if(strcmp(json_string_value(value), "foo"))
+        fail("invalid string value");
+
+    if(json_string_set_nocheck(value, "bar"))
+        fail("json_string_set_nocheck failed");
+    if(strcmp(json_string_value(value), "bar"))
+        fail("invalid string value");
+
+    json_decref(value);
+
+    /* invalid UTF-8 */
+    value = json_string_nocheck("qu\xff");
+    if(!value)
+        fail("json_string_nocheck failed");
+    if(strcmp(json_string_value(value), "qu\xff"))
+        fail("invalid string value");
+
+    if(json_string_set_nocheck(value, "\xfd\xfe\xff"))
+        fail("json_string_set_nocheck failed");
+    if(strcmp(json_string_value(value), "\xfd\xfe\xff"))
+        fail("invalid string value");
+
+    json_decref(value);
+
+
+    value = json_integer(123);
+    if(!value)
+        fail("json_integer failed");
+    if(json_integer_value(value) != 123)
+        fail("invalid integer value");
+    if(json_number_value(value) != 123.0)
+        fail("invalid number value");
+
+    if(json_integer_set(value, 321))
+        fail("json_integer_set failed");
+    if(json_integer_value(value) != 321)
+        fail("invalid integer value");
+    if(json_number_value(value) != 321.0)
+        fail("invalid number value");
+
+    json_decref(value);
+
+    value = json_real(123.123);
+    if(!value)
+        fail("json_real failed");
+    if(json_real_value(value) != 123.123)
+        fail("invalid integer value");
+    if(json_number_value(value) != 123.123)
+        fail("invalid number value");
+
+    if(json_real_set(value, 321.321))
+        fail("json_real_set failed");
+    if(json_real_value(value) != 321.321)
+        fail("invalid real value");
+    if(json_number_value(value) != 321.321)
+        fail("invalid number value");
+
+    json_decref(value);
+
+    value = json_true();
+    if(!value)
+        fail("json_true failed");
+    json_decref(value);
+
+    value = json_false();
+    if(!value)
+        fail("json_false failed");
+    json_decref(value);
+
+    value = json_null();
+    if(!value)
+        fail("json_null failed");
+    json_decref(value);
+
+    return 0;
+}
diff --git a/test/suites/api/util.h b/test/suites/api/util.h
new file mode 100644 (file)
index 0000000..ec8e0e4
--- /dev/null
@@ -0,0 +1,20 @@
+/*
+ * Copyright (c) 2009 Petri Lehtinen <petri@digip.org>
+ *
+ * Jansson is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#ifndef TESTPROGS_UTIL_H
+#define TESTPROGS_UTIL_H
+
+#include <stdlib.h>
+
+#define fail(msg)                                                \
+    do {                                                         \
+        fprintf(stderr, "%s:%s:%d: %s\n",                        \
+                __FILE__, __FUNCTION__, __LINE__, msg);          \
+        exit(1);                                                 \
+    } while(0)
+
+#endif
diff --git a/test/suites/encoding-flags/array/input b/test/suites/encoding-flags/array/input
new file mode 100644 (file)
index 0000000..44e2ace
--- /dev/null
@@ -0,0 +1 @@
+[1, 2]
diff --git a/test/suites/encoding-flags/array/output b/test/suites/encoding-flags/array/output
new file mode 100644 (file)
index 0000000..fd8ef09
--- /dev/null
@@ -0,0 +1 @@
+[1, 2]
\ No newline at end of file
diff --git a/test/suites/encoding-flags/compact-array/env b/test/suites/encoding-flags/compact-array/env
new file mode 100644 (file)
index 0000000..9eab19d
--- /dev/null
@@ -0,0 +1 @@
+export JSON_COMPACT=1
diff --git a/test/suites/encoding-flags/compact-array/input b/test/suites/encoding-flags/compact-array/input
new file mode 100644 (file)
index 0000000..44e2ace
--- /dev/null
@@ -0,0 +1 @@
+[1, 2]
diff --git a/test/suites/encoding-flags/compact-array/output b/test/suites/encoding-flags/compact-array/output
new file mode 100644 (file)
index 0000000..3169929
--- /dev/null
@@ -0,0 +1 @@
+[1,2]
\ No newline at end of file
diff --git a/test/suites/encoding-flags/compact-object/env b/test/suites/encoding-flags/compact-object/env
new file mode 100644 (file)
index 0000000..9eab19d
--- /dev/null
@@ -0,0 +1 @@
+export JSON_COMPACT=1
diff --git a/test/suites/encoding-flags/compact-object/input b/test/suites/encoding-flags/compact-object/input
new file mode 100644 (file)
index 0000000..062e54f
--- /dev/null
@@ -0,0 +1 @@
+{"a": 1, "b": 2}
diff --git a/test/suites/encoding-flags/compact-object/output b/test/suites/encoding-flags/compact-object/output
new file mode 100644 (file)
index 0000000..73a5d70
--- /dev/null
@@ -0,0 +1 @@
+{"a":1,"b":2}
\ No newline at end of file
diff --git a/test/suites/encoding-flags/ensure-ascii/env b/test/suites/encoding-flags/ensure-ascii/env
new file mode 100644 (file)
index 0000000..6a0e0aa
--- /dev/null
@@ -0,0 +1 @@
+export JSON_ENSURE_ASCII=1
diff --git a/test/suites/encoding-flags/ensure-ascii/input b/test/suites/encoding-flags/ensure-ascii/input
new file mode 100644 (file)
index 0000000..69469ce
--- /dev/null
@@ -0,0 +1,8 @@
+[
+    "foo",
+    "å ä ö",
+    "foo åä",
+    "åä foo",
+    "å foo ä",
+    "clef g: 𝄞"
+]
diff --git a/test/suites/encoding-flags/ensure-ascii/output b/test/suites/encoding-flags/ensure-ascii/output
new file mode 100644 (file)
index 0000000..36f8eb5
--- /dev/null
@@ -0,0 +1 @@
+["foo", "\u00e5 \u00e4 \u00f6", "foo \u00e5\u00e4", "\u00e5\u00e4 foo", "\u00e5 foo \u00e4", "clef g: \ud834\udd1e"]
\ No newline at end of file
diff --git a/test/suites/encoding-flags/indent-array/env b/test/suites/encoding-flags/indent-array/env
new file mode 100644 (file)
index 0000000..273232a
--- /dev/null
@@ -0,0 +1 @@
+export JSON_INDENT=4
diff --git a/test/suites/encoding-flags/indent-array/input b/test/suites/encoding-flags/indent-array/input
new file mode 100644 (file)
index 0000000..44e2ace
--- /dev/null
@@ -0,0 +1 @@
+[1, 2]
diff --git a/test/suites/encoding-flags/indent-array/output b/test/suites/encoding-flags/indent-array/output
new file mode 100644 (file)
index 0000000..c57d705
--- /dev/null
@@ -0,0 +1,4 @@
+[
+    1,
+    2
+]
\ No newline at end of file
diff --git a/test/suites/encoding-flags/indent-compact-array/env b/test/suites/encoding-flags/indent-compact-array/env
new file mode 100644 (file)
index 0000000..89c9f78
--- /dev/null
@@ -0,0 +1,2 @@
+export JSON_INDENT=4
+export JSON_COMPACT=1
diff --git a/test/suites/encoding-flags/indent-compact-array/input b/test/suites/encoding-flags/indent-compact-array/input
new file mode 100644 (file)
index 0000000..44e2ace
--- /dev/null
@@ -0,0 +1 @@
+[1, 2]
diff --git a/test/suites/encoding-flags/indent-compact-array/output b/test/suites/encoding-flags/indent-compact-array/output
new file mode 100644 (file)
index 0000000..c57d705
--- /dev/null
@@ -0,0 +1,4 @@
+[
+    1,
+    2
+]
\ No newline at end of file
diff --git a/test/suites/encoding-flags/indent-compact-object/env b/test/suites/encoding-flags/indent-compact-object/env
new file mode 100644 (file)
index 0000000..89c9f78
--- /dev/null
@@ -0,0 +1,2 @@
+export JSON_INDENT=4
+export JSON_COMPACT=1
diff --git a/test/suites/encoding-flags/indent-compact-object/input b/test/suites/encoding-flags/indent-compact-object/input
new file mode 100644 (file)
index 0000000..062e54f
--- /dev/null
@@ -0,0 +1 @@
+{"a": 1, "b": 2}
diff --git a/test/suites/encoding-flags/indent-compact-object/output b/test/suites/encoding-flags/indent-compact-object/output
new file mode 100644 (file)
index 0000000..9cc4294
--- /dev/null
@@ -0,0 +1,4 @@
+{
+    "a":1,
+    "b":2
+}
\ No newline at end of file
diff --git a/test/suites/encoding-flags/indent-object/env b/test/suites/encoding-flags/indent-object/env
new file mode 100644 (file)
index 0000000..273232a
--- /dev/null
@@ -0,0 +1 @@
+export JSON_INDENT=4
diff --git a/test/suites/encoding-flags/indent-object/input b/test/suites/encoding-flags/indent-object/input
new file mode 100644 (file)
index 0000000..062e54f
--- /dev/null
@@ -0,0 +1 @@
+{"a": 1, "b": 2}
diff --git a/test/suites/encoding-flags/indent-object/output b/test/suites/encoding-flags/indent-object/output
new file mode 100644 (file)
index 0000000..0fbddba
--- /dev/null
@@ -0,0 +1,4 @@
+{
+    "a": 1,
+    "b": 2
+}
\ No newline at end of file
diff --git a/test/suites/encoding-flags/object/input b/test/suites/encoding-flags/object/input
new file mode 100644 (file)
index 0000000..062e54f
--- /dev/null
@@ -0,0 +1 @@
+{"a": 1, "b": 2}
diff --git a/test/suites/encoding-flags/object/output b/test/suites/encoding-flags/object/output
new file mode 100644 (file)
index 0000000..ecd219f
--- /dev/null
@@ -0,0 +1 @@
+{"a": 1, "b": 2}
\ No newline at end of file
diff --git a/test/suites/encoding-flags/run b/test/suites/encoding-flags/run
new file mode 100755 (executable)
index 0000000..a65fe5b
--- /dev/null
@@ -0,0 +1,32 @@
+#!/bin/sh
+#
+# Copyright (c) 2009 Petri Lehtinen <petri@digip.org>
+#
+# Jansson is free software; you can redistribute it and/or modify
+# it under the terms of the MIT license. See LICENSE for details.
+
+is_test() {
+    test -d $test_path
+}
+
+run_test() {
+    (
+        if [ -f $test_path/env ]; then
+            . $test_path/env
+        fi
+        $json_process <$test_path/input >$test_log/stdout 2>$test_log/stderr
+    )
+    valgrind_check $test_log/stderr || return 1
+    cmp -s $test_path/output $test_log/stdout
+}
+
+show_error() {
+    valgrind_show_error && return
+
+    echo "EXPECTED OUTPUT:"
+    nl -bn $test_path/output
+    echo "ACTUAL OUTPUT:"
+    nl -bn $test_log/stdout
+}
+
+. $top_srcdir/test/scripts/run-tests.sh
diff --git a/test/suites/encoding-flags/sort-keys/env b/test/suites/encoding-flags/sort-keys/env
new file mode 100644 (file)
index 0000000..00529e7
--- /dev/null
@@ -0,0 +1 @@
+export JSON_SORT_KEYS=1
diff --git a/test/suites/encoding-flags/sort-keys/input b/test/suites/encoding-flags/sort-keys/input
new file mode 100644 (file)
index 0000000..66951d6
--- /dev/null
@@ -0,0 +1 @@
+{"foo": 1, "bar": 2, "baz": 3, "quux": 4}
diff --git a/test/suites/encoding-flags/sort-keys/output b/test/suites/encoding-flags/sort-keys/output
new file mode 100644 (file)
index 0000000..132d9df
--- /dev/null
@@ -0,0 +1 @@
+{"bar": 2, "baz": 3, "foo": 1, "quux": 4}
\ No newline at end of file
diff --git a/test/suites/invalid-strip/apostrophe/error b/test/suites/invalid-strip/apostrophe/error
new file mode 100644 (file)
index 0000000..f30d170
--- /dev/null
@@ -0,0 +1,2 @@
+1
+invalid token near '''
diff --git a/test/suites/invalid-strip/apostrophe/input b/test/suites/invalid-strip/apostrophe/input
new file mode 100644 (file)
index 0000000..8bebe3a
--- /dev/null
@@ -0,0 +1 @@
+['
\ No newline at end of file
diff --git a/test/suites/invalid-strip/ascii-unicode-identifier/error b/test/suites/invalid-strip/ascii-unicode-identifier/error
new file mode 100644 (file)
index 0000000..7469919
--- /dev/null
@@ -0,0 +1,2 @@
+1
+'[' or '{' expected near 'a'
diff --git a/test/suites/invalid-strip/ascii-unicode-identifier/input b/test/suites/invalid-strip/ascii-unicode-identifier/input
new file mode 100644 (file)
index 0000000..ef2ab62
--- /dev/null
@@ -0,0 +1 @@
+aå
\ No newline at end of file
diff --git a/test/suites/invalid-strip/brace-comma/error b/test/suites/invalid-strip/brace-comma/error
new file mode 100644 (file)
index 0000000..394ad5a
--- /dev/null
@@ -0,0 +1,2 @@
+1
+string or '}' expected near ','
diff --git a/test/suites/invalid-strip/brace-comma/input b/test/suites/invalid-strip/brace-comma/input
new file mode 100644 (file)
index 0000000..47bc910
--- /dev/null
@@ -0,0 +1 @@
+{,
\ No newline at end of file
diff --git a/test/suites/invalid-strip/bracket-comma/error b/test/suites/invalid-strip/bracket-comma/error
new file mode 100644 (file)
index 0000000..54c3924
--- /dev/null
@@ -0,0 +1,2 @@
+1
+unexpected token near ','
diff --git a/test/suites/invalid-strip/bracket-comma/input b/test/suites/invalid-strip/bracket-comma/input
new file mode 100644 (file)
index 0000000..6295fdc
--- /dev/null
@@ -0,0 +1 @@
+[,
\ No newline at end of file
diff --git a/test/suites/invalid-strip/bracket-one-comma/error b/test/suites/invalid-strip/bracket-one-comma/error
new file mode 100644 (file)
index 0000000..f00ebc5
--- /dev/null
@@ -0,0 +1,2 @@
+1
+']' expected near end of file
diff --git a/test/suites/invalid-strip/bracket-one-comma/input b/test/suites/invalid-strip/bracket-one-comma/input
new file mode 100644 (file)
index 0000000..6604698
--- /dev/null
@@ -0,0 +1 @@
+[1,
\ No newline at end of file
diff --git a/test/suites/invalid-strip/empty/error b/test/suites/invalid-strip/empty/error
new file mode 100644 (file)
index 0000000..ff19200
--- /dev/null
@@ -0,0 +1,2 @@
+1
+'[' or '{' expected near end of file
diff --git a/test/suites/invalid-strip/empty/input b/test/suites/invalid-strip/empty/input
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/test/suites/invalid-strip/extra-comma-in-array/error b/test/suites/invalid-strip/extra-comma-in-array/error
new file mode 100644 (file)
index 0000000..38afe71
--- /dev/null
@@ -0,0 +1,2 @@
+1
+unexpected token near ']'
diff --git a/test/suites/invalid-strip/extra-comma-in-array/input b/test/suites/invalid-strip/extra-comma-in-array/input
new file mode 100644 (file)
index 0000000..13f6f1d
--- /dev/null
@@ -0,0 +1 @@
+[1,]
\ No newline at end of file
diff --git a/test/suites/invalid-strip/extra-command-in-multiline-array/error b/test/suites/invalid-strip/extra-command-in-multiline-array/error
new file mode 100644 (file)
index 0000000..26c2152
--- /dev/null
@@ -0,0 +1,2 @@
+6
+unexpected token near ']'
diff --git a/test/suites/invalid-strip/extra-command-in-multiline-array/input b/test/suites/invalid-strip/extra-command-in-multiline-array/input
new file mode 100644 (file)
index 0000000..e4d4ddd
--- /dev/null
@@ -0,0 +1,6 @@
+[1,
+2,
+3,
+4,
+5,
+]
\ No newline at end of file
diff --git a/test/suites/invalid-strip/garbage-after-newline/error b/test/suites/invalid-strip/garbage-after-newline/error
new file mode 100644 (file)
index 0000000..90eacdd
--- /dev/null
@@ -0,0 +1,2 @@
+2
+end of file expected near 'foo'
diff --git a/test/suites/invalid-strip/garbage-after-newline/input b/test/suites/invalid-strip/garbage-after-newline/input
new file mode 100644 (file)
index 0000000..3614ac7
--- /dev/null
@@ -0,0 +1,2 @@
+[1,2,3]
+foo
diff --git a/test/suites/invalid-strip/garbage-at-the-end/error b/test/suites/invalid-strip/garbage-at-the-end/error
new file mode 100644 (file)
index 0000000..9076aec
--- /dev/null
@@ -0,0 +1,2 @@
+1
+end of file expected near 'foo'
diff --git a/test/suites/invalid-strip/garbage-at-the-end/input b/test/suites/invalid-strip/garbage-at-the-end/input
new file mode 100644 (file)
index 0000000..adf1aac
--- /dev/null
@@ -0,0 +1 @@
+[1,2,3]foo
\ No newline at end of file
diff --git a/test/suites/invalid-strip/integer-starting-with-zero/error b/test/suites/invalid-strip/integer-starting-with-zero/error
new file mode 100644 (file)
index 0000000..e4eee2f
--- /dev/null
@@ -0,0 +1,2 @@
+1
+invalid token near '0'
diff --git a/test/suites/invalid-strip/integer-starting-with-zero/input b/test/suites/invalid-strip/integer-starting-with-zero/input
new file mode 100644 (file)
index 0000000..7106da1
--- /dev/null
@@ -0,0 +1 @@
+[012]
\ No newline at end of file
diff --git a/test/suites/invalid-strip/invalid-escape/error b/test/suites/invalid-strip/invalid-escape/error
new file mode 100644 (file)
index 0000000..9930ca7
--- /dev/null
@@ -0,0 +1,2 @@
+1
+invalid escape near '"\'
diff --git a/test/suites/invalid-strip/invalid-escape/input b/test/suites/invalid-strip/invalid-escape/input
new file mode 100644 (file)
index 0000000..e305947
--- /dev/null
@@ -0,0 +1 @@
+["\a <-- invalid escape"]
\ No newline at end of file
diff --git a/test/suites/invalid-strip/invalid-identifier/error b/test/suites/invalid-strip/invalid-identifier/error
new file mode 100644 (file)
index 0000000..0f88417
--- /dev/null
@@ -0,0 +1,2 @@
+1
+invalid token near 'troo'
diff --git a/test/suites/invalid-strip/invalid-identifier/input b/test/suites/invalid-strip/invalid-identifier/input
new file mode 100644 (file)
index 0000000..7c06bd5
--- /dev/null
@@ -0,0 +1 @@
+[troo
\ No newline at end of file
diff --git a/test/suites/invalid-strip/invalid-negative-integerr/error b/test/suites/invalid-strip/invalid-negative-integerr/error
new file mode 100644 (file)
index 0000000..f9d930e
--- /dev/null
@@ -0,0 +1,2 @@
+1
+']' expected near 'foo'
diff --git a/test/suites/invalid-strip/invalid-negative-integerr/input b/test/suites/invalid-strip/invalid-negative-integerr/input
new file mode 100644 (file)
index 0000000..fe088a3
--- /dev/null
@@ -0,0 +1 @@
+[-123foo]
\ No newline at end of file
diff --git a/test/suites/invalid-strip/invalid-negative-real/error b/test/suites/invalid-strip/invalid-negative-real/error
new file mode 100644 (file)
index 0000000..f9d930e
--- /dev/null
@@ -0,0 +1,2 @@
+1
+']' expected near 'foo'
diff --git a/test/suites/invalid-strip/invalid-negative-real/input b/test/suites/invalid-strip/invalid-negative-real/input
new file mode 100644 (file)
index 0000000..5fc3c1e
--- /dev/null
@@ -0,0 +1 @@
+[-123.123foo]
\ No newline at end of file
diff --git a/test/suites/invalid-strip/invalid-second-surrogate/error b/test/suites/invalid-strip/invalid-second-surrogate/error
new file mode 100644 (file)
index 0000000..22d0e1f
--- /dev/null
@@ -0,0 +1,2 @@
+1
+invalid Unicode '\uD888\u3210'
diff --git a/test/suites/invalid-strip/invalid-second-surrogate/input b/test/suites/invalid-strip/invalid-second-surrogate/input
new file mode 100644 (file)
index 0000000..25a1ca2
--- /dev/null
@@ -0,0 +1 @@
+["\uD888\u3210 (first surrogate and invalid second surrogate)"]
\ No newline at end of file
diff --git a/test/suites/invalid-strip/lone-open-brace/error b/test/suites/invalid-strip/lone-open-brace/error
new file mode 100644 (file)
index 0000000..73a2947
--- /dev/null
@@ -0,0 +1,2 @@
+1
+string or '}' expected near end of file
diff --git a/test/suites/invalid-strip/lone-open-brace/input b/test/suites/invalid-strip/lone-open-brace/input
new file mode 100644 (file)
index 0000000..81750b9
--- /dev/null
@@ -0,0 +1 @@
+{
\ No newline at end of file
diff --git a/test/suites/invalid-strip/lone-open-bracket/error b/test/suites/invalid-strip/lone-open-bracket/error
new file mode 100644 (file)
index 0000000..f00ebc5
--- /dev/null
@@ -0,0 +1,2 @@
+1
+']' expected near end of file
diff --git a/test/suites/invalid-strip/lone-open-bracket/input b/test/suites/invalid-strip/lone-open-bracket/input
new file mode 100644 (file)
index 0000000..8e2f0be
--- /dev/null
@@ -0,0 +1 @@
+[
\ No newline at end of file
diff --git a/test/suites/invalid-strip/lone-second-surrogate/error b/test/suites/invalid-strip/lone-second-surrogate/error
new file mode 100644 (file)
index 0000000..c96d7f5
--- /dev/null
@@ -0,0 +1,2 @@
+1
+invalid Unicode '\uDFAA'
diff --git a/test/suites/invalid-strip/lone-second-surrogate/input b/test/suites/invalid-strip/lone-second-surrogate/input
new file mode 100644 (file)
index 0000000..6882ee4
--- /dev/null
@@ -0,0 +1 @@
+["\uDFAA (second surrogate on it's own)"]
\ No newline at end of file
diff --git a/test/suites/invalid-strip/minus-sign-without-number/error b/test/suites/invalid-strip/minus-sign-without-number/error
new file mode 100644 (file)
index 0000000..e978234
--- /dev/null
@@ -0,0 +1,2 @@
+1
+invalid token near '-'
diff --git a/test/suites/invalid-strip/minus-sign-without-number/input b/test/suites/invalid-strip/minus-sign-without-number/input
new file mode 100644 (file)
index 0000000..a6d8e78
--- /dev/null
@@ -0,0 +1 @@
+[-foo]
\ No newline at end of file
diff --git a/test/suites/invalid-strip/negative-integer-starting-with-zero/error b/test/suites/invalid-strip/negative-integer-starting-with-zero/error
new file mode 100644 (file)
index 0000000..a758d94
--- /dev/null
@@ -0,0 +1,2 @@
+1
+invalid token near '-0'
diff --git a/test/suites/invalid-strip/negative-integer-starting-with-zero/input b/test/suites/invalid-strip/negative-integer-starting-with-zero/input
new file mode 100644 (file)
index 0000000..67af096
--- /dev/null
@@ -0,0 +1 @@
+[-012]
\ No newline at end of file
diff --git a/test/suites/invalid-strip/null-byte-in-string/error b/test/suites/invalid-strip/null-byte-in-string/error
new file mode 100644 (file)
index 0000000..0fa36e2
--- /dev/null
@@ -0,0 +1,2 @@
+1
+\u0000 is not allowed
diff --git a/test/suites/invalid-strip/null-byte-in-string/input b/test/suites/invalid-strip/null-byte-in-string/input
new file mode 100644 (file)
index 0000000..60f7f7b
--- /dev/null
@@ -0,0 +1 @@
+["\u0000 (null byte not allowed)"]
\ No newline at end of file
diff --git a/test/suites/invalid-strip/null/error b/test/suites/invalid-strip/null/error
new file mode 100644 (file)
index 0000000..de9280b
--- /dev/null
@@ -0,0 +1,2 @@
+1
+'[' or '{' expected near 'null'
diff --git a/test/suites/invalid-strip/null/input b/test/suites/invalid-strip/null/input
new file mode 100644 (file)
index 0000000..ec747fa
--- /dev/null
@@ -0,0 +1 @@
+null
\ No newline at end of file
diff --git a/test/suites/invalid-strip/object-apostrophes/error b/test/suites/invalid-strip/object-apostrophes/error
new file mode 100644 (file)
index 0000000..f7b586a
--- /dev/null
@@ -0,0 +1,2 @@
+1
+string or '}' expected near '''
diff --git a/test/suites/invalid-strip/object-apostrophes/input b/test/suites/invalid-strip/object-apostrophes/input
new file mode 100644 (file)
index 0000000..9dba170
--- /dev/null
@@ -0,0 +1 @@
+{'a'
\ No newline at end of file
diff --git a/test/suites/invalid-strip/object-garbage-at-end/error b/test/suites/invalid-strip/object-garbage-at-end/error
new file mode 100644 (file)
index 0000000..a60dc47
--- /dev/null
@@ -0,0 +1,2 @@
+1
+'}' expected near '123'
diff --git a/test/suites/invalid-strip/object-garbage-at-end/input b/test/suites/invalid-strip/object-garbage-at-end/input
new file mode 100644 (file)
index 0000000..80c42cb
--- /dev/null
@@ -0,0 +1 @@
+{"a":"a" 123}
\ No newline at end of file
diff --git a/test/suites/invalid-strip/object-in-unterminated-array/error b/test/suites/invalid-strip/object-in-unterminated-array/error
new file mode 100644 (file)
index 0000000..f00ebc5
--- /dev/null
@@ -0,0 +1,2 @@
+1
+']' expected near end of file
diff --git a/test/suites/invalid-strip/object-in-unterminated-array/input b/test/suites/invalid-strip/object-in-unterminated-array/input
new file mode 100644 (file)
index 0000000..043a87e
--- /dev/null
@@ -0,0 +1 @@
+[{}
\ No newline at end of file
diff --git a/test/suites/invalid-strip/object-no-colon/error b/test/suites/invalid-strip/object-no-colon/error
new file mode 100644 (file)
index 0000000..7f8b299
--- /dev/null
@@ -0,0 +1,2 @@
+1
+':' expected near end of file
diff --git a/test/suites/invalid-strip/object-no-colon/input b/test/suites/invalid-strip/object-no-colon/input
new file mode 100644 (file)
index 0000000..f3797b3
--- /dev/null
@@ -0,0 +1 @@
+{"a"
\ No newline at end of file
diff --git a/test/suites/invalid-strip/object-no-value/error b/test/suites/invalid-strip/object-no-value/error
new file mode 100644 (file)
index 0000000..085fc8b
--- /dev/null
@@ -0,0 +1,2 @@
+1
+unexpected token near end of file
diff --git a/test/suites/invalid-strip/object-no-value/input b/test/suites/invalid-strip/object-no-value/input
new file mode 100644 (file)
index 0000000..3ef538a
--- /dev/null
@@ -0,0 +1 @@
+{"a":
\ No newline at end of file
diff --git a/test/suites/invalid-strip/object-unterminated-value/error b/test/suites/invalid-strip/object-unterminated-value/error
new file mode 100644 (file)
index 0000000..62f18ab
--- /dev/null
@@ -0,0 +1,2 @@
+1
+premature end of input near '"a'
diff --git a/test/suites/invalid-strip/object-unterminated-value/input b/test/suites/invalid-strip/object-unterminated-value/input
new file mode 100644 (file)
index 0000000..7fe699a
--- /dev/null
@@ -0,0 +1 @@
+{"a":"a
\ No newline at end of file
diff --git a/test/suites/invalid-strip/real-garbage-after-e/error b/test/suites/invalid-strip/real-garbage-after-e/error
new file mode 100644 (file)
index 0000000..d9b83e7
--- /dev/null
@@ -0,0 +1,2 @@
+1
+invalid token near '1e'
diff --git a/test/suites/invalid-strip/real-garbage-after-e/input b/test/suites/invalid-strip/real-garbage-after-e/input
new file mode 100644 (file)
index 0000000..9213dfc
--- /dev/null
@@ -0,0 +1 @@
+[1ea]
\ No newline at end of file
diff --git a/test/suites/invalid-strip/real-negative-overflow/error b/test/suites/invalid-strip/real-negative-overflow/error
new file mode 100644 (file)
index 0000000..af5a850
--- /dev/null
@@ -0,0 +1,2 @@
+1
+real number overflow near '-123123e100000'
diff --git a/test/suites/invalid-strip/real-negative-overflow/input b/test/suites/invalid-strip/real-negative-overflow/input
new file mode 100644 (file)
index 0000000..3d628a9
--- /dev/null
@@ -0,0 +1 @@
+[-123123e100000]
\ No newline at end of file
diff --git a/test/suites/invalid-strip/real-positive-overflow/error b/test/suites/invalid-strip/real-positive-overflow/error
new file mode 100644 (file)
index 0000000..e9becc3
--- /dev/null
@@ -0,0 +1,2 @@
+1
+real number overflow near '123123e100000'
diff --git a/test/suites/invalid-strip/real-positive-overflow/input b/test/suites/invalid-strip/real-positive-overflow/input
new file mode 100644 (file)
index 0000000..54d7d3d
--- /dev/null
@@ -0,0 +1 @@
+[123123e100000]
\ No newline at end of file
diff --git a/test/suites/invalid-strip/real-truncated-at-e/error b/test/suites/invalid-strip/real-truncated-at-e/error
new file mode 100644 (file)
index 0000000..d9b83e7
--- /dev/null
@@ -0,0 +1,2 @@
+1
+invalid token near '1e'
diff --git a/test/suites/invalid-strip/real-truncated-at-e/input b/test/suites/invalid-strip/real-truncated-at-e/input
new file mode 100644 (file)
index 0000000..c3ace3a
--- /dev/null
@@ -0,0 +1 @@
+[1e]
\ No newline at end of file
diff --git a/test/suites/invalid-strip/real-truncated-at-point/error b/test/suites/invalid-strip/real-truncated-at-point/error
new file mode 100644 (file)
index 0000000..7312401
--- /dev/null
@@ -0,0 +1,2 @@
+1
+invalid token near '1.'
diff --git a/test/suites/invalid-strip/real-truncated-at-point/input b/test/suites/invalid-strip/real-truncated-at-point/input
new file mode 100644 (file)
index 0000000..1de287c
--- /dev/null
@@ -0,0 +1 @@
+[1.]
\ No newline at end of file
diff --git a/test/suites/invalid-strip/real-underflow/error b/test/suites/invalid-strip/real-underflow/error
new file mode 100644 (file)
index 0000000..1b65d40
--- /dev/null
@@ -0,0 +1,2 @@
+1
+real number underflow near '123e-10000000'
diff --git a/test/suites/invalid-strip/real-underflow/input b/test/suites/invalid-strip/real-underflow/input
new file mode 100644 (file)
index 0000000..c5236eb
--- /dev/null
@@ -0,0 +1 @@
+[123e-10000000]
\ No newline at end of file
diff --git a/test/suites/invalid-strip/run b/test/suites/invalid-strip/run
new file mode 100755 (executable)
index 0000000..1c6130d
--- /dev/null
@@ -0,0 +1,27 @@
+#!/bin/sh
+#
+# Copyright (c) 2009 Petri Lehtinen <petri@digip.org>
+#
+# Jansson is free software; you can redistribute it and/or modify
+# it under the terms of the MIT license. See LICENSE for details.
+
+is_test() {
+    test -d $test_path
+}
+
+run_test() {
+    $json_process <$test_path/input >$test_log/stdout 2>$test_log/stderr
+    valgrind_check $test_log/stderr || return 1
+    cmp -s $test_path/error $test_log/stderr
+}
+
+show_error() {
+    valgrind_show_error && return
+
+    echo "EXPECTED ERROR:"
+    nl -bn $test_path/error
+    echo "ACTUAL ERROR:"
+    nl -bn $test_log/stderr
+}
+
+. $top_srcdir/test/scripts/run-tests.sh
diff --git a/test/suites/invalid-strip/tab-character-in-string/error b/test/suites/invalid-strip/tab-character-in-string/error
new file mode 100644 (file)
index 0000000..ee81a5f
--- /dev/null
@@ -0,0 +1,2 @@
+1
+control character 0x9 near '"'
diff --git a/test/suites/invalid-strip/tab-character-in-string/input b/test/suites/invalid-strip/tab-character-in-string/input
new file mode 100644 (file)
index 0000000..f3010f5
--- /dev/null
@@ -0,0 +1 @@
+["      <-- tab character"]
\ No newline at end of file
diff --git a/test/suites/invalid-strip/too-big-negative-integer/error b/test/suites/invalid-strip/too-big-negative-integer/error
new file mode 100644 (file)
index 0000000..4245f39
--- /dev/null
@@ -0,0 +1,2 @@
+1
+too big negative integer near '-123123123123123'
diff --git a/test/suites/invalid-strip/too-big-negative-integer/input b/test/suites/invalid-strip/too-big-negative-integer/input
new file mode 100644 (file)
index 0000000..f2366dc
--- /dev/null
@@ -0,0 +1 @@
+[-123123123123123]
\ No newline at end of file
diff --git a/test/suites/invalid-strip/too-big-positive-integer/error b/test/suites/invalid-strip/too-big-positive-integer/error
new file mode 100644 (file)
index 0000000..4761ad3
--- /dev/null
@@ -0,0 +1,2 @@
+1
+too big integer near '123123123123123'
diff --git a/test/suites/invalid-strip/too-big-positive-integer/input b/test/suites/invalid-strip/too-big-positive-integer/input
new file mode 100644 (file)
index 0000000..a787c2c
--- /dev/null
@@ -0,0 +1 @@
+[123123123123123]
\ No newline at end of file
diff --git a/test/suites/invalid-strip/truncated-unicode-surrogate/error b/test/suites/invalid-strip/truncated-unicode-surrogate/error
new file mode 100644 (file)
index 0000000..17b932d
--- /dev/null
@@ -0,0 +1,2 @@
+1
+invalid Unicode '\uDADA'
diff --git a/test/suites/invalid-strip/truncated-unicode-surrogate/input b/test/suites/invalid-strip/truncated-unicode-surrogate/input
new file mode 100644 (file)
index 0000000..3937c2c
--- /dev/null
@@ -0,0 +1 @@
+["\uDADA (first surrogate without the second)"]
\ No newline at end of file
diff --git a/test/suites/invalid-strip/unicode-identifier/error b/test/suites/invalid-strip/unicode-identifier/error
new file mode 100644 (file)
index 0000000..e3817fa
--- /dev/null
@@ -0,0 +1,2 @@
+1
+'[' or '{' expected near 'å'
diff --git a/test/suites/invalid-strip/unicode-identifier/input b/test/suites/invalid-strip/unicode-identifier/input
new file mode 100644 (file)
index 0000000..7284aea
--- /dev/null
@@ -0,0 +1 @@
\ No newline at end of file
diff --git a/test/suites/invalid-strip/unterminated-array-and-object/error b/test/suites/invalid-strip/unterminated-array-and-object/error
new file mode 100644 (file)
index 0000000..73a2947
--- /dev/null
@@ -0,0 +1,2 @@
+1
+string or '}' expected near end of file
diff --git a/test/suites/invalid-strip/unterminated-array-and-object/input b/test/suites/invalid-strip/unterminated-array-and-object/input
new file mode 100644 (file)
index 0000000..7a63c8c
--- /dev/null
@@ -0,0 +1 @@
+[{
\ No newline at end of file
diff --git a/test/suites/invalid-strip/unterminated-array/error b/test/suites/invalid-strip/unterminated-array/error
new file mode 100644 (file)
index 0000000..f00ebc5
--- /dev/null
@@ -0,0 +1,2 @@
+1
+']' expected near end of file
diff --git a/test/suites/invalid-strip/unterminated-array/input b/test/suites/invalid-strip/unterminated-array/input
new file mode 100644 (file)
index 0000000..42a6193
--- /dev/null
@@ -0,0 +1 @@
+["a"
\ No newline at end of file
diff --git a/test/suites/invalid-strip/unterminated-empty-key/error b/test/suites/invalid-strip/unterminated-empty-key/error
new file mode 100644 (file)
index 0000000..dd913cf
--- /dev/null
@@ -0,0 +1,2 @@
+1
+premature end of input near '"'
diff --git a/test/suites/invalid-strip/unterminated-empty-key/input b/test/suites/invalid-strip/unterminated-empty-key/input
new file mode 100644 (file)
index 0000000..20f22da
--- /dev/null
@@ -0,0 +1 @@
+{"
\ No newline at end of file
diff --git a/test/suites/invalid-strip/unterminated-key/error b/test/suites/invalid-strip/unterminated-key/error
new file mode 100644 (file)
index 0000000..62f18ab
--- /dev/null
@@ -0,0 +1,2 @@
+1
+premature end of input near '"a'
diff --git a/test/suites/invalid-strip/unterminated-key/input b/test/suites/invalid-strip/unterminated-key/input
new file mode 100644 (file)
index 0000000..328c30c
--- /dev/null
@@ -0,0 +1 @@
+{"a
\ No newline at end of file
diff --git a/test/suites/invalid-strip/unterminated-object-and-array/error b/test/suites/invalid-strip/unterminated-object-and-array/error
new file mode 100644 (file)
index 0000000..5fe61b5
--- /dev/null
@@ -0,0 +1,2 @@
+1
+string or '}' expected near '['
diff --git a/test/suites/invalid-strip/unterminated-object-and-array/input b/test/suites/invalid-strip/unterminated-object-and-array/input
new file mode 100644 (file)
index 0000000..381ede5
--- /dev/null
@@ -0,0 +1 @@
+{[
\ No newline at end of file
diff --git a/test/suites/invalid-strip/unterminated-string/error b/test/suites/invalid-strip/unterminated-string/error
new file mode 100644 (file)
index 0000000..62f18ab
--- /dev/null
@@ -0,0 +1,2 @@
+1
+premature end of input near '"a'
diff --git a/test/suites/invalid-strip/unterminated-string/input b/test/suites/invalid-strip/unterminated-string/input
new file mode 100644 (file)
index 0000000..9822a6b
--- /dev/null
@@ -0,0 +1 @@
+["a
\ No newline at end of file
diff --git a/test/suites/invalid-unicode/encoded-surrogate-half/error b/test/suites/invalid-unicode/encoded-surrogate-half/error
new file mode 100644 (file)
index 0000000..a1813f7
--- /dev/null
@@ -0,0 +1,2 @@
+-1
+unable to decode byte 0xed at position 2
diff --git a/test/suites/invalid-unicode/encoded-surrogate-half/input b/test/suites/invalid-unicode/encoded-surrogate-half/input
new file mode 100644 (file)
index 0000000..515dd93
--- /dev/null
@@ -0,0 +1 @@
+[" <-- encoded surrogate half"]
diff --git a/test/suites/invalid-unicode/invalid-utf-8-after-backslash/error b/test/suites/invalid-unicode/invalid-utf-8-after-backslash/error
new file mode 100644 (file)
index 0000000..50beb0a
--- /dev/null
@@ -0,0 +1,2 @@
+-1
+unable to decode byte 0xe5 at position 3
diff --git a/test/suites/invalid-unicode/invalid-utf-8-after-backslash/input b/test/suites/invalid-unicode/invalid-utf-8-after-backslash/input
new file mode 100644 (file)
index 0000000..57c8bee
--- /dev/null
@@ -0,0 +1 @@
+["\å"]
diff --git a/test/suites/invalid-unicode/invalid-utf-8-in-array/error b/test/suites/invalid-unicode/invalid-utf-8-in-array/error
new file mode 100644 (file)
index 0000000..4030cd5
--- /dev/null
@@ -0,0 +1,2 @@
+-1
+unable to decode byte 0xe5 at position 1
diff --git a/test/suites/invalid-unicode/invalid-utf-8-in-array/input b/test/suites/invalid-unicode/invalid-utf-8-in-array/input
new file mode 100644 (file)
index 0000000..ebefcd6
--- /dev/null
@@ -0,0 +1 @@
+[å]
diff --git a/test/suites/invalid-unicode/invalid-utf-8-in-bigger-int/error b/test/suites/invalid-unicode/invalid-utf-8-in-bigger-int/error
new file mode 100644 (file)
index 0000000..74252d7
--- /dev/null
@@ -0,0 +1,2 @@
+-1
+unable to decode byte 0xe5 at position 4
diff --git a/test/suites/invalid-unicode/invalid-utf-8-in-bigger-int/input b/test/suites/invalid-unicode/invalid-utf-8-in-bigger-int/input
new file mode 100644 (file)
index 0000000..e512f9a
--- /dev/null
@@ -0,0 +1 @@
+[123å]
diff --git a/test/suites/invalid-unicode/invalid-utf-8-in-escape/error b/test/suites/invalid-unicode/invalid-utf-8-in-escape/error
new file mode 100644 (file)
index 0000000..74252d7
--- /dev/null
@@ -0,0 +1,2 @@
+-1
+unable to decode byte 0xe5 at position 4
diff --git a/test/suites/invalid-unicode/invalid-utf-8-in-escape/input b/test/suites/invalid-unicode/invalid-utf-8-in-escape/input
new file mode 100644 (file)
index 0000000..2b271b8
--- /dev/null
@@ -0,0 +1 @@
+["\uå"]
diff --git a/test/suites/invalid-unicode/invalid-utf-8-in-exponent/error b/test/suites/invalid-unicode/invalid-utf-8-in-exponent/error
new file mode 100644 (file)
index 0000000..74252d7
--- /dev/null
@@ -0,0 +1,2 @@
+-1
+unable to decode byte 0xe5 at position 4
diff --git a/test/suites/invalid-unicode/invalid-utf-8-in-exponent/input b/test/suites/invalid-unicode/invalid-utf-8-in-exponent/input
new file mode 100644 (file)
index 0000000..d8e83c5
--- /dev/null
@@ -0,0 +1 @@
+[1e1å]
diff --git a/test/suites/invalid-unicode/invalid-utf-8-in-identifier/error b/test/suites/invalid-unicode/invalid-utf-8-in-identifier/error
new file mode 100644 (file)
index 0000000..39763ec
--- /dev/null
@@ -0,0 +1,2 @@
+-1
+unable to decode byte 0xe5 at position 2
diff --git a/test/suites/invalid-unicode/invalid-utf-8-in-identifier/input b/test/suites/invalid-unicode/invalid-utf-8-in-identifier/input
new file mode 100644 (file)
index 0000000..ef03851
--- /dev/null
@@ -0,0 +1 @@
+[aå]
diff --git a/test/suites/invalid-unicode/invalid-utf-8-in-int/error b/test/suites/invalid-unicode/invalid-utf-8-in-int/error
new file mode 100644 (file)
index 0000000..39763ec
--- /dev/null
@@ -0,0 +1,2 @@
+-1
+unable to decode byte 0xe5 at position 2
diff --git a/test/suites/invalid-unicode/invalid-utf-8-in-int/input b/test/suites/invalid-unicode/invalid-utf-8-in-int/input
new file mode 100644 (file)
index 0000000..371226e
--- /dev/null
@@ -0,0 +1 @@
+[0å]
diff --git a/test/suites/invalid-unicode/invalid-utf-8-in-real-after-e/error b/test/suites/invalid-unicode/invalid-utf-8-in-real-after-e/error
new file mode 100644 (file)
index 0000000..50beb0a
--- /dev/null
@@ -0,0 +1,2 @@
+-1
+unable to decode byte 0xe5 at position 3
diff --git a/test/suites/invalid-unicode/invalid-utf-8-in-real-after-e/input b/test/suites/invalid-unicode/invalid-utf-8-in-real-after-e/input
new file mode 100644 (file)
index 0000000..17fc29c
--- /dev/null
@@ -0,0 +1 @@
+[1eå]
diff --git a/test/suites/invalid-unicode/invalid-utf-8-in-string/error b/test/suites/invalid-unicode/invalid-utf-8-in-string/error
new file mode 100644 (file)
index 0000000..39763ec
--- /dev/null
@@ -0,0 +1,2 @@
+-1
+unable to decode byte 0xe5 at position 2
diff --git a/test/suites/invalid-unicode/invalid-utf-8-in-string/input b/test/suites/invalid-unicode/invalid-utf-8-in-string/input
new file mode 100644 (file)
index 0000000..00b79c0
--- /dev/null
@@ -0,0 +1 @@
+["å <-- invalid UTF-8"]
diff --git a/test/suites/invalid-unicode/lone-invalid-utf-8/error b/test/suites/invalid-unicode/lone-invalid-utf-8/error
new file mode 100644 (file)
index 0000000..77f16c9
--- /dev/null
@@ -0,0 +1,2 @@
+-1
+unable to decode byte 0xe5 at position 0
diff --git a/test/suites/invalid-unicode/lone-invalid-utf-8/input b/test/suites/invalid-unicode/lone-invalid-utf-8/input
new file mode 100644 (file)
index 0000000..eb80796
--- /dev/null
@@ -0,0 +1 @@
diff --git a/test/suites/invalid-unicode/lone-utf-8-continuation-byte/error b/test/suites/invalid-unicode/lone-utf-8-continuation-byte/error
new file mode 100644 (file)
index 0000000..370738e
--- /dev/null
@@ -0,0 +1,2 @@
+-1
+unable to decode byte 0x81 at position 2
diff --git a/test/suites/invalid-unicode/lone-utf-8-continuation-byte/input b/test/suites/invalid-unicode/lone-utf-8-continuation-byte/input
new file mode 100644 (file)
index 0000000..62a26b6
--- /dev/null
@@ -0,0 +1 @@
+["\81"]
diff --git a/test/suites/invalid-unicode/not-in-unicode-range/error b/test/suites/invalid-unicode/not-in-unicode-range/error
new file mode 100644 (file)
index 0000000..5a00b67
--- /dev/null
@@ -0,0 +1,2 @@
+-1
+unable to decode byte 0xf4 at position 2
diff --git a/test/suites/invalid-unicode/not-in-unicode-range/input b/test/suites/invalid-unicode/not-in-unicode-range/input
new file mode 100644 (file)
index 0000000..1216186
--- /dev/null
@@ -0,0 +1 @@
+[""]
diff --git a/test/suites/invalid-unicode/overlong-3-byte-encoding/error b/test/suites/invalid-unicode/overlong-3-byte-encoding/error
new file mode 100644 (file)
index 0000000..48796d1
--- /dev/null
@@ -0,0 +1,2 @@
+-1
+unable to decode byte 0xe0 at position 2
diff --git a/test/suites/invalid-unicode/overlong-3-byte-encoding/input b/test/suites/invalid-unicode/overlong-3-byte-encoding/input
new file mode 100644 (file)
index 0000000..0bf909f
--- /dev/null
@@ -0,0 +1 @@
+["à\80¢ <-- overlong encoding"]
diff --git a/test/suites/invalid-unicode/overlong-4-byte-encoding/error b/test/suites/invalid-unicode/overlong-4-byte-encoding/error
new file mode 100644 (file)
index 0000000..5c6e37f
--- /dev/null
@@ -0,0 +1,2 @@
+-1
+unable to decode byte 0xf0 at position 2
diff --git a/test/suites/invalid-unicode/overlong-4-byte-encoding/input b/test/suites/invalid-unicode/overlong-4-byte-encoding/input
new file mode 100644 (file)
index 0000000..c6b6313
--- /dev/null
@@ -0,0 +1 @@
+["ð\80\80¢ <-- overlong encoding"]
diff --git a/test/suites/invalid-unicode/overlong-ascii-encoding/error b/test/suites/invalid-unicode/overlong-ascii-encoding/error
new file mode 100644 (file)
index 0000000..8fbf914
--- /dev/null
@@ -0,0 +1,2 @@
+-1
+unable to decode byte 0xc1 at position 2
diff --git a/test/suites/invalid-unicode/overlong-ascii-encoding/input b/test/suites/invalid-unicode/overlong-ascii-encoding/input
new file mode 100644 (file)
index 0000000..ef6e10a
--- /dev/null
@@ -0,0 +1 @@
+["Á"]
diff --git a/test/suites/invalid-unicode/restricted-utf-8/error b/test/suites/invalid-unicode/restricted-utf-8/error
new file mode 100644 (file)
index 0000000..e71089d
--- /dev/null
@@ -0,0 +1,2 @@
+-1
+unable to decode byte 0xfd at position 2
diff --git a/test/suites/invalid-unicode/restricted-utf-8/input b/test/suites/invalid-unicode/restricted-utf-8/input
new file mode 100644 (file)
index 0000000..ba60170
--- /dev/null
@@ -0,0 +1 @@
+["ý"]
diff --git a/test/suites/invalid-unicode/run b/test/suites/invalid-unicode/run
new file mode 100755 (executable)
index 0000000..1c6130d
--- /dev/null
@@ -0,0 +1,27 @@
+#!/bin/sh
+#
+# Copyright (c) 2009 Petri Lehtinen <petri@digip.org>
+#
+# Jansson is free software; you can redistribute it and/or modify
+# it under the terms of the MIT license. See LICENSE for details.
+
+is_test() {
+    test -d $test_path
+}
+
+run_test() {
+    $json_process <$test_path/input >$test_log/stdout 2>$test_log/stderr
+    valgrind_check $test_log/stderr || return 1
+    cmp -s $test_path/error $test_log/stderr
+}
+
+show_error() {
+    valgrind_show_error && return
+
+    echo "EXPECTED ERROR:"
+    nl -bn $test_path/error
+    echo "ACTUAL ERROR:"
+    nl -bn $test_log/stderr
+}
+
+. $top_srcdir/test/scripts/run-tests.sh
diff --git a/test/suites/invalid-unicode/truncated-utf-8/error b/test/suites/invalid-unicode/truncated-utf-8/error
new file mode 100644 (file)
index 0000000..48796d1
--- /dev/null
@@ -0,0 +1,2 @@
+-1
+unable to decode byte 0xe0 at position 2
diff --git a/test/suites/invalid-unicode/truncated-utf-8/input b/test/suites/invalid-unicode/truncated-utf-8/input
new file mode 100644 (file)
index 0000000..bce9e18
--- /dev/null
@@ -0,0 +1 @@
+["àÿ <-- truncated UTF-8"]
diff --git a/test/suites/invalid/apostrophe/error b/test/suites/invalid/apostrophe/error
new file mode 100644 (file)
index 0000000..f30d170
--- /dev/null
@@ -0,0 +1,2 @@
+1
+invalid token near '''
diff --git a/test/suites/invalid/apostrophe/input b/test/suites/invalid/apostrophe/input
new file mode 100644 (file)
index 0000000..f2dd4d2
--- /dev/null
@@ -0,0 +1 @@
+['
diff --git a/test/suites/invalid/ascii-unicode-identifier/error b/test/suites/invalid/ascii-unicode-identifier/error
new file mode 100644 (file)
index 0000000..7469919
--- /dev/null
@@ -0,0 +1,2 @@
+1
+'[' or '{' expected near 'a'
diff --git a/test/suites/invalid/ascii-unicode-identifier/input b/test/suites/invalid/ascii-unicode-identifier/input
new file mode 100644 (file)
index 0000000..c2c0208
--- /dev/null
@@ -0,0 +1 @@
+aå
diff --git a/test/suites/invalid/brace-comma/error b/test/suites/invalid/brace-comma/error
new file mode 100644 (file)
index 0000000..394ad5a
--- /dev/null
@@ -0,0 +1,2 @@
+1
+string or '}' expected near ','
diff --git a/test/suites/invalid/brace-comma/input b/test/suites/invalid/brace-comma/input
new file mode 100644 (file)
index 0000000..74a6628
--- /dev/null
@@ -0,0 +1 @@
+{,
diff --git a/test/suites/invalid/bracket-comma/error b/test/suites/invalid/bracket-comma/error
new file mode 100644 (file)
index 0000000..54c3924
--- /dev/null
@@ -0,0 +1,2 @@
+1
+unexpected token near ','
diff --git a/test/suites/invalid/bracket-comma/input b/test/suites/invalid/bracket-comma/input
new file mode 100644 (file)
index 0000000..5b911f1
--- /dev/null
@@ -0,0 +1 @@
+[,
diff --git a/test/suites/invalid/bracket-one-comma/error b/test/suites/invalid/bracket-one-comma/error
new file mode 100644 (file)
index 0000000..41fd6d6
--- /dev/null
@@ -0,0 +1,2 @@
+2
+']' expected near end of file
diff --git a/test/suites/invalid/bracket-one-comma/input b/test/suites/invalid/bracket-one-comma/input
new file mode 100644 (file)
index 0000000..874691b
--- /dev/null
@@ -0,0 +1 @@
+[1,
diff --git a/test/suites/invalid/empty/error b/test/suites/invalid/empty/error
new file mode 100644 (file)
index 0000000..ff19200
--- /dev/null
@@ -0,0 +1,2 @@
+1
+'[' or '{' expected near end of file
diff --git a/test/suites/invalid/empty/input b/test/suites/invalid/empty/input
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/test/suites/invalid/extra-comma-in-array/error b/test/suites/invalid/extra-comma-in-array/error
new file mode 100644 (file)
index 0000000..38afe71
--- /dev/null
@@ -0,0 +1,2 @@
+1
+unexpected token near ']'
diff --git a/test/suites/invalid/extra-comma-in-array/input b/test/suites/invalid/extra-comma-in-array/input
new file mode 100644 (file)
index 0000000..e8b1a17
--- /dev/null
@@ -0,0 +1 @@
+[1,]
diff --git a/test/suites/invalid/extra-command-in-multiline-array/error b/test/suites/invalid/extra-command-in-multiline-array/error
new file mode 100644 (file)
index 0000000..26c2152
--- /dev/null
@@ -0,0 +1,2 @@
+6
+unexpected token near ']'
diff --git a/test/suites/invalid/extra-command-in-multiline-array/input b/test/suites/invalid/extra-command-in-multiline-array/input
new file mode 100644 (file)
index 0000000..bcb2a75
--- /dev/null
@@ -0,0 +1,6 @@
+[1,
+2,
+3,
+4,
+5,
+]
diff --git a/test/suites/invalid/garbage-after-newline/error b/test/suites/invalid/garbage-after-newline/error
new file mode 100644 (file)
index 0000000..90eacdd
--- /dev/null
@@ -0,0 +1,2 @@
+2
+end of file expected near 'foo'
diff --git a/test/suites/invalid/garbage-after-newline/input b/test/suites/invalid/garbage-after-newline/input
new file mode 100644 (file)
index 0000000..3614ac7
--- /dev/null
@@ -0,0 +1,2 @@
+[1,2,3]
+foo
diff --git a/test/suites/invalid/garbage-at-the-end/error b/test/suites/invalid/garbage-at-the-end/error
new file mode 100644 (file)
index 0000000..9076aec
--- /dev/null
@@ -0,0 +1,2 @@
+1
+end of file expected near 'foo'
diff --git a/test/suites/invalid/garbage-at-the-end/input b/test/suites/invalid/garbage-at-the-end/input
new file mode 100644 (file)
index 0000000..55aee53
--- /dev/null
@@ -0,0 +1 @@
+[1,2,3]foo
diff --git a/test/suites/invalid/integer-starting-with-zero/error b/test/suites/invalid/integer-starting-with-zero/error
new file mode 100644 (file)
index 0000000..e4eee2f
--- /dev/null
@@ -0,0 +1,2 @@
+1
+invalid token near '0'
diff --git a/test/suites/invalid/integer-starting-with-zero/input b/test/suites/invalid/integer-starting-with-zero/input
new file mode 100644 (file)
index 0000000..12f67e2
--- /dev/null
@@ -0,0 +1 @@
+[012]
diff --git a/test/suites/invalid/invalid-escape/error b/test/suites/invalid/invalid-escape/error
new file mode 100644 (file)
index 0000000..9930ca7
--- /dev/null
@@ -0,0 +1,2 @@
+1
+invalid escape near '"\'
diff --git a/test/suites/invalid/invalid-escape/input b/test/suites/invalid/invalid-escape/input
new file mode 100644 (file)
index 0000000..64c7b70
--- /dev/null
@@ -0,0 +1 @@
+["\a <-- invalid escape"]
diff --git a/test/suites/invalid/invalid-identifier/error b/test/suites/invalid/invalid-identifier/error
new file mode 100644 (file)
index 0000000..0f88417
--- /dev/null
@@ -0,0 +1,2 @@
+1
+invalid token near 'troo'
diff --git a/test/suites/invalid/invalid-identifier/input b/test/suites/invalid/invalid-identifier/input
new file mode 100644 (file)
index 0000000..3d2860d
--- /dev/null
@@ -0,0 +1 @@
+[troo
diff --git a/test/suites/invalid/invalid-negative-integerr/error b/test/suites/invalid/invalid-negative-integerr/error
new file mode 100644 (file)
index 0000000..f9d930e
--- /dev/null
@@ -0,0 +1,2 @@
+1
+']' expected near 'foo'
diff --git a/test/suites/invalid/invalid-negative-integerr/input b/test/suites/invalid/invalid-negative-integerr/input
new file mode 100644 (file)
index 0000000..6196980
--- /dev/null
@@ -0,0 +1 @@
+[-123foo]
diff --git a/test/suites/invalid/invalid-negative-real/error b/test/suites/invalid/invalid-negative-real/error
new file mode 100644 (file)
index 0000000..f9d930e
--- /dev/null
@@ -0,0 +1,2 @@
+1
+']' expected near 'foo'
diff --git a/test/suites/invalid/invalid-negative-real/input b/test/suites/invalid/invalid-negative-real/input
new file mode 100644 (file)
index 0000000..3c763d3
--- /dev/null
@@ -0,0 +1 @@
+[-123.123foo]
diff --git a/test/suites/invalid/invalid-second-surrogate/error b/test/suites/invalid/invalid-second-surrogate/error
new file mode 100644 (file)
index 0000000..22d0e1f
--- /dev/null
@@ -0,0 +1,2 @@
+1
+invalid Unicode '\uD888\u3210'
diff --git a/test/suites/invalid/invalid-second-surrogate/input b/test/suites/invalid/invalid-second-surrogate/input
new file mode 100644 (file)
index 0000000..b21453f
--- /dev/null
@@ -0,0 +1 @@
+["\uD888\u3210 (first surrogate and invalid second surrogate)"]
diff --git a/test/suites/invalid/lone-open-brace/error b/test/suites/invalid/lone-open-brace/error
new file mode 100644 (file)
index 0000000..a31caf7
--- /dev/null
@@ -0,0 +1,2 @@
+2
+string or '}' expected near end of file
diff --git a/test/suites/invalid/lone-open-brace/input b/test/suites/invalid/lone-open-brace/input
new file mode 100644 (file)
index 0000000..98232c6
--- /dev/null
@@ -0,0 +1 @@
+{
diff --git a/test/suites/invalid/lone-open-bracket/error b/test/suites/invalid/lone-open-bracket/error
new file mode 100644 (file)
index 0000000..41fd6d6
--- /dev/null
@@ -0,0 +1,2 @@
+2
+']' expected near end of file
diff --git a/test/suites/invalid/lone-open-bracket/input b/test/suites/invalid/lone-open-bracket/input
new file mode 100644 (file)
index 0000000..558ed37
--- /dev/null
@@ -0,0 +1 @@
+[
diff --git a/test/suites/invalid/lone-second-surrogate/error b/test/suites/invalid/lone-second-surrogate/error
new file mode 100644 (file)
index 0000000..c96d7f5
--- /dev/null
@@ -0,0 +1,2 @@
+1
+invalid Unicode '\uDFAA'
diff --git a/test/suites/invalid/lone-second-surrogate/input b/test/suites/invalid/lone-second-surrogate/input
new file mode 100644 (file)
index 0000000..328e35c
--- /dev/null
@@ -0,0 +1 @@
+["\uDFAA (second surrogate on it's own)"]
diff --git a/test/suites/invalid/minus-sign-without-number/error b/test/suites/invalid/minus-sign-without-number/error
new file mode 100644 (file)
index 0000000..e978234
--- /dev/null
@@ -0,0 +1,2 @@
+1
+invalid token near '-'
diff --git a/test/suites/invalid/minus-sign-without-number/input b/test/suites/invalid/minus-sign-without-number/input
new file mode 100644 (file)
index 0000000..0337883
--- /dev/null
@@ -0,0 +1 @@
+[-foo]
diff --git a/test/suites/invalid/negative-integer-starting-with-zero/error b/test/suites/invalid/negative-integer-starting-with-zero/error
new file mode 100644 (file)
index 0000000..a758d94
--- /dev/null
@@ -0,0 +1,2 @@
+1
+invalid token near '-0'
diff --git a/test/suites/invalid/negative-integer-starting-with-zero/input b/test/suites/invalid/negative-integer-starting-with-zero/input
new file mode 100644 (file)
index 0000000..6fbb7a2
--- /dev/null
@@ -0,0 +1 @@
+[-012]
diff --git a/test/suites/invalid/null-byte-in-string/error b/test/suites/invalid/null-byte-in-string/error
new file mode 100644 (file)
index 0000000..0fa36e2
--- /dev/null
@@ -0,0 +1,2 @@
+1
+\u0000 is not allowed
diff --git a/test/suites/invalid/null-byte-in-string/input b/test/suites/invalid/null-byte-in-string/input
new file mode 100644 (file)
index 0000000..22ae82b
--- /dev/null
@@ -0,0 +1 @@
+["\u0000 (null byte not allowed)"]
diff --git a/test/suites/invalid/null/error b/test/suites/invalid/null/error
new file mode 100644 (file)
index 0000000..de9280b
--- /dev/null
@@ -0,0 +1,2 @@
+1
+'[' or '{' expected near 'null'
diff --git a/test/suites/invalid/null/input b/test/suites/invalid/null/input
new file mode 100644 (file)
index 0000000..19765bd
--- /dev/null
@@ -0,0 +1 @@
+null
diff --git a/test/suites/invalid/object-apostrophes/error b/test/suites/invalid/object-apostrophes/error
new file mode 100644 (file)
index 0000000..f7b586a
--- /dev/null
@@ -0,0 +1,2 @@
+1
+string or '}' expected near '''
diff --git a/test/suites/invalid/object-apostrophes/input b/test/suites/invalid/object-apostrophes/input
new file mode 100644 (file)
index 0000000..52b2905
--- /dev/null
@@ -0,0 +1 @@
+{'a'
diff --git a/test/suites/invalid/object-garbage-at-end/error b/test/suites/invalid/object-garbage-at-end/error
new file mode 100644 (file)
index 0000000..a60dc47
--- /dev/null
@@ -0,0 +1,2 @@
+1
+'}' expected near '123'
diff --git a/test/suites/invalid/object-garbage-at-end/input b/test/suites/invalid/object-garbage-at-end/input
new file mode 100644 (file)
index 0000000..62c19d7
--- /dev/null
@@ -0,0 +1 @@
+{"a":"a" 123}
diff --git a/test/suites/invalid/object-in-unterminated-array/error b/test/suites/invalid/object-in-unterminated-array/error
new file mode 100644 (file)
index 0000000..41fd6d6
--- /dev/null
@@ -0,0 +1,2 @@
+2
+']' expected near end of file
diff --git a/test/suites/invalid/object-in-unterminated-array/input b/test/suites/invalid/object-in-unterminated-array/input
new file mode 100644 (file)
index 0000000..ca9ec37
--- /dev/null
@@ -0,0 +1 @@
+[{}
diff --git a/test/suites/invalid/object-no-colon/error b/test/suites/invalid/object-no-colon/error
new file mode 100644 (file)
index 0000000..80c9249
--- /dev/null
@@ -0,0 +1,2 @@
+2
+':' expected near end of file
diff --git a/test/suites/invalid/object-no-colon/input b/test/suites/invalid/object-no-colon/input
new file mode 100644 (file)
index 0000000..107e626
--- /dev/null
@@ -0,0 +1 @@
+{"a"
diff --git a/test/suites/invalid/object-no-value/error b/test/suites/invalid/object-no-value/error
new file mode 100644 (file)
index 0000000..f252060
--- /dev/null
@@ -0,0 +1,2 @@
+2
+unexpected token near end of file
diff --git a/test/suites/invalid/object-no-value/input b/test/suites/invalid/object-no-value/input
new file mode 100644 (file)
index 0000000..f68f262
--- /dev/null
@@ -0,0 +1 @@
+{"a":
diff --git a/test/suites/invalid/object-unterminated-value/error b/test/suites/invalid/object-unterminated-value/error
new file mode 100644 (file)
index 0000000..9b725a2
--- /dev/null
@@ -0,0 +1,2 @@
+1
+unexpected newline near '"a'
diff --git a/test/suites/invalid/object-unterminated-value/input b/test/suites/invalid/object-unterminated-value/input
new file mode 100644 (file)
index 0000000..b854d7e
--- /dev/null
@@ -0,0 +1 @@
+{"a":"a
diff --git a/test/suites/invalid/real-garbage-after-e/error b/test/suites/invalid/real-garbage-after-e/error
new file mode 100644 (file)
index 0000000..d9b83e7
--- /dev/null
@@ -0,0 +1,2 @@
+1
+invalid token near '1e'
diff --git a/test/suites/invalid/real-garbage-after-e/input b/test/suites/invalid/real-garbage-after-e/input
new file mode 100644 (file)
index 0000000..6a945ac
--- /dev/null
@@ -0,0 +1 @@
+[1ea]
diff --git a/test/suites/invalid/real-negative-overflow/error b/test/suites/invalid/real-negative-overflow/error
new file mode 100644 (file)
index 0000000..af5a850
--- /dev/null
@@ -0,0 +1,2 @@
+1
+real number overflow near '-123123e100000'
diff --git a/test/suites/invalid/real-negative-overflow/input b/test/suites/invalid/real-negative-overflow/input
new file mode 100644 (file)
index 0000000..b5bd21c
--- /dev/null
@@ -0,0 +1 @@
+[-123123e100000]
diff --git a/test/suites/invalid/real-positive-overflow/error b/test/suites/invalid/real-positive-overflow/error
new file mode 100644 (file)
index 0000000..e9becc3
--- /dev/null
@@ -0,0 +1,2 @@
+1
+real number overflow near '123123e100000'
diff --git a/test/suites/invalid/real-positive-overflow/input b/test/suites/invalid/real-positive-overflow/input
new file mode 100644 (file)
index 0000000..524e53b
--- /dev/null
@@ -0,0 +1 @@
+[123123e100000]
diff --git a/test/suites/invalid/real-truncated-at-e/error b/test/suites/invalid/real-truncated-at-e/error
new file mode 100644 (file)
index 0000000..d9b83e7
--- /dev/null
@@ -0,0 +1,2 @@
+1
+invalid token near '1e'
diff --git a/test/suites/invalid/real-truncated-at-e/input b/test/suites/invalid/real-truncated-at-e/input
new file mode 100644 (file)
index 0000000..1d67b7b
--- /dev/null
@@ -0,0 +1 @@
+[1e]
diff --git a/test/suites/invalid/real-truncated-at-point/error b/test/suites/invalid/real-truncated-at-point/error
new file mode 100644 (file)
index 0000000..7312401
--- /dev/null
@@ -0,0 +1,2 @@
+1
+invalid token near '1.'
diff --git a/test/suites/invalid/real-truncated-at-point/input b/test/suites/invalid/real-truncated-at-point/input
new file mode 100644 (file)
index 0000000..b652b3f
--- /dev/null
@@ -0,0 +1 @@
+[1.]
diff --git a/test/suites/invalid/real-underflow/error b/test/suites/invalid/real-underflow/error
new file mode 100644 (file)
index 0000000..1b65d40
--- /dev/null
@@ -0,0 +1,2 @@
+1
+real number underflow near '123e-10000000'
diff --git a/test/suites/invalid/real-underflow/input b/test/suites/invalid/real-underflow/input
new file mode 100644 (file)
index 0000000..dc70996
--- /dev/null
@@ -0,0 +1 @@
+[123e-10000000]
diff --git a/test/suites/invalid/run b/test/suites/invalid/run
new file mode 100755 (executable)
index 0000000..1c6130d
--- /dev/null
@@ -0,0 +1,27 @@
+#!/bin/sh
+#
+# Copyright (c) 2009 Petri Lehtinen <petri@digip.org>
+#
+# Jansson is free software; you can redistribute it and/or modify
+# it under the terms of the MIT license. See LICENSE for details.
+
+is_test() {
+    test -d $test_path
+}
+
+run_test() {
+    $json_process <$test_path/input >$test_log/stdout 2>$test_log/stderr
+    valgrind_check $test_log/stderr || return 1
+    cmp -s $test_path/error $test_log/stderr
+}
+
+show_error() {
+    valgrind_show_error && return
+
+    echo "EXPECTED ERROR:"
+    nl -bn $test_path/error
+    echo "ACTUAL ERROR:"
+    nl -bn $test_log/stderr
+}
+
+. $top_srcdir/test/scripts/run-tests.sh
diff --git a/test/suites/invalid/tab-character-in-string/error b/test/suites/invalid/tab-character-in-string/error
new file mode 100644 (file)
index 0000000..ee81a5f
--- /dev/null
@@ -0,0 +1,2 @@
+1
+control character 0x9 near '"'
diff --git a/test/suites/invalid/tab-character-in-string/input b/test/suites/invalid/tab-character-in-string/input
new file mode 100644 (file)
index 0000000..3ebae09
--- /dev/null
@@ -0,0 +1 @@
+["      <-- tab character"]
diff --git a/test/suites/invalid/too-big-negative-integer/error b/test/suites/invalid/too-big-negative-integer/error
new file mode 100644 (file)
index 0000000..4245f39
--- /dev/null
@@ -0,0 +1,2 @@
+1
+too big negative integer near '-123123123123123'
diff --git a/test/suites/invalid/too-big-negative-integer/input b/test/suites/invalid/too-big-negative-integer/input
new file mode 100644 (file)
index 0000000..40ed1bb
--- /dev/null
@@ -0,0 +1 @@
+[-123123123123123]
diff --git a/test/suites/invalid/too-big-positive-integer/error b/test/suites/invalid/too-big-positive-integer/error
new file mode 100644 (file)
index 0000000..4761ad3
--- /dev/null
@@ -0,0 +1,2 @@
+1
+too big integer near '123123123123123'
diff --git a/test/suites/invalid/too-big-positive-integer/input b/test/suites/invalid/too-big-positive-integer/input
new file mode 100644 (file)
index 0000000..d045071
--- /dev/null
@@ -0,0 +1 @@
+[123123123123123]
diff --git a/test/suites/invalid/truncated-unicode-surrogate/error b/test/suites/invalid/truncated-unicode-surrogate/error
new file mode 100644 (file)
index 0000000..17b932d
--- /dev/null
@@ -0,0 +1,2 @@
+1
+invalid Unicode '\uDADA'
diff --git a/test/suites/invalid/truncated-unicode-surrogate/input b/test/suites/invalid/truncated-unicode-surrogate/input
new file mode 100644 (file)
index 0000000..2b340f4
--- /dev/null
@@ -0,0 +1 @@
+["\uDADA (first surrogate without the second)"]
diff --git a/test/suites/invalid/unicode-identifier/error b/test/suites/invalid/unicode-identifier/error
new file mode 100644 (file)
index 0000000..e3817fa
--- /dev/null
@@ -0,0 +1,2 @@
+1
+'[' or '{' expected near 'å'
diff --git a/test/suites/invalid/unicode-identifier/input b/test/suites/invalid/unicode-identifier/input
new file mode 100644 (file)
index 0000000..aad321c
--- /dev/null
@@ -0,0 +1 @@
diff --git a/test/suites/invalid/unterminated-array-and-object/error b/test/suites/invalid/unterminated-array-and-object/error
new file mode 100644 (file)
index 0000000..a31caf7
--- /dev/null
@@ -0,0 +1,2 @@
+2
+string or '}' expected near end of file
diff --git a/test/suites/invalid/unterminated-array-and-object/input b/test/suites/invalid/unterminated-array-and-object/input
new file mode 100644 (file)
index 0000000..cd9dc64
--- /dev/null
@@ -0,0 +1 @@
+[{
diff --git a/test/suites/invalid/unterminated-array/error b/test/suites/invalid/unterminated-array/error
new file mode 100644 (file)
index 0000000..41fd6d6
--- /dev/null
@@ -0,0 +1,2 @@
+2
+']' expected near end of file
diff --git a/test/suites/invalid/unterminated-array/input b/test/suites/invalid/unterminated-array/input
new file mode 100644 (file)
index 0000000..727ee81
--- /dev/null
@@ -0,0 +1 @@
+["a"
diff --git a/test/suites/invalid/unterminated-empty-key/error b/test/suites/invalid/unterminated-empty-key/error
new file mode 100644 (file)
index 0000000..6691458
--- /dev/null
@@ -0,0 +1,2 @@
+1
+unexpected newline near '"'
diff --git a/test/suites/invalid/unterminated-empty-key/input b/test/suites/invalid/unterminated-empty-key/input
new file mode 100644 (file)
index 0000000..4117452
--- /dev/null
@@ -0,0 +1 @@
+{"
diff --git a/test/suites/invalid/unterminated-key/error b/test/suites/invalid/unterminated-key/error
new file mode 100644 (file)
index 0000000..9b725a2
--- /dev/null
@@ -0,0 +1,2 @@
+1
+unexpected newline near '"a'
diff --git a/test/suites/invalid/unterminated-key/input b/test/suites/invalid/unterminated-key/input
new file mode 100644 (file)
index 0000000..705948c
--- /dev/null
@@ -0,0 +1 @@
+{"a
diff --git a/test/suites/invalid/unterminated-object-and-array/error b/test/suites/invalid/unterminated-object-and-array/error
new file mode 100644 (file)
index 0000000..5fe61b5
--- /dev/null
@@ -0,0 +1,2 @@
+1
+string or '}' expected near '['
diff --git a/test/suites/invalid/unterminated-object-and-array/input b/test/suites/invalid/unterminated-object-and-array/input
new file mode 100644 (file)
index 0000000..da35a86
--- /dev/null
@@ -0,0 +1 @@
+{[
diff --git a/test/suites/invalid/unterminated-string/error b/test/suites/invalid/unterminated-string/error
new file mode 100644 (file)
index 0000000..9b725a2
--- /dev/null
@@ -0,0 +1,2 @@
+1
+unexpected newline near '"a'
diff --git a/test/suites/invalid/unterminated-string/input b/test/suites/invalid/unterminated-string/input
new file mode 100644 (file)
index 0000000..38ab6b0
--- /dev/null
@@ -0,0 +1 @@
+["a
diff --git a/test/suites/valid-strip/complex-array/input b/test/suites/valid-strip/complex-array/input
new file mode 100644 (file)
index 0000000..7255532
--- /dev/null
@@ -0,0 +1,5 @@
+[1,2,3,4,
+"a", "b", "c",
+{"foo": "bar", "core": "dump"},
+true, false, true, true, null, false
+]
\ No newline at end of file
diff --git a/test/suites/valid-strip/complex-array/output b/test/suites/valid-strip/complex-array/output
new file mode 100644 (file)
index 0000000..7aefe56
--- /dev/null
@@ -0,0 +1 @@
+[1, 2, 3, 4, "a", "b", "c", {"core": "dump", "foo": "bar"}, true, false, true, true, null, false]
\ No newline at end of file
diff --git a/test/suites/valid-strip/empty-array/input b/test/suites/valid-strip/empty-array/input
new file mode 100644 (file)
index 0000000..0637a08
--- /dev/null
@@ -0,0 +1 @@
+[]
\ No newline at end of file
diff --git a/test/suites/valid-strip/empty-array/output b/test/suites/valid-strip/empty-array/output
new file mode 100644 (file)
index 0000000..0637a08
--- /dev/null
@@ -0,0 +1 @@
+[]
\ No newline at end of file
diff --git a/test/suites/valid-strip/empty-object-in-array/input b/test/suites/valid-strip/empty-object-in-array/input
new file mode 100644 (file)
index 0000000..ee1aac4
--- /dev/null
@@ -0,0 +1 @@
+[{}]
\ No newline at end of file
diff --git a/test/suites/valid-strip/empty-object-in-array/output b/test/suites/valid-strip/empty-object-in-array/output
new file mode 100644 (file)
index 0000000..ee1aac4
--- /dev/null
@@ -0,0 +1 @@
+[{}]
\ No newline at end of file
diff --git a/test/suites/valid-strip/empty-object/input b/test/suites/valid-strip/empty-object/input
new file mode 100644 (file)
index 0000000..9e26dfe
--- /dev/null
@@ -0,0 +1 @@
+{}
\ No newline at end of file
diff --git a/test/suites/valid-strip/empty-object/output b/test/suites/valid-strip/empty-object/output
new file mode 100644 (file)
index 0000000..9e26dfe
--- /dev/null
@@ -0,0 +1 @@
+{}
\ No newline at end of file
diff --git a/test/suites/valid-strip/empty-string/input b/test/suites/valid-strip/empty-string/input
new file mode 100644 (file)
index 0000000..93b6be2
--- /dev/null
@@ -0,0 +1 @@
+[""]
\ No newline at end of file
diff --git a/test/suites/valid-strip/empty-string/output b/test/suites/valid-strip/empty-string/output
new file mode 100644 (file)
index 0000000..93b6be2
--- /dev/null
@@ -0,0 +1 @@
+[""]
\ No newline at end of file
diff --git a/test/suites/valid-strip/escaped-utf-control-char/input b/test/suites/valid-strip/escaped-utf-control-char/input
new file mode 100644 (file)
index 0000000..07221b7
--- /dev/null
@@ -0,0 +1 @@
+["\u0012 escaped control character"]
\ No newline at end of file
diff --git a/test/suites/valid-strip/escaped-utf-control-char/output b/test/suites/valid-strip/escaped-utf-control-char/output
new file mode 100644 (file)
index 0000000..07221b7
--- /dev/null
@@ -0,0 +1 @@
+["\u0012 escaped control character"]
\ No newline at end of file
diff --git a/test/suites/valid-strip/false/input b/test/suites/valid-strip/false/input
new file mode 100644 (file)
index 0000000..67b2f07
--- /dev/null
@@ -0,0 +1 @@
+[false]
\ No newline at end of file
diff --git a/test/suites/valid-strip/false/output b/test/suites/valid-strip/false/output
new file mode 100644 (file)
index 0000000..67b2f07
--- /dev/null
@@ -0,0 +1 @@
+[false]
\ No newline at end of file
diff --git a/test/suites/valid-strip/negative-int/input b/test/suites/valid-strip/negative-int/input
new file mode 100644 (file)
index 0000000..8e30f8b
--- /dev/null
@@ -0,0 +1 @@
+[-123]
\ No newline at end of file
diff --git a/test/suites/valid-strip/negative-int/output b/test/suites/valid-strip/negative-int/output
new file mode 100644 (file)
index 0000000..8e30f8b
--- /dev/null
@@ -0,0 +1 @@
+[-123]
\ No newline at end of file
diff --git a/test/suites/valid-strip/negative-one/input b/test/suites/valid-strip/negative-one/input
new file mode 100644 (file)
index 0000000..99d21a2
--- /dev/null
@@ -0,0 +1 @@
+[-1]
\ No newline at end of file
diff --git a/test/suites/valid-strip/negative-one/output b/test/suites/valid-strip/negative-one/output
new file mode 100644 (file)
index 0000000..99d21a2
--- /dev/null
@@ -0,0 +1 @@
+[-1]
\ No newline at end of file
diff --git a/test/suites/valid-strip/negative-zero/input b/test/suites/valid-strip/negative-zero/input
new file mode 100644 (file)
index 0000000..37af131
--- /dev/null
@@ -0,0 +1 @@
+[-0]
\ No newline at end of file
diff --git a/test/suites/valid-strip/negative-zero/output b/test/suites/valid-strip/negative-zero/output
new file mode 100644 (file)
index 0000000..6e7ea63
--- /dev/null
@@ -0,0 +1 @@
+[0]
\ No newline at end of file
diff --git a/test/suites/valid-strip/null/input b/test/suites/valid-strip/null/input
new file mode 100644 (file)
index 0000000..500db4a
--- /dev/null
@@ -0,0 +1 @@
+[null]
\ No newline at end of file
diff --git a/test/suites/valid-strip/null/output b/test/suites/valid-strip/null/output
new file mode 100644 (file)
index 0000000..500db4a
--- /dev/null
@@ -0,0 +1 @@
+[null]
\ No newline at end of file
diff --git a/test/suites/valid-strip/one-byte-utf-8/input b/test/suites/valid-strip/one-byte-utf-8/input
new file mode 100644 (file)
index 0000000..f26edd0
--- /dev/null
@@ -0,0 +1 @@
+["\u002c one-byte UTF-8"]
\ No newline at end of file
diff --git a/test/suites/valid-strip/one-byte-utf-8/output b/test/suites/valid-strip/one-byte-utf-8/output
new file mode 100644 (file)
index 0000000..c33d250
--- /dev/null
@@ -0,0 +1 @@
+[", one-byte UTF-8"]
\ No newline at end of file
diff --git a/test/suites/valid-strip/real-capital-e-negative-exponent/input b/test/suites/valid-strip/real-capital-e-negative-exponent/input
new file mode 100644 (file)
index 0000000..0a01bd3
--- /dev/null
@@ -0,0 +1 @@
+[1E-2]
\ No newline at end of file
diff --git a/test/suites/valid-strip/real-capital-e-negative-exponent/output b/test/suites/valid-strip/real-capital-e-negative-exponent/output
new file mode 100644 (file)
index 0000000..75b9ef9
--- /dev/null
@@ -0,0 +1 @@
+[0.01]
\ No newline at end of file
diff --git a/test/suites/valid-strip/real-capital-e-positive-exponent/input b/test/suites/valid-strip/real-capital-e-positive-exponent/input
new file mode 100644 (file)
index 0000000..5a8fc09
--- /dev/null
@@ -0,0 +1 @@
+[1E+2]
\ No newline at end of file
diff --git a/test/suites/valid-strip/real-capital-e-positive-exponent/output b/test/suites/valid-strip/real-capital-e-positive-exponent/output
new file mode 100644 (file)
index 0000000..d8ff702
--- /dev/null
@@ -0,0 +1 @@
+[100.0]
\ No newline at end of file
diff --git a/test/suites/valid-strip/real-capital-e/input b/test/suites/valid-strip/real-capital-e/input
new file mode 100644 (file)
index 0000000..6edbdfc
--- /dev/null
@@ -0,0 +1 @@
+[1E22]
\ No newline at end of file
diff --git a/test/suites/valid-strip/real-capital-e/output b/test/suites/valid-strip/real-capital-e/output
new file mode 100644 (file)
index 0000000..88e90ce
--- /dev/null
@@ -0,0 +1 @@
+[1e+22]
\ No newline at end of file
diff --git a/test/suites/valid-strip/real-exponent/input b/test/suites/valid-strip/real-exponent/input
new file mode 100644 (file)
index 0000000..da2522d
--- /dev/null
@@ -0,0 +1 @@
+[123e45]
\ No newline at end of file
diff --git a/test/suites/valid-strip/real-exponent/output b/test/suites/valid-strip/real-exponent/output
new file mode 100644 (file)
index 0000000..ac910d6
--- /dev/null
@@ -0,0 +1 @@
+[1.2299999999999999e+47]
\ No newline at end of file
diff --git a/test/suites/valid-strip/real-fraction-exponent/input b/test/suites/valid-strip/real-fraction-exponent/input
new file mode 100644 (file)
index 0000000..3944a7a
--- /dev/null
@@ -0,0 +1 @@
+[123.456e78]
\ No newline at end of file
diff --git a/test/suites/valid-strip/real-fraction-exponent/output b/test/suites/valid-strip/real-fraction-exponent/output
new file mode 100644 (file)
index 0000000..4b87bda
--- /dev/null
@@ -0,0 +1 @@
+[1.23456e+80]
\ No newline at end of file
diff --git a/test/suites/valid-strip/real-negative-exponent/input b/test/suites/valid-strip/real-negative-exponent/input
new file mode 100644 (file)
index 0000000..ca40d3c
--- /dev/null
@@ -0,0 +1 @@
+[1e-2]
\ No newline at end of file
diff --git a/test/suites/valid-strip/real-negative-exponent/output b/test/suites/valid-strip/real-negative-exponent/output
new file mode 100644 (file)
index 0000000..75b9ef9
--- /dev/null
@@ -0,0 +1 @@
+[0.01]
\ No newline at end of file
diff --git a/test/suites/valid-strip/real-positive-exponent/input b/test/suites/valid-strip/real-positive-exponent/input
new file mode 100644 (file)
index 0000000..343601d
--- /dev/null
@@ -0,0 +1 @@
+[1e+2]
\ No newline at end of file
diff --git a/test/suites/valid-strip/real-positive-exponent/output b/test/suites/valid-strip/real-positive-exponent/output
new file mode 100644 (file)
index 0000000..d8ff702
--- /dev/null
@@ -0,0 +1 @@
+[100.0]
\ No newline at end of file
diff --git a/test/suites/valid-strip/run b/test/suites/valid-strip/run
new file mode 100755 (executable)
index 0000000..aec5649
--- /dev/null
@@ -0,0 +1,29 @@
+#!/bin/sh
+#
+# Copyright (c) 2009 Petri Lehtinen <petri@digip.org>
+#
+# Jansson is free software; you can redistribute it and/or modify
+# it under the terms of the MIT license. See LICENSE for details.
+
+export JSON_SORT_KEYS=1
+
+is_test() {
+    test -d $test_path
+}
+
+run_test() {
+    $json_process <$test_path/input >$test_log/stdout 2>$test_log/stderr
+    valgrind_check $test_log/stderr || return 1
+    cmp -s $test_path/output $test_log/stdout
+}
+
+show_error() {
+    valgrind_show_error && return
+
+    echo "EXPECTED OUTPUT:"
+    nl -bn $test_path/output
+    echo "ACTUAL OUTPUT:"
+    nl -bn $test_log/stdout
+}
+
+. $top_srcdir/test/scripts/run-tests.sh
diff --git a/test/suites/valid-strip/short-string/input b/test/suites/valid-strip/short-string/input
new file mode 100644 (file)
index 0000000..eac5f7b
--- /dev/null
@@ -0,0 +1 @@
+["a"]
\ No newline at end of file
diff --git a/test/suites/valid-strip/short-string/output b/test/suites/valid-strip/short-string/output
new file mode 100644 (file)
index 0000000..eac5f7b
--- /dev/null
@@ -0,0 +1 @@
+["a"]
\ No newline at end of file
diff --git a/test/suites/valid-strip/simple-ascii-string/input b/test/suites/valid-strip/simple-ascii-string/input
new file mode 100644 (file)
index 0000000..90358ab
--- /dev/null
@@ -0,0 +1 @@
+["abcdefghijklmnopqrstuvwxyz1234567890 "]
\ No newline at end of file
diff --git a/test/suites/valid-strip/simple-ascii-string/output b/test/suites/valid-strip/simple-ascii-string/output
new file mode 100644 (file)
index 0000000..90358ab
--- /dev/null
@@ -0,0 +1 @@
+["abcdefghijklmnopqrstuvwxyz1234567890 "]
\ No newline at end of file
diff --git a/test/suites/valid-strip/simple-int-0/input b/test/suites/valid-strip/simple-int-0/input
new file mode 100644 (file)
index 0000000..6e7ea63
--- /dev/null
@@ -0,0 +1 @@
+[0]
\ No newline at end of file
diff --git a/test/suites/valid-strip/simple-int-0/output b/test/suites/valid-strip/simple-int-0/output
new file mode 100644 (file)
index 0000000..6e7ea63
--- /dev/null
@@ -0,0 +1 @@
+[0]
\ No newline at end of file
diff --git a/test/suites/valid-strip/simple-int-1/input b/test/suites/valid-strip/simple-int-1/input
new file mode 100644 (file)
index 0000000..bace2a0
--- /dev/null
@@ -0,0 +1 @@
+[1]
\ No newline at end of file
diff --git a/test/suites/valid-strip/simple-int-1/output b/test/suites/valid-strip/simple-int-1/output
new file mode 100644 (file)
index 0000000..bace2a0
--- /dev/null
@@ -0,0 +1 @@
+[1]
\ No newline at end of file
diff --git a/test/suites/valid-strip/simple-int-123/input b/test/suites/valid-strip/simple-int-123/input
new file mode 100644 (file)
index 0000000..e47f69a
--- /dev/null
@@ -0,0 +1 @@
+[123]
\ No newline at end of file
diff --git a/test/suites/valid-strip/simple-int-123/output b/test/suites/valid-strip/simple-int-123/output
new file mode 100644 (file)
index 0000000..e47f69a
--- /dev/null
@@ -0,0 +1 @@
+[123]
\ No newline at end of file
diff --git a/test/suites/valid-strip/simple-object/input b/test/suites/valid-strip/simple-object/input
new file mode 100644 (file)
index 0000000..a34fb49
--- /dev/null
@@ -0,0 +1 @@
+{"a":[]}
diff --git a/test/suites/valid-strip/simple-object/output b/test/suites/valid-strip/simple-object/output
new file mode 100644 (file)
index 0000000..982abe8
--- /dev/null
@@ -0,0 +1 @@
+{"a": []}
\ No newline at end of file
diff --git a/test/suites/valid-strip/simple-real/input b/test/suites/valid-strip/simple-real/input
new file mode 100644 (file)
index 0000000..b02878e
--- /dev/null
@@ -0,0 +1 @@
+[123.456789]
\ No newline at end of file
diff --git a/test/suites/valid-strip/simple-real/output b/test/suites/valid-strip/simple-real/output
new file mode 100644 (file)
index 0000000..b02878e
--- /dev/null
@@ -0,0 +1 @@
+[123.456789]
\ No newline at end of file
diff --git a/test/suites/valid-strip/string-escapes/input b/test/suites/valid-strip/string-escapes/input
new file mode 100644 (file)
index 0000000..7f49553
--- /dev/null
@@ -0,0 +1 @@
+["\"\\\/\b\f\n\r\t"]
\ No newline at end of file
diff --git a/test/suites/valid-strip/string-escapes/output b/test/suites/valid-strip/string-escapes/output
new file mode 100644 (file)
index 0000000..ca5c1c6
--- /dev/null
@@ -0,0 +1 @@
+["\"\\/\b\f\n\r\t"]
\ No newline at end of file
diff --git a/test/suites/valid-strip/three-byte-utf-8/input b/test/suites/valid-strip/three-byte-utf-8/input
new file mode 100644 (file)
index 0000000..25dcede
--- /dev/null
@@ -0,0 +1 @@
+["\u0821 three-byte UTF-8"]
\ No newline at end of file
diff --git a/test/suites/valid-strip/three-byte-utf-8/output b/test/suites/valid-strip/three-byte-utf-8/output
new file mode 100644 (file)
index 0000000..c44d124
--- /dev/null
@@ -0,0 +1 @@
+["ࠡ three-byte UTF-8"]
\ No newline at end of file
diff --git a/test/suites/valid-strip/true/input b/test/suites/valid-strip/true/input
new file mode 100644 (file)
index 0000000..de601e3
--- /dev/null
@@ -0,0 +1 @@
+[true]
\ No newline at end of file
diff --git a/test/suites/valid-strip/true/output b/test/suites/valid-strip/true/output
new file mode 100644 (file)
index 0000000..de601e3
--- /dev/null
@@ -0,0 +1 @@
+[true]
\ No newline at end of file
diff --git a/test/suites/valid-strip/two-byte-utf-8/input b/test/suites/valid-strip/two-byte-utf-8/input
new file mode 100644 (file)
index 0000000..627046a
--- /dev/null
@@ -0,0 +1 @@
+["\u0123 two-byte UTF-8"]
\ No newline at end of file
diff --git a/test/suites/valid-strip/two-byte-utf-8/output b/test/suites/valid-strip/two-byte-utf-8/output
new file mode 100644 (file)
index 0000000..1f0988d
--- /dev/null
@@ -0,0 +1 @@
+["ģ two-byte UTF-8"]
\ No newline at end of file
diff --git a/test/suites/valid-strip/utf-8-string/input b/test/suites/valid-strip/utf-8-string/input
new file mode 100644 (file)
index 0000000..5372865
--- /dev/null
@@ -0,0 +1 @@
+["€þıœəßð some utf-8 ĸʒ×ŋµåäö𝄞"]
\ No newline at end of file
diff --git a/test/suites/valid-strip/utf-8-string/output b/test/suites/valid-strip/utf-8-string/output
new file mode 100644 (file)
index 0000000..5372865
--- /dev/null
@@ -0,0 +1 @@
+["€þıœəßð some utf-8 ĸʒ×ŋµåäö𝄞"]
\ No newline at end of file
diff --git a/test/suites/valid-strip/utf-surrogate-four-byte-encoding/input b/test/suites/valid-strip/utf-surrogate-four-byte-encoding/input
new file mode 100644 (file)
index 0000000..183855a
--- /dev/null
@@ -0,0 +1 @@
+["\uD834\uDD1E surrogate, four-byte UTF-8"]
\ No newline at end of file
diff --git a/test/suites/valid-strip/utf-surrogate-four-byte-encoding/output b/test/suites/valid-strip/utf-surrogate-four-byte-encoding/output
new file mode 100644 (file)
index 0000000..fa806d2
--- /dev/null
@@ -0,0 +1 @@
+["𝄞 surrogate, four-byte UTF-8"]
\ No newline at end of file
diff --git a/test/suites/valid/complex-array/input b/test/suites/valid/complex-array/input
new file mode 100644 (file)
index 0000000..1b9bbb9
--- /dev/null
@@ -0,0 +1,5 @@
+[1,2,3,4,
+"a", "b", "c",
+{"foo": "bar", "core": "dump"},
+true, false, true, true, null, false
+]
diff --git a/test/suites/valid/complex-array/output b/test/suites/valid/complex-array/output
new file mode 100644 (file)
index 0000000..7aefe56
--- /dev/null
@@ -0,0 +1 @@
+[1, 2, 3, 4, "a", "b", "c", {"core": "dump", "foo": "bar"}, true, false, true, true, null, false]
\ No newline at end of file
diff --git a/test/suites/valid/empty-array/input b/test/suites/valid/empty-array/input
new file mode 100644 (file)
index 0000000..fe51488
--- /dev/null
@@ -0,0 +1 @@
+[]
diff --git a/test/suites/valid/empty-array/output b/test/suites/valid/empty-array/output
new file mode 100644 (file)
index 0000000..0637a08
--- /dev/null
@@ -0,0 +1 @@
+[]
\ No newline at end of file
diff --git a/test/suites/valid/empty-object-in-array/input b/test/suites/valid/empty-object-in-array/input
new file mode 100644 (file)
index 0000000..93d5140
--- /dev/null
@@ -0,0 +1 @@
+[{}]
diff --git a/test/suites/valid/empty-object-in-array/output b/test/suites/valid/empty-object-in-array/output
new file mode 100644 (file)
index 0000000..ee1aac4
--- /dev/null
@@ -0,0 +1 @@
+[{}]
\ No newline at end of file
diff --git a/test/suites/valid/empty-object/input b/test/suites/valid/empty-object/input
new file mode 100644 (file)
index 0000000..0967ef4
--- /dev/null
@@ -0,0 +1 @@
+{}
diff --git a/test/suites/valid/empty-object/output b/test/suites/valid/empty-object/output
new file mode 100644 (file)
index 0000000..9e26dfe
--- /dev/null
@@ -0,0 +1 @@
+{}
\ No newline at end of file
diff --git a/test/suites/valid/empty-string/input b/test/suites/valid/empty-string/input
new file mode 100644 (file)
index 0000000..66a1e18
--- /dev/null
@@ -0,0 +1 @@
+[""]
diff --git a/test/suites/valid/empty-string/output b/test/suites/valid/empty-string/output
new file mode 100644 (file)
index 0000000..93b6be2
--- /dev/null
@@ -0,0 +1 @@
+[""]
\ No newline at end of file
diff --git a/test/suites/valid/escaped-utf-control-char/input b/test/suites/valid/escaped-utf-control-char/input
new file mode 100644 (file)
index 0000000..9a98545
--- /dev/null
@@ -0,0 +1 @@
+["\u0012 escaped control character"]
diff --git a/test/suites/valid/escaped-utf-control-char/output b/test/suites/valid/escaped-utf-control-char/output
new file mode 100644 (file)
index 0000000..07221b7
--- /dev/null
@@ -0,0 +1 @@
+["\u0012 escaped control character"]
\ No newline at end of file
diff --git a/test/suites/valid/false/input b/test/suites/valid/false/input
new file mode 100644 (file)
index 0000000..4343652
--- /dev/null
@@ -0,0 +1 @@
+[false]
diff --git a/test/suites/valid/false/output b/test/suites/valid/false/output
new file mode 100644 (file)
index 0000000..67b2f07
--- /dev/null
@@ -0,0 +1 @@
+[false]
\ No newline at end of file
diff --git a/test/suites/valid/negative-int/input b/test/suites/valid/negative-int/input
new file mode 100644 (file)
index 0000000..a96d5cd
--- /dev/null
@@ -0,0 +1 @@
+[-123]
diff --git a/test/suites/valid/negative-int/output b/test/suites/valid/negative-int/output
new file mode 100644 (file)
index 0000000..8e30f8b
--- /dev/null
@@ -0,0 +1 @@
+[-123]
\ No newline at end of file
diff --git a/test/suites/valid/negative-one/input b/test/suites/valid/negative-one/input
new file mode 100644 (file)
index 0000000..2363a1a
--- /dev/null
@@ -0,0 +1 @@
+[-1]
diff --git a/test/suites/valid/negative-one/output b/test/suites/valid/negative-one/output
new file mode 100644 (file)
index 0000000..99d21a2
--- /dev/null
@@ -0,0 +1 @@
+[-1]
\ No newline at end of file
diff --git a/test/suites/valid/negative-zero/input b/test/suites/valid/negative-zero/input
new file mode 100644 (file)
index 0000000..40fc49c
--- /dev/null
@@ -0,0 +1 @@
+[-0]
diff --git a/test/suites/valid/negative-zero/output b/test/suites/valid/negative-zero/output
new file mode 100644 (file)
index 0000000..6e7ea63
--- /dev/null
@@ -0,0 +1 @@
+[0]
\ No newline at end of file
diff --git a/test/suites/valid/null/input b/test/suites/valid/null/input
new file mode 100644 (file)
index 0000000..62864b3
--- /dev/null
@@ -0,0 +1 @@
+[null]
diff --git a/test/suites/valid/null/output b/test/suites/valid/null/output
new file mode 100644 (file)
index 0000000..500db4a
--- /dev/null
@@ -0,0 +1 @@
+[null]
\ No newline at end of file
diff --git a/test/suites/valid/one-byte-utf-8/input b/test/suites/valid/one-byte-utf-8/input
new file mode 100644 (file)
index 0000000..8bda468
--- /dev/null
@@ -0,0 +1 @@
+["\u002c one-byte UTF-8"]
diff --git a/test/suites/valid/one-byte-utf-8/output b/test/suites/valid/one-byte-utf-8/output
new file mode 100644 (file)
index 0000000..c33d250
--- /dev/null
@@ -0,0 +1 @@
+[", one-byte UTF-8"]
\ No newline at end of file
diff --git a/test/suites/valid/real-capital-e-negative-exponent/input b/test/suites/valid/real-capital-e-negative-exponent/input
new file mode 100644 (file)
index 0000000..1e9fa51
--- /dev/null
@@ -0,0 +1 @@
+[1E-2]
diff --git a/test/suites/valid/real-capital-e-negative-exponent/output b/test/suites/valid/real-capital-e-negative-exponent/output
new file mode 100644 (file)
index 0000000..75b9ef9
--- /dev/null
@@ -0,0 +1 @@
+[0.01]
\ No newline at end of file
diff --git a/test/suites/valid/real-capital-e-positive-exponent/input b/test/suites/valid/real-capital-e-positive-exponent/input
new file mode 100644 (file)
index 0000000..6a6ab93
--- /dev/null
@@ -0,0 +1 @@
+[1E+2]
diff --git a/test/suites/valid/real-capital-e-positive-exponent/output b/test/suites/valid/real-capital-e-positive-exponent/output
new file mode 100644 (file)
index 0000000..d8ff702
--- /dev/null
@@ -0,0 +1 @@
+[100.0]
\ No newline at end of file
diff --git a/test/suites/valid/real-capital-e/input b/test/suites/valid/real-capital-e/input
new file mode 100644 (file)
index 0000000..e703223
--- /dev/null
@@ -0,0 +1 @@
+[1E22]
diff --git a/test/suites/valid/real-capital-e/output b/test/suites/valid/real-capital-e/output
new file mode 100644 (file)
index 0000000..88e90ce
--- /dev/null
@@ -0,0 +1 @@
+[1e+22]
\ No newline at end of file
diff --git a/test/suites/valid/real-exponent/input b/test/suites/valid/real-exponent/input
new file mode 100644 (file)
index 0000000..b2a69b9
--- /dev/null
@@ -0,0 +1 @@
+[123e45]
diff --git a/test/suites/valid/real-exponent/output b/test/suites/valid/real-exponent/output
new file mode 100644 (file)
index 0000000..ac910d6
--- /dev/null
@@ -0,0 +1 @@
+[1.2299999999999999e+47]
\ No newline at end of file
diff --git a/test/suites/valid/real-fraction-exponent/input b/test/suites/valid/real-fraction-exponent/input
new file mode 100644 (file)
index 0000000..0c1660d
--- /dev/null
@@ -0,0 +1 @@
+[123.456e78]
diff --git a/test/suites/valid/real-fraction-exponent/output b/test/suites/valid/real-fraction-exponent/output
new file mode 100644 (file)
index 0000000..4b87bda
--- /dev/null
@@ -0,0 +1 @@
+[1.23456e+80]
\ No newline at end of file
diff --git a/test/suites/valid/real-negative-exponent/input b/test/suites/valid/real-negative-exponent/input
new file mode 100644 (file)
index 0000000..daa4af9
--- /dev/null
@@ -0,0 +1 @@
+[1e-2]
diff --git a/test/suites/valid/real-negative-exponent/output b/test/suites/valid/real-negative-exponent/output
new file mode 100644 (file)
index 0000000..75b9ef9
--- /dev/null
@@ -0,0 +1 @@
+[0.01]
\ No newline at end of file
diff --git a/test/suites/valid/real-positive-exponent/input b/test/suites/valid/real-positive-exponent/input
new file mode 100644 (file)
index 0000000..f378077
--- /dev/null
@@ -0,0 +1 @@
+[1e+2]
diff --git a/test/suites/valid/real-positive-exponent/output b/test/suites/valid/real-positive-exponent/output
new file mode 100644 (file)
index 0000000..d8ff702
--- /dev/null
@@ -0,0 +1 @@
+[100.0]
\ No newline at end of file
diff --git a/test/suites/valid/run b/test/suites/valid/run
new file mode 100755 (executable)
index 0000000..aec5649
--- /dev/null
@@ -0,0 +1,29 @@
+#!/bin/sh
+#
+# Copyright (c) 2009 Petri Lehtinen <petri@digip.org>
+#
+# Jansson is free software; you can redistribute it and/or modify
+# it under the terms of the MIT license. See LICENSE for details.
+
+export JSON_SORT_KEYS=1
+
+is_test() {
+    test -d $test_path
+}
+
+run_test() {
+    $json_process <$test_path/input >$test_log/stdout 2>$test_log/stderr
+    valgrind_check $test_log/stderr || return 1
+    cmp -s $test_path/output $test_log/stdout
+}
+
+show_error() {
+    valgrind_show_error && return
+
+    echo "EXPECTED OUTPUT:"
+    nl -bn $test_path/output
+    echo "ACTUAL OUTPUT:"
+    nl -bn $test_log/stdout
+}
+
+. $top_srcdir/test/scripts/run-tests.sh
diff --git a/test/suites/valid/short-string/input b/test/suites/valid/short-string/input
new file mode 100644 (file)
index 0000000..0c3426d
--- /dev/null
@@ -0,0 +1 @@
+["a"]
diff --git a/test/suites/valid/short-string/output b/test/suites/valid/short-string/output
new file mode 100644 (file)
index 0000000..eac5f7b
--- /dev/null
@@ -0,0 +1 @@
+["a"]
\ No newline at end of file
diff --git a/test/suites/valid/simple-ascii-string/input b/test/suites/valid/simple-ascii-string/input
new file mode 100644 (file)
index 0000000..929b215
--- /dev/null
@@ -0,0 +1 @@
+["abcdefghijklmnopqrstuvwxyz1234567890 "]
diff --git a/test/suites/valid/simple-ascii-string/output b/test/suites/valid/simple-ascii-string/output
new file mode 100644 (file)
index 0000000..90358ab
--- /dev/null
@@ -0,0 +1 @@
+["abcdefghijklmnopqrstuvwxyz1234567890 "]
\ No newline at end of file
diff --git a/test/suites/valid/simple-int-0/input b/test/suites/valid/simple-int-0/input
new file mode 100644 (file)
index 0000000..111bb86
--- /dev/null
@@ -0,0 +1 @@
+[0]
diff --git a/test/suites/valid/simple-int-0/output b/test/suites/valid/simple-int-0/output
new file mode 100644 (file)
index 0000000..6e7ea63
--- /dev/null
@@ -0,0 +1 @@
+[0]
\ No newline at end of file
diff --git a/test/suites/valid/simple-int-1/input b/test/suites/valid/simple-int-1/input
new file mode 100644 (file)
index 0000000..7660873
--- /dev/null
@@ -0,0 +1 @@
+[1]
diff --git a/test/suites/valid/simple-int-1/output b/test/suites/valid/simple-int-1/output
new file mode 100644 (file)
index 0000000..bace2a0
--- /dev/null
@@ -0,0 +1 @@
+[1]
\ No newline at end of file
diff --git a/test/suites/valid/simple-int-123/input b/test/suites/valid/simple-int-123/input
new file mode 100644 (file)
index 0000000..3214bfe
--- /dev/null
@@ -0,0 +1 @@
+[123]
diff --git a/test/suites/valid/simple-int-123/output b/test/suites/valid/simple-int-123/output
new file mode 100644 (file)
index 0000000..e47f69a
--- /dev/null
@@ -0,0 +1 @@
+[123]
\ No newline at end of file
diff --git a/test/suites/valid/simple-object/input b/test/suites/valid/simple-object/input
new file mode 100644 (file)
index 0000000..a34fb49
--- /dev/null
@@ -0,0 +1 @@
+{"a":[]}
diff --git a/test/suites/valid/simple-object/output b/test/suites/valid/simple-object/output
new file mode 100644 (file)
index 0000000..982abe8
--- /dev/null
@@ -0,0 +1 @@
+{"a": []}
\ No newline at end of file
diff --git a/test/suites/valid/simple-real/input b/test/suites/valid/simple-real/input
new file mode 100644 (file)
index 0000000..0fed7df
--- /dev/null
@@ -0,0 +1 @@
+[123.456789]
diff --git a/test/suites/valid/simple-real/output b/test/suites/valid/simple-real/output
new file mode 100644 (file)
index 0000000..b02878e
--- /dev/null
@@ -0,0 +1 @@
+[123.456789]
\ No newline at end of file
diff --git a/test/suites/valid/string-escapes/input b/test/suites/valid/string-escapes/input
new file mode 100644 (file)
index 0000000..d994564
--- /dev/null
@@ -0,0 +1 @@
+["\"\\\/\b\f\n\r\t"]
diff --git a/test/suites/valid/string-escapes/output b/test/suites/valid/string-escapes/output
new file mode 100644 (file)
index 0000000..ca5c1c6
--- /dev/null
@@ -0,0 +1 @@
+["\"\\/\b\f\n\r\t"]
\ No newline at end of file
diff --git a/test/suites/valid/three-byte-utf-8/input b/test/suites/valid/three-byte-utf-8/input
new file mode 100644 (file)
index 0000000..ccc0bfa
--- /dev/null
@@ -0,0 +1 @@
+["\u0821 three-byte UTF-8"]
diff --git a/test/suites/valid/three-byte-utf-8/output b/test/suites/valid/three-byte-utf-8/output
new file mode 100644 (file)
index 0000000..c44d124
--- /dev/null
@@ -0,0 +1 @@
+["ࠡ three-byte UTF-8"]
\ No newline at end of file
diff --git a/test/suites/valid/true/input b/test/suites/valid/true/input
new file mode 100644 (file)
index 0000000..29513c4
--- /dev/null
@@ -0,0 +1 @@
+[true]
diff --git a/test/suites/valid/true/output b/test/suites/valid/true/output
new file mode 100644 (file)
index 0000000..de601e3
--- /dev/null
@@ -0,0 +1 @@
+[true]
\ No newline at end of file
diff --git a/test/suites/valid/two-byte-utf-8/input b/test/suites/valid/two-byte-utf-8/input
new file mode 100644 (file)
index 0000000..05ae854
--- /dev/null
@@ -0,0 +1 @@
+["\u0123 two-byte UTF-8"]
diff --git a/test/suites/valid/two-byte-utf-8/output b/test/suites/valid/two-byte-utf-8/output
new file mode 100644 (file)
index 0000000..1f0988d
--- /dev/null
@@ -0,0 +1 @@
+["ģ two-byte UTF-8"]
\ No newline at end of file
diff --git a/test/suites/valid/utf-8-string/input b/test/suites/valid/utf-8-string/input
new file mode 100644 (file)
index 0000000..20dc64a
--- /dev/null
@@ -0,0 +1 @@
+["€þıœəßð some utf-8 ĸʒ×ŋµåäö𝄞"]
diff --git a/test/suites/valid/utf-8-string/output b/test/suites/valid/utf-8-string/output
new file mode 100644 (file)
index 0000000..5372865
--- /dev/null
@@ -0,0 +1 @@
+["€þıœəßð some utf-8 ĸʒ×ŋµåäö𝄞"]
\ No newline at end of file
diff --git a/test/suites/valid/utf-surrogate-four-byte-encoding/input b/test/suites/valid/utf-surrogate-four-byte-encoding/input
new file mode 100644 (file)
index 0000000..c598b41
--- /dev/null
@@ -0,0 +1 @@
+["\uD834\uDD1E surrogate, four-byte UTF-8"]
diff --git a/test/suites/valid/utf-surrogate-four-byte-encoding/output b/test/suites/valid/utf-surrogate-four-byte-encoding/output
new file mode 100644 (file)
index 0000000..fa806d2
--- /dev/null
@@ -0,0 +1 @@
+["𝄞 surrogate, four-byte UTF-8"]
\ No newline at end of file