Create and user helper functions for serialization
This commit is contained in:
parent
af8fde1222
commit
f0f33c4ac7
1 changed files with 28 additions and 19 deletions
|
|
@ -34,12 +34,38 @@ struct Person
|
|||
int age;
|
||||
};
|
||||
|
||||
void serialize_person(Json* json, const struct Person* person)
|
||||
{
|
||||
json_begin_object(json);
|
||||
{
|
||||
json_add_object_field(json, "name");
|
||||
json_add_string(json, person->name);
|
||||
|
||||
json_add_object_field(json, "age");
|
||||
json_add_long(json, person->age);
|
||||
}
|
||||
json_end_object(json);
|
||||
}
|
||||
|
||||
struct Status
|
||||
{
|
||||
const char* description;
|
||||
bool is_success;
|
||||
};
|
||||
|
||||
void serialize_status(Json* json, const struct Status* status)
|
||||
{
|
||||
json_begin_object(json);
|
||||
{
|
||||
json_add_object_field(json, "description");
|
||||
json_add_string(json, status->description);
|
||||
|
||||
json_add_object_field(json, "is_success");
|
||||
json_add_bool(json, status->is_success);
|
||||
}
|
||||
json_end_object(json);
|
||||
}
|
||||
|
||||
struct PersonsResult
|
||||
{
|
||||
struct Status status;
|
||||
|
|
@ -65,15 +91,7 @@ void test_json_complex()
|
|||
json_begin_object(&json);
|
||||
{
|
||||
json_add_object_field(&json, "status");
|
||||
json_begin_object(&json);
|
||||
{
|
||||
json_add_object_field(&json, "description");
|
||||
json_add_string(&json, persons_result.status.description);
|
||||
|
||||
json_add_object_field(&json, "is_success");
|
||||
json_add_bool(&json, persons_result.status.is_success);
|
||||
}
|
||||
json_end_object(&json);
|
||||
serialize_status(&json, &persons_result.status);
|
||||
|
||||
json_add_object_field(&json, "length");
|
||||
json_add_long(&json, persons_result.length);
|
||||
|
|
@ -84,16 +102,7 @@ void test_json_complex()
|
|||
for (auto i = 0; i < persons_result.length; ++i)
|
||||
{
|
||||
auto person = persons_result.persons[i];
|
||||
|
||||
json_begin_object(&json);
|
||||
{
|
||||
json_add_object_field(&json, "name");
|
||||
json_add_string(&json, person.name);
|
||||
|
||||
json_add_object_field(&json, "age");
|
||||
json_add_long(&json, person.age);
|
||||
}
|
||||
json_end_object(&json);
|
||||
serialize_person(&json, &person);
|
||||
}
|
||||
}
|
||||
json_end_array(&json);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue