Declarative
JSON
Import
Export
jstruct
How it works
//@json
struct my_json_data {
/*
@schema {
"title": "ID",
"description": "unique object id",
"type": "int"
}
*/
uint64_t id;
/* don't include in json */
//@private
int _id;
bool active;
/* add the ability to null this field even though it's not a pointer */
//@nullable
double ratio;
char *name;
/* automatically parsed as an array atm */
char **tags;
}
what about arrays of structs?
//@json
struct my_json_container {
struct my_json_data main_data;
/* static arrays are automatic */
struct my_json_data array_data[5];
/* as are struct arrays atm*/
struct my_json_data *alloc_array_data;
}
jstruct_array_malloc(container, alloc_array_data, struct my_json_data, 2)
Foreign Structs
struct foreign_struct {
uint64_t id;
int _id;
enum jstruct_error err;
bool active;
double ratio_double;
char *name;
unsigned long long ull;
char **tags;
};
/* @json {
"_id": "@private",
"ratio_double": {
"@nullable": true,
"@name": "ratio"
},
"@name": "other_name"
} */
struct my_json_foreign_struct {
// place all members of a struct 'inline' inside this struct
// @inline
struct foreign_struct fs;
};
Problem 1
struct json_object *entry = json_object_new_object();
json_object_object_add(entry, "sentbyte", json_object_new_int(
chart_stats[i].bytes_sent));
json_object_object_add(entry, "rcvdbyte", json_object_new_int(
chart_stats[i].bytes_rcvd));
json_object_object_add(entry, "sessions", json_object_new_int(
chart_stats[i].sessions));
json_object_object_add(entry, "session_block", json_object_new_int(
chart_stats[i].utmincident[THM_STAT_IDX_BLOCKED]));
json_object_object_add(entry, "session_allow", json_object_new_int(
chart_stats[i].utmincident[THM_STAT_IDX_PASSTHROUGH]));
json_object_object_add(entry, "score_low", json_object_new_int(
chart_stats[i].utmscore[THM_SEVERITY_LOW]));
json_object_object_add(entry, "score_medium", json_object_new_int(
chart_stats[i].utmscore[THM_SEVERITY_MEDIUM]));
json_object_object_add(entry, "score_high", json_object_new_int(
chart_stats[i].utmscore[THM_SEVERITY_HIGH]));
json_object_object_add(entry, "score_critical", json_object_new_int(
chart_stats[i].utmscore[THM_SEVERITY_CRITICAL]));
json_object_array_add(series, entry);
Problem 2
for (i = rev_list_length - 1, rev = &rev_list[i]; i >= 0; i--, rev--) {
// Version details for category groupings
if (memcmp(&imgversion, &rev->version, sizeof(imgversion))) {
struct json_object *new_rev_cat = json_object_new_object();
snprintf(version, sizeof(version), "%d", ++version_id);
json_object_object_add(new_rev_cat, "major_version", json_object_new_uint(rev->version.major_ver));
json_object_object_add(new_rev_cat, "minor_version", json_object_new_uint(rev->version.minor_ver));
json_object_object_add(new_rev_cat, "patch_number", json_object_new_uint(rev->version.patch_num));
json_object_object_add(new_rev_cat, "build_number", json_object_new_uint(rev->version.build_num));
json_object_object_add(new_rev_cat, "release_number", json_object_new_uint(rev->version.release_num));
json_object_object_add(rev_cats, version, new_rev_cat);
memcpy(&imgversion, &rev->version, sizeof(imgversion));
}
struct json_object *new_rev = json_object_new_object();
json_object_object_add(new_rev, "id", json_object_new_int(rev->id));
json_object_object_add(new_rev, "time", json_object_new_uint((uint32_t) rev->timestamp));
json_object_object_add(new_rev, "version_id", json_object_new_string(version));
json_object_object_add(new_rev, "admin", json_object_new_string(rev->admin));
json_object_object_add(new_rev, "comment", json_object_new_string(rev->comment));
json_object_array_add(revs, new_rev);
}
CHANGES
libjstruct
jstruct
By Jamie Pate
jstruct
- 1,101