From 1a7a0cbc480cd9b8c34ad2c846d9a283b6762ef7 Mon Sep 17 00:00:00 2001 From: "me@ow.nekobit.net" Date: Sun, 27 Feb 2022 22:32:20 +0000 Subject: [PATCH] JSON uint function FossilOrigin-Name: f540905360811fec24e363a46b75ff56f770aa43bcca22d7768cb827864ba9b9 --- include/mastodont_json_helper.h | 1 + src/account.c | 41 ++++++++++++++++----------------- src/json_helper.c | 11 +++++++++ src/status.c | 3 +++ 4 files changed, 35 insertions(+), 21 deletions(-) diff --git a/include/mastodont_json_helper.h b/include/mastodont_json_helper.h index c440297..b2123bb 100644 --- a/include/mastodont_json_helper.h +++ b/include/mastodont_json_helper.h @@ -34,6 +34,7 @@ int _mstdnt_key_val_ref(cJSON* v, struct _mstdnt_val_ref* refs, void _mstdnt_val_string_call(cJSON* v, void* _type); void _mstdnt_val_bool_call(cJSON* v, void* _type); +void _mstdnt_val_uint_call(cJSON* v, void* _type); /* DEPRECATED */ struct _mstdnt_str_val diff --git a/src/account.c b/src/account.c index b127bbe..e325384 100644 --- a/src/account.c +++ b/src/account.c @@ -61,30 +61,29 @@ cleanup: int mstdnt_load_account_from_json(struct mstdnt_account* acct, cJSON* js) { cJSON* v; - struct _mstdnt_str_val strings[] = { - { "id", &(acct->id) }, - { "username", &(acct->username) }, - { "acct", &(acct->acct) }, - { "display_name", &(acct->display_name) }, - { "created_at", &(acct->created_at) }, - { "note", &(acct->note) }, - { "url", &(acct->url) }, - { "avatar", &(acct->avatar) }, - { "avatar_static", &(acct->avatar_static) }, - { "header", &(acct->header) }, - { "header_static", &(acct->header_static) }, - { "last_status_at", &(acct->last_status_at) }, - { "mute_expires_at", &(acct->mute_expires_at) } - }; - - struct _mstdnt_bool_val bools[] = { - { "locked", &(acct->locked) }, - { "bot", &(acct->bot) } + struct _mstdnt_val_ref refs[] = { + { "id", &(acct->id), _mstdnt_val_string_call }, + { "username", &(acct->username), _mstdnt_val_string_call }, + { "acct", &(acct->acct), _mstdnt_val_string_call }, + { "display_name", &(acct->display_name), _mstdnt_val_string_call }, + { "created_at", &(acct->created_at), _mstdnt_val_string_call }, + { "note", &(acct->note), _mstdnt_val_string_call }, + { "url", &(acct->url), _mstdnt_val_string_call }, + { "avatar", &(acct->avatar), _mstdnt_val_string_call }, + { "avatar_static", &(acct->avatar_static), _mstdnt_val_string_call }, + { "header", &(acct->header), _mstdnt_val_string_call }, + { "header_static", &(acct->header_static), _mstdnt_val_string_call }, + { "last_status_at", &(acct->last_status_at), _mstdnt_val_string_call }, + { "mute_expires_at", &(acct->mute_expires_at), _mstdnt_val_string_call }, + { "locked", &(acct->locked), _mstdnt_val_bool_call }, + { "bot", &(acct->bot), _mstdnt_val_bool_call }, + { "statuses_count", &(acct->statuses_count), _mstdnt_val_uint_call }, + { "followers_count", &(acct->followers_count), _mstdnt_val_uint_call }, + { "following_count", &(acct->following_count), _mstdnt_val_uint_call }, }; for (v = js; v; v = v->next) { - _mstdnt_key_val_iter(v, strings, _mstdnt_arr_len(strings), - bools, _mstdnt_arr_len(bools)); + _mstdnt_key_val_ref(v, refs, _mstdnt_arr_len(refs)); } } diff --git a/src/json_helper.c b/src/json_helper.c index 13db60d..c4d0dc7 100644 --- a/src/json_helper.c +++ b/src/json_helper.c @@ -64,6 +64,17 @@ void _mstdnt_val_bool_call(cJSON* v, void* _type) *type = cJSON_IsTrue(v); } +void _mstdnt_val_uint_call(cJSON* v, void* _type) +{ + unsigned* type = _type; + if (!cJSON_IsNumber(v)) + { + *type = 0; + return; + } + *type = v->valueint; +} + int _mstdnt_key_val_iter(cJSON* v, struct _mstdnt_str_val* str, size_t str_len, diff --git a/src/status.c b/src/status.c index 9e9f705..2b08dd2 100644 --- a/src/status.c +++ b/src/status.c @@ -40,6 +40,9 @@ int mstdnt_load_status_from_json(struct mstdnt_status* status, cJSON* js) { "muted", &(status->muted), _mstdnt_val_bool_call, NULL }, { "bookmarked", &(status->bookmarked), _mstdnt_val_bool_call, NULL }, { "pinned", &(status->pinned), _mstdnt_val_bool_call, NULL }, + { "reblogs_count", &(status->reblogs_count), _mstdnt_val_uint_call, NULL }, + { "favourites_count", &(status->favourites_count), _mstdnt_val_uint_call, NULL }, + { "replies_count", &(status->replies_count), _mstdnt_val_uint_call, NULL }, }; for (v = js; v; v = v->next)