danielaparker / jsoncons

A C++, header-only library for constructing JSON and JSON-like data formats, with JSON Pointer, JSON Patch, JSON Schema, JSONPath, JMESPath, CSV, MessagePack, CBOR, BSON, UBJSON
https://danielaparker.github.io/jsoncons
Other
715 stars 163 forks source link

Unable to decode back a UBJSON-encoded object #449

Closed fceconel closed 1 year ago

fceconel commented 1 year ago

Describe the bug

The UBJSON extension Fails to decode a packet that was encoded by that same extension if the original object contains a byte string field.

The code below, when executed, throws an exception while decoding.

#include <jsoncons/json.hpp>
#include <jsoncons_ext/jsonpath/jsonpath.hpp>
#include <jsoncons_ext/ubjson/ubjson.hpp>
#include <iostream>

using namespace jsoncons;

int main()
{
    // An object is created having just one element "payload", a byte string
    json j;
    std::vector<uint8_t> payload = {130, 13, 0, 1, 0, 0, 7, 109, 121, 116, 111, 112, 105, 99, 1};
    j["payload"] = json(byte_string_arg, payload);
    std::cout << "(1) \n" << pretty_print(j) << "\n\n";

    // Now the object will be encoded as UBJSON
    std::vector<uint8_t> buffer;
    ubjson::encode_ubjson(j, buffer);
    std::cout << "(2) \n" << byte_string_view(buffer) << "\n\n";    

    // Then the buffer is decoded back to an object
    json j2 = ubjson::decode_ubjson<json>(buffer);
    std::cout << "(3) \n" << pretty_print(j2) << "\n\n";
}

Output:

(1) 
{
    "payload": "gg0AAQAAB215dG9waWMB"
}

(2) 
7b,23,55,01,55,07,70,61,79,6c,6f,61,64,5b,24,55,55,0f,82,0d,00,01,00,00,07,6d,79,74,6f,70,69,63,01

terminate called after throwing an instance of 'jsoncons::ser_error'
  what():  Type is specified for container, but count is not specified at position 16
Aborted (core dumped)

What compiler, architecture, and operating system?

What jsoncons library version?

danielaparker commented 1 year ago

Fixed on master. The issue was with encoding the binary data, we were missing a count_marker before the count of the number of uint8 values.