json.h

00001 /*
00002  * Copyright (c) 2005-2012 by KoanLogic s.r.l. - All rights reserved.
00003  */
00004 
00005 #ifndef _U_JSON_H_
00006 #define _U_JSON_H_
00007 
00008 #include <sys/types.h>
00009 #include <u/libu_conf.h>
00010 #include <u/toolbox/lexer.h>
00011 
00012 #ifdef __cplusplus
00013 extern "C" {
00014 #endif
00015 
00016 /* Forward decls. */ 
00017 struct u_json_s;
00018 
00025 typedef struct u_json_s u_json_t;
00026 
00028 typedef struct { u_json_t *cur; } u_json_it_t;
00029 
00031 enum { 
00032     U_JSON_WALK_PREORDER,   
00033     U_JSON_WALK_POSTORDER   
00034 };
00035 
00037 typedef enum {
00038     U_JSON_TYPE_UNKNOWN = 0,
00039     U_JSON_TYPE_STRING,         
00040     U_JSON_TYPE_NUMBER,         
00041     U_JSON_TYPE_OBJECT,         
00042     U_JSON_TYPE_ARRAY,          
00043     U_JSON_TYPE_TRUE,           
00044     U_JSON_TYPE_FALSE,          
00045     U_JSON_TYPE_NULL            
00046 } u_json_type_t;
00047 
00048 
00051 #ifndef U_JSON_FQN_SZ
00052 #define U_JSON_FQN_SZ   256
00053 #endif  /* !U_JSON_FQN_SZ */
00054 
00057 #ifndef U_JSON_MAX_DEPTH
00058 #define U_JSON_MAX_DEPTH   16
00059 #endif  /* !U_JSON_MAX_DEPTH */
00060 
00061 /* Encode/Decode/Validate. */
00062 int u_json_decode (const char *json, u_json_t **pjo);
00063 int u_json_encode (u_json_t *jo, char **ps);
00064 int u_json_validate (const char *json, char status[U_LEXER_ERR_SZ]);
00065 
00066 /* Cache creation/destruction. */
00067 int u_json_index (u_json_t *jo);
00068 int u_json_unindex (u_json_t *jo);
00069 
00070 /* Cache-aided get/set operations. */
00071 u_json_t *u_json_cache_get (u_json_t *jo, const char *name);
00072 const char *u_json_cache_get_val (u_json_t *jo, const char *name);
00073 int u_json_cache_get_int (u_json_t *jo, const char *name, long *pval);
00074 int u_json_cache_get_real (u_json_t *jo, const char *name, double *pval);
00075 int u_json_cache_get_bool (u_json_t *jo, const char *name, char *pval);
00076 
00077 int u_json_cache_set_tv (u_json_t *jo, const char *name, 
00078         u_json_type_t type, const char *val);
00079 
00080 /* JSON base objects [cd]tor. */
00081 int u_json_new (u_json_t **pjo);
00082 void u_json_free (u_json_t *jo);
00083 
00084 int u_json_new_object (const char *key, u_json_t **pjo);
00085 int u_json_new_array (const char *key, u_json_t **pjo);
00086 int u_json_new_string (const char *key, const char *val, u_json_t **pjo);
00087 int u_json_new_number (const char *key, const char *val, u_json_t **pjo);
00088 int u_json_new_real (const char *key, double val, u_json_t **pjo);
00089 int u_json_new_int (const char *key, long val, u_json_t **pjo);
00090 int u_json_new_null (const char *key, u_json_t **pjo);
00091 int u_json_new_bool (const char *key, char val, u_json_t **pjo);
00092 
00093 /* JSON base objects get/set/add/remove operations. */
00094 int u_json_set_key (u_json_t *jo, const char *key);
00095 int u_json_set_val (u_json_t *jo, const char *val);
00096 int u_json_set_type (u_json_t *jo, u_json_type_t type); 
00097 int u_json_add (u_json_t *head, u_json_t *jo);
00098 int u_json_remove (u_json_t *jo);
00099 const char *u_json_get_val (u_json_t *jo);
00100 int u_json_set_val_ex (u_json_t *jo, const char *val, char validate);
00101 int u_json_get_int (u_json_t *jo, long *pl);
00102 int u_json_get_real (u_json_t *jo, double *pd);
00103 int u_json_get_bool (u_json_t *jo, char *pb);
00104 
00105 /* Access to children of a container object. */
00106 u_json_t *u_json_child_first (u_json_t *jo);
00107 u_json_t *u_json_child_last (u_json_t *jo);
00108 
00109 /* Array specific ops. */
00110 unsigned int u_json_array_count (u_json_t *jo);
00111 u_json_t *u_json_array_get_nth (u_json_t *jo, unsigned int n);
00112 
00113 /* Misc. */
00114 void u_json_print (u_json_t *jo);
00115 void u_json_walk (u_json_t *jo, int strategy, size_t l, 
00116         void (*cb)(u_json_t *, size_t, void *), void *cb_args);
00117 
00118 /* Iterators. */
00119 int u_json_it (u_json_t *jo, u_json_it_t *it);
00120 u_json_t *u_json_it_next (u_json_it_t *it);
00121 u_json_t *u_json_it_prev (u_json_it_t *it);
00122 
00127 #ifdef __cplusplus
00128 }
00129 #endif
00130 
00131 #endif  /* !_U_JSON_H_ */

←Products
© 2005-2012 - KoanLogic S.r.l. - All rights reserved