USCiLab / cereal

A C++11 library for serialization
BSD 3-Clause "New" or "Revised" License
4.25k stars 767 forks source link

I need a way to skip prologue and epilogue in JSON sometimes. #837

Open Rational-pi opened 1 month ago

Rational-pi commented 1 month ago

I recurrently need to write some C++ objects not as objects in JSON. For example, QString. If I implement the standard serialization, I end up with: "QStingVariableName"{ "Value0":"whatever"} but I need it to be: "QStingVariableName":"whatever" I hacked a bit the json archive to do so but It would be great to have a way to do so canonicaly

there is my hack: diff.txt

I use it like that for exemple for QString:

namespace cereal /*QString*/{
template <> struct SkipPrologFlag<QString>{};
template <class Ar>
inline void save(Ar& ar, const QString& str){
    ar.saveValue(str.toStdString());
}
template <class Ar>
inline void load(Ar& ar, QString& str){
    std::string tmp;
    ar.loadValue(tmp);
    str=QString::fromStdString(tmp);
}
}