cxxtools::JsonSerializer Class Reference

Here is a small example to use this class. More...

#include <cxxtools/jsonserializer.h>

Public Member Functions

 JsonSerializer ()
 JsonSerializer (std::basic_ostream< cxxtools::Char > &ts)
 JsonSerializer (std::ostream &os, TextCodec< cxxtools::Char, char > *codec=0)
 ~JsonSerializer ()
JsonSerializerbegin (std::basic_ostream< cxxtools::Char > &ts)
JsonSerializerbegin (std::ostream &os, TextCodec< cxxtools::Char, char > *codec=0)
void finish ()
template<typename T >
JsonSerializerserialize (const T &v, const std::string &name)
template<typename T >
JsonSerializerserialize (const T &v)
void setObject ()
bool object () const
bool beautify () const
void beautify (bool sw)
bool plainkey () const
void plainkey (bool sw)

Static Public Member Functions

template<typename T >
static std::string toString (const T &type, const std::string &name, bool beautify=false)

Detailed Description

Here is a small example to use this class.

#include <iostream>
// define a configuration object
struct Configuration
{
struct Indent
{
int length;
bool useSpace;
};
std::string encoding;
std::vector<std::string> plugIns;
Indent indent;
};
// define how to serialize the indent struct of configuration
void operator<<= (cxxtools::SerializationInfo& si, const Configuration::Indent& indent)
{
si.addMember("length") <<= indent.length;
si.addMember("use_space") <<= indent.useSpace;
}
// define how to serialize the configuration
void operator<<= (cxxtools::SerializationInfo& si, const Configuration& config)
{
si.addMember("encoding") <<= config.encoding;
si.addMember("plug-ins") <<= config.plugIns;
si.addMember("indent") <<= config.indent;
}
int main(int argc, char* argv[])
{
try
{
// create a configuration object:
Configuration config;
config.encoding = "UTF-8";
config.plugIns.push_back("python");
config.plugIns.push_back("c++");
config.plugIns.push_back("ruby");
config.indent.length = 3;
config.indent.useSpace = true;
// serialize to json
cxxtools::JsonSerializer serializer(std::cout);
serializer.beautify(true); // this makes it just nice to read
serializer.serialize(config).finish();
}
catch (const std::exception& e)
{
std::cerr << e.what() << std::endl;
}
}

Constructor & Destructor Documentation

cxxtools::JsonSerializer::JsonSerializer ( )
inline
cxxtools::JsonSerializer::JsonSerializer ( std::basic_ostream< cxxtools::Char > &  ts)
inlineexplicit
cxxtools::JsonSerializer::JsonSerializer ( std::ostream &  os,
TextCodec< cxxtools::Char, char > *  codec = 0 
)
explicit
cxxtools::JsonSerializer::~JsonSerializer ( )
inline

Member Function Documentation

bool cxxtools::JsonSerializer::beautify ( ) const
inline
void cxxtools::JsonSerializer::beautify ( bool  sw)
inline
JsonSerializer& cxxtools::JsonSerializer::begin ( std::basic_ostream< cxxtools::Char > &  ts)
inline
JsonSerializer& cxxtools::JsonSerializer::begin ( std::ostream &  os,
TextCodec< cxxtools::Char, char > *  codec = 0 
)
void cxxtools::JsonSerializer::finish ( )
inline
bool cxxtools::JsonSerializer::object ( ) const
inline
bool cxxtools::JsonSerializer::plainkey ( ) const
inline
void cxxtools::JsonSerializer::plainkey ( bool  sw)
inline
template<typename T >
JsonSerializer& cxxtools::JsonSerializer::serialize ( const T &  v,
const std::string &  name 
)
inline
template<typename T >
JsonSerializer& cxxtools::JsonSerializer::serialize ( const T &  v)
inline
void cxxtools::JsonSerializer::setObject ( )
inline
template<typename T >
static std::string cxxtools::JsonSerializer::toString ( const T &  type,
const std::string &  name,
bool  beautify = false 
)
inlinestatic

The documentation for this class was generated from the following file: