Starting a JSON archive

This commit is contained in:
Randolph Voorhies
2013-06-12 20:37:36 -07:00
parent b1ef8f9ddb
commit 1b7d5387d2
5 changed files with 122 additions and 31 deletions

View File

@@ -1,2 +1,2 @@
all: test.cpp cereal.hpp
g++ -std=c++0x test.cpp -o test
g++ -std=c++0x test.cpp -o test -ljsoncpp

View File

@@ -54,7 +54,7 @@ namespace cereal
save(BinaryOutputArchive & ar, T const & t)
{
ar.save_binary(std::addressof(t), sizeof(t));
std::cout << "Saving POD size: " << sizeof(T) << " [" << t << "]" << std::endl;
//std::cout << "Saving POD size: " << sizeof(T) << " [" << t << "]" << std::endl;
}
//! Loading for POD types from binary
@@ -63,14 +63,14 @@ namespace cereal
load(BinaryInputArchive & ar, T & t)
{
ar.load_binary(std::addressof(t), sizeof(t));
std::cout << "Loading POD size: " << sizeof(T) << " [" << t << "]" << std::endl;
//std::cout << "Loading POD size: " << sizeof(T) << " [" << t << "]" << std::endl;
}
//! Saving for NVP types to binary
template<class T>
void save(BinaryOutputArchive & ar, NameValuePair<T> const & t)
{
std::cout << "Saving NVP: " << t.name << std::endl;
//std::cout << "Saving NVP: " << t.name << std::endl;
ar & t.value;
}
@@ -78,7 +78,7 @@ namespace cereal
template<class T>
void load(BinaryInputArchive & ar, NameValuePair<T> t)
{
std::cout << "Loading NVP... " << std::endl;
//std::cout << "Loading NVP... " << std::endl;
ar & t.value;
}
@@ -90,7 +90,7 @@ namespace cereal
ar & str.size();
ar.save_binary(str.data(), str.size() * sizeof(CharT));
std::cout << "Saving string: " << str << std::endl;
//std::cout << "Saving string: " << str << std::endl;
}
//! Serialization for basic_string types from binary
@@ -101,6 +101,6 @@ namespace cereal
ar & size;
str.resize(size);
ar.load_binary(const_cast<CharT*>(str.data()), size * sizeof(CharT));
std::cout << "Loading string: " << str << std::endl;
//std::cout << "Loading string: " << str << std::endl;
}
}

View File

@@ -1,3 +1,5 @@
#pragma once
#include <iostream>
#include <type_traits>
#include "traits.hpp"
@@ -39,8 +41,6 @@ namespace cereal
ArchiveType &>::type
operator & (T && t)
{
std::cout << "Member serialize" << std::endl;
t.serialize(*self);
return *self;
}
@@ -51,8 +51,6 @@ namespace cereal
ArchiveType &>::type
operator & (T && t)
{
std::cout << "Non member serialize" << std::endl;
serialize(*self, std::forward<T>(t));
return *self;
}
@@ -63,8 +61,6 @@ namespace cereal
ArchiveType &>::type
operator & (T const & t)
{
std::cout << "Member split" << std::endl;
t.save(*self);
return *self;
}
@@ -75,7 +71,6 @@ namespace cereal
ArchiveType &>::type
operator & (T const & t)
{
std::cout << "Non member split" << std::endl;
save(*self, t);
return *self;
}
@@ -114,8 +109,6 @@ namespace cereal
ArchiveType &>::type
operator & (T && t)
{
std::cout << "Member serialize" << std::endl;
t.serialize(*self);
return *self;
}
@@ -126,8 +119,6 @@ namespace cereal
ArchiveType &>::type
operator & (T && t)
{
std::cout << "Non member serialize" << std::endl;
serialize(*self, t);
return *self;
}
@@ -138,8 +129,6 @@ namespace cereal
ArchiveType &>::type
operator & (T && t)
{
std::cout << "Member split" << std::endl;
t.load(*self);
return *self;
}
@@ -150,7 +139,6 @@ namespace cereal
ArchiveType &>::type
operator & (T && t)
{
std::cout << "Non member split" << std::endl;
load(*self, std::forward<T>(t));
return *self;
}

98
json_archive.hpp Normal file
View File

