1 #include "itemstackmetadata.h"
2 #include "util/serialize.h"
3 #include "util/strfnd.h"
5 #define DESERIALIZE_START '\x01'
6 #define DESERIALIZE_KV_DELIM '\x02'
7 #define DESERIALIZE_PAIR_DELIM '\x03'
8 #define DESERIALIZE_START_STR "\x01"
9 #define DESERIALIZE_KV_DELIM_STR "\x02"
10 #define DESERIALIZE_PAIR_DELIM_STR "\x03"
12 void ItemStackMetadata::serialize(std::ostream &os) const
14 std::ostringstream os2;
15 os2 << DESERIALIZE_START;
16 for (const auto &stringvar : m_stringvars) {
17 if (!stringvar.first.empty() || !stringvar.second.empty())
18 os2 << stringvar.first << DESERIALIZE_KV_DELIM
19 << stringvar.second << DESERIALIZE_PAIR_DELIM;
21 os << serializeJsonStringIfNeeded(os2.str());
24 void ItemStackMetadata::deSerialize(std::istream &is)
26 std::string in = deSerializeJsonStringIfNeeded(is);
31 if (in[0] == DESERIALIZE_START) {
34 while (!fnd.at_end()) {
35 std::string name = fnd.next(DESERIALIZE_KV_DELIM_STR);
36 std::string var = fnd.next(DESERIALIZE_PAIR_DELIM_STR);
37 m_stringvars[name] = var;
40 // BACKWARDS COMPATIBILITY
41 m_stringvars[""] = in;