c++jsoncbor

C++ jsoncons : cbor to json


I would like to convert cbor into json using the C++ jsoncons library (https://github.com/danielaparker/jsoncons/blob/master/doc/ref/cbor/cbor.md). But when I print the result to console some entries are weird.

My Code:

const std::vector<uint8_t> data1 = {  0xa4, 0x01, 0x58 , 0x65 , 0x83 , 0x43 , 0xa1 , 0x01
                                             , 0x0a , 0xa1 , 0x05 , 0x4d , 0xba , 0xe3 , 0xff , 0x8c
                                             , 0x34 , 0x9e , 0x98 , 0x15 , 0xe1 , 0x79 , 0x85 , 0x4c
                                             , 0xec , 0x58 , 0x4e , 0x7b , 0x3e , 0x19 , 0x05 , 0x32
                                             , 0x00 , 0x79 , 0x72 , 0x8f , 0x48 , 0xe4 , 0xe0 , 0x12
                                             , 0x96 , 0xb2 , 0xd9 , 0x84 , 0xfd , 0xd5 , 0x62 , 0x7e
                                             , 0x42 , 0x6d , 0x68 , 0xe9 , 0xde , 0xe2 , 0xf2 , 0xc4
                                             , 0xef , 0xe6 , 0xf2 , 0x72 , 0x76 , 0xad , 0xee , 0xbf
                                             , 0x17 , 0x06 , 0x91 , 0xca , 0x15 , 0x7f , 0x09 , 0x45
                                             , 0x08 , 0x67 , 0xdc , 0xeb , 0x7d , 0xf8 , 0xeb , 0xea
                                             , 0x02 , 0x01 , 0x99 , 0x7a , 0xc0 , 0xc6 , 0x7b , 0x17
                                             , 0xf2 , 0x2e , 0xd5 , 0x76 , 0x9d , 0x88 , 0xc0 , 0xfc
                                             , 0x20 , 0x41 , 0x7d , 0x9d , 0x8a , 0x17 , 0x91 , 0xec
                                             , 0x01 , 0x06 , 0x1a , 0x61 , 0x73 , 0x19 , 0x72 , 0x07
                                             , 0x01 , 0x08 , 0xa1 , 0x01 , 0xa2 , 0x01 , 0x04 , 0x20
                                             , 0x50 , 0xf7 , 0x77 , 0xd3 , 0xb7 , 0x82 , 0x01 , 0xef
                                             , 0x29 , 0x49 , 0x09 , 0xde , 0xe0 , 0x8b , 0x7a , 0xd3
                                             , 0x59};

json j = cbor::decode_cbor<json>(data1);

// Pretty print
std::cout << "(1)\n" << pretty_print(j) << "\n\n";


And the Output is:

(1)
{
    "1": "g0OhAQqhBU264_-MNJ6YFeF5hUzsWE57PhkFMgB5co9I5OASlrLZhP3VYn5CbWjp3uLyxO_m8nJ2re6_FwaRyhV_CUUIZ9zrffjr6gIBmXrAxnsX8i7Vdp2IwPwgQX2diheR7AE", 
    "6": 1634933106, 
    "7": 1, 
    "8": {
        "1": {
            "-1": "93fTt4IB7ylJCd7gi3rTWQ", 
            "1": 4
        }
    }
}


Everything seems fine but compared to an online Tool to convert cbor to string (http://cbor.me/). The results are different Output from online Tool (the expected output):

{
    "1": h'8343A1010AA1054DBAE3FF8C349E9815E179854CEC584E7B3E1905320079728F48E4E01296B2D984FDD5627E426D68E9DEE2F2C4EFE6F27276ADEEBF170691CA157F09450867DCEB7DF8EBEA0201997AC0C67B17F22ED5769D88C0FC20417D9D8A1791EC01', 
    "6": 1634933106, 
    "7": 1, 
    "8": {
        "1": {
            "-1": h'F777D3B78201EF294909DEE08B7AD359', 
            "1": 4
        }
    }
}

Values of 1 and -1 are different which I dont understand. I would guess that typing is the problem but I dont know how I can print the values of 1 and -1 properly. This might help (https://github.com/danielaparker/jsoncons/blob/master/doc/ref/cbor/cbor.md). It shows the type conversion from cbor to json but I still struggle with printing the values of 1 and -1 correctly (the only ones I really need).


Solution

  • The values in your output are base64-encoded, but you expect them in hex. See Base64 decode snippet in C++ for help with decoding in C++. Or see this example from the jsoncons documentation.