2020-05-30 06:58:55 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
#include <hio.h>
|
|
|
|
#include <hio-json.h>
|
2020-05-30 06:58:55 +00:00
|
|
|
#include <stdio.h>
|
2020-05-31 17:36:11 +00:00
|
|
|
#include <string.h>
|
2020-05-30 06:58:55 +00:00
|
|
|
|
2021-07-14 11:31:29 +00:00
|
|
|
#define DEBUG
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
static int on_json_inst (hio_json_t* json, hio_json_inst_t inst, hio_oow_t level, hio_oow_t index, hio_json_state_t container_state, const hio_oocs_t* str, void* ctx)
|
2020-05-30 06:58:55 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_t* hio = hio_json_gethio(json);
|
|
|
|
hio_oow_t i;
|
2020-06-06 18:53:09 +00:00
|
|
|
|
2020-05-30 06:58:55 +00:00
|
|
|
switch (inst)
|
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
case HIO_JSON_INST_START_ARRAY:
|
2020-06-06 18:53:09 +00:00
|
|
|
if (level > 0)
|
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
if (index > 0) hio_logbfmt (hio, HIO_LOG_STDOUT, ",\n");
|
|
|
|
for (i = 0; i < level; i++) hio_logbfmt (hio, HIO_LOG_STDOUT, "\t");
|
2020-06-06 18:53:09 +00:00
|
|
|
}
|
2021-07-14 11:31:29 +00:00
|
|
|
#if defined(DEBUG)
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_logbfmt (hio, HIO_LOG_STDOUT, "<%lu>[\n", (unsigned long)index);
|
2021-07-14 11:31:29 +00:00
|
|
|
#else
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_logbfmt (hio, HIO_LOG_STDOUT, "[\n");
|
2021-07-14 11:31:29 +00:00
|
|
|
#endif
|
2020-05-30 06:58:55 +00:00
|
|
|
break;
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
case HIO_JSON_INST_END_ARRAY:
|
|
|
|
hio_logbfmt (hio, HIO_LOG_STDOUT, "\n");
|
|
|
|
for (i = 0; i < level; i++) hio_logbfmt (hio, HIO_LOG_STDOUT, "\t");
|
2021-07-14 11:31:29 +00:00
|
|
|
#if defined(DEBUG)
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_logbfmt (hio, HIO_LOG_STDOUT, "<%lu>]", (unsigned long)index);
|
2021-07-14 11:31:29 +00:00
|
|
|
#else
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_logbfmt (hio, HIO_LOG_STDOUT, "]");
|
2021-07-14 11:31:29 +00:00
|
|
|
#endif
|
2020-05-30 06:58:55 +00:00
|
|
|
break;
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
case HIO_JSON_INST_START_OBJECT:
|
2020-06-06 18:53:09 +00:00
|
|
|
if (level > 0)
|
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
if (index > 0) hio_logbfmt (hio, HIO_LOG_STDOUT, ",\n");
|
|
|
|
for (i = 0; i < level; i++) hio_logbfmt (hio, HIO_LOG_STDOUT, "\t");
|
2020-06-06 18:53:09 +00:00
|
|
|
}
|
2021-07-14 11:31:29 +00:00
|
|
|
#if defined(DEBUG)
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_logbfmt (hio, HIO_LOG_STDOUT, "<%lu>{\n", (unsigned long)index);
|
2021-07-14 11:31:29 +00:00
|
|
|
#else
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_logbfmt (hio, HIO_LOG_STDOUT, "{\n");
|
2021-07-14 11:31:29 +00:00
|
|
|
#endif
|
2020-05-30 06:58:55 +00:00
|
|
|
break;
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
case HIO_JSON_INST_END_OBJECT:
|
|
|
|
hio_logbfmt (hio, HIO_LOG_STDOUT, "\n");
|
|
|
|
for (i = 0; i < level; i++) hio_logbfmt (hio, HIO_LOG_STDOUT, "\t");
|
2021-07-14 11:31:29 +00:00
|
|
|
#if defined(DEBUG)
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_logbfmt (hio, HIO_LOG_STDOUT, "<%lu>}", (unsigned long)index);
|
2021-07-14 11:31:29 +00:00
|
|
|
#else
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_logbfmt (hio, HIO_LOG_STDOUT, "}");
|
2021-07-14 11:31:29 +00:00
|
|
|
#endif
|
2020-05-30 06:58:55 +00:00
|
|
|
break;
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
case HIO_JSON_INST_KEY:
|
|
|
|
if (index > 0) hio_logbfmt (hio, HIO_LOG_STDOUT, ",\n");
|
|
|
|
for (i = 0; i < level; i++) hio_logbfmt (hio, HIO_LOG_STDOUT, "\t");
|
2021-07-14 11:31:29 +00:00
|
|
|
#if defined(DEBUG)
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_logbfmt (hio, HIO_LOG_STDOUT, "<%lu>%.*js: ", (unsigned long)index, str->len, str->ptr);
|
2021-07-14 11:31:29 +00:00
|
|
|
#else
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_logbfmt (hio, HIO_LOG_STDOUT, "%.*js: ", str->len, str->ptr);
|
2021-07-14 11:31:29 +00:00
|
|
|
#endif
|
2020-05-30 06:58:55 +00:00
|
|
|
break;
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
case HIO_JSON_INST_NIL:
|
2020-06-06 18:53:09 +00:00
|
|
|
if (level > 0)
|
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
if (index > 0) hio_logbfmt (hio, HIO_LOG_STDOUT, ",\n");
|
|
|
|
for (i = 0; i < level; i++) hio_logbfmt (hio, HIO_LOG_STDOUT, "\t");
|
2020-06-06 18:53:09 +00:00
|
|
|
}
|
2021-07-14 11:31:29 +00:00
|
|
|
#if defined(DEBUG)
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_logbfmt (hio, HIO_LOG_STDOUT, "<%lu>null", (unsigned long)index);
|
2021-07-14 11:31:29 +00:00
|
|
|
#else
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_logbfmt (hio, HIO_LOG_STDOUT, "null");
|
2021-07-14 11:31:29 +00:00
|
|
|
#endif
|
2020-05-30 06:58:55 +00:00
|
|
|
break;
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
case HIO_JSON_INST_TRUE:
|
2020-06-06 18:53:09 +00:00
|
|
|
if (level > 0)
|
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
if (index > 0) hio_logbfmt (hio, HIO_LOG_STDOUT, ",\n");
|
|
|
|
for (i = 0; i < level; i++) hio_logbfmt (hio, HIO_LOG_STDOUT, "\t");
|
2020-06-06 18:53:09 +00:00
|
|
|
}
|
2021-07-14 11:31:29 +00:00
|
|
|
#if defined(DEBUG)
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_logbfmt (hio, HIO_LOG_STDOUT, "<%lu>true", (unsigned long)index);
|
2021-07-14 11:31:29 +00:00
|
|
|
#else
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_logbfmt (hio, HIO_LOG_STDOUT, "true");
|
2021-07-14 11:31:29 +00:00
|
|
|
#endif
|
2020-05-30 06:58:55 +00:00
|
|
|
break;
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
case HIO_JSON_INST_FALSE:
|
2020-06-06 18:53:09 +00:00
|
|
|
if (level > 0)
|
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
if (index > 0) hio_logbfmt (hio, HIO_LOG_STDOUT, ",\n");
|
|
|
|
for (i = 0; i < level; i++) hio_logbfmt (hio, HIO_LOG_STDOUT, "\t");
|
2020-06-06 18:53:09 +00:00
|
|
|
}
|
2021-07-14 11:31:29 +00:00
|
|
|
#if defined(DEBUG)
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_logbfmt (hio, HIO_LOG_STDOUT, "<%lu>false", (unsigned long)index);
|
2021-07-14 11:31:29 +00:00
|
|
|
#else
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_logbfmt (hio, HIO_LOG_STDOUT, "false");
|
2021-07-14 11:31:29 +00:00
|
|
|
#endif
|
2020-05-30 06:58:55 +00:00
|
|
|
break;
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
case HIO_JSON_INST_NUMBER:
|
2020-06-06 18:53:09 +00:00
|
|
|
if (level > 0)
|
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
if (index > 0) hio_logbfmt (hio, HIO_LOG_STDOUT, ",\n");
|
|
|
|
for (i = 0; i < level; i++) hio_logbfmt (hio, HIO_LOG_STDOUT, "\t");
|
2020-06-06 18:53:09 +00:00
|
|
|
}
|
2021-07-14 11:31:29 +00:00
|
|
|
#if defined(DEBUG)
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_logbfmt (hio, HIO_LOG_STDOUT, "<%lu>%.*js", (unsigned long)index, str->len, str->ptr);
|
2021-07-14 11:31:29 +00:00
|
|
|
#else
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_logbfmt (hio, HIO_LOG_STDOUT, "%.*js", str->len, str->ptr);
|
2021-07-14 11:31:29 +00:00
|
|
|
#endif
|
2020-05-30 06:58:55 +00:00
|
|
|
break;
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
case HIO_JSON_INST_STRING:
|
2020-06-06 18:53:09 +00:00
|
|
|
if (level > 0)
|
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
if (index > 0) hio_logbfmt (hio, HIO_LOG_STDOUT, ",\n");
|
|
|
|
for (i = 0; i < level; i++) hio_logbfmt (hio, HIO_LOG_STDOUT, "\t");
|
2020-06-06 18:53:09 +00:00
|
|
|
}
|
2021-07-14 11:31:29 +00:00
|
|
|
#if defined(DEBUG)
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_logbfmt (hio, HIO_LOG_STDOUT, "<%lu>\"%.*js\"", (unsigned long)index, str->len, str->ptr); /* TODO: escaping */
|
2021-07-14 11:31:29 +00:00
|
|
|
#else
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_logbfmt (hio, HIO_LOG_STDOUT, "\"%.*js\"", str->len, str->ptr); /* TODO: escaping */
|
2021-07-14 11:31:29 +00:00
|
|
|
#endif
|
2020-05-30 06:58:55 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_logbfmt (hio, HIO_LOG_STDOUT, "*****UNKNOWN*****\n", str->len, str->ptr);
|
2020-05-30 06:58:55 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
static int write_json_element (hio_jsonwr_t* jsonwr, const hio_bch_t* dptr, hio_oow_t dlen, void* ctx)
|
2020-05-31 17:36:11 +00:00
|
|
|
{
|
2020-06-17 13:36:49 +00:00
|
|
|
fwrite (dptr, 1, dlen, stdout);
|
2020-05-31 17:36:11 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-30 06:58:55 +00:00
|
|
|
int main (int argc, char* argv[])
|
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_t* hio = HIO_NULL;
|
|
|
|
hio_bitmask_t o = 0;
|
2021-07-14 05:18:19 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 1; i < argc; i++)
|
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
if (strcmp(argv[i], "--permit-word-key") == 0) o |= HIO_JSON_PERMIT_WORD_KEY;
|
|
|
|
if (strcmp(argv[i], "--optional-comma") == 0) o |= HIO_JSON_OPTIONAL_COMMA;
|
|
|
|
if (strcmp(argv[i], "--line-comment") == 0) o |= HIO_JSON_LINE_COMMENT;
|
2021-07-14 05:18:19 +00:00
|
|
|
}
|
2020-05-30 06:58:55 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
hio = hio_open(HIO_NULL, 0, HIO_NULL, HIO_FEATURE_ALL, 512, HIO_NULL);
|
|
|
|
if (!hio)
|
2020-05-30 06:58:55 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
printf ("Cannot open hio\n");
|
2020-05-30 06:58:55 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-05-31 17:36:11 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_json_t* json = HIO_NULL;
|
2020-05-31 17:36:11 +00:00
|
|
|
char buf[128];
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_oow_t rem;
|
2020-06-17 13:36:49 +00:00
|
|
|
size_t size;
|
2020-05-30 06:58:55 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
json = hio_json_open(hio, 0);
|
2020-05-30 06:58:55 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_json_setoption (json, o);
|
|
|
|
hio_json_setinstcb (json, on_json_inst, HIO_NULL);
|
2020-05-30 06:58:55 +00:00
|
|
|
|
2020-05-31 17:36:11 +00:00
|
|
|
rem = 0;
|
2020-06-17 09:51:20 +00:00
|
|
|
while (!feof(stdin) || rem > 0)
|
2020-05-30 06:58:55 +00:00
|
|
|
{
|
2020-05-31 17:36:11 +00:00
|
|
|
int x;
|
|
|
|
|
2020-06-17 09:51:20 +00:00
|
|
|
if (!feof(stdin))
|
|
|
|
{
|
|
|
|
size = fread(&buf[rem], 1, sizeof(buf) - rem, stdin);
|
|
|
|
if (size <= 0) break;
|
|
|
|
}
|
2020-06-17 13:36:49 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
size = rem;
|
|
|
|
rem = 0;
|
|
|
|
}
|
2020-05-31 17:36:11 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
if ((x = hio_json_feed(json, buf, size + rem, &rem, 1)) <= -1)
|
2020-05-31 17:36:11 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_logbfmt (hio, HIO_LOG_STDOUT, "**** ERROR - %js ****\n", hio_geterrmsg(hio));
|
2021-07-14 05:18:19 +00:00
|
|
|
goto done;
|
2020-05-31 17:36:11 +00:00
|
|
|
}
|
|
|
|
|
2020-06-17 09:51:20 +00:00
|
|
|
if (x > 0)
|
|
|
|
{
|
2021-07-14 03:22:51 +00:00
|
|
|
/* document completed.
|
|
|
|
* if only whitespaces are given, x is still greater 0. */
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_logbfmt (hio, HIO_LOG_STDOUT, "\n-----------------------------------\n");
|
2020-06-17 09:51:20 +00:00
|
|
|
}
|
|
|
|
|
2020-06-17 13:36:49 +00:00
|
|
|
/*printf ("--> x %d input %d left-over %d => [%.*s]\n", (int)x, (int)size, (int)rem, (int)rem, &buf[size - rem]);*/
|
2020-05-31 17:36:11 +00:00
|
|
|
if (rem > 0) memcpy (buf, &buf[size - rem], rem);
|
2020-05-30 06:58:55 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_logbfmt (hio, HIO_LOG_STDOUT, "\n");
|
|
|
|
if (json->state_stack != &json->state_top) hio_logbfmt (hio, HIO_LOG_STDOUT, "**** ERROR - incomplete ****\n");
|
2021-07-14 05:18:19 +00:00
|
|
|
|
|
|
|
done:
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_json_close (json);
|
2020-05-31 17:36:11 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_logbfmt (hio, HIO_LOG_STDOUT, "\n===================================\n");
|
2020-05-31 17:36:11 +00:00
|
|
|
|
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_jsonwr_t* jsonwr = HIO_NULL;
|
|
|
|
hio_uch_t ddd[4] = { 'D', '\0', 'R', 'Q' };
|
|
|
|
hio_uch_t ddv[5] = { L'밝', L'혀', L'졌', L'는', L'데' };
|
2020-05-31 17:36:11 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
jsonwr = hio_jsonwr_open (hio, 0, HIO_JSONWR_FLAG_PRETTY);
|
2020-05-31 17:36:11 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_jsonwr_setwritecb (jsonwr, write_json_element, HIO_NULL);
|
2020-05-31 17:36:11 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_jsonwr_startarray (jsonwr);
|
2020-06-01 07:32:28 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_jsonwr_writestringwithbchars (jsonwr, "hello", 5);
|
|
|
|
hio_jsonwr_writestringwithbchars (jsonwr, "world", 5);
|
2020-06-01 07:32:28 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_jsonwr_startobject (jsonwr);
|
|
|
|
hio_jsonwr_writekeywithbchars (jsonwr, "abc", 3);
|
|
|
|
hio_jsonwr_writestringwithbchars (jsonwr, "computer", 8);
|
|
|
|
hio_jsonwr_writekeywithbchars (jsonwr, "k", 1);
|
|
|
|
hio_jsonwr_writestringwithbchars (jsonwr, "play nice", 9);
|
|
|
|
hio_jsonwr_writekeywithuchars (jsonwr, ddd, 4);
|
|
|
|
hio_jsonwr_writestringwithuchars (jsonwr, ddv, 5);
|
|
|
|
hio_jsonwr_endobject (jsonwr);
|
2020-06-01 07:32:28 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_jsonwr_writestringwithbchars (jsonwr, "tyler", 5);
|
2020-06-01 07:32:28 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_jsonwr_startarray (jsonwr);
|
|
|
|
hio_jsonwr_writestringwithbchars (jsonwr, "airplain", 8);
|
|
|
|
hio_jsonwr_writestringwithbchars (jsonwr, "gro\0wn\nup", 9);
|
|
|
|
hio_jsonwr_writetrue (jsonwr);
|
|
|
|
hio_jsonwr_endarray (jsonwr);
|
2020-06-01 07:32:28 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_jsonwr_endarray (jsonwr);
|
2020-05-31 17:36:11 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_jsonwr_close (jsonwr);
|
2020-05-30 06:58:55 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_close (hio);
|
2020-05-30 06:58:55 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|