msgpack-c icon indicating copy to clipboard operation
msgpack-c copied to clipboard

Deserialization from human-readable string

Open taushkanov opened this issue 5 years ago • 3 comments

Is there any convinient way to deserialize msgpack object from string like this: [[2,17,1,0,""]] ?

taushkanov avatar Apr 27 '20 14:04 taushkanov

Here is a document: https://github.com/msgpack/msgpack-c/wiki/v_2_0_cpp_json

From JSON string to msgpack::object conversion is not directly supported but to msgpack formatted byte stream is supported by JSON for Modern C++.

See https://github.com/nlohmann/json/blob/bab5826504ac04695c33954c07ff832fc7b4b2c8/doc/binary_formats.md

So you can get msgpack::object using the following step:

  1. Convert from JSON to msgpack formatted byte stream using JSON for Modern C++
  2. Convert from msgpack formatted byte stream to msgpack::object using msgpack-c. It is called unpack.

redboltz avatar Apr 27 '20 15:04 redboltz

Thank you very much. The string "[[2,17,1,0,""]]" is created by code like this:

stringstream ss; msgpack::object obj; ss << obj; string str = ss.str();

Its sadly that msgpack doesnt have internal backward conversion.

taushkanov avatar Apr 27 '20 17:04 taushkanov

EXT types including timestamp aren't supported but other types output as json. See https://github.com/msgpack/msgpack-c/blob/master/include/msgpack/v1/object.hpp#L396 [[2,17,1,0,""]] is json.

msgpack::object has visitor interface. See https://github.com/msgpack/msgpack-c/wiki/v2_0_cpp_visitor. This means you can generate any output format.

json to msgpack::object is different. It is out of scope, so far. In order to implement it, msgpack-c needs to support json parser. Fortunately, some of json parser has msgpack formatted binary data converter. If you want to msgpack::object from json, you can implement it using json library.

redboltz avatar May 01 '20 11:05 redboltz