29 #ifndef CEREAL_ARCHIVES_JSON_HPP_
30 #define CEREAL_ARCHIVES_JSON_HPP_
32 #include <cereal/cereal.hpp>
33 #include <cereal/details/util.hpp>
35 #include <cereal/external/rapidjson/prettywriter.h>
36 #include <cereal/external/rapidjson/genericstream.h>
37 #include <cereal/external/base64.hpp>
54 typedef rapidjson::GenericWriteStream WriteStream;
55 typedef rapidjson::PrettyWriter<WriteStream> JSONWriter;
63 itsWriteStream(stream),
64 itsWriter(itsWriteStream)
66 itsNameCounter.push(0);
67 itsWriter.StartObject();
73 itsWriter.EndObject();
76 void saveValue(
bool b) { itsWriter.Bool(b); }
77 void saveValue(
int i) { itsWriter.Int(i); }
78 void saveValue(
unsigned u) { itsWriter.Uint(u); }
79 void saveValue(int64_t i64) { itsWriter.Int64(i64); }
80 void saveValue(uint64_t u64) { itsWriter.Uint64(u64); }
81 void saveValue(
double d) { itsWriter.Double(d); }
82 void saveValue(std::string
const & s) { rawString(s); }
83 void saveValue(
char const * s) { itsWriter.String(s); }
84 void saveNull() { itsWriter.Null(); }
88 if(itsNextName ==
nullptr)
90 std::string name =
"value" + std::to_string( itsNameCounter.top()++ ) +
"\0";
95 saveValue(itsNextName);
96 itsNextName =
nullptr;
100 void rawString(std::string
const & s)
102 itsWriter.String(s.c_str(), s.size());
114 itsWriter.StartObject();
116 itsNameCounter.push(0);
122 itsWriter.EndObject();
124 itsNameCounter.pop();
140 void setOutputType(
bool outputType)
142 itsOutputType = outputType;
148 WriteStream itsWriteStream;
149 JSONWriter itsWriter;
150 std::ostringstream itsOS;
152 char const * itsNextName;
153 std::stack<uint32_t> itsNameCounter;
178 void epilogue( JSONOutputArchive &, NameValuePair<T>
const & )
184 void prologue( JSONOutputArchive &, SizeTag<T>
const & )
190 void epilogue( JSONOutputArchive &, SizeTag<T>
const & )
197 typename std::enable_if<!std::is_arithmetic<T>::value,
void>::type
198 prologue( JSONOutputArchive & ar, T
const & data )
206 typename std::enable_if<!std::is_arithmetic<T>::value,
void>::type
207 epilogue( JSONOutputArchive & ar, T
const & data )
213 typename std::enable_if<std::is_arithmetic<T>::value,
void>::type
214 prologue( JSONOutputArchive & ar, T
const & data )
220 typename std::enable_if<std::is_arithmetic<T>::value,
void>::type
221 epilogue( JSONOutputArchive & ar, T
const & data )
225 template<
class CharT,
class Traits,
class Alloc>
inline
226 void prologue(JSONOutputArchive & ar, std::basic_string<CharT, Traits, Alloc>
const & str)
231 template<
class CharT,
class Traits,
class Alloc>
inline
232 void epilogue(JSONOutputArchive & ar, std::basic_string<CharT, Traits, Alloc>
const & str)
240 template <
class Archive,
class T>
inline
241 CEREAL_ARCHIVE_RESTRICT(JSONInputArchive, JSONOutputArchive)
242 serialize( Archive & ar, NameValuePair<T> & t )
244 ar.setNextName( t.name );
249 template <
class Archive,
class T>
inline
250 CEREAL_ARCHIVE_RESTRICT(JSONInputArchive, JSONOutputArchive)
251 serialize( Archive & ar, SizeTag<T> & )
255 template<
class T>
inline
256 typename std::enable_if<std::is_arithmetic<T>::value,
void>::type
257 save(JSONOutputArchive & ar, T
const & t)
263 template<
class CharT,
class Traits,
class Alloc>
inline
264 void save(JSONOutputArchive & ar, std::basic_string<CharT, Traits, Alloc>
const & str)
274 #endif // CEREAL_ARCHIVES_JSON_HPP_