Skip to content

Using Nullable

Dmitry Utkin edited this page Jun 1, 2016 · 1 revision

To support JSON null value on C++ side ngrest::Nullable class is provided.

ngrest::Nullable is a template class which accepts any supported with ngrest datatype as template parameter. It can be used as request parameter, response, as well as structure member.

Here is few examples on how ngrest::Nullable can be used:

struct User
{
    ngrest::Nullable<int> id;  // used when retrieving user information by id
    std::string email;
    std::string login;
    ngrest::Nullable<std::string> password; // used when registering user
    ngrest::Nullable<UserInfo> additionalInfo; // used sometimes
    ...
};

class UsersService: public ngrest::Service
{
public:
    // returns null if no user with such id found
    ngrest::Nullable<User> get(int id);

    // search users by either of login or email or both
    std::list<User> find(const ngrest::Nullable<std::string>& login, const ngrest::Nullable<std::string>& email);
    ...
};