123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- #include "config.h"
- #include <cjson/cJSON.h>
- #include <stdbool.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include "json_help.h"
- #include "mosquitto.h"
- int json_get_bool(cJSON *json, const char *name, bool *value, bool optional, bool default_value)
- {
- cJSON *jtmp;
- if(optional == true){
- *value = default_value;
- }
- jtmp = cJSON_GetObjectItem(json, name);
- if(jtmp){
- if(cJSON_IsBool(jtmp) == false){
- return MOSQ_ERR_INVAL;
- }
- *value = cJSON_IsTrue(jtmp);
- }else{
- if(optional == false){
- return MOSQ_ERR_INVAL;
- }
- }
- return MOSQ_ERR_SUCCESS;
- }
- int json_get_int(cJSON *json, const char *name, int *value, bool optional, int default_value)
- {
- cJSON *jtmp;
- if(optional == true){
- *value = default_value;
- }
- jtmp = cJSON_GetObjectItem(json, name);
- if(jtmp){
- if(cJSON_IsNumber(jtmp) == false){
- return MOSQ_ERR_INVAL;
- }
- *value = jtmp->valueint;
- }else{
- if(optional == false){
- return MOSQ_ERR_INVAL;
- }
- }
- return MOSQ_ERR_SUCCESS;
- }
- int json_get_string(cJSON *json, const char *name, char **value, bool optional)
- {
- cJSON *jtmp;
- *value = NULL;
- jtmp = cJSON_GetObjectItem(json, name);
- if(jtmp){
- if(cJSON_IsString(jtmp) == false){
- return MOSQ_ERR_INVAL;
- }
- *value = jtmp->valuestring;
- }else{
- if(optional == false){
- return MOSQ_ERR_INVAL;
- }
- }
- return MOSQ_ERR_SUCCESS;
- }
- 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);
- }
|