diff --git a/core/JSON.cc b/core/JSON.cc index 414e544e92f0c6364ff6a52d92a60c6081550318..a464735bb5bf27c95d157755bcb21726af909e18 100644 --- a/core/JSON.cc +++ b/core/JSON.cc @@ -1103,8 +1103,8 @@ void json2bson_coding(TTCN_Buffer& buff, JSON_Tokenizer& tok, bool in_object, size_t len; size_t prev_pos = tok.get_buf_pos(); tok.get_next_token(&token, &content, &len); - if (in_object == false && token != JSON_TOKEN_OBJECT_START) { - TTCN_error("Json document must be an object when encoding with json2bson()"); + if (in_object == false && token != JSON_TOKEN_OBJECT_START && token != JSON_TOKEN_ARRAY_START) { + TTCN_error("Json document must be an object or array when encoding with json2bson()"); } switch(token) { case JSON_TOKEN_OBJECT_START: { @@ -1246,9 +1246,13 @@ void json2bson_coding(TTCN_Buffer& buff, JSON_Tokenizer& tok, bool in_object, put_name(buff, length, obj_name, in_array); break; } case JSON_TOKEN_ARRAY_START: { - buff.put_c(4); // array - length = length + 1; - put_name(buff, length, obj_name, in_array); + if (in_object == false) { // The top level json is an array + in_object = true; + } else { + buff.put_c(4); // array + length = length + 1; + put_name(buff, length, obj_name, in_array); + } obj_name = "0"; // arrays are objects but the key is a number which increases TTCN_Buffer sub_buff; INTEGER sub_length = 0; diff --git a/regression_test/json/Bson.ttcn b/regression_test/json/Bson.ttcn index 0cf68499fcaceff5ab453e6c121b266717be2104..d4932162ad2478015db1d88b706a4cba3c668958 100644 --- a/regression_test/json/Bson.ttcn +++ b/regression_test/json/Bson.ttcn @@ -582,6 +582,30 @@ module Bson { setverdict(fail, match(decoded, json)); } + json := "[1,2]" + expected_os := '13000000103000010000001031000200000000'O; + os := json2bson(json);log(os); + if (not match(os, expected_os)) { + setverdict(fail, match(os, expected_os)); + } + decoded_json := "{\"0\":1,\"1\":2}"; + decoded := bson2json(os); + if (not match(decoded, decoded_json)) { + setverdict(fail, match(decoded, decoded_json)); + } + + json := "[{\"$code\":\"aaa\",\"$scope\":{\"a\":\"b\"}},{\"a\":3},2,\"abc\"]" + expected_os := '430000000F30001A00000004000000616161000E000000026100020000006200000330000C000000106100030000000010310002000000023200040000006162630000'O; + os := json2bson(json);log(os); + if (not match(os, expected_os)) { + setverdict(fail, match(os, expected_os)); + } + decoded_json := "{\"0\":{\"$code\":\"aaa\",\"$scope\":{\"a\":\"b\"}},\"0\":{\"a\":3},\"1\":2,\"2\":\"abc\"}" + decoded := bson2json(os); + if (not match(decoded, decoded_json)) { + setverdict(fail, match(decoded, decoded_json)); + } + setverdict(pass); } diff --git a/usrguide/referenceguide.doc b/usrguide/referenceguide.doc index 2b6744feeb90866970859a5a5e0132cf0aa5d5b8..cbb5552312f874141151ce7f0f92d61abd22f49c 100644 Binary files a/usrguide/referenceguide.doc and b/usrguide/referenceguide.doc differ