Wt
4.11.1
|
An action to serialize objects to JSON. More...
#include <Wt/Dbo/Json.h>
Public Member Functions | |
JsonSerializer (std::ostream &out) | |
Creates a JsonSerializer that writes to an std::ostream. More... | |
virtual | ~JsonSerializer () |
Destructor. | |
template<typename T > | |
void | serialize (const T &t) |
Serialize the given object. More... | |
template<typename T > | |
void | serialize (const ptr< T > &t) |
Serialize the object that is pointed to by the given ptr. More... | |
template<typename T > | |
void | serialize (const std::vector< ptr< T > > &v) |
Serialize an std::vector of ptrs. More... | |
template<typename T > | |
void | serialize (const collection< ptr< T > > &c) |
Serialize a collection of ptrs. More... | |
An action to serialize objects to JSON.
This class is an Action
that serializes objects to an ostream. These objects must implement the persist()
method. It also has support for serializing ptrs to these objects, std::vectors of ptrs, and collections of ptrs.
It will follow one-to-one and ManyToOne relations in one way: weak_ptr and collection fields are followed and serialized, for ptr fields only the id is output.
Including Wt/Dbo/WtJsonSqlTraits.h
also makes this class able to serialize WString, WDate, WDateTime and WTime.
By default, WDate, WDateTime and WTime are serialized using the default format of their respective class. It can be changed by setting sql_value_traits<WDate>::format
, sql_value_traits<WDateTime>::format
and sql_value_traits<WTime>::format
.
No extraneous whitespace is output.
Wt::Dbo::JsonSerializer::JsonSerializer | ( | std::ostream & | out | ) |
Creates a JsonSerializer that writes to an std::ostream.
Note that the std::ostream is not flushed to automatically. The flush will happen automatically when this JsonSerializer is destructed.
void Wt::Dbo::JsonSerializer::serialize | ( | const collection< ptr< T > > & | c | ) |
Serialize a collection of ptrs.
Serializes each ptr in the collection individually, and puts it in an Array
.
The typical usage scenario of this method is to serialize the results of a query to JSON.
void Wt::Dbo::JsonSerializer::serialize | ( | const ptr< T > & | t | ) |
void Wt::Dbo::JsonSerializer::serialize | ( | const std::vector< ptr< T > > & | v | ) |
void Wt::Dbo::JsonSerializer::serialize | ( | const T & | t | ) |
Serialize the given object.
Serializes a plain object that implements the persist()
method.