Skip to content

byte_string seems broken #208

@geiseri

Description

@geiseri

Greetings, I upgraded and moved my code to the new tags interface and I seem to have a regression. Here is my json_type_traits in case I am doing something wrong.

template <class Json>
struct jsoncons::json_type_traits<Json, std::shared_ptr<EncryptedRecord>> {
    using allocator_type = typename Json::allocator_type;

    static bool is(const Json &val) noexcept
    {
        return (val.is_object() && val.contains("id") && val.contains("data"));
    }

    static std::shared_ptr<EncryptedRecord> as(const Json &val)
    {

        SPDLOG_DEBUG("type {} tag {}", val.at("data").type(), val.at("data").tag());
        auto id    = val["id"].template as<std::string>();
        auto data  = val["data"].template as<std::vector<uint8_t>>();
        return std::make_shared<EncryptedRecord>(id, data);
    }

    static Json to_json(const std::shared_ptr<EncryptedRecord> &val, allocator_type allocator = allocator_type())
    {
        Json json(jsoncons::json_object_arg, jsoncons::semantic_tag::none, allocator);
        json["id"]    = val->id;
        json["data"]  = Json(jsoncons::byte_string_arg, val->data, jsoncons::semantic_tag::base64url, allocator);
        SPDLOG_DEBUG("type {} tag {}", json.at("data").type(), json.at("data").tag());
        return json;
    }
};

I then do the following:

auto j = jsoncons::json(r).to_string();
SPDLOG_DEBUG("Buffer1 {}", j);
auto out = jsoncons::json::parse(j).as<std::shared_ptr<EncryptedRecord>>();

The output from Buffer1 {} is correct:

{
  "data": "ny2hSd09EU0zbuyk7pb2CjfV8mC9fN4aNXQEIgDft0dfAfB-jv4-kAsXbqHf14bQr0r8vHjgLtRZiiMAtYQQk8erTWgckuQ4JNnK7tDby2IsLFgHdYdqvsimXBSlrDEJo5p5rwRwSv7CULClK2zZ63sQog1ajRpp6eORsqFnAHUr9p41M9Fgmu9XqXDaW0tFTE1i1WyF_c0oCc3Wq3JBmtrBDgCRXva-WY4GcQSiiXIVs0kWez4BmG2JIPtRFRf_ufKRsCiDHj8_rz9kpG5TGTv4",
  "id": "test",
}

The issue is when I try to parse it I get a json cannot be converted to vector. I also noticed the following on the printing the tags:

to_json:

[13:01:34][D][105340:105340][record_tests.cpp:124] type 7 tag 9

as:

[13:01:34][D][105340:105340][record_tests.cpp:108] type 6 tag 0

I assume the string is correct on the read, but shouldn't the parser be able to try to convert the string to a byte_string/vector<uint8_t>?

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions