Wt  4.11.1
Public Member Functions | List of all members
Wt::Dbo::JsonSerializer Class Reference

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...
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ JsonSerializer()

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.

Member Function Documentation

◆ serialize() [1/4]

template<typename T >
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.

◆ serialize() [2/4]

template<typename T >
void Wt::Dbo::JsonSerializer::serialize ( const ptr< T > &  t)

Serialize the object that is pointed to by the given ptr.

This method does the same as the plain object serializer, but also adds an extra id field.

◆ serialize() [3/4]

template<typename T >
void Wt::Dbo::JsonSerializer::serialize ( const std::vector< ptr< T > > &  v)

Serialize an std::vector of ptrs.

Serializes each ptr in the vector individually, and puts it in an Array.

◆ serialize() [4/4]

template<typename T >
void Wt::Dbo::JsonSerializer::serialize ( const T &  t)

Serialize the given object.

Serializes a plain object that implements the persist() method.