用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; }
相关文章推荐
- 用c写一个简单json处理器之头文件,可能会不断完善吧(二)
- 一个GUI的简单练习-------- Java记事本 简单实现 陆续完善中……
- 设计一个简易的处理器(6)--简单的流水线实现PIPE-
- 自己动手实现一个简单的JSON解析器
- 简单工厂(Factory)-提供一个创建对象实例的功能,而无须关心其具体实现。被创建实例的类型可以是接口、抽象类,也可以是具体的类。
- 用c写一个简单json处理器(一)
- 一个简单的springboot的案例实现一个请求控制层返回一个JSON
- php实现的一个简单json rpc框架实例
- extJs+json实现的一个登陆验证的简单例子
- 一个简单的Android客户端从服务器端获取json数据并解析的实现代码
- 一个简单线程池的实现---需进一步完善
- 分享实现类似QQ的自动登陆的方法,代码比较简单,主要是给大家提供一个实现逻辑,具体的要结合自身的app来做
- 写的一个比较简单的会员登陆系统,具体功能在完善中,提点意见
- 一个可以随时插入的json的简单实现
- 自己写了一个链表功能还不完善但是简单的增删改查功能都已经实现了
- 简单完整的代码,通过这个代码你将对RSA加密算法在Java中的实现方法有一个初步的了解,这个类,你可以直接使用,水平高的,就自己修改完善下代码。
- 使用 flask 实现一个简单的可以返回 json 的 RESTful API 服务端
- Git 分支 - 分支的新建与合并 分支的新建与合并 让我们来看一个简单的分支新建与分支合并的例子,实际工作中你可能会用到类似的工作流。 你将经历如下步骤: 开发某个网站。 为实现某个新的需求,创
- php实现的一个简单json rpc框架实例
- 一个小型的网站,比如个人网站,可以使用最简单的html静态页面就实现了,配合一些图片达到美化效果,所有的页面均存放在一个目录下,这样的网站对系统架构、性能的要求都很简单,随着互联网业务的不断丰富,网站