4#include <cjson/cJSON.h>
11static bool json_read_bool(cJSON* json_node,
const char* key,
bool* value) {
12 cJSON* item = cJSON_GetObjectItem(json_node, key);
13 return cJSON_IsBool(item) ? *value = cJSON_IsTrue(item), true :
false;
17static void json_write_bool(cJSON* json_node,
const char* key,
bool value) {
18 cJSON* item = cJSON_GetObjectItem(json_node, key);
20 if(cJSON_IsBool(item)) {
21 cJSON_SetBoolValue(item, value);
23 cJSON_DeleteItemFromObject(json_node, key);
24 cJSON_AddBoolToObject(json_node, key, value);
29static bool json_read_int(cJSON* json_node,
const char* key,
int* value) {
30 cJSON* item = cJSON_GetObjectItem(json_node, key);
31 return cJSON_IsNumber(item) ? *value = item->valueint, true :
false;
35static void json_write_int(cJSON* json_node,
const char* key,
int value) {
36 cJSON* item = cJSON_GetObjectItem(json_node, key);
38 if(cJSON_IsNumber(item)) {
39 cJSON_SetIntValue(item, value);
41 cJSON_DeleteItemFromObject(json_node, key);
42 cJSON_AddNumberToObject(json_node, key, value);
47static bool json_read_float(cJSON* json_node,
const char* key,
float* value) {
48 cJSON* item = cJSON_GetObjectItem(json_node, key);
49 return cJSON_IsNumber(item) ? *value = item->valuedouble, true :
false;
53static void json_write_float(cJSON* json_node,
const char* key,
float value) {
54 cJSON* item = cJSON_GetObjectItem(json_node, key);
56 if(cJSON_IsNumber(item)) {
57 cJSON_SetNumberValue(item, value);
59 cJSON_DeleteItemFromObject(json_node, key);
60 cJSON_AddNumberToObject(json_node, key, value);
65static bool json_read_string(cJSON* json_node,
const char* key,
const char** value) {
66 cJSON* item = cJSON_GetObjectItem(json_node, key);
67 return cJSON_IsString(item) ? *value = item->valuestring, true :
false;
71static void json_write_string(cJSON* json_node,
const char* key,
const char* value) {
72 cJSON* item = cJSON_GetObjectItem(json_node, key);
74 if(cJSON_IsString(item)) {
75 cJSON_SetValuestring(item, value);
77 cJSON_DeleteItemFromObject(json_node, key);
78 cJSON_AddStringToObject(json_node, key, value);
83static bool json_read_object(cJSON* json_node,
const char* key, cJSON** value) {
84 cJSON* item = cJSON_GetObjectItem(json_node, key);
85 return cJSON_IsObject(item) ? *value = item, true :
false;
89static void json_write_object(cJSON* json_node,
const char* key, cJSON* value) {
90 cJSON_DeleteItemFromObject(json_node, key);
91 cJSON_AddItemToObject(json_node, key, value);
95static bool json_read_any(cJSON* json_node,
const char* key, cJSON** value) {
96 cJSON* item = cJSON_GetObjectItem(json_node, key);
97 return item ? *value = item, true :
false;
101static void json_write_any(cJSON* json_node,
const char* key, cJSON* value) {
102 cJSON_DeleteItemFromObject(json_node, key);
103 cJSON_AddItemToObject(json_node, key, value);