Skip to content

Commit

Permalink
#102
Browse files Browse the repository at this point in the history
  • Loading branch information
adamansky committed Oct 23, 2013
1 parent e1e9dc4 commit 8e255b2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
13 changes: 8 additions & 5 deletions tcejdb/bson.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2528,8 +2528,8 @@ int bson2json(const char *bsdata, char **buf, int *sp) {


#include "nxjson.h" #include "nxjson.h"


static void _json2bson(bson *out, const nx_json *json) { static void _json2bson(bson *out, const nx_json *json, const char *forcekey) {
const char *key = json->key; const char *key = forcekey ? forcekey : json->key;
switch (json->type) { switch (json->type) {
case NX_JSON_NULL: case NX_JSON_NULL:
assert(key); assert(key);
Expand All @@ -2541,7 +2541,7 @@ static void _json2bson(bson *out, const nx_json *json) {
bson_append_start_object(out, key); bson_append_start_object(out, key);
} }
for (nx_json* js = json->child; js; js = js->next) { for (nx_json* js = json->child; js; js = js->next) {
_json2bson(out, js); _json2bson(out, js, NULL);
} }
if (key) { if (key) {
bson_append_finish_object(out); bson_append_finish_object(out);
Expand All @@ -2553,8 +2553,11 @@ static void _json2bson(bson *out, const nx_json *json) {
if (key) { if (key) {
bson_append_start_array(out, key); bson_append_start_array(out, key);
} }
int c = 0;
char kbuf[TCNUMBUFSIZ];
for (nx_json* js = json->child; js; js = js->next) { for (nx_json* js = json->child; js; js = js->next) {
_json2bson(out, js); bson_numstrn(kbuf, TCNUMBUFSIZ, c++);
_json2bson(out, js, kbuf);
} }
if (key) { if (key) {
bson_append_finish_array(out); bson_append_finish_array(out);
Expand Down Expand Up @@ -2600,7 +2603,7 @@ bson* json2bson(const char *jsonstr) {
err = true; err = true;
goto finish; goto finish;
} }
_json2bson(out, nxjson); _json2bson(out, nxjson, NULL);
bson_finish(out); bson_finish(out);
err = out->err; err = out->err;
finish: finish:
Expand Down
13 changes: 12 additions & 1 deletion tcejdb/testejdb/t1.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ int clean_suite(void) {
return 0; return 0;
} }


void testTicket102() {
const char *json = "[0, 1, 2]";
bson *ret = json2bson(json);
CU_ASSERT_PTR_NOT_NULL(ret);
bson_print_raw(bson_data(ret), 0);
CU_ASSERT_EQUAL(bson_compare_long(0, bson_data(ret), "0"), 0);
CU_ASSERT_EQUAL(bson_compare_long(1, bson_data(ret), "1"), 0);
CU_ASSERT_EQUAL(bson_compare_long(2, bson_data(ret), "2"), 0);
}

void testSaveLoad() { void testSaveLoad() {
CU_ASSERT_PTR_NOT_NULL_FATAL(jb); CU_ASSERT_PTR_NOT_NULL_FATAL(jb);
bson_oid_t oid; bson_oid_t oid;
Expand Down Expand Up @@ -272,7 +282,8 @@ int main() {
/* Add the tests to the suite */ /* Add the tests to the suite */
if ((NULL == CU_add_test(pSuite, "testSaveLoad", testSaveLoad)) || if ((NULL == CU_add_test(pSuite, "testSaveLoad", testSaveLoad)) ||
(NULL == CU_add_test(pSuite, "testBuildQuery1", testBuildQuery1)) || (NULL == CU_add_test(pSuite, "testBuildQuery1", testBuildQuery1)) ||
(NULL == CU_add_test(pSuite, "testDBOptions", testDBOptions)) (NULL == CU_add_test(pSuite, "testDBOptions", testDBOptions)) ||
(NULL == CU_add_test(pSuite, "testTicket102", testTicket102))


) { ) {
CU_cleanup_registry(); CU_cleanup_registry();
Expand Down

0 comments on commit 8e255b2

Please sign in to comment.