36 size_t extra_type_size = 0;
62 printf(
"Warning : Unhandled JSON type...\n");
66 if( extra_type_size == 0 )
76 json_t * json = malloc(
sizeof(
json_t ) + extra_type_size );
91 pthread_spin_init( &json->
lock, 0 );
130 printf(
"Warning : Unhandled JSON type...\n");
135 pthread_spin_destroy( &json->
lock );
179 s->
value = strdup(
string );
191 s->
value = malloc( len + 1 );
199 memcpy( s->
value ,
string, len );
200 s->
value[ len ] =
'\0';
252 if( !strlen( key ) || !elem )
264 ret->
key = strdup( key );
315 for( i = 0 ; i < pow2_size ; i++ )
336 for( i = 0 ; i < ht->
size ; i++ )
348 static inline unsigned int hashString(
char *s ,
int bit_width )
350 unsigned int ret = 0;
356 ret ^= (*s << (((i++)%7) * 4));
360 int dec = 32 - bit_width;
363 ret = (ret << dec) >> dec;
365 return ret?ret-1:ret;
411 if( !elem || !ht || !key )
451 if( ret != ent->
prev )
510 ret = ent?ent->
elem:NULL;
560 if( a->
size < offset )
561 overflow = offset - a->
size;
575 for( i = a->
size; i < offset; i++ )
582 if( offset < a->size )
587 a->
size += count + overflow;
624 if( array->
size <= offset )
628 ret = array->
content[ offset ];
639 if( array->
size <= offset )
659 if( array->
size <= offset )
667 if( array->
size == 1 )
678 if( offset < (array->
size - 1) )
706 for( i = 0 ; i < array->
size ; i++ )
746 int len = strlen( s );
764 memcpy( sb->
s + sb->
len , s, len + 1 );
783 for( i = 0 ; i < depth && i < 4095 ; i++)
786 tmp_buff[depth] =
'\0';
814 snprintf( tmp_buff, 4096,
" %s ",
json_to_bool( json )->value?
"true":
"false" );
824 snprintf( tmp_buff, 4096,
" %ld ",
json_to_int( json )->value );
828 snprintf( tmp_buff, 4096,
" %g ",
json_to_real( json )->value );
833 snprintf( tmp_buff, 4096,
" {%s",
c_newline( newline ) );
873 snprintf( tmp_buff, 4096,
",%s",
c_newline( newline ) );
881 snprintf( tmp_buff, 4096,
"%s",
c_newline( newline ) );
890 snprintf( tmp_buff, 4096,
"} ");
896 snprintf( tmp_buff, 4096,
" [%s",
c_newline( newline ) );
902 for( i = 0 ; i < a->
size; i++ )
908 if( i != ( a->
size - 1 ) )
910 snprintf( tmp_buff, 4096,
",%s",
c_newline( newline ) );
916 snprintf( tmp_buff, 4096,
"%s",
c_newline( newline ) );
925 snprintf( tmp_buff, 4096,
"] ");
929 printf(
"Warning : Unhandled JSON type...\n");
940 char *tmp_buff = malloc( 4096 );
971 __json_dump( tmp_buff, &out, json, 0, indent, newline, lock );
984 fprintf( f,
"%s" , dump );
1000 if(
'a' <= *c && *c <=
'z' )
1003 if(
'A' <= *c && *c <=
'Z' )
1006 if(
'0' <= *c && *c <=
'9' )
1017 if(
'0' <= *c && *c <=
'9' )
1029 while( *s && *s != expect )
1032 if( *s !=
' ' && *s !=
'\n' && *s !=
'\t' )
1058 char *string_start =
s;
1060 int skip_next_quote = 0;
1063 while( (*s !=
'"' || skip_next_quote ) && *s )
1067 skip_next_quote = 1;
1069 if( skip_next_quote )
1070 skip_next_quote = 0;
1080 *json_string = s + 1;
1082 return string_start;
1088 if( *s ==
't' || *s ==
'T')
1090 if( *(s + 1 ) ==
'r' || *(s + 1 ) ==
'R' )
1092 if( *(s + 2 ) ==
'u' || *(s + 2 ) ==
'U' )
1094 if( *(s + 3 ) ==
'e' || *(s + 3 ) ==
'E' )
1108 if( *s ==
'f' || *s ==
'F')
1110 if( *(s + 1 ) ==
'a' || *(s + 1 ) ==
'A' )
1112 if( *(s + 2 ) ==
'l' || *(s + 2 ) ==
'L' )
1114 if( *(s + 3 ) ==
's' || *(s + 3 ) ==
'S' )
1116 if( *(s + 4 ) ==
'e' || *(s + 4 ) ==
'E' )
1131 if( *s ==
'n' || *s ==
'N')
1133 if( *(s + 1 ) ==
'u' || *(s + 1 ) ==
'U' )
1135 if( *(s + 2 ) ==
'l' || *(s + 2 ) ==
'L' )
1137 if( *(s + 3 ) ==
'l' || *(s + 3 ) ==
'L' )
1155 if( *s ==
',' || *s ==
'}' || *s ==
']' )
1172 while( *s && *s !=
'}' && *s !=
']' && *s !=
',' )
1174 if( *s ==
'e' || *s ==
'E' || *s ==
'.' )
1202 while( *s && *s !=
',' && *s !=
']' && *s !=
'}' )
1212 long int val = strtoll( begin , &end, 10);
1214 if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
1215 || (errno != 0 && val == 0)) {
1236 while( *s && *s !=
',' && *s !=
']' && *s !=
'}' )
1246 long double val = strtold(begin, &end);
1248 if ((errno == ERANGE ) || (errno != 0 && val == 0.0)) {
1319 while( *s && *s !=
']' )
1325 goto json_parse_array_error;
1336 while( *s && *s !=
',' && *s !=
']' )
1351 goto json_parse_array_error;
1356 goto json_parse_array_error;
1359 *json_string = s + 1;
1363 json_parse_array_error:
1388 *json_string = s + 1;
1411 while( *s !=
'}' && *s)
1419 goto json_parse_object_error;
1427 goto json_parse_object_error;
1440 while( *s && *s !=
',' && *s !=
'}' )
1454 goto json_parse_object_error;
1457 *json_string = s + 1;
1461 json_parse_object_error:
1506 char * tmp = strdup( json );
1543 printf(
"ERROR : There is no cache !\n");
1554 printf(
"ERROR : There is no cache !\n");
1572 printf(
"==> Delete %s\n", key );
1597 printf(
"ERROR : There is no cache !\n");
1603 printf(
"Warning : No element provided !\n");
1633 if( !c->
cache || !json )
void jsonCache_set(struct jsonCache *c, char *key, json_t *elem)
json_t * json_parse_array(char **json_string)
enum json_type_e json_type
void _jsonCache_set(struct jsonCache *c, char *key, json_t *elem, int do_commit)
static void json_unlock(json_t *json)
void jsonCache_delete(struct jsonCache *c, char *key)
json_t * json_object_get(json_t *json, char *key)
int json_infer_type(char *ent)
json_t * json_string(char *string)
void _jsonCache_delete(struct jsonCache *c, char *key, int docommit)
void ObjectHT_entry_release(struct ObjectHT_entry *ent)
void jsonCache_set_json(struct jsonCache *c, char *key, char *json)
json_t * json_array_del(json_t *json, unsigned int offset)
json_t * json_parse_object(char **json_string)
struct ObjectHT_entry ** entries
char * json_parse_key(char **json_string)
struct json_real_s json_real_t
json_t * json_int(int64_t value)
struct json_null_s json_null_t
struct ObjectHT_entry * ObjectHT_get(struct ObjectHT *ht, char *key)
json_t * __json_t_init(json_type type)
void json_bool_destroy(json_t *json)
void jsonCache_init(struct jsonCache *c, void(*set_handler)(char *, json_t *, void *), void(*delete_handler)(char *, void *), void *ctx)
void __json_t_release(json_t *json)
static void json_object_iterator_init(json_object_iterator *it, json_t *json)
void json_string_destroy(json_t *json)
void json_array_destroy(json_t *json)
void json_decref(json_t *json)
static void json_lock(json_t *json)
json_t * json_array_push_at(json_t *json, unsigned int offset, json_t *elem)
json_t * json_array_set(json_t *json, unsigned int offset, json_t *elem)
static int json_decref_lf(json_t *json)
void __json_dump(char *tmp_buff, struct string_buff *out, json_t *json, int depth, int indent, int newline, int lock)
void json_dump_f(FILE *f, json_t *json, json_format mode)
json_t * json_bool(int thruth)
struct ObjectHT_entry * ObjectHT_entry_push(struct ObjectHT_entry *prev, char *key, json_t *elem)
static void json_incref_lf(json_t *json)
struct ObjectHT_entry * prev
void jsonCache_commit(struct jsonCache *c, char *key)
struct json_bool_s json_bool_t
void _jsonCache_set_json(struct jsonCache *c, char *key, char *json, int do_commit)
void ObjectHT_delete(struct ObjectHT *ht, char *key)
int is_alphanumeric(char *c)
void jsonCache_set_json_nocommit(struct jsonCache *c, char *key, char *json)
json_t * json_object_set(json_t *json, char *key, json_t *elem)
struct json_array_s json_array_t
static int same_string(char *a, char *b)
struct json_string_s json_string_t
struct ObjectHT_entry * ObjectHT_entry_new(char *key, json_t *elem)
void jsonCache_release(struct jsonCache *c)
void string_buff_release(struct string_buff *sb)
struct json_object_s json_object_t
struct ObjectHT_entry * __ObjectHT_delete(struct ObjectHT_entry *ent, char *key, int did_delete)
json_t * jsonCache_get(struct jsonCache *c, char *key)
json_t * json_parse_real(char **buff)
json_t * json_array_get(json_t *json, unsigned int offset)
int pad_buff(char **buff, char expect)
void stream_indent(char *tmp_buff, struct string_buff *out, int depth)
json_t * json_parse(char *json)
json_t * json_string_l(char *string, int len)
void ObjectHT_release(struct ObjectHT *ht)
#define json_to_string(a)
char * json_dump(json_t *json, json_format mode)
void ObjectHT_init(struct ObjectHT *ht, int pow2_size)
json_t * json_parse_int(char **buff)
void(* delete_handler)(char *, void *)
void json_object_destroy(json_t *json)
void jsonCache_delete_nocommit(struct jsonCache *c, char *key)
void string_buff_init(struct string_buff *sb)
json_t * _json_parse(char **buff)
char * parse_string(char **json_string)
void json_int_destroy(json_t *json)
static json_t * json_object_iterator_elem(json_object_iterator *it)
void jsonCache_set_nocommit(struct jsonCache *c, char *key, json_t *elem)
void string_buff_push(struct string_buff *sb, char *s)
void json_incref(json_t *json)
static int json_object_iterator_next(json_object_iterator *it)
json_t * json_parse_bool(char **json_string)
static unsigned int hashString(char *s, int bit_width)
json_t * json_object_delete(json_t *json, char *key)
struct json_int_s json_int_t
void json_null_destroy(json_t *json)
json_t * json_array_push(json_t *json, json_t *elem)
void json_real_destroy(json_t *json)
#define json_to_object(a)
char * jsonCache_json(struct jsonCache *c, char *key, json_format mode)
json_t * json_parse_string(char **buff)
int json_array_guarantee(json_array_t *a, unsigned int offset, int count)
void(* set_handler)(char *, json_t *, void *)
static char * json_object_iterator_key(json_object_iterator *it)
void ObjectHT_set(struct ObjectHT *ht, char *key, json_t *elem)
char * c_newline(newline)
json_t * json_real(double value)