Skip to content

Commit

Permalink
Change cJSON to json-c
Browse files Browse the repository at this point in the history
  • Loading branch information
subjectxbj committed Mar 23, 2018
1 parent cd925fd commit afe0f19
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 60 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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/

Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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
Expand Down
49 changes: 21 additions & 28 deletions config.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include<netdb.h>
#include<signal.h>
#include<fcntl.h>
#include "cJSON.h"
#include "json.h"
#include "config.h"

int isCommentLine(char *line){
Expand All @@ -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;
Expand All @@ -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));
Expand All @@ -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);
Expand All @@ -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;

}

12 changes: 6 additions & 6 deletions config.h
Original file line number Diff line number Diff line change
@@ -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;
39 changes: 20 additions & 19 deletions lwa.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include<netdb.h>
#include<signal.h>
#include<fcntl.h>
#include "cJSON.h"
#include "json.h"
#include "lwa.h"
#include "curl.h"
#include "config.h"
Expand Down Expand Up @@ -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));
Expand All @@ -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, "&");

Expand Down Expand Up @@ -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");
Expand Down
4 changes: 2 additions & 2 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include<netdb.h>
#include<signal.h>
#include<fcntl.h>
#include "cJSON.h"
#include "json.h"
#include "lwa.h"
#include "config.h"

Expand All @@ -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 *);
Expand Down

0 comments on commit afe0f19

Please sign in to comment.