/*
* $Id: json_object.c,v 1.13 2005/06/14 22:41:51 mclark Exp $
*
* Copyright Metaparadigm Pte. Ltd. 2004.
* Michael Clark <michael@metaparadigm.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public (LGPL)
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details: http://www.gnu.org/
*
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "debug.h"
#include "printbuf.h"
#include "linkhash.h"
#include "arraylist.h"
#include "json_object.h"
#include "json_object_private.h"
#include "json_tokener.h"
/* #define REFCOUNT_DEBUG 1 */
char *json_number_chars = "0123456789.+-e";
char *json_hex_chars = "0123456789abcdef";
#ifdef REFCOUNT_DEBUG
static char* json_type_name[] = {
"null",
"boolean",
"double",
"int",
"object",
"array",
"string",
};
#endif /* REFCOUNT_DEBUG */
static void json_object_generic_delete(struct json_object* this);
static struct json_object* json_object_new(enum json_type o_type);
/* ref count debugging */
#ifdef REFCOUNT_DEBUG
static struct lh_table *json_object_table;
static void json_object_init() __attribute__ ((constructor));
static void json_object_init() {
mc_debug("json_object_init: creating object table\n");
json_object_table = lh_kptr_table_new(128, "json_object_table", NULL);
}
static void json_object_fini() __attribute__ ((destructor));
static void json_object_fini() {
struct lh_entry *ent;
if(mc_get_debug() && json_object_table->count) {
mc_debug("json_object_fini: %d referenced objects at exit\n",
json_object_table->count);
lh_foreach(json_object_table, ent) {
struct json_object* obj = (struct json_object*)ent->v;
mc_debug("\t%s:%p\n", json_type_name[obj->o_type], obj);
}
}
mc_debug("json_object_fini: freeing object table\n");
lh_table_free(json_object_table);
}
#endif /* REFCOUNT_DEBUG */
/* string escaping */
static int json_escape_str(struct printbuf *pb, char *str)
{
int pos = 0, start_offset = 0;
char c;
do {
c = str[pos];
switch(c) {
case '\b':
case '\n':
case '\r':
case '\t':
case '"':
if(pos - start_offset > 0)
printbuf_memappend(pb, str + start_offset, pos - start_offset);
if(c == '\b') printbuf_memappend(pb, "\\b", 2);
else if(c == '\n') printbuf_memappend(pb, "\\n", 2);
else if(c == '\r') printbuf_memappend(pb, "\\r", 2);
else if(c == '\t') printbuf_memappend(pb, "\\t", 2);
else if(c == '"') printbuf_memappend(pb, "\\\"", 2);
start_offset = ++pos;
break;
default:
if(c && c < ' ') {
if(pos - start_offset > 0)
printbuf_memappend(pb, str + start_offset, pos - start_offset);
sprintbuf(pb, "\\u00%c%c",
json_hex_chars[c >> 4],
json_hex_chars[c & 0xf]);
start_offset = ++pos;
} else if(c) pos++;
}
} while(c);
if(pos - start_offset > 0)
printbuf_memappend(pb, str + start_offset, pos - start_offset);
return 0;
}
/* reference counting */
extern struct json_object* json_object_get(struct json_object *this)
{
if(this) {
this->_ref_count++;
}
return this;
}
extern void json_object_put(struct json_object *this)
{
if(this) {
this->_ref_count--;
if(!this->_ref_count) this->_delete(this);
}
}
/* generic object construction and destruction parts */
static void json_object_generic_delete(struct json_object* this)
{
#ifdef REFCOUNT_DEBUG
mc_debug("json_object_delete_%s: %p\n",
json_type_name[this->o_type], this);
lh_table_delete(json_object_table, this);
#endif /* REFCOUNT_DEBUG */
printbuf_free(this->_pb);
free(this);
}
static struct json_object* json_object_new(enum json_type o_type)
{
struct json_object *this = calloc(sizeof(struct json_object), 1);
if(!this) return NULL;
this->o_type = o_type;
this->_ref_count = 1;
this->_delete = &json_object_generic_delete;
#ifdef REFCOUNT_DEBUG
lh_table_insert(json_object_table, this, this);
mc_debug("json_object_new_%s: %p\n", json_type_name[this->o_type], this);
#endif /* REFCOUNT_DEBUG */
return this;
}
/* type checking functions */
int json_object_is_type(struct json_object *this, enum json_type type)
{
return (this->o_type == type);
}
enum json_type json_object_get_type(struct json_object *this)
{
return this->o_type;
}
/* json_object_to_json_string */
char* json_object_to_json_string(struct json_object *this)
{
if(!this) return "null";
if(!this->_pb) {
if(!(this->_pb = printbuf_new())) return NULL;
} else {
printbuf_reset(this->_pb);
}
if(this->_to_json_string(this, this->_pb) < 0) return NULL;
return this->_pb->buf;
}
/* json_object_object */
static int json_object_object_to_json_string(struct json_object* this,
struct printbuf *pb)
{
int i=0;
struct json_object_iter iter;
sprintbuf(pb, "{");
/* CAW: scope operator to make ANSI correctness */
/* CAW: switched to json_object_object_foreachC which uses an iterator struct */
json_object_object_foreachC(this, iter) {
if(i) sprintbuf(pb, ",");
sprintbuf(pb, " \"");
json_escape_str(pb, iter.key);
sprintbuf(pb, "\": ");
if(iter.val == NULL) sprintbuf(pb, "null");
else iter.val->_to_json_string(iter.val, pb);
i++;
}
return sprintbuf(pb, " }");
}
static void json_object_lh_entry_free(struct lh_entry *ent)
{
free(ent->k);
json_object_put((struct json_object*)ent->v);
}
static void json_object_object_delete(struct json_object* this)
{
lh_table_free(this->o.c_object);
json_object_generic_delete(this);
}
struct json_object* json_object_new_object()
{
struct json_object *this = json_object_new(json_type_object);
if(!this) return NULL;
this->_delete = &json_object_object_delete;
this->_to_json_string = &json_object_object_to_json_string;
this->o.c_object = lh_kchar_table_new(JSON_OBJECT_DEF_HASH_ENTIRES,
NULL, &json_object_lh_entry_free);
return this;
}
struct lh_table* json_object_get_object(struct json_object *this)
{
if(!this) return NULL;
switch(this->o_type) {
case json_type_object:
return this->o.c_object;
default:
return NULL;
}
}
void json_object_object_add(struct json_object* this, char *key,
struct json_object *val)
{
lh_table_delete(this->o.c_object, key);
lh_table_insert(this->o.c_object, strdup(key), val);
}
struct json_object* json_object_object_get(struct json_object* this, char *key)
{
return (struct json_object*) lh_table_lookup(this->o.c_object, key);
}
void json_object_object_del(struct json_object* this, char *key)
{
lh_table_delete(this->o.c_object, key);
}
/* json_object_boolean */
static int json_object_boolean_to_json_string(struct json_object* this,
struct printbuf *pb)
{
if(this->o.c_boolean) return sprintbuf(pb, "true");
else return sprintbuf(pb, "false");
}
struct json_object* json_object_new_boolean(boolean b)
{
struct json_object *this = json_object_new(json_type_boolean);
if(!this) return NULL;
this->_to_json_string = &json_object_boolean_to_json_string;
this->o.c_boolean = b;
return this;
}
boolean json_object_get_boolean(struct json_object *this)
{
if(!this) return FALSE;
switch(this->o_type) {
case json_type_boolean:
return this->o.c_boolean;
case json_type_int:
return (this->o.c_int != 0);
case json_type_double:
return (this->o.c_double != 0);
case json_type_string:
if(strlen(this->o.c_string)) return TRUE;
default:
return TRUE;
}
}
/* json_object_int */
static int json_object_int_to_json_string(struct json_object* this,
struct printbuf *pb)
{
return sprintbuf(pb, "%d", this->o.c_int);
}
struct js
没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
收起资源包目录
json-c-0.3.tar.gz (44个子文件)
json-c-0.3
json_object.c 12KB
json.h 900B
bits.h 2KB
json_tokener.h 2KB
config.h.win32 3KB
mkinstalldirs 2KB
README-WIN32.html 3KB
config.h.in 3KB
depcomp 12KB
README.html 2KB
test2.c 756B
json_object.h 10KB
printbuf.h 1KB
aclocal.m4 233KB
test1.c 5KB
printbuf.c 4KB
Doxyfile 46KB
linkhash.h 6KB
config.guess 43KB
arraylist.h 1KB
Makefile.am 698B
config.sub 31KB
configure.in 726B
README 437B
Makefile.in 18KB
INSTALL 9KB
missing 10KB
install-sh 6KB
linkhash.c 5KB
AUTHORS 84B
configure 673KB
json_util.c 3KB
json_util.h 1KB
debug.h 1KB
ltmain.sh 180KB
debug.c 2KB
NEWS 5B
arraylist.c 2KB
ChangeLog 1KB
json-c.vcproj 4KB
json_object_private.h 1KB
COPYING 25KB
json.pc.in 217B
json_tokener.c 12KB
共 44 条
- 1
我虽横行却不霸道
- 粉丝: 72
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0