1 Star 0 Fork 37

gaap_yin / Exjson

forked from Josin / Exjson 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
exjson.c 20.97 KB
一键复制 编辑 原始数据 按行查看 历史
liqiongfan 提交于 2019-08-28 21:37 . Fix coding style
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
/**
* File:exjson.c for project exjson.
* Author: Josin
* Email: xeapplee@gmail.com
* Website: https://www.supjos.cn
*/
#include "exjson.h"
EXJSON_V *
INIT_EXJSON_V()
{
EXJSON_V *ptr = malloc(sizeof(EXJSON_V));
if ( ptr == NULL )
{
free(ptr); return NULL;
}
memset(ptr, 0, sizeof(EXJSON_V));
EV_NAME_P(ptr) = NULL;
EV_VALUE_P(ptr) = NULL;
EV_TYPE_P(ptr) = 0;
return ptr;
}
EXJSON *
INIT_EXJSON()
{
EXJSON *ptr = malloc(sizeof(EXJSON));
if ( ptr == NULL )
{
free(ptr); return NULL;
}
memset(ptr, 0, sizeof(EXJSON));
E_DATA_P(ptr) = NULL;
E_NUM_P(ptr) = 0;
E_TYPE_P(ptr) = 0;
return ptr;
}
char *str_slash(char *str, long str_len)
{
char *r;
long i, k;
char s = '\\';
if ( str_len <= 0 ) return NULL;
r = malloc( sizeof(char) * ( str_len * 2 + 1 ) );
memset(r, 0, sizeof(char) * ( str_len * 2 + 1));
for ( k = 0, i = 0; i < str_len; ++i, ++k )
{
if ( str[i] == '"') {
memcpy(r + k, &s, sizeof(char) );
++k;
}
memcpy(r + k, &str[i], sizeof(char) );
}
return r;
}
int add_object_int(EXJSON *exjson, char *key, long val)
{
EXJSON_V *val_ptr = E_DATA_P(exjson),
*temp_ptr = INIT_EXJSON_V(),
*ptr;
unsigned long size = E_NUM_P(exjson) + 1,
key_len = strlen(key);
/* Allocate key memory and store it. */
EV_NAME_P(temp_ptr) = malloc(sizeof(char) * (key_len + 1));
if ( EV_NAME_P(temp_ptr) == NULL )
{
free(EV_NAME_P(temp_ptr)); return 0;
}
memset(EV_NAME_P(temp_ptr), 0, key_len + 1);
memcpy(EV_NAME_P(temp_ptr), key, key_len);
/* Allocate data memory and store it */
EV_VALUE_P(temp_ptr) = malloc( sizeof(long) );
if ( EV_VALUE_P(temp_ptr) == NULL )
{
free(EV_VALUE_P(temp_ptr));return 0;
}
memcpy(EV_VALUE_P(temp_ptr), &val, sizeof(long));
/* Set value type */
EV_TYPE_P(temp_ptr) = EXJSON_INT;
/* add to exjson */
ptr = realloc(val_ptr, sizeof(EXJSON_V)*size);
if ( ptr == NULL )
{
free(val_ptr); return 0;
}
memcpy(ptr + E_NUM_P(exjson), temp_ptr, sizeof(EXJSON_V));
free(temp_ptr);
E_NUM_P(exjson)++;
E_DATA_P(exjson) = ptr;
E_TYPE_P(exjson) = EX_OBJECT;
return 1;
}
int add_object_double(EXJSON *exjson, char *key, double val)
{
EXJSON_V *val_ptr = E_DATA_P(exjson),
*temp_ptr = INIT_EXJSON_V(),
*ptr;
unsigned long size = E_NUM_P(exjson) + 1,
key_len = strlen(key);
/* Allocate key memory and store it. */
EV_NAME_P(temp_ptr) = malloc(sizeof(char) * (key_len + 1));
if ( EV_NAME_P(temp_ptr) == NULL )
{
free( EV_NAME_P(temp_ptr) ); return 0;
}
assert(EV_NAME_P(temp_ptr) != NULL);
memset(EV_NAME_P(temp_ptr), 0, (key_len + 1));
memcpy(EV_NAME_P(temp_ptr), key, (key_len));
/* Allocate data memory and store it */
EV_VALUE_P(temp_ptr) = malloc( sizeof( double) );
if ( EV_VALUE_P(temp_ptr) == NULL )
{
free(EV_VALUE_P(temp_ptr)); return 0;
}
memcpy(EV_VALUE_P(temp_ptr), &val, sizeof( double ));
/* Set value type */
EV_TYPE_P(temp_ptr) = EXJSON_DOUBLE;
/* add to exjson */
ptr = realloc(val_ptr, sizeof(EXJSON_V)*size);
if ( ptr == NULL )
{
free(val_ptr); return 0;
}
memcpy(ptr + E_NUM_P(exjson), temp_ptr, sizeof(EXJSON_V));
free(temp_ptr);
E_NUM_P(exjson)++;
E_DATA_P(exjson) = ptr;
E_TYPE_P(exjson) = EX_OBJECT;
return 1;
}
int add_object_string(EXJSON *exjson, char *key, char *val)
{
EXJSON_V *val_ptr = E_DATA_P(exjson),
*temp_ptr = INIT_EXJSON_V(),
*ptr;
char *tval = str_slash(val, strlen(val));
unsigned long size = E_NUM_P(exjson) + 1,
key_len = strlen(key), val_len = strlen(tval);
/* Allocate key memory and store it. */
EV_NAME_P(temp_ptr) = malloc(sizeof(char) * (key_len + 1));
if ( EV_NAME_P(temp_ptr) == NULL )
{
free( EV_NAME_P(temp_ptr) );return 0;
}
assert(EV_NAME_P(temp_ptr) != NULL);
memset(EV_NAME_P(temp_ptr), 0, key_len + 1);
memcpy(EV_NAME_P(temp_ptr), key, key_len);
/* Allocate data memory and store it */
EV_VALUE_P(temp_ptr) = malloc( sizeof( char) * ( val_len + 1 ));
if ( EV_VALUE_P(temp_ptr) == NULL )
{
free( EV_VALUE_P(temp_ptr) ); return 0;
}
memset(EV_VALUE_P(temp_ptr), 0, val_len + 1);
assert(EV_VALUE_P(temp_ptr) != NULL);
memcpy(EV_VALUE_P(temp_ptr), tval, (val_len));
free(tval);
/* Set value type */
EV_TYPE_P(temp_ptr) = EXJSON_STRING;
/* add to exjson */
ptr = realloc(val_ptr, sizeof(EXJSON_V)*size);
if ( ptr == NULL )
{
free(val_ptr); return 0;
}
memcpy(ptr + E_NUM_P(exjson), temp_ptr, sizeof(EXJSON_V));
free(temp_ptr);
E_NUM_P(exjson)++;
E_DATA_P(exjson) = ptr;
E_TYPE_P(exjson) = EX_OBJECT;
return 1;
}
int add_object_object(EXJSON *exjson, char *key, void *val)
{
EXJSON_V *val_ptr = E_DATA_P(exjson),
*temp_ptr = INIT_EXJSON_V(),
*ptr;
unsigned long size = E_NUM_P(exjson) + 1,
key_len = strlen(key);
/* Allocate key memory and store it. */
EV_NAME_P(temp_ptr) = malloc(sizeof(char) * (key_len + 1));
if ( EV_NAME_P(temp_ptr) == NULL )
{
free( EV_NAME_P(temp_ptr) ); return 0;
}
memset(EV_NAME_P(temp_ptr), 0, key_len + 1);
memcpy(EV_NAME_P(temp_ptr), key, key_len);
/* Allocate data memory and store it */
EV_VALUE_P(temp_ptr) = val;
/* Set value type */
EV_TYPE_P(temp_ptr) = EXJSON_OBJECT;
/* add to exjson */
ptr = realloc(val_ptr, sizeof(EXJSON_V)*size);
if ( ptr == NULL )
{
free(val_ptr); return 0;
}
memcpy(ptr + E_NUM_P(exjson), temp_ptr, sizeof(EXJSON_V));
free(temp_ptr);
E_NUM_P(exjson)++;
E_DATA_P(exjson) = ptr;
E_TYPE_P(exjson) = EX_OBJECT;
return 1;
}
int add_object_array(EXJSON *exjson, char *key, void *val)
{
EXJSON_V *val_ptr = E_DATA_P(exjson),
*temp_ptr = INIT_EXJSON_V(),
*ptr;
unsigned long size = E_NUM_P(exjson) + 1,
key_len = strlen(key);
/* Allocate key memory and store it. */
EV_NAME_P(temp_ptr) = malloc(sizeof(char) * (key_len + 1));
if ( EV_NAME_P(temp_ptr) == NULL )
{
free( EV_NAME_P(temp_ptr) ); return 0;
}
memset(EV_NAME_P(temp_ptr), 0, key_len + 1);
memcpy(EV_NAME_P(temp_ptr), key, key_len);
/* Allocate data memory and store it */
EV_VALUE_P(temp_ptr) = val;
/* Set value type */
EV_TYPE_P(temp_ptr) = EXJSON_ARRAY;
/* add to exjson */
ptr = realloc(val_ptr, sizeof(EXJSON_V) * size);
if ( ptr == NULL )
{
free( val_ptr ); return 0;
}
memcpy(ptr + E_NUM_P(exjson), temp_ptr, sizeof(EXJSON_V));
free(temp_ptr);
E_NUM_P(exjson)++;
E_DATA_P(exjson) = ptr;
E_TYPE_P(exjson) = EX_OBJECT;
return 1;
}
int add_object_ptr(EXJSON *exjson, char *key, void *val, unsigned char val_type)
{
switch ( val_type )
{
case EXJSON_INT:
return add_object_int(exjson, key, *(long *)val);
case EXJSON_DOUBLE:
return add_object_double(exjson, key, *( double *)val);
case EXJSON_STRING:
return add_object_string(exjson, key, (char *)val);
case EXJSON_OBJECT:
return add_object_object(exjson, key, val);
case EXJSON_ARRAY:
return add_object_array(exjson, key, val);
}
return 0;
}
int add_array_int(EXJSON *exjson, long val)
{
EXJSON_V *val_ptr = E_DATA_P(exjson),
*temp_ptr = INIT_EXJSON_V(),
*ptr;
unsigned long size = E_NUM_P(exjson) + 1;
/* Allocate key memory and store it. */
EV_NAME_P(temp_ptr) = NULL;
/* Allocate data memory and store it */
EV_VALUE_P(temp_ptr) = malloc( sizeof( long) );
if ( EV_VALUE_P(temp_ptr) == NULL )
{
free( EV_VALUE_P(temp_ptr) ); return 0;
}
memcpy(EV_VALUE_P(temp_ptr), &val, sizeof( long));
/* Set value type */
EV_TYPE_P(temp_ptr) = EXJSON_INT;
/* add to exjson */
ptr = realloc(val_ptr, sizeof(EXJSON_V)*size);
if ( ptr == NULL )
{
free(val_ptr); return 0;
}
memcpy(ptr + E_NUM_P(exjson), temp_ptr, sizeof(EXJSON_V));
free(temp_ptr);
E_NUM_P(exjson)++;
E_DATA_P(exjson) = ptr;
E_TYPE_P(exjson) = EX_ARRAY;
return 1;
}
int add_array_double(EXJSON *exjson, double val)
{
EXJSON_V *val_ptr = E_DATA_P(exjson),
*temp_ptr = INIT_EXJSON_V(),
*ptr;
unsigned long size = E_NUM_P(exjson) + 1;
/* Allocate key memory and store it. */
EV_NAME_P(temp_ptr) = NULL;
/* Allocate data memory and store it */
EV_VALUE_P(temp_ptr) = malloc( sizeof( double ) );
if ( EV_VALUE_P(temp_ptr) == NULL )
{
free( EV_VALUE_P(temp_ptr) ); return 0;
}
memcpy(EV_VALUE_P(temp_ptr), &val, sizeof( double ));
/* Set value type */
EV_TYPE_P(temp_ptr) = EXJSON_DOUBLE;
/* add to exjson */
ptr = realloc(val_ptr, sizeof(EXJSON_V)*size);
if ( ptr == NULL )
{
free( val_ptr ); return 0;
}
memcpy(ptr + E_NUM_P(exjson), temp_ptr, sizeof(EXJSON_V));
free(temp_ptr);
E_NUM_P(exjson)++;
E_DATA_P(exjson) = ptr;
E_TYPE_P(exjson) = EX_ARRAY;
return 1;
}
int add_array_string(EXJSON *exjson, char *val)
{
EXJSON_V *val_ptr = E_DATA_P(exjson),
*temp_ptr = INIT_EXJSON_V(),
*ptr;
char *tval = str_slash(val, strlen(val));
unsigned long size = E_NUM_P(exjson) + 1,
val_len = strlen(tval);
/* Allocate key memory and store it. */
EV_NAME_P(temp_ptr) = NULL;
/* Allocate data memory and store it */
EV_VALUE_P(temp_ptr) = malloc( sizeof( char) * ( val_len + 1 ));
if ( EV_VALUE_P(temp_ptr) == NULL )
{
free( EV_VALUE_P(temp_ptr) ); return 0;
}
memset(EV_VALUE_P(temp_ptr), 0, val_len + 1);
memcpy(EV_VALUE_P(temp_ptr), tval, (val_len));
free(tval);
/* Set value type */
EV_TYPE_P(temp_ptr) = EXJSON_STRING;
/* add to exjson */
ptr = realloc(val_ptr, sizeof(EXJSON_V)*size);
if ( ptr == NULL )
{
free(val_ptr); return 0;
}
memcpy(ptr + E_NUM_P(exjson), temp_ptr, sizeof(EXJSON_V));
free(temp_ptr);
E_NUM_P(exjson)++;
E_DATA_P(exjson) = ptr;
E_TYPE_P(exjson) = EX_ARRAY;
return 1;
}
int add_array_object(EXJSON *exjson, void *val)
{
EXJSON_V *val_ptr = E_DATA_P(exjson),
*temp_ptr = INIT_EXJSON_V(),
*ptr;
unsigned long size = E_NUM_P(exjson) + 1;
/* Allocate key memory and store it. */
EV_NAME_P(temp_ptr) = NULL;
/* Allocate data memory and store it */
EV_VALUE_P(temp_ptr) = val;
/* Set value type */
EV_TYPE_P(temp_ptr) = EXJSON_OBJECT;
/* add to exjson */
ptr = realloc(val_ptr, sizeof(EXJSON_V)*size);
if ( ptr == NULL )
{
free(val_ptr); return 0;
}
memcpy(ptr + E_NUM_P(exjson), temp_ptr, sizeof(EXJSON_V));
free(temp_ptr);
E_NUM_P(exjson)++;
E_DATA_P(exjson) = ptr;
E_TYPE_P(exjson) = EX_ARRAY;
return 1;
}
int add_array_array(EXJSON *exjson, void *val)
{
EXJSON_V *val_ptr = E_DATA_P(exjson),
*temp_ptr = INIT_EXJSON_V(),
*ptr;
unsigned long size = E_NUM_P(exjson) + 1;
/* Allocate key memory and store it. */
EV_NAME_P(temp_ptr) = NULL;
/* Allocate data memory and store it */
EV_VALUE_P(temp_ptr) = val;
/* Set value type */
EV_TYPE_P(temp_ptr) = EXJSON_ARRAY;
/* add to exjson */
ptr = realloc(val_ptr, sizeof(EXJSON_V)*size);
if ( ptr == NULL )
{
free(val_ptr); return 0;
}
memcpy(ptr + E_NUM_P(exjson), temp_ptr, sizeof(EXJSON_V));
E_NUM_P(exjson)++;
E_DATA_P(exjson) = ptr;
E_TYPE_P(exjson) = EX_ARRAY;
return 1;
}
int add_array_ptr(EXJSON *exjson, void *val, unsigned char val_type)
{
switch (val_type)
{
case EXJSON_INT:
return add_array_int(exjson, *(long *)val);
case EXJSON_DOUBLE:
return add_array_double(exjson, *(double *)val);
case EXJSON_STRING:
return add_array_string(exjson, (char *)val);
case EXJSON_ARRAY:
return add_array_array(exjson, val);
case EXJSON_OBJECT:
return add_array_object(exjson, val);
}
return 0;
}
EXJSON *
/* Get the value from exjson with key */
exjson_get_array_or_object_from_key(EXJSON *exjson, char *key_name)
{
if ( !exjson ) return NULL;
EXJSON_V *temp = E_DATA_P(exjson);
unsigned long i = 0, num = E_NUM_P(exjson);
if ( E_TYPE_P(exjson) == EX_ARRAY )
{
perror("Object only can be get with string key");
return NULL;
}
for ( ; i < num; i++ )
{
if ( strncmp(key_name, EV_NAME_P(temp), strlen(key_name)) == 0 )
{
return EV_VALUE_P(temp);
}
temp = temp + 1;
}
return NULL;
}
EXJSON *
exjson_get_array_or_object_from_index(EXJSON *exjson, int index)
{
if ( !exjson ) return NULL;
EXJSON_V *temp = E_DATA_P(exjson);
if ( E_TYPE_P(exjson) == EX_OBJECT )
{
perror("Array can only be get by index");
return NULL;
}
if ( E_NUM_P(exjson) < index ) {
perror("Index out of range");
return NULL;
}
temp = temp + index;
return EV_VALUE_P(temp);
}
void *
exjson_get_val_from_key(EXJSON *exjson, char *key)
{
if ( !exjson ) return NULL;
EXJSON_V *temp = E_DATA_P(exjson);
unsigned long i = 0, num = E_NUM_P(exjson);
for ( ; i < num; i++ )
{
if (strncmp(EV_NAME_P(temp), key, strlen(key)) == 0)
{
return EV_VALUE_P(temp);
}
temp = temp + 1;
}
return NULL;
}
void *
exjson_get_val_from_index(EXJSON *exjson, int index)
{
if ( !exjson ) return NULL;
EXJSON_V *temp = E_DATA_P(exjson);
unsigned long num = E_NUM_P(exjson);
if ( num <= index ) {
perror("Index out of range");
return NULL;
}
temp = temp + index;
return EV_VALUE_P(temp);
}
void
/* Print EXJSON not fully ok, as the format has some mistask.
* but encoding & decoding are perfectly ok. */
print_exjson(EXJSON *exjson, int _num)
{
if ( !exjson ) return ;
EXJSON_V *temp = E_DATA_P(exjson);
int j = 0;
unsigned long i = 0, num = E_NUM_P(exjson);
switch( E_TYPE_P(exjson) )
{
case EX_OBJECT:
printf("{\n"); break;
case EX_ARRAY:
printf("[\n"); break;
}
for ( ; i < num; i++ )
{
if ( EV_NAME_P(temp) ) {
for (j = 0; j <= _num; ++j) {
printf("\t");
}
printf("%s: ", EV_NAME_P(temp));
}
switch (EV_TYPE_P(temp))
{
case EXJSON_INT:
if ( EV_NAME_P(temp) == NULL )
for (j = 0; j <= _num; ++j) {
printf("\t");
}
printf("%ld\n", *(long *)EV_VALUE_P(temp));
break;
case EXJSON_DOUBLE:
if ( EV_NAME_P(temp) == NULL )
for (j = 0; j <= _num; ++j) {
printf("\t");
}
printf("%f\n", *(double *)EV_VALUE_P(temp));
break;
case EXJSON_STRING:
if ( EV_NAME_P(temp) == NULL )
for (j = 0; j <= _num; ++j) {
printf("\t");
}
printf("%s\n", (char *)EV_VALUE_P(temp));
break;
case EXJSON_ARRAY:
case EXJSON_OBJECT:
print_exjson(EV_VALUE_P(temp), _num + 1);
}
temp = temp + 1;
}
for (j = 0; j < _num; ++j) {
printf("\t");
}
switch( E_TYPE_P(exjson) )
{
case EX_OBJECT:
printf("}\n"); break;
case EX_ARRAY:
printf("]\n"); break;
}
}
static inline char *
/* for example:
* char *str = malloc(sizeof(1000));
* int _dest_space = 1000, _used_num = 0;
* _exjson_strcat_(str, "hello", &_dest_space, &_used_num);
* _exjson_strcat_(str, "world", &_dest_space, &_used_num); */
_exjson_strcat_(char *dest, char *source, int *_dest_space, int *_used_num)
{
int _time_incr_ = 512;
char *_temp_ptr = NULL;
unsigned long _source_len = strlen(source);
if ( *_used_num > ( *_dest_space - 1 ) )
{
_temp_ptr = realloc(dest, sizeof(char) * (*_dest_space + _time_incr_));
if ( _temp_ptr == NULL )
{
free(dest); return NULL;
}
assert(_temp_ptr != NULL);
dest = _temp_ptr;
*_dest_space+= _time_incr_;
}
/* if the source string is not enough to store */
if ( _source_len > (*_dest_space - *_used_num) )
{
if ( _source_len > _time_incr_ )
{
_temp_ptr = realloc(dest, sizeof(char) * (*_dest_space + _source_len + 1));
if ( _temp_ptr == NULL )
{
free(dest); return NULL;
}
assert(_temp_ptr != NULL);
dest = _temp_ptr;
*_dest_space+= _source_len + 1;
}
else
{
_temp_ptr = realloc(dest, sizeof(char) * (*_dest_space + _time_incr_));
if ( _temp_ptr == NULL )
{
free(dest); return NULL;
}
assert(_temp_ptr != NULL);
dest = _temp_ptr;
*_dest_space+= _time_incr_;
}
}
/* Space is enough */
strncat(dest, source, _source_len);
*_used_num += _source_len;
return dest;
}
/* Output the EXJSON structure to the JSON string
* NOTE that: free the returned string after used
* if not memory leak will occurred! */
char *encode_json(EXJSON *exjson)
{
if ( !exjson ) return NULL;
EXJSON_V *temp = E_DATA_P(exjson);
int _sum = 1, _used_num = 0;
unsigned long i = 0, num = E_NUM_P(exjson);
char *inner_str = NULL;
char *_result_str = malloc(sizeof(char) * (_sum + 1) );
if ( _result_str == NULL )
{
free(_result_str); return NULL;
}
assert(_result_str != NULL);
memset(_result_str, 0, sizeof(char) * ( _sum + 1 ) );
switch( E_TYPE_P(exjson) )
{
case EX_OBJECT:
_result_str = _exjson_strcat_(_result_str, "{", &_sum, &_used_num); break;
case EX_ARRAY:
_result_str = _exjson_strcat_(_result_str, "[", &_sum, &_used_num); break;
}
for ( ; i < num; i++ )
{
if ( EV_NAME_P(temp) ) {
_result_str = _exjson_strcat_(_result_str, "\"", &_sum, &_used_num);
_result_str = _exjson_strcat_(_result_str, EV_NAME_P(temp), &_sum, &_used_num);
_result_str = _exjson_strcat_(_result_str, "\":", &_sum, &_used_num);
}
char _temp[20] = {0};
switch (EV_TYPE_P(temp))
{
case EXJSON_INT:
memset(_temp, 0, 20);
sprintf(_temp, "%ld", *(long *)EV_VALUE_P(temp));
_result_str = _exjson_strcat_(_result_str, _temp, &_sum, &_used_num);
break;
case EXJSON_DOUBLE:
memset(_temp, 0, 20);
sprintf(_temp, "%f", *(double *)EV_VALUE_P(temp));
_result_str = _exjson_strcat_(_result_str, _temp, &_sum, &_used_num);
break;
case EXJSON_STRING:
_result_str = _exjson_strcat_(_result_str, "\"", &_sum, &_used_num);
_result_str = _exjson_strcat_(_result_str, EV_VALUE_P(temp), &_sum, &_used_num);
_result_str = _exjson_strcat_(_result_str, "\"", &_sum, &_used_num);
break;
case EXJSON_ARRAY:
case EXJSON_OBJECT:
inner_str = encode_json(EV_VALUE_P(temp));
_result_str = _exjson_strcat_(_result_str, inner_str, &_sum, &_used_num);
free(inner_str);
break;
}
if ( i < num - 1 ) {
_result_str = _exjson_strcat_(_result_str, ",", &_sum, &_used_num);
}
temp = temp + 1;
}
switch( E_TYPE_P(exjson) ) {
case EX_OBJECT:
_result_str = _exjson_strcat_(_result_str, "}", &_sum, &_used_num);
break;
case EX_ARRAY:
_result_str = _exjson_strcat_(_result_str, "]", &_sum, &_used_num);
break;
}
return _result_str;
}
void destroy_exjson(EXJSON *exjson)
{
if ( !exjson ) return ;
unsigned long i;
static int _num = 0;
for ( i = 0; i < E_NUM_P(exjson); ++i )
{
EXJSON_V *temp = E_DATA_P(exjson) + i;
switch ( EV_TYPE_P(temp) )
{
case EXJSON_OBJECT:
case EXJSON_ARRAY:
free( EV_NAME_P(temp) );
++_num;
destroy_exjson(EV_VALUE_P(temp));
--_num;
free( EV_VALUE_P(temp) );
break;
default:
free( EV_NAME_P(temp) );
free( EV_VALUE_P(temp) );
break;
}
if ( i == E_NUM_P(exjson) - 1 ) {
free( E_DATA_P(exjson) );
}
}
if ( _num == 0 ) free(exjson);
}
C
1
https://gitee.com/gaat_yin/Exjson.git
git@gitee.com:gaat_yin/Exjson.git
gaat_yin
Exjson
Exjson
master

搜索帮助