Permalink
Browse files

#102

  • Loading branch information...
1 parent ac6d543 commit ccbf8055abfb8a530ae8a687b602e6cced8d19e5 @adamansky adamansky committed Oct 23, 2013
Showing with 20 additions and 6 deletions.
  1. +8 −5 tcejdb/bson.c
  2. +12 −1 tcejdb/testejdb/t1.c
View
@@ -2528,8 +2528,8 @@ int bson2json(const char *bsdata, char **buf, int *sp) {
#include "nxjson.h"
-static void _json2bson(bson *out, const nx_json *json) {
- const char *key = json->key;
+static void _json2bson(bson *out, const nx_json *json, const char *forcekey) {
+ const char *key = forcekey ? forcekey : json->key;
switch (json->type) {
case NX_JSON_NULL:
assert(key);
@@ -2541,7 +2541,7 @@ static void _json2bson(bson *out, const nx_json *json) {
bson_append_start_object(out, key);
}
for (nx_json* js = json->child; js; js = js->next) {
- _json2bson(out, js);
+ _json2bson(out, js, NULL);
}
if (key) {
bson_append_finish_object(out);
@@ -2553,8 +2553,11 @@ static void _json2bson(bson *out, const nx_json *json) {
if (key) {
bson_append_start_array(out, key);
}
+ int c = 0;
+ char kbuf[TCNUMBUFSIZ];
for (nx_json* js = json->child; js; js = js->next) {
- _json2bson(out, js);
+ bson_numstrn(kbuf, TCNUMBUFSIZ, c++);
+ _json2bson(out, js, kbuf);
}
if (key) {
bson_append_finish_array(out);
@@ -2600,7 +2603,7 @@ bson* json2bson(const char *jsonstr) {
err = true;
goto finish;
}
- _json2bson(out, nxjson);
+ _json2bson(out, nxjson, NULL);
bson_finish(out);
err = out->err;
finish:
View
@@ -31,6 +31,16 @@ int clean_suite(void) {
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() {
CU_ASSERT_PTR_NOT_NULL_FATAL(jb);
bson_oid_t oid;
@@ -272,7 +282,8 @@ int main() {
/* Add the tests to the suite */
if ((NULL == CU_add_test(pSuite, "testSaveLoad", testSaveLoad)) ||
(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();

0 comments on commit ccbf805

Please sign in to comment.