Skip to content

Commit

Permalink
CDRIVER-398 support bson_init_from_json via array
Browse files Browse the repository at this point in the history
didn't work before, works now
  • Loading branch information
hanumantmk committed Sep 12, 2014
1 parent 200f3f7 commit a15493b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 10 deletions.
39 changes: 29 additions & 10 deletions src/bson/bson-json.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,26 +218,29 @@ static yajl_alloc_funcs gYajlAllocFuncs = {
#define STACK_PUSH_ARRAY(statement) \
do { \
if (bson->n >= (STACK_MAX - 1)) { return 0; } \
if (bson->n == -1) { return 0; } \
bson->n++; \
STACK_I = 0; \
STACK_IS_ARRAY = 1; \
statement; \
if (bson->n != 0) { \
statement; \
} \
} while (0)
#define STACK_PUSH_DOC(statement) \
do { \
if (bson->n >= (STACK_MAX - 1)) { return 0; } \
bson->n++; \
STACK_IS_ARRAY = 0; \
if (bson->n != 0) { \
STACK_IS_ARRAY = 0; \
statement; \
} \
} while (0)
#define STACK_POP_ARRAY(statement) \
do { \
if (!STACK_IS_ARRAY) { return 0; } \
if (bson->n <= 0) { return 0; } \
statement; \
if (bson->n < 0) { return 0; } \
if (bson->n > 0) { \
statement; \
} \
bson->n--; \
} while (0)
#define STACK_POP_DOC(statement) \
Expand Down Expand Up @@ -348,7 +351,7 @@ _bson_json_read_fixup_key (bson_json_reader_bson_t *bson) /* IN */
{
BSON_ASSERT (bson);

if (bson->n > 0 && STACK_IS_ARRAY) {
if (bson->n >= 0 && STACK_IS_ARRAY) {
_bson_json_buf_ensure (&bson->key_buf, 12);
bson->key_buf.len = bson_uint32_to_string (STACK_I, &bson->key,
(char *)bson->key_buf.buf, 12);
Expand Down Expand Up @@ -919,11 +922,23 @@ _bson_json_read_end_map (void *_ctx) /* IN */
static int
_bson_json_read_start_array (void *_ctx) /* IN */
{
BASIC_YAJL_CB_PREAMBLE;
BASIC_YAJL_CB_BAIL_IF_NOT_NORMAL ("[");
const char *key;
size_t len;
bson_json_reader_t *reader = (bson_json_reader_t *)_ctx;
bson_json_reader_bson_t *bson = &reader->bson;

if (bson->n < 0) {
STACK_PUSH_ARRAY ();
} else {
_bson_json_read_fixup_key (bson);
key = bson->key;
len = bson->key_buf.len;

STACK_PUSH_ARRAY (bson_append_array_begin (STACK_BSON_PARENT, key, (int)len,
STACK_BSON_CHILD));
BASIC_YAJL_CB_BAIL_IF_NOT_NORMAL ("[");

STACK_PUSH_ARRAY (bson_append_array_begin (STACK_BSON_PARENT, key, (int)len,
STACK_BSON_CHILD));
}

return 1;
}
Expand All @@ -943,6 +958,10 @@ _bson_json_read_end_array (void *_ctx) /* IN */

STACK_POP_ARRAY (bson_append_array_end (STACK_BSON_PARENT,
STACK_BSON_CHILD));
if (bson->n == -1) {
bson->read_state = BSON_JSON_DONE;
return 0;
}

return 1;
}
Expand Down
24 changes: 24 additions & 0 deletions tests/test-json.c
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,29 @@ test_bson_json_inc (void)
bson_destroy (&b);
}

static void
test_bson_json_array (void)
{
bson_error_t error;
const char *json = "[ 0, 1, 2, 3 ]";
bson_t b, compare;
bool r;

bson_init(&compare);
bson_append_int32(&compare, "0", 1, 0);
bson_append_int32(&compare, "1", 1, 1);
bson_append_int32(&compare, "2", 1, 2);
bson_append_int32(&compare, "3", 1, 3);

r = bson_init_from_json (&b, json, -1, &error);
if (!r) fprintf (stderr, "%s\n", error.message);
assert (r);

bson_eq_bson (&b, &compare);
bson_destroy (&compare);
bson_destroy (&b);
}

static void
test_bson_array_as_json (void)
{
Expand Down Expand Up @@ -565,6 +588,7 @@ test_json_install (TestSuite *suite)
TestSuite_Add (suite, "/bson/array_as_json", test_bson_array_as_json);
TestSuite_Add (suite, "/bson/json/read", test_bson_json_read);
TestSuite_Add (suite, "/bson/json/inc", test_bson_json_inc);
TestSuite_Add (suite, "/bson/json/array", test_bson_json_array);
TestSuite_Add (suite, "/bson/json/read/missing_complex", test_bson_json_read_missing_complex);
TestSuite_Add (suite, "/bson/json/read/invalid_json", test_bson_json_read_invalid_json);
TestSuite_Add (suite, "/bson/json/read/bad_cb", test_bson_json_read_bad_cb);
Expand Down

0 comments on commit a15493b

Please sign in to comment.