mirror of
https://github.com/luanti-org/luanti.git
synced 2025-10-28 14:15:18 +01:00
Item metadata:Use ASCII control characters 0x01 to 0x03 as key/value delimiters, and don't JSON-encode 2 times.
This commit is contained in:
@@ -27,6 +27,14 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||||||
#include "constants.h" // MAP_BLOCKSIZE
|
#include "constants.h" // MAP_BLOCKSIZE
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
|
//delimiter constants
|
||||||
|
#define DESERIALIZE_START '\x01'
|
||||||
|
#define DESERIALIZE_KV_DELIM '\x02'
|
||||||
|
#define DESERIALIZE_PAIR_DELIM '\x03'
|
||||||
|
#define DESERIALIZE_START_STR "\x01"
|
||||||
|
#define DESERIALIZE_KV_DELIM_STR "\x02"
|
||||||
|
#define DESERIALIZE_PAIR_DELIM_STR "\x03"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
ItemStackMetadata
|
ItemStackMetadata
|
||||||
*/
|
*/
|
||||||
@@ -34,12 +42,12 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||||||
std::string ItemStackMetadata::serialize() const
|
std::string ItemStackMetadata::serialize() const
|
||||||
{
|
{
|
||||||
std::ostringstream os;
|
std::ostringstream os;
|
||||||
os << "$";
|
os << DESERIALIZE_START;
|
||||||
for (StringMap::const_iterator
|
for (StringMap::const_iterator
|
||||||
it = m_stringvars.begin();
|
it = m_stringvars.begin();
|
||||||
it != m_stringvars.end(); ++it) {
|
it != m_stringvars.end(); ++it) {
|
||||||
os << it->first << "=";
|
os << it->first << DESERIALIZE_KV_DELIM;
|
||||||
os << serializeJsonString(it->second) << "\n";
|
os << serializeJsonString(it->second) << DESERIALIZE_PAIR_DELIM;
|
||||||
}
|
}
|
||||||
return os.str();
|
return os.str();
|
||||||
}
|
}
|
||||||
@@ -47,13 +55,13 @@ std::string ItemStackMetadata::serialize() const
|
|||||||
void ItemStackMetadata::deSerialize(std::string &in)
|
void ItemStackMetadata::deSerialize(std::string &in)
|
||||||
{
|
{
|
||||||
m_stringvars.clear();
|
m_stringvars.clear();
|
||||||
//to indicate new serialisation version, a dollar sign has to be the first char in the serialized string. if not, apply legacy deserialization
|
//to indicate new serialisation version, a DESERIALIZE_START character has to be the first char in the serialized string. if not, apply legacy deserialization
|
||||||
if (in[0] == '$') {
|
if (in[0] == DESERIALIZE_START) {
|
||||||
Strfnd fnd(in);
|
Strfnd fnd(in);
|
||||||
fnd.to(1);
|
fnd.to(1);
|
||||||
while ( !fnd.at_end() ) {
|
while ( !fnd.at_end() ) {
|
||||||
std::string name = fnd.next("=");
|
std::string name = fnd.next(DESERIALIZE_KV_DELIM_STR);
|
||||||
std::string var_json=fnd.next("\n");
|
std::string var_json=fnd.next(DESERIALIZE_PAIR_DELIM_STR);
|
||||||
std::istringstream var_stream(var_json);
|
std::istringstream var_stream(var_json);
|
||||||
std::string var = deSerializeJsonString(var_stream);
|
std::string var = deSerializeJsonString(var_stream);
|
||||||
m_stringvars[name] = var;
|
m_stringvars[name] = var;
|
||||||
|
|||||||
Reference in New Issue
Block a user