fixed a json reader to accept a non-array/object at the start level

This commit is contained in:
hyung-hwan 2021-07-14 03:22:51 +00:00
parent a7cbda7208
commit d6b196fbf1
2 changed files with 51 additions and 26 deletions

View File

@ -8,7 +8,7 @@ static int on_json_inst (mio_json_t* json, mio_json_inst_t inst, mio_oow_t level
{ {
mio_t* mio = mio_json_getmio(json); mio_t* mio = mio_json_getmio(json);
mio_oow_t i; mio_oow_t i;
int* pending = (int*)ctx;
switch (inst) switch (inst)
{ {
@ -19,12 +19,14 @@ static int on_json_inst (mio_json_t* json, mio_json_inst_t inst, mio_oow_t level
for (i = 0; i < level; i++) mio_logbfmt (mio, MIO_LOG_STDOUT, "\t"); for (i = 0; i < level; i++) mio_logbfmt (mio, MIO_LOG_STDOUT, "\t");
} }
mio_logbfmt (mio, MIO_LOG_STDOUT, "[\n"); mio_logbfmt (mio, MIO_LOG_STDOUT, "[\n");
(*pending)++;
break; break;
case MIO_JSON_INST_END_ARRAY: case MIO_JSON_INST_END_ARRAY:
mio_logbfmt (mio, MIO_LOG_STDOUT, "\n"); mio_logbfmt (mio, MIO_LOG_STDOUT, "\n");
for (i = 0; i < level; i++) mio_logbfmt (mio, MIO_LOG_STDOUT, "\t"); for (i = 0; i < level; i++) mio_logbfmt (mio, MIO_LOG_STDOUT, "\t");
mio_logbfmt (mio, MIO_LOG_STDOUT, "]"); mio_logbfmt (mio, MIO_LOG_STDOUT, "]");
(*pending)--;
break; break;
case MIO_JSON_INST_START_OBJECT: case MIO_JSON_INST_START_OBJECT:
@ -34,12 +36,14 @@ static int on_json_inst (mio_json_t* json, mio_json_inst_t inst, mio_oow_t level
for (i = 0; i < level; i++) mio_logbfmt (mio, MIO_LOG_STDOUT, "\t"); for (i = 0; i < level; i++) mio_logbfmt (mio, MIO_LOG_STDOUT, "\t");
} }
mio_logbfmt (mio, MIO_LOG_STDOUT, "{\n"); mio_logbfmt (mio, MIO_LOG_STDOUT, "{\n");
(*pending)++;
break; break;
case MIO_JSON_INST_END_OBJECT: case MIO_JSON_INST_END_OBJECT:
mio_logbfmt (mio, MIO_LOG_STDOUT, "\n"); mio_logbfmt (mio, MIO_LOG_STDOUT, "\n");
for (i = 0; i < level; i++) mio_logbfmt (mio, MIO_LOG_STDOUT, "\t"); for (i = 0; i < level; i++) mio_logbfmt (mio, MIO_LOG_STDOUT, "\t");
mio_logbfmt (mio, MIO_LOG_STDOUT, "}"); mio_logbfmt (mio, MIO_LOG_STDOUT, "}");
(*pending)--;
break; break;
case MIO_JSON_INST_KEY: case MIO_JSON_INST_KEY:
@ -123,10 +127,11 @@ int main (int argc, char* argv[])
char buf[128]; char buf[128];
mio_oow_t rem; mio_oow_t rem;
size_t size; size_t size;
int pending = 0;
json = mio_json_open(mio, 0); json = mio_json_open(mio, 0);
mio_json_setinstcb (json, on_json_inst, MIO_NULL); mio_json_setinstcb (json, on_json_inst, &pending);
rem = 0; rem = 0;
while (!feof(stdin) || rem > 0) while (!feof(stdin) || rem > 0)
@ -152,7 +157,8 @@ int main (int argc, char* argv[])
if (x > 0) if (x > 0)
{ {
/* document completed */ /* document completed.
* if only whitespaces are given, x is still greater 0. */
mio_logbfmt (mio, MIO_LOG_STDOUT, "\n-----------------------------------\n"); mio_logbfmt (mio, MIO_LOG_STDOUT, "\n-----------------------------------\n");
} }
@ -161,6 +167,7 @@ int main (int argc, char* argv[])
} }
mio_logbfmt (mio, MIO_LOG_STDOUT, "\n"); mio_logbfmt (mio, MIO_LOG_STDOUT, "\n");
if (pending) mio_logbfmt (mio, MIO_LOG_STDOUT, "**** ERROR - incomplete ****\n");
mio_json_close (json); mio_json_close (json);
} }

View File

