summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLloyd Hilaiel <lloyd@hilaiel.com>2011-04-23 13:42:19 -0600
committerLloyd Hilaiel <lloyd@hilaiel.com>2011-04-23 13:42:19 -0600
commit25133e5508b979fdf6814832d4355f46bd26db43 (patch)
treee49e7296c1f82f78f359636ce6aa600e7c3ef73a
parentb36a8bd294cbeeaab7149402f369fdd9be95aec1 (diff)
downloadyajl-25133e5508b979fdf6814832d4355f46bd26db43.tar.gz
documentation updates
-rw-r--r--src/YAJL.dxy2
-rw-r--r--src/yajl132
2 files changed, 70 insertions, 64 deletions
diff --git a/src/YAJL.dxy b/src/YAJL.dxy
index 680eb43..3bdae86 100644
--- a/src/YAJL.dxy
+++ b/src/YAJL.dxy
@@ -151,7 +151,7 @@ MULTILINE_CPP_IS_BRIEF = NO
# If set to NO, the detailed description appears after the member
# documentation.
-DETAILS_AT_TOP = YES
+# DETAILS_AT_TOP = YES
# If the INHERIT_DOCS tag is set to YES (the default) then an undocumented
# member inherits the documentation from any documented member that it
diff --git a/src/yajl b/src/yajl
index fea800f..c59dd96 100644
--- a/src/yajl
+++ b/src/yajl
@@ -1,11 +1,13 @@
/*!
\mainpage Yet Another JSON Library (YAJL)
\author Lloyd Hilaiel
- \date 2010
+ \date 2007-2011
Yet Another JSON Library (YAJL) is a small event-driven (SAX-style)
JSON parser written in ANSI C, and a small validating JSON
-generator. YAJL is released under the ISC license.
+generator. It also includes a small simplified tree interface for
+simplified parsing and extraction of data from smallish JSON documents.
+YAJL is released under the permissive ISC license.
\section features Features
@@ -32,67 +34,58 @@ demonstrates how to perform stream parsing and generation.
static int reformat_null(void * ctx)
{
yajl_gen g = (yajl_gen) ctx;
- yajl_gen_null(g);
- return 1;
+ return yajl_gen_status_ok == yajl_gen_null(g);
}
static int reformat_boolean(void * ctx, int boolean)
{
yajl_gen g = (yajl_gen) ctx;
- yajl_gen_bool(g, boolean);
- return 1;
+ return yajl_gen_status_ok == yajl_gen_bool(g, boolean);
}
static int reformat_number(void * ctx, const char * s, size_t l)
{
yajl_gen g = (yajl_gen) ctx;
- yajl_gen_number(g, s, l);
- return 1;
+ return yajl_gen_status_ok == yajl_gen_number(g, s, l);
}
static int reformat_string(void * ctx, const unsigned char * stringVal,
size_t stringLen)
{
yajl_gen g = (yajl_gen) ctx;
- yajl_gen_string(g, stringVal, stringLen);
- return 1;
+ return yajl_gen_status_ok == yajl_gen_string(g, stringVal, stringLen);
}
static int reformat_map_key(void * ctx, const unsigned char * stringVal,
size_t stringLen)
{
yajl_gen g = (yajl_gen) ctx;
- yajl_gen_string(g, stringVal, stringLen);
- return 1;
+ return yajl_gen_status_ok == yajl_gen_string(g, stringVal, stringLen);
}
static int reformat_start_map(void * ctx)
{
yajl_gen g = (yajl_gen) ctx;
- yajl_gen_map_open(g);
- return 1;
+ return yajl_gen_status_ok == yajl_gen_map_open(g);
}
static int reformat_end_map(void * ctx)
{
yajl_gen g = (yajl_gen) ctx;
- yajl_gen_map_close(g);
- return 1;
+ return yajl_gen_status_ok == yajl_gen_map_close(g);
}
static int reformat_start_array(void * ctx)
{
yajl_gen g = (yajl_gen) ctx;
- yajl_gen_array_open(g);
- return 1;
+ return yajl_gen_status_ok == yajl_gen_array_open(g);
}
static int reformat_end_array(void * ctx)
{
yajl_gen g = (yajl_gen) ctx;
- yajl_gen_array_close(g);
- return 1;
+ return yajl_gen_status_ok == yajl_gen_array_close(g);
}
static yajl_callbacks callbacks = {
@@ -112,7 +105,8 @@ static yajl_callbacks callbacks = {
static void
usage(const char * progname)
{
- fprintf(stderr, "usage: %s <filename>\n"
+ fprintf(stderr, "%s: reformat json from stdin\n"
+ "usage: json_reformat [options]\n"
" -m minimize json rather than beautify (default)\n"
" -u allow invalid UTF8 inside strings during parsing\n",
progname);
@@ -126,71 +120,83 @@ main(int argc, char ** argv)
yajl_handle hand;
static unsigned char fileData[65536];
/* generator config */
- yajl_gen_config conf = { 1, " " };
- yajl_gen g;
+ yajl_gen g;
yajl_status stat;
size_t rd;
- /* allow comments */
- yajl_parser_config cfg = { 1, 1 };
- int done = 0;
-
- /* check arguments. We expect exactly one! */
- if (argc == 2) {
- if (!strcmp("-m", argv[1])) {
- conf.beautify = 0;
-
- } else if (!strcmp("-u", argv[1])) {
- cfg.checkUTF8 = 0;
- } else {
- usage(argv[0]);
+ int retval = 0;
+
+ g = yajl_gen_alloc(NULL);
+ yajl_gen_config(g, yajl_gen_beautify, 1);
+ yajl_gen_config(g, yajl_gen_validate_utf8, 1);
+
+ /* ok. open file. let's read and parse */
+ hand = yajl_alloc(&callbacks, NULL, (void *) g);
+ /* and let's allow comments by default */
+ yajl_config(hand, yajl_allow_comments, 1);
+
+ /* check arguments.*/
+ int a = 1;
+ while ((a < argc) && (argv[a][0] == '-') && (strlen(argv[a]) > 1)) {
+ unsigned int i;
+ for ( i=1; i < strlen(argv[a]); i++) {
+ switch (argv[a][i]) {
+ case 'm':
+ yajl_gen_config(g, yajl_gen_beautify, 0);
+ break;
+ case 'u':
+ yajl_config(hand, yajl_dont_validate_strings, 1);
+ break;
+ default:
+ fprintf(stderr, "unrecognized option: '%c'\n\n",
+ argv[a][i]);
+ usage(argv[0]);
+ }
}
- } else if (argc != 1) {
+ ++a;
+ }
+ if (a < argc) {
usage(argv[0]);
}
-
- g = yajl_gen_alloc(&conf, NULL);
- /* ok. open file. let's read and parse */
- hand = yajl_alloc(&callbacks, &cfg, NULL, (void *) g);
-
- while (!done) {
+
+ for (;;) {
rd = fread((void *) fileData, 1, sizeof(fileData) - 1, stdin);
-
+
if (rd == 0) {
if (!feof(stdin)) {
fprintf(stderr, "error on file read.\n");
- break;
+ retval = 1;
}
- done = 1;
+ break;
}
fileData[rd] = 0;
-
- if (done)
- /* parse any remaining buffered data */
- stat = yajl_complete_parse(hand);
- else
- /* read file data, pass to parser */
- stat = yajl_parse(hand, fileData, rd);
-
- if (stat != yajl_status_ok &&
- stat != yajl_status_insufficient_data)
+
+ stat = yajl_parse(hand, fileData, rd);
+
+ if (stat != yajl_status_ok) break;
+
{
- unsigned char * str = yajl_get_error(hand, 1, fileData, rd);
- fprintf(stderr, (const char *) str);
- yajl_free_error(hand, str);
- } else {
const unsigned char * buf;
- size_tnq len;
+ size_t len;
yajl_gen_get_buf(g, &buf, &len);
fwrite(buf, 1, len, stdout);
yajl_gen_clear(g);
}
}
+ stat = yajl_complete_parse(hand);
+
+ if (stat != yajl_status_ok) {
+ unsigned char * str = yajl_get_error(hand, 1, fileData, rd);
+ fprintf(stderr, "%s", (const char *) str);
+ yajl_free_error(hand, str);
+ retval = 1;
+ }
+
yajl_gen_free(g);
yajl_free(hand);
-
- return 0;
+
+ return retval;
}
\endcode
*/