Skip to content

Using complex data types

Dmitry edited this page Oct 23, 2018 · 3 revisions

Using complex data types

Supported data types

Due to limitations only subset of C++ data types is supported for requests and responses. ngrest can automatically serialize and deserialize data types as follows:

  1. Basic types: bool, integer types, float types;
  2. Enumerated types: enum and enum class. Nested enums with struct parent are supported too;
  3. STL string: std::string;
  4. C++ Structures. Nested structures and inherited structures are supported too;
  5. STL array containers: std::vector, std::list holding supported data types (1-7);
  6. STL map containers: std::map, std::unordeder_map holding data types 1-3 as a key and data types 1-7 as value;
  7. Typedefs of the data types above;

Additional ngrest types supported in requests:

  1. ngrest::Callback<T> - asynchronous callback type;
  2. ngrest::MessageContext - message context with partially processed data. Used to get full control on message processing or to perform reply with custom content type.

Unsupported data types

ngrest cannot serialize:

  1. Pointers;
  2. References;
  3. Templates;
  4. Classes -- they're completely ignored;
  5. Private and protected fields of structures;
  6. Private and protected structure inheritance;
  7. Any other things which is not declared as supported.

Examples of mapping the C++ data types to JSON:

Structures used in examples below:

struct Person {
    enum class JobType {
        Unknown,
        Employee,
        Homemaker
    };

    int id;
    std::string name;
    std::string email;
    bool emailConfirmed;
    int age;
    JobType jobType;
    float rating;
};

struct Simple {
    int id;
    std::string name;
};

struct Parent: public Simple {
    struct Nested {
        int someData;
    };

    Nested nested;
};

struct Child: public Parent {
    int type;
};

Examples on how data types are mapped to JSON:


C++:

Person p = {
  12, // id
  "John Smith", // name
  "johnsmith@example.org", // email
  true, // email confirmed
  21, // age
  JobType::Employee, // job type
  1.0f // rating
};

JSON:

{
  "id": 12,
  "name": "John Smith",
  "email": "johnsmith@example.org",
  "emailConfirmed": true,
  "age": 21,
  "jobType": "Employee",
  "rating": 1.0
};

C++:

Child c = {
    5, // id
    "Thing", // name
    { // nested 
        145 // someData
    }
};

JSON:

{
    "id": 5,
    "name": "Thing",
    "someData": 145
}

C++:

std::list<int> l = {5, 4, 3, 2, 1};

JSON:

[5, 4, 3, 2, 1]

C++:

std::list<Simple> l = {{1, "One"}, {2, "Two"}};

JSON:

[
  {"id": 1, "name": "One"},
  {"id": 2, "name": "Two"}
]

C++:

std::map<int, std::string> m = {
    {1, "One"},
    {2, "Two"}
};

JSON:

{
  "1": "One",
  "2": "Two"
}

C++:

std::map<std::string, Simple> m = {
    {"one", {1, "One"}},
    {"two", {2, "Two"}}
};

JSON:

{
  "one": {"id": 1, "name": "One"},
  "two": {"id": 2, "name": "Two"}
}

See also Example of ngrest service with using the complex types