Skip to content

Include already encoded CBOR #550

Answered by danielaparker
Pique7 asked this question in Q&A
Discussion options

You must be logged in to vote

The supported way is to regenerate the events from buf1 and feed them to encoder2, e.g.

    std::vector<uint8_t> buf1;
    cbor::cbor_bytes_encoder encoder1(buf1);
    encoder1.string_value("test1");
    encoder1.flush();

    std::vector<uint8_t> buf2;
    cbor::cbor_bytes_encoder encoder2(buf2);
    encoder2.begin_array(2); // will also work for indefinite array (omitting length) 
    encoder2.string_value("test2");
    cbor::cbor_bytes_reader reader(buf1, encoder2); // feeds the string_value event into buf2
    reader.read();
    encoder2.end_array();
    encoder2.flush();

    auto j = cbor::decode_cbor<jsoncons::json>(buf2);
    std::cout << j << "\n";

Output:

["test2","test1"]

But …

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@danielaparker
Comment options

Comment options

You must be logged in to vote
1 reply
@Pique7
Comment options

Answer selected by Pique7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants