diff --git a/include/jsoncons_ext/cbor/cbor_encoder.hpp b/include/jsoncons_ext/cbor/cbor_encoder.hpp index ee613d83a..20005c1f3 100644 --- a/include/jsoncons_ext/cbor/cbor_encoder.hpp +++ b/include/jsoncons_ext/cbor/cbor_encoder.hpp @@ -52,10 +52,10 @@ class basic_cbor_encoder final : public basic_json_visitor { cbor_container_type type_; std::size_t length_; - std::size_t count_; + std::size_t index_; stack_item(cbor_container_type type, std::size_t length = 0) noexcept - : type_(type), length_(length), count_(0) + : type_(type), length_(length), index_(0) { } @@ -66,7 +66,7 @@ class basic_cbor_encoder final : public basic_json_visitor std::size_t count() const { - return count_; + return is_object() ? index_/2 : index_; } bool is_object() const @@ -328,10 +328,9 @@ class basic_cbor_encoder final : public basic_json_visitor return true; } - bool visit_key(const string_view_type& name, const ser_context&, std::error_code&) override + bool visit_key(const string_view_type& name, const ser_context& context, std::error_code& ec) override { - write_string(name); - return true; + return visit_string(name, semantic_tag::none, context, ec); } bool visit_null(semantic_tag tag, const ser_context&, std::error_code&) override @@ -1732,7 +1731,7 @@ class basic_cbor_encoder final : public basic_json_visitor { if (!stack_.empty()) { - ++stack_.back().count_; + ++stack_.back().index_; } } }; diff --git a/include/jsoncons_ext/msgpack/msgpack_encoder.hpp b/include/jsoncons_ext/msgpack/msgpack_encoder.hpp index 88c74d2a8..0c42160ec 100644 --- a/include/jsoncons_ext/msgpack/msgpack_encoder.hpp +++ b/include/jsoncons_ext/msgpack/msgpack_encoder.hpp @@ -45,10 +45,10 @@ namespace msgpack { { msgpack_container_type type_; std::size_t length_; - std::size_t count_; + std::size_t index_; stack_item(msgpack_container_type type, std::size_t length = 0) noexcept - : type_(type), length_(length), count_(0) + : type_(type), length_(length), index_(0) { } @@ -59,7 +59,7 @@ namespace msgpack { std::size_t count() const { - return count_; + return is_object() ? index_/2 : index_; } bool is_object() const @@ -235,10 +235,9 @@ namespace msgpack { return true; } - bool visit_key(const string_view_type& name, const ser_context&, std::error_code&) final + bool visit_key(const string_view_type& name, const ser_context& context, std::error_code& ec) override { - write_string_value(name); - return true; + return visit_string(name, semantic_tag::none, context, ec); } bool visit_null(semantic_tag, const ser_context&, std::error_code&) final @@ -728,7 +727,7 @@ namespace msgpack { { if (!stack_.empty()) { - ++stack_.back().count_; + ++stack_.back().index_; } } }; diff --git a/test/cbor/src/cbor_encoder_tests.cpp b/test/cbor/src/cbor_encoder_tests.cpp index 237ee73d9..71cbf64ef 100644 --- a/test/cbor/src/cbor_encoder_tests.cpp +++ b/test/cbor/src/cbor_encoder_tests.cpp @@ -36,16 +36,16 @@ TEST_CASE("cbor encode multi dim array test") encoder.end_multi_dim(); byte_string_view bstr(v); - std::cout << "bstr: " << bstr << "\n\n"; + //std::cout << "bstr: " << bstr << "\n\n"; - for (auto ch : bstr) - { - std::cout << (int)ch << " "; - } - std::cout << "\n\n"; + //for (auto ch : bstr) + //{ + // std::cout << (int)ch << " "; + //} + //std::cout << "\n\n"; auto j = cbor::decode_cbor(v); - std::cout << pretty_print(j) << "\n\n"; + //std::cout << pretty_print(j) << "\n\n"; } @@ -84,24 +84,15 @@ TEST_CASE("serialize array to cbor") { std::vector v; cbor::cbor_bytes_encoder encoder(v); - //encoder.begin_object(1); encoder.begin_array(3); encoder.bool_value(true); encoder.bool_value(false); encoder.null_value(); encoder.end_array(); - //encoder.end_object(); encoder.flush(); - JSONCONS_TRY - { - json result = cbor::decode_cbor(v); - //std::cout << result << std::endl; - } - JSONCONS_CATCH (const std::exception& e) - { - std::cout << e.what() << std::endl; - } + json result; + REQUIRE_NOTHROW(result = cbor::decode_cbor(v)); } TEST_CASE("test_serialize_indefinite_length_array") @@ -118,16 +109,28 @@ TEST_CASE("test_serialize_indefinite_length_array") encoder.end_array(); encoder.flush(); - JSONCONS_TRY - { - json result = cbor::decode_cbor(v); - //std::cout << result << std::endl; - } - JSONCONS_CATCH (const std::exception& e) + json result; + REQUIRE_NOTHROW(result = cbor::decode_cbor(v)); +} + +TEST_CASE("serialize object to cbor") +{ + SECTION("definite length") { - std::cout << e.what() << std::endl; + std::vector v; + cbor::cbor_bytes_encoder encoder(v); + encoder.begin_object(2); + encoder.uint64_value(1); + encoder.string_value("value1"); + encoder.uint64_value(2); + encoder.string_value("value2"); + REQUIRE_NOTHROW(encoder.end_object()); + encoder.flush(); + json result; + REQUIRE_NOTHROW(result = cbor::decode_cbor(v)); } -} +} + TEST_CASE("test_serialize_bignum") { std::vector v; @@ -141,15 +144,9 @@ TEST_CASE("test_serialize_bignum") encoder.end_array(); encoder.flush(); - JSONCONS_TRY - { - json result = cbor::decode_cbor(v); - CHECK(result[0].as() == std::string("18446744073709551616")); - } - JSONCONS_CATCH (const std::exception& e) - { - std::cout << e.what() << std::endl; - } + json result; + REQUIRE_NOTHROW(result = cbor::decode_cbor(v)); + CHECK(result[0].as() == std::string("18446744073709551616")); } TEST_CASE("test_serialize_negative_bignum1") @@ -529,3 +526,5 @@ TEMPLATE_TEST_CASE("test_cbor_encoder_reset", "", f.encoder.flush(); CHECK(f.bytes2() == expected_full); } + + diff --git a/test/msgpack/src/msgpack_encoder_tests.cpp b/test/msgpack/src/msgpack_encoder_tests.cpp index a29e34fc1..4093cef0c 100644 --- a/test/msgpack/src/msgpack_encoder_tests.cpp +++ b/test/msgpack/src/msgpack_encoder_tests.cpp @@ -28,16 +28,27 @@ TEST_CASE("serialize array to msgpack") //encoder.end_object(); encoder.flush(); - JSONCONS_TRY - { - json result = msgpack::decode_msgpack(v); - std::cout << result << std::endl; - } - JSONCONS_CATCH (const std::exception& e) + json result; + REQUIRE_NOTHROW(result = msgpack::decode_msgpack(v)); +} + +TEST_CASE("serialize object to msgpack") +{ + SECTION("definite length") { - std::cout << e.what() << std::endl; + std::vector v; + msgpack::msgpack_bytes_encoder encoder(v); + encoder.begin_object(2); + encoder.uint64_value(1); + encoder.string_value("value1"); + encoder.uint64_value(2); + encoder.string_value("value2"); + REQUIRE_NOTHROW(encoder.end_object()); + encoder.flush(); + json result; + REQUIRE_NOTHROW(result = msgpack::decode_msgpack(v)); } -} +} TEST_CASE("Too many and too few items in MessagePack object or array") {