您的位置:首页 > Web前端 > JavaScript

用c写一个简单json处理器之具体实现,应该可能会不断完善吧(三)

2016-09-09 15:31 375 查看
#include <stdlib.h>
#include <string.h>

#include "json.h"

static int json_match_array(const JsonValue *key1,const JsonValue *key2);
static int json_match_obj(const JsonValue *key1,const JsonValue *key2);

/**
初始化json
**/
void json_init(Json *json,void (*create)(void *data),void (*destroy)(void *data)){
json->objCount  = 0;
json->totalCount = 0;
json->create = create;
json->destroy = destroy;
}

/**
添加数据
**/
int json_append(Json *json,JsonValue *pj,JsonValue *jvalue){
//如果没有数据
if(json_count(json) == 0){
json->head = pj;
json->tail = pj;
json->objCount++;
json->totalCount++;
return 0;
}
if(pj == NULL){
json->tail->next = jvalue;
json->tail = jvalue;
}else{
if(pj->next == NULL){
json->tail = jvalue;
}
jvalue->next = pj->next;
pj->next = jvalue;
}
json->objCount++;
json->totalCount++;
return 0;
}

/**
比较两个json对象的值是否相等
-1 错误
0 相等
1 不等
**/
int json_match(const JsonValue *key1,const JsonValue *key2){
if(key1 == NULL || key2 == NULL) return -1;

if(key1->jsonType != key2->jsonType) return -1;
int ret  = 0;
JSON_TYPE type;
type = key1->jsonType;
switch(type){
case JSON_NULL:
ret = 0;
case JSON_BOOL:
ret = jbase_bool(key1) == jbase_bool(key2) ? 0:1;
break;
case JSON_INT:
ret = jbase_number(key1) == jbase_number(key2) ? 0:1;
break;
case JSON_FLOAT:
ret = jbase_double(key1) == jbase_double(key2) ? 0:1;
break;
case JSON_STRING:
ret = strcmp(jbase_string(key1),jbase_string(key2)) == 0?0:1;
break;
case JSON_ARRAY:
ret = json_match_array(key1,key2);
break;
case JSON_OBJECT:
ret = json_match_obj(key1,key2);
break;
}
return ret;
}

//创建基础的数据类型
JsonValue* jcreate(short create_data){
JsonValue *jv = (JsonValue*)malloc(sizeof(JsonValue));
if(jv == NULL) exit(BASEDATA_MALLOC_FAILD);
jv->child = NULL;
jv->next = NULL;
jv->refValue  = NULL;
if(create_data == 1){
jv->refValue = (Refvalue*)malloc(sizeof(Refvalue));
}
return jv;
}

//释放内存
void jdestroy(JsonValue* jv){
if(jv->refValue != NULL){
//非引用
if(jv->refValue->isRef  == False || jv->refValue->refCount == 0){
free(jv->refValue);
}
//存在引用
if(jv->refValue->isRef == True ){
jv->refValue->refCount --;
if(jv->refValue->refCount == 0){
jv->refValue->isRef = False;
}
}
}
free(jv);
}

//start 创建基本数据
//create null
JsonValue* jcreate_null(){
JsonValue *jv = jcreate(0);
if(jv == NULL) exit(BASEDATA_MALLOC_FAILD);
jv->jsonType = JSON_NULL;
return jv;
}

//create bool
JsonValue* jcreate_bool(Jbool val){
JsonValue *jv = jcreate(1);
if(jv == NULL) exit(BASEDATA_MALLOC_FAILD);
jv->jsonType = JSON_BOOL;
jbase_bool(jv) = val;
jbase_is_ref(jv)= False;
jbase_is_refcount(jv) = 0;
return jv;
}
//create num
JsonValue* jcreate_int(Jnumber val){
JsonValue *jv = jcreate(1);
if(jv == NULL) exit(BASEDATA_MALLOC_FAILD);
jv->jsonType = JSON_INT;
jbase_number(jv) = val;
jbase_is_ref(jv)= False;
jbase_is_refcount(jv) = 0;
return jv;
}
//create float
JsonValue* jcreate_float(Jfloat val){
JsonValue *jv = jcreate(1);
if(jv == NULL) exit(BASEDATA_MALLOC_FAILD);
jv->jsonType = JSON_FLOAT;
jbase_double(jv) = val;
jbase_is_ref(jv)= False;
jbase_is_refcount(jv) = 0;
return jv;
}
//create string
JsonValue* jcreate_string(const Jstring val){
JsonValue *jv = jcreate(1);
if(jv == NULL) exit(BASEDATA_MALLOC_FAILD);
jv->jsonType = JSON_FLOAT;

unsigned long len = strlen(val);
if(len <= 0)exit(STRING_LEN_ERR);
//用于存储'\0' len += 1;
//分配内存
jbase_string(jv) = (char*)malloc((len+1)*sizeof(char));
strcpy(jbase_string(jv),val);
(jbase_string(jv))[len] = '\0';
jbase_is_ref(jv)= False;
jbase_is_refcount(jv) = 0;
return jv;
}
//end 创建基本数据

//创建数组
int jcreate_array(){

return 0;
}

//创建对象
int jcreate_object(){

return 0;
}

/**
比较数组,现不支持
**/
static int json_match_array(const JsonValue *key1,const JsonValue *key2){
return 1;
}
/**
比较对象,现不支持
**/
static int json_match_obj(const JsonValue *key1,const JsonValue *key2){
return 1;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