简体   繁体   中英

Serialize/Deserialize a byte array using Json in c++

I'm working with Json::Value and I need to Serialize/Deserialize a byte array.

How can I convert a byte array into Json object in c++? and also the opposite way

Thanks

Are you using a C++ library for JSON? The jsoncpp project calls its types Json::Value so you may be using that. In jsoncpp values are serialized as UTF-8 strings which are essentially byte arrays.

You can use Json::FastWriter to serialize:

Json::FastWriter fastWritter;
std::string serialized = fastWritter.write(value);    
serialized.c_str(); // this is the raw byte array (null terminated).

And Json::Reader to deserialize:

Json::Reader reader;
Json::Value value;
reader.parse(serializedString, value);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM