Skip to content

Commit

Permalink
Fixed object length issue for cbor and msgpack
Browse files Browse the repository at this point in the history
  • Loading branch information
danielaparker committed Dec 22, 2024
1 parent 401a473 commit c060490
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 57 deletions.
13 changes: 6 additions & 7 deletions include/jsoncons_ext/cbor/cbor_encoder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ class basic_cbor_encoder final : public basic_json_visitor<char>
{
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)
{
}

Expand All @@ -66,7 +66,7 @@ class basic_cbor_encoder final : public basic_json_visitor<char>

std::size_t count() const
{
return count_;
return is_object() ? index_/2 : index_;
}

bool is_object() const
Expand Down Expand Up @@ -328,10 +328,9 @@ class basic_cbor_encoder final : public basic_json_visitor<char>
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
Expand Down Expand Up @@ -1732,7 +1731,7 @@ class basic_cbor_encoder final : public basic_json_visitor<char>
{
if (!stack_.empty())
{
++stack_.back().count_;
++stack_.back().index_;
}
}
};
Expand Down
13 changes: 6 additions & 7 deletions include/jsoncons_ext/msgpack/msgpack_encoder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
}

Expand All @@ -59,7 +59,7 @@ namespace msgpack {

std::size_t count() const
{
return count_;
return is_object() ? index_/2 : index_;
}

bool is_object() const
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -728,7 +727,7 @@ namespace msgpack {
{
if (!stack_.empty())
{
++stack_.back().count_;
++stack_.back().index_;
}
}
};
Expand Down
69 changes: 34 additions & 35 deletions test/cbor/src/cbor_encoder_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<json>(v);
std::cout << pretty_print(j) << "\n\n";
//std::cout << pretty_print(j) << "\n\n";

}

Expand Down Expand Up @@ -84,24 +84,15 @@ TEST_CASE("serialize array to cbor")
{
std::vector<uint8_t> 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<json>(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<json>(v));
}

TEST_CASE("test_serialize_indefinite_length_array")
Expand All @@ -118,16 +109,28 @@ TEST_CASE("test_serialize_indefinite_length_array")
encoder.end_array();
encoder.flush();

JSONCONS_TRY
{
json result = cbor::decode_cbor<json>(v);
//std::cout << result << std::endl;
}
JSONCONS_CATCH (const std::exception& e)
json result;
REQUIRE_NOTHROW(result = cbor::decode_cbor<json>(v));
}

TEST_CASE("serialize object to cbor")
{
SECTION("definite length")
{
std::cout << e.what() << std::endl;
std::vector<uint8_t> 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<json>(v));
}
}
}

TEST_CASE("test_serialize_bignum")
{
std::vector<uint8_t> v;
Expand All @@ -141,15 +144,9 @@ TEST_CASE("test_serialize_bignum")
encoder.end_array();
encoder.flush();

JSONCONS_TRY
{
json result = cbor::decode_cbor<json>(v);
CHECK(result[0].as<std::string>() == std::string("18446744073709551616"));
}
JSONCONS_CATCH (const std::exception& e)
{
std::cout << e.what() << std::endl;
}
json result;
REQUIRE_NOTHROW(result = cbor::decode_cbor<json>(v));
CHECK(result[0].as<std::string>() == std::string("18446744073709551616"));
}

TEST_CASE("test_serialize_negative_bignum1")
Expand Down Expand Up @@ -529,3 +526,5 @@ TEMPLATE_TEST_CASE("test_cbor_encoder_reset", "",
f.encoder.flush();
CHECK(f.bytes2() == expected_full);
}


27 changes: 19 additions & 8 deletions test/msgpack/src/msgpack_encoder_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,27 @@ TEST_CASE("serialize array to msgpack")
//encoder.end_object();
encoder.flush();

JSONCONS_TRY
{
json result = msgpack::decode_msgpack<json>(v);
std::cout << result << std::endl;
}
JSONCONS_CATCH (const std::exception& e)
json result;
REQUIRE_NOTHROW(result = msgpack::decode_msgpack<json>(v));
}

TEST_CASE("serialize object to msgpack")
{
SECTION("definite length")
{
std::cout << e.what() << std::endl;
std::vector<uint8_t> 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<json>(v));
}
}
}

TEST_CASE("Too many and too few items in MessagePack object or array")
{
Expand Down

0 comments on commit c060490

Please sign in to comment.