@@ -0,0 +1,98 @@
#pragma once
#include "cereal.hpp"
#include <cassert>
#include <jsoncpp/json/json.h>
namespace cereal
{
// ######################################################################
class JSONOutputArchive : public OutputArchive<JSONOutputArchive>
{
public:
JSONOutputArchive(std::ostream & stream) :
OutputArchive<JSONOutputArchive>(this),
itsStream(stream)
{ }
Json::StyledStreamWriter & writer() { return itsWriter; }
std::ostream & stream() {return itsStream; }
private:
std::ostream & itsStream;
Json::StyledStreamWriter itsWriter;
};
// ######################################################################
class JSONInputArchive : public InputArchive<JSONInputArchive>
{
public:
JSONInputArchive(std::istream & stream) :
InputArchive<JSONInputArchive>(this),
itsStream(stream)
{ }
std::istream & stream() {return itsStream; }
private:
std::istream & itsStream;
};
//! Saving for POD types to json
template<class T>
typename std::enable_if<std::is_arithmetic<T>::value, void>::type
save(JSONOutputArchive & ar, T const & t)
{
//ar.stream() << t;
}
//! Loading for POD types from json
template<class T>
typename std::enable_if<std::is_arithmetic<T>::value, void>::type
load(JSONInputArchive & ar, T & t)
{
//ar.stream() >> t;
}
//! Saving for NVP types to json
template<class T>
void save(JSONOutputArchive & ar, NameValuePair<T> const & t)
{
//ar.stream() << t.name << " : ";
//
//ar & t.value;
//ar.stream() << "\n";
Json::Value v;
v[t.name] = t.value;
ar.writer().write(ar.stream(), v);
//ar.writer().write(ar.stream(), Json::Value(t.name));
//ar.writer().write(ar.stream(), Json::Value(t.value));
}
//! Loading for NVP types from json
template<class T>
void load(JSONInputArchive & ar, NameValuePair<T> t)
{
ar & t.value;
}
//! Serialization for basic_string types to json
template<class CharT, class Traits, class Alloc>
void save(JSONOutputArchive & ar, std::basic_string<CharT, Traits, Alloc> const & str)
{
//ar.stream() << "\"" << str << "\"";
}
//! Serialization for basic_string types from json
template<class CharT, class Traits, class Alloc>
void load(JSONInputArchive & ar, std::basic_string<CharT, Traits, Alloc> & str)
{
//size_t size;
//ar & size;
//str.resize(size);
//ar.load_json(const_cast<CharT*>(str.data()), size * sizeof(CharT));
//std::cout << "Loading string: " << str << std::endl;
}
}

View File

@@ -1,5 +1,6 @@
#include "cereal.hpp"
#include "binary_archive.hpp"
#include "json_archive.hpp"
#include <cxxabi.h>
#include <sstream>
#include <fstream>
@@ -13,7 +14,7 @@ struct Test1
template<class Archive>
void serialize(Archive & ar)
{
ar & a;
ar & CEREAL_NVP(a);
}
};
@@ -25,13 +26,13 @@ struct Test2
template<class Archive>
void save(Archive & ar) const
{
ar & a;
ar & CEREAL_NVP(a);
}
template<class Archive>
void load(Archive & ar)
{
ar & a;
ar & CEREAL_NVP(a);
}
};
@@ -44,7 +45,7 @@ struct Test3
template<class Archive>
void serialize(Archive & ar, Test3 & t)
{
ar & t.a;
ar & CEREAL_NVP(t.a);
}
namespace test4
@@ -58,13 +59,13 @@ namespace test4
template<class Archive>
void save(Archive & ar, Test4 const & t)
{
ar & t.a;
ar & CEREAL_NVP(t.a);
}
template<class Archive>
void load(Archive & ar, Test4 & t)
{
ar & t.a;
ar & CEREAL_NVP(t.a);
}
}
@@ -119,20 +120,24 @@ int main()
{
std::ofstream os("out.txt");
cereal::BinaryOutputArchive archive(os);
archive & e_out;
archive & CEREAL_NVP(e_out);
}
std::cout << "----------------" << std::endl;
Everything e_in;
{
std::ifstream is("out.txt");
cereal::BinaryInputArchive archive(is);
archive & e_in;
archive & CEREAL_NVP(e_in);
}
assert(e_in == e_out);
cereal::JSONOutputArchive json(std::cout);
std::string hello = "Hello, World!";
json & CEREAL_NVP(hello);
//json & CEREAL_NVP(e_out); <<< Need to figure out how to recurse!
return 0;
}