|
@ -60,68 +60,83 @@ void json_field_replace(cJSON *entry, const char *key, cJSON *content) { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
int json_int_value(char *key, cJSON *json) { // json int or string value -> int
|
|
|
int json_int_value(char *caption, cJSON *json) { // json int or string value -> int
|
|
|
if (cJSON_IsNumber(json)) { |
|
|
if (cJSON_IsNumber(json)) { |
|
|
return json->valueint; |
|
|
return json->valueint; |
|
|
} else if (cJSON_IsString(json)) { |
|
|
} else if (cJSON_IsString(json)) { |
|
|
char *p; |
|
|
char *p; |
|
|
int int_ret = (int)strtol(json->valuestring, &p, 10); |
|
|
int int_ret = (int)strtol(json->valuestring, &p, 10); |
|
|
if (int_ret == 0 && strcmp(json->valuestring, "0") != 0) { // invalid number in string
|
|
|
if (int_ret == 0 && strcmp(json->valuestring, "0") != 0) { // invalid number in string
|
|
|
log_fatal("`%s` not a valid number", key); |
|
|
log_fatal("`%s` not a valid number", caption); |
|
|
} |
|
|
} |
|
|
return int_ret; |
|
|
return int_ret; |
|
|
} else { |
|
|
} else { |
|
|
log_fatal("`%s` must be number or string", key); |
|
|
log_fatal("`%s` must be number or string", caption); |
|
|
} |
|
|
} |
|
|
return 0; // never reach
|
|
|
return 0; // never reach
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
uint8_t json_bool_value(char *key, cJSON *json) { // json bool value -> bool
|
|
|
uint8_t json_bool_value(char *caption, cJSON *json) { // json bool value -> bool
|
|
|
if (!cJSON_IsBool(json)) { |
|
|
if (!cJSON_IsBool(json)) { |
|
|
log_fatal("`%s` must be boolean", key); |
|
|
log_fatal("`%s` must be boolean", caption); |
|
|
} |
|
|
} |
|
|
return json->valueint; |
|
|
return json->valueint; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
char* json_string_value(char* key, cJSON *json) { // json string value -> string
|
|
|
char* json_string_value(char* caption, cJSON *json) { // json string value -> string
|
|
|
if (!cJSON_IsString(json)) { |
|
|
if (!cJSON_IsString(json)) { |
|
|
log_fatal("`%s` must be string", key); |
|
|
log_fatal("`%s` must be string", caption); |
|
|
} |
|
|
} |
|
|
return string_init(json->valuestring); |
|
|
return string_init(json->valuestring); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
char** json_string_list_value(char *key, cJSON *json, char **string_list) { // json string array -> string list
|
|
|
char** json_string_list_value(char *caption, cJSON *json, char **string_list) { // json string array
|
|
|
if (cJSON_IsString(json)) { |
|
|
if (cJSON_IsString(json)) { |
|
|
string_list_append(&string_list, json->valuestring); |
|
|
string_list_append(&string_list, json->valuestring); |
|
|
} else if (cJSON_IsArray(json)) { |
|
|
} else if (cJSON_IsArray(json)) { |
|
|
json = json->child; |
|
|
json = json->child; |
|
|
while (json != NULL) { |
|
|
while (json != NULL) { |
|
|
if (!cJSON_IsString(json)) { |
|
|
if (!cJSON_IsString(json)) { |
|
|
log_fatal("`%s` must be string array", key); |
|
|
log_fatal("`%s` must be string array", caption); |
|
|
} |
|
|
} |
|
|
string_list_append(&string_list, json->valuestring); |
|
|
string_list_append(&string_list, json->valuestring); |
|
|
json = json->next; // next key
|
|
|
json = json->next; // next key
|
|
|
} |
|
|
} |
|
|
} else if (!cJSON_IsNull(json)) { // allow null -> empty string list
|
|
|
} else if (!cJSON_IsNull(json)) { // allow null -> empty string list
|
|
|
log_fatal("`%s` must be array or string", key); |
|
|
log_fatal("`%s` must be array or string", caption); |
|
|
} |
|
|
} |
|
|
return string_list; |
|
|
return string_list; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
uint32_t** json_uint32_list_value(char *key, cJSON *json, uint32_t **uint32_list) { // json uint32 array -> uint32 list
|
|
|
uint32_t** json_uint32_list_value(char *caption, cJSON *json, uint32_t **uint32_list) { // json uint32 array
|
|
|
if (cJSON_IsNumber(json)) { |
|
|
if (cJSON_IsNumber(json)) { |
|
|
uint32_list_append(&uint32_list, json->valueint); |
|
|
uint32_list_append(&uint32_list, json->valueint); |
|
|
} else if (cJSON_IsArray(json)) { |
|
|
} else if (cJSON_IsArray(json)) { |
|
|
json = json->child; |
|
|
json = json->child; |
|
|
while (json != NULL) { |
|
|
while (json != NULL) { |
|
|
if (!cJSON_IsNumber(json)) { |
|
|
if (!cJSON_IsNumber(json)) { |
|
|
log_fatal("`%s` must be number array", key); |
|
|
log_fatal("`%s` must be number array", caption); |
|
|
} |
|
|
} |
|
|
uint32_list_append(&uint32_list, json->valueint); |
|
|
uint32_list_append(&uint32_list, json->valueint); |
|
|
json = json->next; // next key
|
|
|
json = json->next; // next key
|
|
|
} |
|
|
} |
|
|
} else if (!cJSON_IsNull(json)) { // allow null -> empty uint32 list
|
|
|
} else if (!cJSON_IsNull(json)) { // allow null -> empty uint32 list
|
|
|
log_fatal("`%s` must be array or number", key); |
|
|
log_fatal("`%s` must be array or number", caption); |
|
|
} |
|
|
} |
|
|
return uint32_list; |
|
|
return uint32_list; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void json_string_map_value(char *caption, cJSON *json, char ***key_list, char ***value_list) { // json string map
|
|
|
|
|
|
if (!cJSON_IsObject(json)) { |
|
|
|
|
|
log_fatal("`%s` must be map", caption); |
|
|
|
|
|
} |
|
|
|
|
|
json = json->child; |
|
|
|
|
|
while (json != NULL) { // traverse all json field
|
|
|
|
|
|
if (!cJSON_IsString(json)) { |
|
|
|
|
|
log_fatal("`%s` must be string-string map", caption); |
|
|
|
|
|
} |
|
|
|
|
|
string_list_append(key_list, json->string); |
|
|
|
|
|
string_list_append(value_list, json->valuestring); |
|
|
|
|
|
json = json->next; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|