Wt: Wt::Dbo::JsonSerializer Class Reference

Wt 4.10.4

Loading...
Searching...
No Matches
Public Member Functions | List of all members
Wt::Dbo::JsonSerializer Class Reference
Database Objects (Wt::Dbo)

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.
virtual | ~JsonSerializer ()
| Destructor.
template<typename T >
void | serialize (const T &t)
| Serialize the given object.
template<typename T >
void | serialize (const ptr< T > &t)
| Serialize the object that is pointed to by the given ptr.
template<typename T >
void | serialize (const std::vector< ptr< T > > &v)
| Serialize an std::vector of ptrs.
template<typename T >
void | serialize (const collection< ptr< T > > &c)
| Serialize a collection of ptrs.

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

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.