From afe0f197a0078462df94489a5825f04375aa1ebb Mon Sep 17 00:00:00 2001 From: subjectxbj Date: Fri, 23 Mar 2018 07:16:40 +0000 Subject: [PATCH] Change cJSON to json-c --- Makefile | 4 ++-- README.md | 8 +++++--- config.c | 49 +++++++++++++++++++++---------------------------- config.h | 12 ++++++------ lwa.c | 39 ++++++++++++++++++++------------------- main.c | 4 ++-- 6 files changed, 56 insertions(+), 60 deletions(-) diff --git a/Makefile b/Makefile index fceb781..ed742f6 100644 --- a/Makefile +++ b/Makefile @@ -1,12 +1,12 @@ EXE = avs_server OBJS = main.o lwa.o config.o -LIBS = -lcjson -lcurl +LIBS = -ljson-c -lcurl #CC = gcc CFLAGS = -g #CFLAGS += -DHARDCODE_CONFIG -ALLOWED_INCLUDE_PATHS = -I/usr/local/include/cjson \ +ALLOWED_INCLUDE_PATHS = -I/usr/local/include/json-c \ -I/usr/local/include/curl ALLOWED_LIB_PATHS = -L/usr/local/lib/ diff --git a/README.md b/README.md index ca617d8..665835b 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ This is too expensive for an embled device. So this project will use ANSI c to implement similar function. -This application has dependency on following items: curl and cJSON. +This application has dependency on following items: curl and json-c. So before build this project, need to build and install these libraries: @@ -17,8 +17,10 @@ So before build this project, need to build and install these libraries: make make install - git clone https://github.com/DaveGamble/cJSON.git - cd cJSON + git clone https://github.com/json-c/json-c.git + cd json-c + ./autogen.sh + ./configure make make install ldconfig diff --git a/config.c b/config.c index 4d27f64..fb3c193 100644 --- a/config.c +++ b/config.c @@ -9,7 +9,7 @@ #include #include #include -#include "cJSON.h" +#include "json.h" #include "config.h" int isCommentLine(char *line){ @@ -35,7 +35,7 @@ int isCommentLine(char *line){ return 0; } -cJSON* getCleanConfig(char *filename) { +json_object* getCleanConfig(char *filename) { FILE *fd; char buffer[102400]={0}; char *pline = NULL; @@ -60,8 +60,8 @@ cJSON* getCleanConfig(char *filename) { fclose(fd); //printf("\n==============>Clean Config: %s\n", buffer); - cJSON *json = NULL; - json = cJSON_Parse(buffer); + json_object *json = NULL; + json = json_tokener_parse(buffer); if (json) { //printf("JSON parse OK.\n"); //printf(cJSON_Print(json)); @@ -73,12 +73,12 @@ cJSON* getCleanConfig(char *filename) { } -int writeConfig(cJSON *json, char *filename) { +int writeConfig(json_object *json, char *filename) { FILE *fd; fd = fopen(filename, "w+"); if (fd) { char *output; - output = cJSON_Print(json); + output = json_object_to_json_string(json); if (output) { size_t len = strlen(output); fwrite(output, len, 1, fd); @@ -92,49 +92,42 @@ int writeConfig(cJSON *json, char *filename) { } -cJSON *get_config_param(cJSON *config, char *name){ - cJSON *authDelegate = cJSON_GetObjectItem(config, "authDelegate"); +json_object *get_config_param(json_object *config, char *name){ + json_object *authDelegate = json_object_object_get(config, "authDelegate"); if(authDelegate == NULL) { return NULL; } if (strcmp(name, "clientSecret") == 0) { - cJSON *clientSecret = cJSON_GetObjectItem(authDelegate, "clientSecret"); + json_object *clientSecret = json_object_object_get(authDelegate, "clientSecret"); return clientSecret; }else if (strcmp(name, "deviceSerialNumber") == 0) { - cJSON *deviceSerialNumber = cJSON_GetObjectItem(authDelegate, "deviceSerialNumber"); + json_object *deviceSerialNumber = json_object_object_get(authDelegate, "deviceSerialNumber"); return deviceSerialNumber; }else if (strcmp(name, "refreshToken") == 0) { - cJSON *refreshToken = cJSON_GetObjectItem(authDelegate, "refreshToken"); + json_object *refreshToken = json_object_object_get(authDelegate, "refreshToken"); return refreshToken; }else if (strcmp(name, "clientId") == 0) { - cJSON *clientId = cJSON_GetObjectItem(authDelegate, "clientId"); + json_object *clientId = json_object_object_get(authDelegate, "clientId"); return clientId; }else if (strcmp(name, "productId") == 0) { - cJSON *productId = cJSON_GetObjectItem(authDelegate, "productId"); + json_object *productId = json_object_object_get(authDelegate, "productId"); return productId; } return NULL; } -char *get_config_param_value(cJSON *config, char *name){ - cJSON *param; +char *get_config_param_value(json_object *config, char *name){ + json_object *param; param = get_config_param(config, name); - return param? param->valuestring: NULL; + return param? json_object_get_string(param): NULL; } -int update_config_param(cJSON *config, char *name, char *value) { - char *new_string = malloc(strlen(value)+1); +int update_config_param(json_object *config, char *name, char *value) { + json_object_object_del(config, name); + + json_object_object_add(config, name, json_object_new_string(value)); - strcpy(new_string, value); - cJSON *param; - param = get_config_param(config, name); - if (param) { - free(param->valuestring); - param->valuestring = new_string; - return 0; - }else{ - return 1; - } + return 0; } diff --git a/config.h b/config.h index b0eb169..b6f1ea5 100644 --- a/config.h +++ b/config.h @@ -1,8 +1,8 @@ -cJSON* getCleanConfig(char *filename); -int writeConfig(cJSON *json, char *filename); -int update_config_param(cJSON *config, char *name, char *value) ; -char *get_config_param_value(cJSON *config, char *name); -cJSON *get_config_param(cJSON *config, char *name); +json_object* getCleanConfig(char *filename); +int writeConfig(json_object *json, char *filename); +int update_config_param(json_object *config, char *name, char *value) ; +char *get_config_param_value(json_object *config, char *name); +json_object *get_config_param(json_object *config, char *name); -extern cJSON *json_config; +extern json_object *json_config; diff --git a/lwa.c b/lwa.c index b5623bc..19df30f 100644 --- a/lwa.c +++ b/lwa.c @@ -9,7 +9,7 @@ #include #include #include -#include "cJSON.h" +#include "json.h" #include "lwa.h" #include "curl.h" #include "config.h" @@ -48,22 +48,22 @@ char * urlencode(char *json_string) char* getRedirectUrl(){ static char lwaUrl[1000]; char *scopeData; - cJSON *alexa_all, *attr; - cJSON *root = cJSON_CreateObject(); - cJSON_AddItemToObject(root, "alexa:all", alexa_all=cJSON_CreateObject()); + json_object *alexa_all, *attr; + json_object *root = json_object_new_object(); + json_object_object_add(root, "alexa:all", alexa_all=json_object_new_object()); #ifdef HARDCODE_CONFIG - cJSON_AddStringToObject(alexa_all, "productID", productId); + json_object_object_add(alexa_all, "productID", json_object_new_string(productId)); #else - cJSON_AddStringToObject(alexa_all, "productID",get_config_param_value(json_config, "productId")); + json_object_object_add(alexa_all, "productID",json_object_new_string(get_config_param_value(json_config, "productId"))); #endif - cJSON_AddItemToObject(alexa_all, "productInstanceAttributes", attr=cJSON_CreateObject()); + json_object_object_add(alexa_all, "productInstanceAttributes", attr=json_object_new_object()); #ifdef HARDCODE_CONFIG - cJSON_AddStringToObject(attr, "deviceSerialNumber", deviceSerialNumber); + json_object_object_add(attr, "deviceSerialNumber", json_object_new_string(deviceSerialNumber)); #else - cJSON_AddStringToObject(attr, "deviceSerialNumber", get_config_param_value(json_config, "deviceSerialNumber")); + json_object_object_add(attr, "deviceSerialNumber", json_object_new_string(get_config_param_value(json_config, "deviceSerialNumber"))); #endif - scopeData = cJSON_Print(root); - cJSON_Delete(root); + scopeData = json_object_to_json_string(root); + json_object_put(root); //printf("%s", scopeData); memset(lwaUrl, 0, sizeof(lwaUrl)); @@ -75,6 +75,7 @@ char* getRedirectUrl(){ #else sprintf(param, "client_id=%s", get_config_param_value(json_config, "clientId")); #endif + printf("param:%s\n",param); strcat(lwaUrl, param); strcat(lwaUrl, "&"); @@ -103,34 +104,34 @@ char* getRedirectUrl(){ } int parseResponse(char *response) { - cJSON *json = NULL; + json_object *json = NULL; int ret; - json = cJSON_Parse(response); + json = json_tokener_parse(response); if (json) { - cJSON *refreshTokenObject = cJSON_GetObjectItem(json, "refresh_token"); + json_object *refreshTokenObject = json_object_object_get(json, "refresh_token"); if(refreshTokenObject) { - char *refreshToken = refreshTokenObject->valuestring; + char *refreshToken = json_object_get_string(refreshTokenObject); if(refreshToken) { printf("\n==============>Get Refresh Token: [%s]\n", refreshToken); ret = update_config_param(json_config, "refreshToken", refreshToken); if (ret != 0){ printf("\nERROR: Failed to update refresh token\n"); - cJSON_Delete(json); + json_object_put(json); return -1; } ret = writeConfig(json_config, config_out_path); if (ret != 0){ printf("\nERROR: Failed to write config to [%s]\n", config_out_path); - cJSON_Delete(json); + json_object_put(json); return -1; } } }else{ printf("\nERROR: Failed to get refresh_token from response\n"); - cJSON_Delete(json); + json_object_put(json); return -1; } - cJSON_Delete(json); + json_object_put(json); return 0; }else{ printf("\nERROR: Failed to parse response\n"); diff --git a/main.c b/main.c index a46a597..773621e 100644 --- a/main.c +++ b/main.c @@ -10,7 +10,7 @@ #include #include #include -#include "cJSON.h" +#include "json.h" #include "lwa.h" #include "config.h" @@ -26,7 +26,7 @@ char config_out_path[256]={0}; char redirect_uri[256]={0}; char port[16]={0}; int listenfd, clients[CONNMAX]; -cJSON *json_config = NULL; +json_object *json_config = NULL; void error(char *);