From 25e37445719cf3f4914d2d8196b858db3cf6de6d Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Mon, 1 Jun 2020 07:32:28 +0000 Subject: [PATCH] added more json writing macros for convenience --- mio/bin/t03.c | 38 ++++++------- mio/lib/json.c | 129 ++------------------------------------------- mio/lib/mio-json.h | 28 ++++++++-- 3 files changed, 47 insertions(+), 148 deletions(-) diff --git a/mio/bin/t03.c b/mio/bin/t03.c index 0e2faa2..3ebe603 100644 --- a/mio/bin/t03.c +++ b/mio/bin/t03.c @@ -75,7 +75,6 @@ static int on_json_inst (mio_json_t* json, mio_json_inst_t inst, mio_oow_t level break; case MIO_JSON_INST_NUMBER: - case MIO_JSON_INST_CHARACTER: mio_logbfmt (mio, MIO_LOG_STDOUT, "%.*js\n", str->len, str->ptr); break; @@ -149,28 +148,29 @@ int main (int argc, char* argv[]) mio_jsonwr_setwritecb (jsonwr, write_json_element); - mio_jsonwr_write (jsonwr, MIO_JSON_INST_START_ARRAY, 0, MIO_NULL, 0); - mio_jsonwr_write (jsonwr, MIO_JSON_INST_STRING, 0, "hello", 5); - mio_jsonwr_write (jsonwr, MIO_JSON_INST_STRING, 0, "world", 5); + mio_jsonwr_startarray (jsonwr); + + mio_jsonwr_writestringwithbchars (jsonwr, "hello", 5); + mio_jsonwr_writestringwithbchars (jsonwr, "world", 5); - mio_jsonwr_write (jsonwr, MIO_JSON_INST_START_DIC, 0, MIO_NULL, 0); - mio_jsonwr_write (jsonwr, MIO_JSON_INST_KEY, 0, "abc", 3); - mio_jsonwr_write (jsonwr, MIO_JSON_INST_STRING, 0, "computer", 8); - mio_jsonwr_write (jsonwr, MIO_JSON_INST_KEY, 0, "k", 1); - mio_jsonwr_write (jsonwr, MIO_JSON_INST_STRING, 0, "play nice", 9); - mio_jsonwr_write (jsonwr, MIO_JSON_INST_KEY, 1, ddd, 4); - mio_jsonwr_write (jsonwr, MIO_JSON_INST_STRING, 1, ddv, 5); - mio_jsonwr_write (jsonwr, MIO_JSON_INST_END_DIC, 0, MIO_NULL, 0); + mio_jsonwr_startdic (jsonwr); + mio_jsonwr_writekeywithbchars (jsonwr, "abc", 3); + mio_jsonwr_writestringwithbchars (jsonwr, "computer", 8); + mio_jsonwr_writekeywithbchars (jsonwr, "k", 1); + mio_jsonwr_writestringwithbchars (jsonwr, "play nice", 9); + mio_jsonwr_writekeywithuchars (jsonwr, ddd, 4); + mio_jsonwr_writestringwithuchars (jsonwr, ddv, 5); + mio_jsonwr_enddic (jsonwr); - mio_jsonwr_write (jsonwr, MIO_JSON_INST_STRING, 0, "tyler", 5); + mio_jsonwr_writestringwithbchars (jsonwr, "tyler", 5); - mio_jsonwr_write (jsonwr, MIO_JSON_INST_START_ARRAY, 0, MIO_NULL, 0); - mio_jsonwr_write (jsonwr, MIO_JSON_INST_STRING, 0, "airplain", 8); - mio_jsonwr_write (jsonwr, MIO_JSON_INST_STRING, 0, "gro\0wn\nup", 9); - mio_jsonwr_write (jsonwr, MIO_JSON_INST_TRUE, 0, MIO_NULL, 0); - mio_jsonwr_write (jsonwr, MIO_JSON_INST_END_ARRAY, 0, MIO_NULL, 0); + mio_jsonwr_startarray (jsonwr); + mio_jsonwr_writestringwithbchars (jsonwr, "airplain", 8); + mio_jsonwr_writestringwithbchars (jsonwr, "gro\0wn\nup", 9); + mio_jsonwr_writetrue (jsonwr); + mio_jsonwr_endarray (jsonwr); - mio_jsonwr_write (jsonwr, MIO_JSON_INST_END_ARRAY, 0, MIO_NULL, 0); + mio_jsonwr_endarray (jsonwr); mio_jsonwr_close (jsonwr); } diff --git a/mio/lib/json.c b/mio/lib/json.c index 54dcfe4..aee8a9d 100644 --- a/mio/lib/json.c +++ b/mio/lib/json.c @@ -272,114 +272,6 @@ static int handle_string_value_char (mio_json_t* json, mio_ooci_t c) return ret; } -static int handle_character_value_char (mio_json_t* json, mio_ooci_t c) -{ - /* The real JSON dones't support character literal. this is MIO's own extension. */ - int ret = 1; - - if (json->state_stack->u.cv.escaped == 3) - { - if (c >= '0' && c <= '7') - { - json->state_stack->u.cv.acc = json->state_stack->u.cv.acc * 8 + c - '0'; - json->state_stack->u.cv.digit_count++; - if (json->state_stack->u.cv.digit_count >= json->state_stack->u.cv.escaped) goto add_cv_acc; - } - else - { - ret = 0; - goto add_cv_acc; - } - } - if (json->state_stack->u.cv.escaped >= 2) - { - if (c >= '0' && c <= '9') - { - json->state_stack->u.cv.acc = json->state_stack->u.cv.acc * 16 + c - '0'; - json->state_stack->u.cv.digit_count++; - if (json->state_stack->u.cv.digit_count >= json->state_stack->u.cv.escaped) goto add_cv_acc; - } - else if (c >= 'a' && c <= 'f') - { - json->state_stack->u.cv.acc = json->state_stack->u.cv.acc * 16 + c - 'a' + 10; - json->state_stack->u.cv.digit_count++; - if (json->state_stack->u.cv.digit_count >= json->state_stack->u.cv.escaped) goto add_cv_acc; - } - else if (c >= 'A' && c <= 'F') - { - json->state_stack->u.cv.acc = json->state_stack->u.cv.acc * 16 + c - 'A' + 10; - json->state_stack->u.cv.digit_count++; - if (json->state_stack->u.cv.digit_count >= json->state_stack->u.cv.escaped) goto add_cv_acc; - } - else - { - ret = 0; - add_cv_acc: - if (add_char_to_token(json, json->state_stack->u.cv.acc) <= -1) return -1; - json->state_stack->u.cv.escaped = 0; - } - } - else if (json->state_stack->u.cv.escaped == 1) - { - if (c >= '0' && c <= '8') - { - json->state_stack->u.cv.escaped = 3; - json->state_stack->u.cv.digit_count = 0; - json->state_stack->u.cv.acc = c - '0'; - } - else if (c == 'x') - { - json->state_stack->u.cv.escaped = 2; - json->state_stack->u.cv.digit_count = 0; - json->state_stack->u.cv.acc = 0; - } - else if (c == 'u') - { - json->state_stack->u.cv.escaped = 4; - json->state_stack->u.cv.digit_count = 0; - json->state_stack->u.cv.acc = 0; - } - else if (c == 'U') - { - json->state_stack->u.cv.escaped = 8; - json->state_stack->u.cv.digit_count = 0; - json->state_stack->u.cv.acc = 0; - } - else - { - json->state_stack->u.cv.escaped = 0; - if (add_char_to_token(json, unescape(c)) <= -1) return -1; - } - } - else if (c == '\\') - { - json->state_stack->u.cv.escaped = 1; - } - else if (c == '\'') - { - pop_read_state (json); - - if (json->tok.len < 1) - { - mio_seterrbfmt (json->mio, MIO_EINVAL, "no character in a character literal"); - return -1; - } - if (invoke_data_inst(json, MIO_JSON_INST_CHARACTER) <= -1) return -1; - } - else - { - if (add_char_to_token(json, c) <= -1) return -1; - } - - if (json->tok.len > 1) - { - mio_seterrbfmt (json->mio, MIO_EINVAL, "too many characters in a character literal - %.*js", json->tok.len, json->tok.ptr); - return -1; - } - - return ret; -} - static int handle_numeric_value_char (mio_json_t* json, mio_ooci_t c) { if (mio_is_ooch_digit(c) || (json->tok.len == 0 && (c == '+' || c == '-'))) @@ -505,12 +397,6 @@ static int handle_char_in_array (mio_json_t* json, mio_ooci_t c) clear_token (json); return 1; } - else if (c == '\'') - { - if (push_read_state(json, MIO_JSON_STATE_IN_CHARACTER_VALUE) <= -1) return -1; - clear_token (json); - return 1; - } /* TOOD: else if (c == '#') MIO radixed number */ else if (mio_is_ooch_digit(c) || c == '+' || c == '-') @@ -602,12 +488,6 @@ static int handle_char_in_dic (mio_json_t* json, mio_ooci_t c) clear_token (json); return 1; } - else if (c == '\'') - { - if (push_read_state(json, MIO_JSON_STATE_IN_CHARACTER_VALUE) <= -1) return -1; - clear_token (json); - return 1; - } /* TOOD: else if (c == '#') MIO radixed number */ else if (mio_is_ooch_digit(c) || c == '+' || c == '-') @@ -689,10 +569,6 @@ start_over: case MIO_JSON_STATE_IN_STRING_VALUE: x = handle_string_value_char(json, c); break; - - case MIO_JSON_STATE_IN_CHARACTER_VALUE: - x = handle_character_value_char(json, c); - break; case MIO_JSON_STATE_IN_NUMERIC_VALUE: x = handle_numeric_value_char(json, c); @@ -1036,6 +912,7 @@ static int write_bytes_noesc (mio_jsonwr_t* jsonwr, const mio_bch_t* dptr, mio_o MIO_MEMCPY (&jsonwr->wbuf[jsonwr->wbuf_len], dptr, rem); jsonwr->wbuf_len += rem; + dptr += rem; dlen -= rem; if (flush_wbuf(jsonwr) <= -1) return -1; } @@ -1152,7 +1029,10 @@ int mio_jsonwr_write (mio_jsonwr_t* jsonwr, mio_json_inst_t inst, int is_uchars, { mio_jsonwr_state_node_t* sn = jsonwr->state_stack; +/* ============================= */ jsonwr->pretty = 1; +/* ============================= */ + switch (inst) { case MIO_JSON_INST_START_ARRAY: @@ -1242,7 +1122,6 @@ jsonwr->pretty = 1; break; case MIO_JSON_INST_NUMBER: - case MIO_JSON_INST_CHARACTER: PREACTION_FOR_VLAUE (jsonwr, sn); if (is_uchars) WRITE_UCHARS (jsonwr, 0, dptr, dlen); diff --git a/mio/lib/mio-json.h b/mio/lib/mio-json.h index d61e09f..e5f3e9a 100644 --- a/mio/lib/mio-json.h +++ b/mio/lib/mio-json.h @@ -44,8 +44,7 @@ enum mio_json_state_t MIO_JSON_STATE_IN_WORD_VALUE, MIO_JSON_STATE_IN_NUMERIC_VALUE, - MIO_JSON_STATE_IN_STRING_VALUE, - MIO_JSON_STATE_IN_CHARACTER_VALUE + MIO_JSON_STATE_IN_STRING_VALUE }; typedef enum mio_json_state_t mio_json_state_t; @@ -59,7 +58,6 @@ enum mio_json_inst_t MIO_JSON_INST_KEY, - MIO_JSON_INST_CHARACTER, /* there is no such element as character in real JSON */ MIO_JSON_INST_STRING, MIO_JSON_INST_NUMBER, MIO_JSON_INST_NIL, @@ -158,7 +156,7 @@ struct mio_jsonwr_t mio_jsonwr_state_node_t* state_stack; int pretty; - mio_bch_t wbuf[4096]; + mio_bch_t wbuf[8192]; mio_oow_t wbuf_len; }; @@ -278,6 +276,28 @@ MIO_EXPORT int mio_jsonwr_write ( mio_oow_t dlen ); + + +#define mio_jsonwr_startarray(jsonwr) mio_jsonwr_write(jsonwr, MIO_JSON_INST_START_ARRAY, 0, MIO_NULL, 0) +#define mio_jsonwr_endarray(jsonwr) mio_jsonwr_write(jsonwr, MIO_JSON_INST_END_ARRAY, 0, MIO_NULL, 0) + +#define mio_jsonwr_startdic(jsonwr) mio_jsonwr_write(jsonwr, MIO_JSON_INST_START_DIC, 0, MIO_NULL, 0) +#define mio_jsonwr_enddic(jsonwr) mio_jsonwr_write(jsonwr, MIO_JSON_INST_END_DIC, 0, MIO_NULL, 0) + +#define mio_jsonwr_writenil(jsonwr) mio_jsonwr_write(jsonwr, MIO_JSON_INST_NIL, 0, MIO_NULL, 0) +#define mio_jsonwr_writetrue(jsonwr) mio_jsonwr_write(jsonwr, MIO_JSON_INST_TRUE, 0, MIO_NULL, 0) +#define mio_jsonwr_writefalse(jsonwr) mio_jsonwr_write(jsonwr, MIO_JSON_INST_FALSE, 0, MIO_NULL, 0) + +#define mio_jsonwr_writekeywithuchars(jsonwr,dptr,dlen) mio_jsonwr_write(jsonwr, MIO_JSON_INST_KEY, 1, dptr, dlen) +#define mio_jsonwr_writekeywithbchars(jsonwr,dptr,dlen) mio_jsonwr_write(jsonwr, MIO_JSON_INST_KEY, 0, dptr, dlen) + +#define mio_jsonwr_writenumberwithuchars(jsonwr,dptr,dlen) mio_jsonwr_write(jsonwr, MIO_JSON_INST_NUMBER, 1, dptr, dlen) +#define mio_jsonwr_writenumberwithbchars(jsonwr,dptr,dlen) mio_jsonwr_write(jsonwr, MIO_JSON_INST_NUMBER, 0, dptr, dlen) + +#define mio_jsonwr_writestringwithuchars(jsonwr,dptr,dlen) mio_jsonwr_write(jsonwr, MIO_JSON_INST_STRING, 1, dptr, dlen) +#define mio_jsonwr_writestringwithbchars(jsonwr,dptr,dlen) mio_jsonwr_write(jsonwr, MIO_JSON_INST_STRING, 0, dptr, dlen) + + #if defined(__cplusplus) } #endif