From f0f33c4ac7ef6ff5affd73541affe6fd79480c88 Mon Sep 17 00:00:00 2001 From: ktkk Date: Mon, 13 Oct 2025 12:30:42 +0000 Subject: [PATCH] Create and user helper functions for serialization --- tests/json_tests_complex.c | 47 +++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/tests/json_tests_complex.c b/tests/json_tests_complex.c index 0ebfbdd..8f3f3c0 100644 --- a/tests/json_tests_complex.c +++ b/tests/json_tests_complex.c @@ -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);