mirror of
https://github.com/json-c/json-c.git
synced 2026-04-09 23:39:06 +08:00
Add a json_type_to_name() function which returns a string that describes the type. Useful for logging.
git-svn-id: http://svn.metaparadigm.com/svn/json-c/trunk@67 327403b1-1117-474d-bef2-5cb71233fd97
This commit is contained in:
@@ -34,18 +34,6 @@
|
|||||||
const char *json_number_chars = "0123456789.+-eE";
|
const char *json_number_chars = "0123456789.+-eE";
|
||||||
const char *json_hex_chars = "0123456789abcdef";
|
const char *json_hex_chars = "0123456789abcdef";
|
||||||
|
|
||||||
#ifdef REFCOUNT_DEBUG
|
|
||||||
static const char* json_type_name[] = {
|
|
||||||
"null",
|
|
||||||
"boolean",
|
|
||||||
"double",
|
|
||||||
"int",
|
|
||||||
"object",
|
|
||||||
"array",
|
|
||||||
"string",
|
|
||||||
};
|
|
||||||
#endif /* REFCOUNT_DEBUG */
|
|
||||||
|
|
||||||
static void json_object_generic_delete(struct json_object* jso);
|
static void json_object_generic_delete(struct json_object* jso);
|
||||||
static struct json_object* json_object_new(enum json_type o_type);
|
static struct json_object* json_object_new(enum json_type o_type);
|
||||||
|
|
||||||
@@ -71,7 +59,7 @@ static void json_object_fini(void) {
|
|||||||
json_object_table->count);
|
json_object_table->count);
|
||||||
lh_foreach(json_object_table, ent) {
|
lh_foreach(json_object_table, ent) {
|
||||||
struct json_object* obj = (struct json_object*)ent->v;
|
struct json_object* obj = (struct json_object*)ent->v;
|
||||||
MC_DEBUG("\t%s:%p\n", json_type_name[obj->o_type], obj);
|
MC_DEBUG("\t%s:%p\n", json_type_to_name(obj->o_type), obj);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -150,7 +138,7 @@ static void json_object_generic_delete(struct json_object* jso)
|
|||||||
{
|
{
|
||||||
#ifdef REFCOUNT_DEBUG
|
#ifdef REFCOUNT_DEBUG
|
||||||
MC_DEBUG("json_object_delete_%s: %p\n",
|
MC_DEBUG("json_object_delete_%s: %p\n",
|
||||||
json_type_name[jso->o_type], jso);
|
json_type_to_name(jso->o_type), jso);
|
||||||
lh_table_delete(json_object_table, jso);
|
lh_table_delete(json_object_table, jso);
|
||||||
#endif /* REFCOUNT_DEBUG */
|
#endif /* REFCOUNT_DEBUG */
|
||||||
printbuf_free(jso->_pb);
|
printbuf_free(jso->_pb);
|
||||||
@@ -168,7 +156,7 @@ static struct json_object* json_object_new(enum json_type o_type)
|
|||||||
jso->_delete = &json_object_generic_delete;
|
jso->_delete = &json_object_generic_delete;
|
||||||
#ifdef REFCOUNT_DEBUG
|
#ifdef REFCOUNT_DEBUG
|
||||||
lh_table_insert(json_object_table, jso, jso);
|
lh_table_insert(json_object_table, jso, jso);
|
||||||
MC_DEBUG("json_object_new_%s: %p\n", json_type_name[jso->o_type], jso);
|
MC_DEBUG("json_object_new_%s: %p\n", json_type_to_name(jso->o_type), jso);
|
||||||
#endif /* REFCOUNT_DEBUG */
|
#endif /* REFCOUNT_DEBUG */
|
||||||
return jso;
|
return jso;
|
||||||
}
|
}
|
||||||
@@ -186,7 +174,6 @@ enum json_type json_object_get_type(struct json_object *jso)
|
|||||||
return jso->o_type;
|
return jso->o_type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* json_object_to_json_string */
|
/* json_object_to_json_string */
|
||||||
|
|
||||||
const char* json_object_to_json_string(struct json_object *jso)
|
const char* json_object_to_json_string(struct json_object *jso)
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ typedef struct json_tokener json_tokener;
|
|||||||
/* supported object types */
|
/* supported object types */
|
||||||
|
|
||||||
typedef enum json_type {
|
typedef enum json_type {
|
||||||
|
/* If you change this, be sure to update json_type_to_name() too */
|
||||||
json_type_null,
|
json_type_null,
|
||||||
json_type_boolean,
|
json_type_boolean,
|
||||||
json_type_double,
|
json_type_double,
|
||||||
|
|||||||
23
json_util.c
23
json_util.c
@@ -202,3 +202,26 @@ void* rpl_realloc(void* p, size_t n)
|
|||||||
return realloc(p, n);
|
return realloc(p, n);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#define NELEM(a) (sizeof(a) / sizeof(a[0]))
|
||||||
|
static const char* json_type_name[] = {
|
||||||
|
/* If you change this, be sure to update the enum json_type definition too */
|
||||||
|
"null",
|
||||||
|
"boolean",
|
||||||
|
"double",
|
||||||
|
"int",
|
||||||
|
"object",
|
||||||
|
"array",
|
||||||
|
"string",
|
||||||
|
};
|
||||||
|
|
||||||
|
const char *json_type_to_name(enum json_type o_type)
|
||||||
|
{
|
||||||
|
if (o_type < 0 || o_type >= NELEM(json_type_name))
|
||||||
|
{
|
||||||
|
MC_ERROR("json_type_to_name: type %d is out of range [0,%d]\n", o_type, NELEM(json_type_name));
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
return json_type_name[o_type];
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -25,6 +25,12 @@ extern struct json_object* json_object_from_file(const char *filename);
|
|||||||
extern int json_object_to_file(char *filename, struct json_object *obj);
|
extern int json_object_to_file(char *filename, struct json_object *obj);
|
||||||
extern int json_parse_int64(const char *buf, int64_t *retval);
|
extern int json_parse_int64(const char *buf, int64_t *retval);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return a string describing the type of the object.
|
||||||
|
* e.g. "int", or "object", etc...
|
||||||
|
*/
|
||||||
|
extern const char *json_type_to_name(enum json_type o_type);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user