Fix printing of ints in dynsec json.

pull/1874/head
Roger A. Light 5 years ago
parent e95327789e
commit 30bf47b0d1

@ -263,7 +263,6 @@ static int dynsec__config_add_clients(cJSON *j_clients)
struct dynsec__client *client, *client_tmp;
cJSON *j_client, *j_roles, *jtmp;
char *buf;
char s_iterations[10];
HASH_ITER(hh, local_clients, client, client_tmp){
j_client = cJSON_CreateObject();
@ -304,8 +303,7 @@ static int dynsec__config_add_clients(cJSON *j_clients)
if(jtmp == NULL) return 1;
cJSON_AddItemToObject(j_client, "salt", jtmp);
snprintf(s_iterations, sizeof(s_iterations), "%d", client->pw.iterations);
if(cJSON_AddRawToObject(j_client, "iterations", s_iterations) == NULL){
if(cJSON_AddIntToObject(j_client, "iterations", client->pw.iterations) == NULL){
return 1;
}
}
@ -768,7 +766,6 @@ int dynsec_clients__process_list(cJSON *j_responses, struct mosquitto *context,
struct dynsec__client *client, *client_tmp;
cJSON *tree, *j_clients, *j_client, *jtmp, *j_data;
int i, count, offset;
char buf[30];
json_get_bool(command, "verbose", &verbose, true, false);
json_get_int(command, "count", &count, true, -1);
@ -796,8 +793,7 @@ int dynsec_clients__process_list(cJSON *j_responses, struct mosquitto *context,
}
cJSON_AddItemToObject(tree, "data", j_data);
snprintf(buf, sizeof(buf), "%d", HASH_CNT(hh, local_clients));
cJSON_AddRawToObject(j_data, "totalCount", buf);
cJSON_AddIntToObject(j_data, "totalCount", HASH_CNT(hh, local_clients));
j_clients = cJSON_CreateArray();
if(j_clients == NULL){

@ -88,7 +88,6 @@ cJSON *dynsec_clientlists__all_to_json(struct dynsec__clientlist *base_clientlis
{
struct dynsec__clientlist *clientlist, *clientlist_tmp;
cJSON *j_clients, *j_client;
char buf[30];
j_clients = cJSON_CreateArray();
if(j_clients == NULL) return NULL;
@ -101,11 +100,8 @@ cJSON *dynsec_clientlists__all_to_json(struct dynsec__clientlist *base_clientlis
}
cJSON_AddItemToArray(j_clients, j_client);
if(clientlist->priority != -1){
snprintf(buf, sizeof(buf), "%d", clientlist->priority);
}
if(cJSON_AddStringToObject(j_client, "username", clientlist->client->username) == NULL
|| (clientlist->priority != -1 && cJSON_AddRawToObject(j_client, "priority", buf) == NULL)
|| (clientlist->priority != -1 && cJSON_AddIntToObject(j_client, "priority", clientlist->priority) == NULL)
){
cJSON_Delete(j_clients);
@ -120,7 +116,6 @@ cJSON *dynsec_grouplists__all_to_json(struct dynsec__grouplist *base_grouplist)
{
struct dynsec__grouplist *grouplist, *grouplist_tmp;
cJSON *j_groups, *j_group;
char buf[30];
j_groups = cJSON_CreateArray();
if(j_groups == NULL) return NULL;
@ -133,11 +128,8 @@ cJSON *dynsec_grouplists__all_to_json(struct dynsec__grouplist *base_grouplist)
}
cJSON_AddItemToArray(j_groups, j_group);
if(grouplist->priority != -1){
snprintf(buf, sizeof(buf), "%d", grouplist->priority);
}
if(cJSON_AddStringToObject(j_group, "groupname", grouplist->group->groupname) == NULL
|| (grouplist->priority != -1 && cJSON_AddRawToObject(j_group, "priority", buf) == NULL)
|| (grouplist->priority != -1 && cJSON_AddIntToObject(j_group, "priority", grouplist->priority) == NULL)
){
cJSON_Delete(j_groups);
@ -722,7 +714,6 @@ int dynsec_groups__process_list(cJSON *j_responses, struct mosquitto *context, c
cJSON *tree, *j_groups, *j_group, *jtmp, *j_data;
struct dynsec__group *group, *group_tmp;
int i, count, offset;
char buf[30];
json_get_bool(command, "verbose", &verbose, true, false);
json_get_int(command, "count", &count, true, -1);
@ -750,8 +741,7 @@ int dynsec_groups__process_list(cJSON *j_responses, struct mosquitto *context, c
}
cJSON_AddItemToObject(tree, "data", j_data);
snprintf(buf, sizeof(buf), "%d", HASH_CNT(hh, local_groups));
cJSON_AddRawToObject(j_data, "totalCount", buf);
cJSON_AddIntToObject(j_data, "totalCount", HASH_CNT(hh, local_groups));
j_groups = cJSON_CreateArray();
if(j_groups == NULL){

@ -19,6 +19,7 @@ Contributors:
#include <cJSON.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include "mosquitto.h"
@ -105,3 +106,11 @@ double json_get_as_number(const cJSON *json)
return 0.0;
}
}
cJSON *cJSON_AddIntToObject(cJSON * const object, const char * const name, int number)
{
char buf[30];
snprintf(buf, sizeof(buf), "%d", number);
return cJSON_AddRawToObject(object, name, buf);
}

@ -23,4 +23,7 @@ int json_get_int(cJSON *json, const char *name, int *value, bool optional, int d
int json_get_string(cJSON *json, const char *name, char **value, bool optional);
double json_get_as_number(const cJSON *json);
cJSON *cJSON_AddIntToObject(cJSON * const object, const char * const name, int number);
cJSON *cJSON_CreateInt(int num);
#endif

@ -150,7 +150,6 @@ cJSON *dynsec_rolelists__all_to_json(struct dynsec__rolelist *base_rolelist)
{
struct dynsec__rolelist *rolelist, *rolelist_tmp;
cJSON *j_roles, *j_role;
char buf[30];
j_roles = cJSON_CreateArray();
if(j_roles == NULL) return NULL;
@ -163,11 +162,8 @@ cJSON *dynsec_rolelists__all_to_json(struct dynsec__rolelist *base_rolelist)
}
cJSON_AddItemToArray(j_roles, j_role);
if(rolelist->priority != -1){
snprintf(buf, sizeof(buf), "%d", rolelist->priority);
}
if(cJSON_AddStringToObject(j_role, "rolename", rolelist->role->rolename) == NULL
|| (rolelist->priority != -1 && cJSON_AddRawToObject(j_role, "priority", buf) == NULL)
|| (rolelist->priority != -1 && cJSON_AddIntToObject(j_role, "priority", rolelist->priority) == NULL)
){
cJSON_Delete(j_roles);
@ -253,7 +249,7 @@ static int add_single_acl_to_json(cJSON *j_array, const char *acl_type, struct d
if(cJSON_AddStringToObject(j_acl, "acltype", acl_type) == NULL
|| cJSON_AddStringToObject(j_acl, "topic", iter->topic) == NULL
|| cJSON_AddNumberToObject(j_acl, "priority", iter->priority) == NULL
|| cJSON_AddIntToObject(j_acl, "priority", iter->priority) == NULL
|| cJSON_AddBoolToObject(j_acl, "allow", iter->allow) == NULL
){
@ -594,7 +590,6 @@ int dynsec_roles__process_list(cJSON *j_responses, struct mosquitto *context, cJ
struct dynsec__role *role, *role_tmp;
cJSON *tree, *j_roles, *j_role, *jtmp, *j_data;
int i, count, offset;
char buf[30];
json_get_bool(command, "verbose", &verbose, true, false);
json_get_int(command, "count", &count, true, -1);
@ -622,8 +617,7 @@ int dynsec_roles__process_list(cJSON *j_responses, struct mosquitto *context, cJ
}
cJSON_AddItemToObject(tree, "data", j_data);
snprintf(buf, sizeof(buf), "%d", HASH_CNT(hh, local_roles));
cJSON_AddRawToObject(j_data, "totalCount", buf);
cJSON_AddIntToObject(j_data, "totalCount", HASH_CNT(hh, local_roles));
j_roles = cJSON_CreateArray();
if(j_roles == NULL){

Loading…
Cancel
Save