Replace cJSON with json-c
This commit is contained in:
parent
9da4f5e95d
commit
adbac83534
2 changed files with 31 additions and 15 deletions
|
@ -18,23 +18,23 @@ add_executable(pihelper
|
|||
|
||||
include_directories(/usr/local/include)
|
||||
find_library (
|
||||
CJSON
|
||||
NAMES cjson libcjson
|
||||
HINTS /usr/local/lib /usr/lib
|
||||
JSONC
|
||||
NAMES json-c libjson-c
|
||||
HINTS /usr/local/lib /usr/local/lib64 /usr/lib /usr/lib64
|
||||
)
|
||||
find_library (
|
||||
CRYPTO
|
||||
NAMES crypto libcrypto
|
||||
HINTS /usr/local/lib /usr/lib
|
||||
HINTS /usr/local/lib /usr/local/lib64 /usr/lib /usr/lib64
|
||||
)
|
||||
find_library (
|
||||
OPENSSL
|
||||
NAMES ssl libssl
|
||||
HINTS /usr/local/lib /usr/lib
|
||||
HINTS /usr/local/lib /usr/local/lib64 /usr/lib /usr/lib64
|
||||
)
|
||||
|
||||
if (NOT CJSON)
|
||||
message(SEND_ERROR "Did not find cJSON")
|
||||
if (NOT JSONC)
|
||||
message(SEND_ERROR "Did not find json-c")
|
||||
endif()
|
||||
|
||||
if (NOT CRYPTO)
|
||||
|
@ -48,8 +48,8 @@ endif()
|
|||
target_link_libraries(libpihelper curl)
|
||||
target_link_libraries(pihelper curl)
|
||||
|
||||
target_link_libraries(libpihelper ${CJSON})
|
||||
target_link_libraries(pihelper ${CJSON})
|
||||
target_link_libraries(libpihelper ${JSONC})
|
||||
target_link_libraries(pihelper ${JSONC})
|
||||
|
||||
target_link_libraries(libpihelper ${CRYPTO})
|
||||
target_link_libraries(pihelper ${CRYPTO})
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <curl/curl.h>
|
||||
#include <cjson/cJSON.h>
|
||||
#include <json-c/json_object.h>
|
||||
#include <json-c/json_tokener.h>
|
||||
#include <json-c/json_pointer.h>
|
||||
#include "config.h"
|
||||
#include "log.h"
|
||||
#include "network.h"
|
||||
|
@ -108,14 +110,28 @@ static char * prepend_scheme(char * raw_host) {
|
|||
}
|
||||
|
||||
static void parse_status(char * raw_json) {
|
||||
cJSON *json = cJSON_Parse(raw_json);
|
||||
cJSON *status = cJSON_GetObjectItemCaseSensitive(json, "status");
|
||||
if (cJSON_IsString(status) && (status->valuestring != NULL)) {
|
||||
printf("Pi-hole status: %s\n", status->valuestring);
|
||||
json_tokener *tok = json_tokener_new();
|
||||
json_object *jobj = NULL;
|
||||
int stringlen = 0;
|
||||
enum json_tokener_error jerr;
|
||||
do {
|
||||
stringlen = strlen(raw_json);
|
||||
jobj = json_tokener_parse_ex(tok, raw_json, stringlen);
|
||||
} while ((jerr = json_tokener_get_error(tok)) == json_tokener_continue);
|
||||
if (jerr != json_tokener_success) {
|
||||
write_log(LOG_ERROR, "Failed to parse JSON: %s", json_tokener_error_desc(jerr));
|
||||
return;
|
||||
}
|
||||
json_object *status = json_object_new_object();
|
||||
const char * status_string;
|
||||
if (json_pointer_get(jobj, "/status", &status) == 0
|
||||
&& (status_string = json_object_get_string(status)) != NULL) {
|
||||
printf("Pi-hole status: %s\n", status_string);
|
||||
} else {
|
||||
write_log(LOG_DEBUG, "Unable to parse response: %s", raw_json);
|
||||
}
|
||||
cJSON_Delete(json);
|
||||
json_tokener_free(tok);
|
||||
json_object_put(jobj);
|
||||
}
|
||||
|
||||
static void append_query_parameter(char ** host, char * key, char * value) {
|
||||
|
|
Loading…
Reference in a new issue