tests/test_json_parser.c OK.
parent
5f34bc872b
commit
009d33480f
|
|
@ -308,8 +308,9 @@ static inline void json_quote_string(fc_json_context_t
|
|||
*buff = p;
|
||||
}
|
||||
|
||||
const BufferInfo *fc_encode_json_array(fc_json_context_t *context,
|
||||
const string_t *elements, const int count)
|
||||
int fc_encode_json_array_ex(fc_json_context_t *context,
|
||||
const string_t *elements, const int count,
|
||||
BufferInfo *buffer)
|
||||
{
|
||||
const string_t *el;
|
||||
const string_t *end;
|
||||
|
|
@ -322,17 +323,17 @@ const BufferInfo *fc_encode_json_array(fc_json_context_t *context,
|
|||
expect_size += 6 * el->len + 3;
|
||||
}
|
||||
|
||||
if (context->output.alloc_size < expect_size) {
|
||||
if ((context->error_no=fc_realloc_buffer(&context->output,
|
||||
context->init_buff_size, expect_size)) != 0)
|
||||
if (buffer->alloc_size < expect_size) {
|
||||
if ((context->error_no=fc_realloc_buffer(buffer, context->
|
||||
init_buff_size, expect_size)) != 0)
|
||||
{
|
||||
context->error_info.len = snprintf(context->error_info.str,
|
||||
context->error_size, "realloc buffer fail");
|
||||
return NULL;
|
||||
return context->error_no;
|
||||
}
|
||||
}
|
||||
|
||||
p = context->output.buff;
|
||||
p = buffer->buff;
|
||||
*p++ = '[';
|
||||
for (el=elements; el<end; el++) {
|
||||
if (el > elements) {
|
||||
|
|
@ -344,12 +345,13 @@ const BufferInfo *fc_encode_json_array(fc_json_context_t *context,
|
|||
|
||||
*p++ = ']';
|
||||
*p = '\0';
|
||||
context->output.length = p - context->output.buff;
|
||||
return &context->output;
|
||||
buffer->length = p - buffer->buff;
|
||||
return 0;
|
||||
}
|
||||
|
||||
const BufferInfo *fc_encode_json_map(fc_json_context_t *context,
|
||||
const key_value_pair_t *elements, const int count)
|
||||
int fc_encode_json_map_ex(fc_json_context_t *context,
|
||||
const key_value_pair_t *elements, const int count,
|
||||
BufferInfo *buffer)
|
||||
{
|
||||
const key_value_pair_t *pair;
|
||||
const key_value_pair_t *end;
|
||||
|
|
@ -362,17 +364,17 @@ const BufferInfo *fc_encode_json_map(fc_json_context_t *context,
|
|||
expect_size += 6 * (pair->key.len + pair->value.len) + 5;
|
||||
}
|
||||
|
||||
if (context->output.alloc_size < expect_size) {
|
||||
if ((context->error_no=fc_realloc_buffer(&context->output,
|
||||
context->init_buff_size, expect_size)) != 0)
|
||||
if (buffer->alloc_size < expect_size) {
|
||||
if ((context->error_no=fc_realloc_buffer(buffer, context->
|
||||
init_buff_size, expect_size)) != 0)
|
||||
{
|
||||
context->error_info.len = snprintf(context->error_info.str,
|
||||
context->error_size, "realloc buffer fail");
|
||||
return NULL;
|
||||
return context->error_no;
|
||||
}
|
||||
}
|
||||
|
||||
p = context->output.buff;
|
||||
p = buffer->buff;
|
||||
*p++ = '{';
|
||||
for (pair=elements; pair<end; pair++) {
|
||||
if (pair > elements) {
|
||||
|
|
@ -386,8 +388,8 @@ const BufferInfo *fc_encode_json_map(fc_json_context_t *context,
|
|||
|
||||
*p++ = '}';
|
||||
*p = '\0';
|
||||
context->output.length = p - context->output.buff;
|
||||
return &context->output;
|
||||
buffer->length = p - buffer->buff;
|
||||
return 0;
|
||||
}
|
||||
|
||||
const fc_json_array_t *fc_decode_json_array(fc_json_context_t
|
||||
|
|
|
|||
|
|
@ -163,11 +163,37 @@ extern "C" {
|
|||
|
||||
int fc_detect_json_type(const string_t *input);
|
||||
|
||||
const BufferInfo *fc_encode_json_array(fc_json_context_t *context,
|
||||
const string_t *elements, const int count);
|
||||
int fc_encode_json_array_ex(fc_json_context_t *context,
|
||||
const string_t *elements, const int count,
|
||||
BufferInfo *buffer);
|
||||
|
||||
const BufferInfo *fc_encode_json_map(fc_json_context_t *context,
|
||||
const key_value_pair_t *elements, const int count);
|
||||
int fc_encode_json_map_ex(fc_json_context_t *context,
|
||||
const key_value_pair_t *elements, const int count,
|
||||
BufferInfo *buffer);
|
||||
|
||||
static inline const BufferInfo *fc_encode_json_array(fc_json_context_t
|
||||
*context, const string_t *elements, const int count)
|
||||
{
|
||||
if (fc_encode_json_array_ex(context, elements, count,
|
||||
&context->output) == 0)
|
||||
{
|
||||
return &context->output;
|
||||
} else {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
static inline const BufferInfo *fc_encode_json_map(fc_json_context_t
|
||||
*context, const key_value_pair_t *elements, const int count)
|
||||
{
|
||||
if (fc_encode_json_map_ex(context, elements, count,
|
||||
&context->output) == 0)
|
||||
{
|
||||
return &context->output;
|
||||
} else {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
const fc_json_array_t *fc_decode_json_array(fc_json_context_t
|
||||
*context, const string_t *input);
|
||||
|
|
|
|||
|
|
@ -30,9 +30,10 @@ int main(int argc, char *argv[])
|
|||
{
|
||||
int result;
|
||||
int json_type;
|
||||
fc_json_context_t json_ctx;
|
||||
char error_info[256];
|
||||
string_t input;
|
||||
string_t output;
|
||||
BufferInfo output;
|
||||
|
||||
if (argc < 2) {
|
||||
fprintf(stderr, "Usage: %s <json_string | json_array | json_map>\n",
|
||||
|
|
@ -42,52 +43,52 @@ int main(int argc, char *argv[])
|
|||
|
||||
log_init();
|
||||
|
||||
input.str = argv[1];
|
||||
input.len = strlen(input.str);
|
||||
json_type = detect_json_type(&input);
|
||||
if (json_type == FC_JSON_TYPE_ARRAY) {
|
||||
json_array_t array;
|
||||
|
||||
if ((result=decode_json_array(&input, &array, error_info,
|
||||
sizeof(error_info))) != 0)
|
||||
if ((result=fc_init_json_context_ex(&json_ctx, 1024,
|
||||
error_info, sizeof(error_info))) != 0)
|
||||
{
|
||||
fprintf(stderr, "decode json array fail, %s\n", error_info);
|
||||
return result;
|
||||
}
|
||||
memset(&output, 0, sizeof(output));
|
||||
|
||||
if ((result=encode_json_array(&array, &output,
|
||||
error_info, sizeof(error_info))) != 0)
|
||||
input.str = argv[1];
|
||||
input.len = strlen(input.str);
|
||||
json_type = fc_detect_json_type(&input);
|
||||
if (json_type == FC_JSON_TYPE_ARRAY) {
|
||||
const fc_json_array_t *array;
|
||||
|
||||
if ((array=fc_decode_json_array(&json_ctx, &input)) == NULL) {
|
||||
fprintf(stderr, "decode json array fail, %s\n", error_info);
|
||||
return fc_json_parser_get_error_no(&json_ctx);
|
||||
}
|
||||
|
||||
if ((result=fc_encode_json_array_ex(&json_ctx, array->elements,
|
||||
array->count, &output)) != 0)
|
||||
{
|
||||
fprintf(stderr, "encode json array fail, %s\n", error_info);
|
||||
return result;
|
||||
}
|
||||
|
||||
printf("%s\n", output.str);
|
||||
free_json_string(&output);
|
||||
free_json_array(&array);
|
||||
printf("%.*s\n", output.length, output.buff);
|
||||
} else if (json_type == FC_JSON_TYPE_MAP) {
|
||||
json_map_t map;
|
||||
const fc_json_map_t *map;
|
||||
|
||||
if ((result=decode_json_map(&input, &map, error_info,
|
||||
sizeof(error_info))) != 0)
|
||||
{
|
||||
if ((map=fc_decode_json_map(&json_ctx, &input)) == NULL) {
|
||||
fprintf(stderr, "decode json map fail, %s\n", error_info);
|
||||
return result;
|
||||
return fc_json_parser_get_error_no(&json_ctx);
|
||||
}
|
||||
|
||||
if ((result=encode_json_map(&map, &output,
|
||||
error_info, sizeof(error_info))) != 0)
|
||||
if ((result=fc_encode_json_map_ex(&json_ctx, map->elements,
|
||||
map->count, &output)) != 0)
|
||||
{
|
||||
fprintf(stderr, "encode json map fail, %s\n", error_info);
|
||||
return result;
|
||||
}
|
||||
|
||||
printf("%s\n", output.str);
|
||||
free_json_string(&output);
|
||||
free_json_map(&map);
|
||||
printf("%.*s\n", output.length, output.buff);
|
||||
} else {
|
||||
fprintf(stderr, "string\n");
|
||||
}
|
||||
|
||||
fc_destroy_json_context(&json_ctx);
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue