json-c初探(二)

json对象类型:

/* supported object types */
typedef enum json_type {
  /* If you change this, be sure to update json_type_to_name() too */
  json_type_null,
  json_type_boolean,
  json_type_double,
  json_type_int,
  json_type_object,
  json_type_array,
  json_type_string
} json_type;

json对象:

/**
 * Type of a custom serialization function.  See json_object_set_serializer.
 */
typedef int (json_object_to_json_string_fn)(struct json_object *jso,
						struct printbuf *pb,
						int level,
						int flags);
typedef void (json_object_private_delete_fn)(struct json_object *o);
/**
 * Type of custom user delete functions.  See json_object_set_serializer.
 */
typedef void (json_object_delete_fn)(struct json_object *jso, void *userdata);

struct json_object
{
	enum json_type o_type;
	json_object_private_delete_fn *_delete;
	json_object_to_json_string_fn *_to_json_string;
	int _ref_count;
	struct printbuf *_pb;
	union data {
		json_bool c_boolean;
		double c_double;
		int64_t c_int64;
		struct lh_table *c_object;
		struct array_list *c_array;
		struct {
			union {
				/* optimize: if we have small strings, we can store them
				 * directly. This saves considerable CPU cycles AND memory.
				 */
				char *ptr;
				char data[LEN_DIRECT_STRING_DATA];
			} str;
			int len;
		} c_string;
	} o;
	json_object_delete_fn *_user_delete;
	void *_userdata;
};

struct printbuf {
  char *buf;
  int bpos;
  int size;
};

json对象使用hash table结构管理

/**
 * An entry in the hash table
 */
struct lh_entry {
	/**
	 * The key.  Use lh_entry_k() instead of accessing this directly.
	 */
	const void *k;
	/**
	 * A flag for users of linkhash to know whether or not they
	 * need to free k.
	 */
	int k_is_constant;
	/**
	 * The value.  Use lh_entry_v() instead of accessing this directly.
	 */
	const void *v;
	/**
	 * The next entry
	 */
	struct lh_entry *next;
	/**
	 * The previous entry.
	 */
	struct lh_entry *prev;
};

/**
 * callback function prototypes
 */
typedef void (lh_entry_free_fn) (struct lh_entry *e);
/**
 * callback function prototypes
 */
typedef unsigned long (lh_hash_fn) (const void *k);
/**
 * callback function prototypes
 */
typedef int (lh_equal_fn) (const void *k1, const void *k2);

/**
 * The hash table structure.
 */
struct lh_table {
	/**
	 * Size of our hash.
	 */
	int size;
	/**
	 * Numbers of entries.
	 */
	int count;

	/**
	 * The first entry.
	 */
	struct lh_entry *head;

	/**
	 * The last entry.
	 */
	struct lh_entry *tail;

	struct lh_entry *table;

	/**
	 * A pointer onto the function responsible for freeing an entry.
	 */
	lh_entry_free_fn *free_fn;
	lh_hash_fn *hash_fn;
	lh_equal_fn *equal_fn;
};

json数组结构:

typedef void (array_list_free_fn) (void *data);

struct array_list
{
  void **array;
  size_t length;
  size_t size;
  array_list_free_fn *free_fn;
};
原文地址:https://www.cnblogs.com/hustluotao/p/14697934.html