-
Notifications
You must be signed in to change notification settings - Fork 93
Using complex data types
Dmitry edited this page Oct 23, 2018
·
3 revisions
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:
- Basic types: bool, integer types, float types;
- Enumerated types:
enum
andenum class
. Nestedenums
withstruct
parent are supported too; - STL string:
std::string
; - C++ Structures. Nested structures and inherited structures are supported too;
- STL array containers:
std::vector
,std::list
holding supported data types (1-7); - STL map containers:
std::map
,std::unordeder_map
holding data types 1-3 as a key and data types 1-7 as value; - Typedefs of the data types above;
Additional ngrest types supported in requests:
-
ngrest::Callback<T>
- asynchronous callback type; -
ngrest::MessageContext
- message context with partially processed data. Used to get full control on message processing or to perform reply with custom content type.
ngrest cannot serialize:
- Pointers;
- References;
- Templates;
- Classes -- they're completely ignored;
- Private and protected fields of structures;
- Private and protected structure inheritance;
- Any other things which is not declared as supported.
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
"[email protected]", // email
true, // email confirmed
21, // age
JobType::Employee, // job type
1.0f // rating
};
JSON:
{
"id": 12,
"name": "John Smith",
"email": "[email protected]",
"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