-1

I am trying to construct a MsgPack object, using MsgPuck library for the following JSON object:

{
    "Key_1": "val_1",
    "array_1": [ 
            {
                "key_3": "val_3",
                "key_4": "val_4"
            }
        ]
}

The following is my code:

{
    char buff[1024];
    char *ptr = buff;

    ptr = mp_encode_map(ptr, 2);
    ptr = mp_encode_str(ptr, "key_1", strlen("key_1"));
    ptr = mp_encode_str(ptr, "val_1", strlen("val_1"));
    ptr = mp_encode_str(ptr, "array_1", strlen("array_1"));
    ptr = mp_encode_array(ptr, 2);
    ptr = mp_encode_map(ptr, 2);
    ptr = mp_encode_str(ptr, "key_3", strlen("key_3"));
    ptr = mp_encode_str(ptr, "val_3", strlen("val_3"));
    ptr = mp_encode_str(ptr, "key_4", strlen("key_4"));
    ptr = mp_encode_str(ptr, "val_4", strlen("val_4"));

    FILE *fp = fopen("/tmp/test.mp","w");
    fwrite(buff, 1, ptr - buff, fp);
    fclose(fp);
}

But the generated MsgPack data is as following:

$ xxd /tmp/test.mp
00000000: 82a5 6b65 795f 31a5 7661 6c5f 31a7 6172  ..key_1.val_1.ar
00000010: 7261 795f 3192 82a5 6b65 795f 33a5 7661  ray_1...key_3.va
// Expected val 91 -> ^^
00000020: 6c5f 33a5 6b65 795f 34a5 7661 6c5f 34    l_3.key_4.val_4

What am I doing wrong here?

Edit:

I found the problem in the code: I should use,

 ptr = mp_encode_array(ptr, 1);

instead of

 ptr = mp_encode_array(ptr, 2);
user58626
  • 1
  • 1

1 Answers1

0

I found the problem in the code: I should use,

 ptr = mp_encode_array(ptr, 1);

instead of

 ptr = mp_encode_array(ptr, 2);

– user58626

Armali
  • 18,255
  • 14
  • 57
  • 171