@ -418,7 +418,33 @@ static int handle_word_value_char (mio_json_t* json, mio_ooci_t c)
static int handle_start_char (mio_json_t* json, mio_ooci_t c) static int handle_start_char (mio_json_t* json, mio_ooci_t c)
{ {
if (c == '[') if (mio_is_ooch_space(c))
{
/* do nothing */
return 1;
}
else if (c == '\"')
{
if (push_read_state(json, MIO_JSON_STATE_IN_STRING_VALUE) <= -1) return -1;
clear_token (json);
return 1; /* the quote dosn't form a string. so no start-over */
}
/* TOOD: else if (c == '#') MIO radixed number
*/
else if (mio_is_ooch_digit(c) || c == '+' || c == '-')
{
if (push_read_state(json, MIO_JSON_STATE_IN_NUMERIC_VALUE) <= -1) return -1;
clear_token (json);
json->state_stack->u.nv.progress = 0;
return 0; /* start over to process c under the new state */
}
else if (mio_is_ooch_alpha(c))
{
if (push_read_state(json, MIO_JSON_STATE_IN_WORD_VALUE) <= -1) return -1;
clear_token (json);
return 0; /* start over to process c under the new state */
}
else if (c == '[')
{ {
if (invoke_data_inst(json, MIO_JSON_INST_START_ARRAY) <= -1) return -1; if (invoke_data_inst(json, MIO_JSON_INST_START_ARRAY) <= -1) return -1;
return 1; return 1;
@ -428,24 +454,21 @@ static int handle_start_char (mio_json_t* json, mio_ooci_t c)
if (invoke_data_inst(json, MIO_JSON_INST_START_OBJECT) <= -1) return -1; if (invoke_data_inst(json, MIO_JSON_INST_START_OBJECT) <= -1) return -1;
return 1; return 1;
} }
#if 0
/* this check is not needed for screening in feed_json_data() */
else if (mio_is_ooch_space(c))
{
/* do nothing */
return 1;
}
#endif
else else
{ {
mio_seterrbfmt (json->mio, MIO_EINVAL, "not starting with [ or { - %jc", (mio_ooch_t)c); mio_seterrbfmt (json->mio, MIO_EINVAL, "not starting with an allowed initial character - %jc", (mio_ooch_t)c);
return -1; return -1;
} }
} }
static int handle_char_in_array (mio_json_t* json, mio_ooci_t c) static int handle_char_in_array (mio_json_t* json, mio_ooci_t c)
{ {
if (c == ']') if (mio_is_ooch_space(c))
{
/* do nothing */
return 1;
}
else if (c == ']')
{ {
pop_read_state (json); pop_read_state (json);
/* START_ARRAY incremented index by 1. so subtract 1 from index before invoking instcb for END_ARRAY. */ /* START_ARRAY incremented index by 1. so subtract 1 from index before invoking instcb for END_ARRAY. */
@ -462,11 +485,6 @@ static int handle_char_in_array (mio_json_t* json, mio_ooci_t c)
json->state_stack->u.ia.got_value = 0; json->state_stack->u.ia.got_value = 0;
return 1; return 1;
} }
else if (mio_is_ooch_space(c))
{
/* do nothing */
return 1;
}
else else
{ {
if (json->state_stack->u.ia.got_value) if (json->state_stack->u.ia.got_value)
@ -516,7 +534,12 @@ static int handle_char_in_array (mio_json_t* json, mio_ooci_t c)
static int handle_char_in_object (mio_json_t* json, mio_ooci_t c) static int handle_char_in_object (mio_json_t* json, mio_ooci_t c)
{ {
if (c == '}') if (mio_is_ooch_space(c))
{
/* do nothing */
return 1;
}
else if (c == '}')
{ {
pop_read_state (json); pop_read_state (json);
/* START_OBJECT incremented index by 1. so subtract 1 from index before invoking instcb for END_OBJECT. */ /* START_OBJECT incremented index by 1. so subtract 1 from index before invoking instcb for END_OBJECT. */
@ -543,11 +566,6 @@ static int handle_char_in_object (mio_json_t* json, mio_ooci_t c)
json->state_stack->u.io.state = 0; json->state_stack->u.io.state = 0;
return 1; return 1;
} }
else if (mio_is_ooch_space(c))
{
/* do nothing */
return 1;
}
else else
{ {
if (json->state_stack->u.io.state == 1) if (json->state_stack->u.io.state == 1)
@ -703,6 +721,7 @@ static int feed_json_data (mio_json_t* json, const mio_bch_t* data, mio_oow_t le
#endif #endif
if (json->state_stack->state == MIO_JSON_STATE_START && mio_is_ooch_space(c)) continue; /* skip white space */ if (json->state_stack->state == MIO_JSON_STATE_START && mio_is_ooch_space(c)) continue; /* skip white space */
if (stop_if_ever_completed && ever_completed) if (stop_if_ever_completed && ever_completed)
{ {
*xlen = optr - data; *xlen = optr - data;
@ -1293,4 +1312,3 @@ int mio_jsonwr_writerawbcstr (mio_jsonwr_t* jsonwr, const mio_bch_t* dptr)
WRITE_BYTES_NOESC (jsonwr, dptr, mio_count_bcstr(dptr)); WRITE_BYTES_NOESC (jsonwr, dptr, mio_count_bcstr(dptr));
return 0; return 0;
} }