zoukankan      html  css  js  c++  java
  • 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;
    };
    
  • 相关阅读:
    Windows系统自带工具的 cmd 命令
    阿里巴巴集团2016校园招聘-Python工程师笔试题(附加题+部分答案)
    小米3刷机说明
    第3章 常用运算符
    第1章Java入门体验
    jQuery表单验证案例
    jQuery超链接提示,提示跟随鼠标动
    [转载]我的Java后端书架 (2016年暖冬4.0版)
    PHP代码重用与函数编写
    PHP数组操作
  • 原文地址:https://www.cnblogs.com/hustluotao/p/14697934.html
Copyright © 2011-2022 走看看