Add comments
This commit is contained in:
1 parent
c535766c22
commit
7ba0e03b4f
1 file changed
+7
@@ -5,6 +5,7 @@
|
|||||||
// Serial json is not supported for standard types if not C11
|
// Serial json is not supported for standard types if not C11
|
||||||
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
|
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
|
||||||
|
|
||||||
|
// Let's define a 'person' composed of several fields.
|
||||||
TUPLE_DEF2(person,
|
TUPLE_DEF2(person,
|
||||||
(surname, string_t),
|
(surname, string_t),
|
||||||
(age, int),
|
(age, int),
|
||||||
@@ -12,25 +13,31 @@ TUPLE_DEF2(person,
|
|||||||
)
|
)
|
||||||
#define M_OPL_person_t() TUPLE_OPLIST(person, M_DEFAULT_OPLIST, STRING_OPLIST, M_DEFAULT_OPLIST )
|
#define M_OPL_person_t() TUPLE_OPLIST(person, M_DEFAULT_OPLIST, STRING_OPLIST, M_DEFAULT_OPLIST )
|
||||||
|
|
||||||
|
// Let's define an array of person
|
||||||
ARRAY_DEF(base, person_t)
|
ARRAY_DEF(base, person_t)
|
||||||
#define M_OPL_base_t() ARRAY_OPLIST(base, M_OPL_person_t())
|
#define M_OPL_base_t() ARRAY_OPLIST(base, M_OPL_person_t())
|
||||||
|
|
||||||
|
// Let's read an array of person from the given JSON file
|
||||||
static void read(base_t base, const char filename[])
|
static void read(base_t base, const char filename[])
|
||||||
{
|
{
|
||||||
m_serial_read_t in;
|
m_serial_read_t in;
|
||||||
m_serial_return_code_t ret;
|
m_serial_return_code_t ret;
|
||||||
|
|
||||||
|
// Open the file
|
||||||
FILE *f = fopen(filename, "rt");
|
FILE *f = fopen(filename, "rt");
|
||||||
if (!f) {
|
if (!f) {
|
||||||
fprintf(stderr, "ERROR: Cannot open file '%s'.\n", filename);
|
fprintf(stderr, "ERROR: Cannot open file '%s'.\n", filename);
|
||||||
exit(2);
|
exit(2);
|
||||||
}
|
}
|
||||||
|
// Initialize the serializer with the file
|
||||||
m_serial_json_read_init(in, f);
|
m_serial_json_read_init(in, f);
|
||||||
|
// Read the array
|
||||||
ret = base_in_serial(base, in);
|
ret = base_in_serial(base, in);
|
||||||
if (ret != M_SERIAL_OK_DONE) {
|
if (ret != M_SERIAL_OK_DONE) {
|
||||||
fprintf(stderr, "ERROR: Cannot read JSON data from file '%s'.\n", filename);
|
fprintf(stderr, "ERROR: Cannot read JSON data from file '%s'.\n", filename);
|
||||||
exit(2);
|
exit(2);
|
||||||
}
|
}
|
||||||
|
// Let's close & clear all
|
||||||
m_serial_json_read_clear(in);
|
m_serial_json_read_clear(in);
|
||||||
fclose(f);
|
fclose(f);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user