2020-05-30 06:58:55 +00:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
Copyright (c) 2016-2020 Chung, Hyung-Hwan. All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
1. Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
documentation and/or other materials provided with the distribution.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
|
|
|
|
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WAfRRANTIES
|
|
|
|
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <mio.h>
|
|
|
|
#include <mio-json.h>
|
|
|
|
#include <stdio.h>
|
2020-05-31 17:36:11 +00:00
|
|
|
#include <string.h>
|
2020-05-30 06:58:55 +00:00
|
|
|
|
2020-06-06 19:45:11 +00:00
|
|
|
static int on_json_inst (mio_json_t* json, mio_json_inst_t inst, mio_oow_t level, mio_oow_t index, mio_json_state_t container_state, const mio_oocs_t* str, void* ctx)
|
2020-05-30 06:58:55 +00:00
|
|
|
{
|
|
|
|
mio_t* mio = mio_json_getmio(json);
|
|
|
|
mio_oow_t i;
|
|
|
|
|
2020-06-06 18:53:09 +00:00
|
|
|
|
2020-05-30 06:58:55 +00:00
|
|
|
switch (inst)
|
|
|
|
{
|
|
|
|
case MIO_JSON_INST_START_ARRAY:
|
2020-06-06 18:53:09 +00:00
|
|
|
if (level > 0)
|
|
|
|
{
|
|
|
|
if (index > 0) mio_logbfmt (mio, MIO_LOG_STDOUT, ",\n");
|
|
|
|
for (i = 0; i < level; i++) mio_logbfmt (mio, MIO_LOG_STDOUT, "\t");
|
|
|
|
}
|
2020-05-30 06:58:55 +00:00
|
|
|
mio_logbfmt (mio, MIO_LOG_STDOUT, "[\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MIO_JSON_INST_END_ARRAY:
|
2020-06-06 18:53:09 +00:00
|
|
|
mio_logbfmt (mio, MIO_LOG_STDOUT, "\n");
|
2020-05-30 06:58:55 +00:00
|
|
|
for (i = 0; i < level; i++) mio_logbfmt (mio, MIO_LOG_STDOUT, "\t");
|
2020-06-06 18:53:09 +00:00
|
|
|
mio_logbfmt (mio, MIO_LOG_STDOUT, "]");
|
2020-05-30 06:58:55 +00:00
|
|
|
break;
|
|
|
|
|
2020-06-07 14:44:54 +00:00
|
|
|
case MIO_JSON_INST_START_OBJECT:
|
2020-06-06 18:53:09 +00:00
|
|
|
if (level > 0)
|
|
|
|
{
|
|
|
|
if (index > 0) mio_logbfmt (mio, MIO_LOG_STDOUT, ",\n");
|
|
|
|
for (i = 0; i < level; i++) mio_logbfmt (mio, MIO_LOG_STDOUT, "\t");
|
|
|
|
}
|
2020-05-30 06:58:55 +00:00
|
|
|
mio_logbfmt (mio, MIO_LOG_STDOUT, "{\n");
|
|
|
|
break;
|
|
|
|
|
2020-06-07 14:44:54 +00:00
|
|
|
case MIO_JSON_INST_END_OBJECT:
|
2020-06-06 18:53:09 +00:00
|
|
|
mio_logbfmt (mio, MIO_LOG_STDOUT, "\n");
|
2020-05-30 06:58:55 +00:00
|
|
|
for (i = 0; i < level; i++) mio_logbfmt (mio, MIO_LOG_STDOUT, "\t");
|
2020-06-06 18:53:09 +00:00
|
|
|
mio_logbfmt (mio, MIO_LOG_STDOUT, "}");
|
2020-05-30 06:58:55 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MIO_JSON_INST_KEY:
|
2020-06-06 18:53:09 +00:00
|
|
|
if (index > 0) mio_logbfmt (mio, MIO_LOG_STDOUT, ",\n");
|
|
|
|
for (i = 0; i < level; i++) mio_logbfmt (mio, MIO_LOG_STDOUT, "\t");
|
2020-05-30 06:58:55 +00:00
|
|
|
mio_logbfmt (mio, MIO_LOG_STDOUT, "%.*js: ", str->len, str->ptr);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MIO_JSON_INST_NIL:
|
2020-06-06 18:53:09 +00:00
|
|
|
if (level > 0)
|
|
|
|
{
|
|
|
|
if (index > 0) mio_logbfmt (mio, MIO_LOG_STDOUT, ",\n");
|
|
|
|
for (i = 0; i < level; i++) mio_logbfmt (mio, MIO_LOG_STDOUT, "\t");
|
|
|
|
}
|
|
|
|
mio_logbfmt (mio, MIO_LOG_STDOUT, "null");
|
2020-05-30 06:58:55 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MIO_JSON_INST_TRUE:
|
2020-06-06 18:53:09 +00:00
|
|
|
if (level > 0)
|
|
|
|
{
|
|
|
|
if (index > 0) mio_logbfmt (mio, MIO_LOG_STDOUT, ",\n");
|
|
|
|
for (i = 0; i < level; i++) mio_logbfmt (mio, MIO_LOG_STDOUT, "\t");
|
|
|
|
}
|
|
|
|
mio_logbfmt (mio, MIO_LOG_STDOUT, "true");
|
2020-05-30 06:58:55 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MIO_JSON_INST_FALSE:
|
2020-06-06 18:53:09 +00:00
|
|
|
if (level > 0)
|
|
|
|
{
|
|
|
|
if (index > 0) mio_logbfmt (mio, MIO_LOG_STDOUT, ",\n");
|
|
|
|
for (i = 0; i < level; i++) mio_logbfmt (mio, MIO_LOG_STDOUT, "\t");
|
|
|
|
}
|
|
|
|
mio_logbfmt (mio, MIO_LOG_STDOUT, "false");
|
2020-05-30 06:58:55 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MIO_JSON_INST_NUMBER:
|
2020-06-06 18:53:09 +00:00
|
|
|
if (level > 0)
|
|
|
|
{
|
|
|
|
if (index > 0) mio_logbfmt (mio, MIO_LOG_STDOUT, ",\n");
|
|
|
|
for (i = 0; i < level; i++) mio_logbfmt (mio, MIO_LOG_STDOUT, "\t");
|
|
|
|
}
|
|
|
|
mio_logbfmt (mio, MIO_LOG_STDOUT, "%.*js", str->len, str->ptr);
|
2020-05-30 06:58:55 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MIO_JSON_INST_STRING:
|
2020-06-06 18:53:09 +00:00
|
|
|
if (level > 0)
|
|
|
|
{
|
|
|
|
if (index > 0) mio_logbfmt (mio, MIO_LOG_STDOUT, ",\n");
|
|
|
|
for (i = 0; i < level; i++) mio_logbfmt (mio, MIO_LOG_STDOUT, "\t");
|
|
|
|
}
|
|
|
|
mio_logbfmt (mio, MIO_LOG_STDOUT, "\"%.*js\"", str->len, str->ptr); /* TODO: escaping */
|
2020-05-30 06:58:55 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
mio_logbfmt (mio, MIO_LOG_STDOUT, "*****UNKNOWN*****\n", str->len, str->ptr);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-06-01 08:52:43 +00:00
|
|
|
static int write_json_element (mio_jsonwr_t* jsonwr, const mio_bch_t* dptr, mio_oow_t dlen, void* ctx)
|
2020-05-31 17:36:11 +00:00
|
|
|
{
|
|
|
|
write (1, dptr, dlen);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-30 06:58:55 +00:00
|
|
|
int main (int argc, char* argv[])
|
|
|
|
{
|
|
|
|
mio_t* mio = MIO_NULL;
|
|
|
|
|
|
|
|
mio = mio_open(MIO_NULL, 0, MIO_NULL, 512, MIO_NULL);
|
|
|
|
if (!mio)
|
|
|
|
{
|
|
|
|
printf ("Cannot open mio\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-05-31 17:36:11 +00:00
|
|
|
{
|
|
|
|
mio_json_t* json = MIO_NULL;
|
|
|
|
char buf[128];
|
|
|
|
mio_oow_t rem;
|
2020-05-30 06:58:55 +00:00
|
|
|
|
2020-05-31 17:36:11 +00:00
|
|
|
json = mio_json_open(mio, 0);
|
2020-05-30 06:58:55 +00:00
|
|
|
|
2020-06-02 09:49:08 +00:00
|
|
|
mio_json_setinstcb (json, on_json_inst, MIO_NULL);
|
2020-05-30 06:58:55 +00:00
|
|
|
|
|
|
|
|
2020-05-31 17:36:11 +00:00
|
|
|
rem = 0;
|
2020-06-09 05:52:30 +00:00
|
|
|
while (!feof(stdin))
|
2020-05-30 06:58:55 +00:00
|
|
|
{
|
2020-05-31 17:36:11 +00:00
|
|
|
int x;
|
|
|
|
size_t size = fread(&buf[rem], 1, sizeof(buf) - rem, stdin);
|
|
|
|
if (size <= 0) break;
|
|
|
|
|
|
|
|
|
|
|
|
if ((x = mio_json_feed(json, buf, size + rem, &rem, 1)) <= -1)
|
|
|
|
{
|
|
|
|
printf ("**** ERROR ****\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
//printf ("--> x %d input %d left-over %d\n", (int)x, (int)size, (int)rem);
|
|
|
|
if (rem > 0) memcpy (buf, &buf[size - rem], rem);
|
2020-05-30 06:58:55 +00:00
|
|
|
}
|
|
|
|
|
2020-06-06 18:53:09 +00:00
|
|
|
mio_logbfmt (mio, MIO_LOG_STDOUT, "\n");
|
2020-05-31 17:36:11 +00:00
|
|
|
mio_json_close (json);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
mio_jsonwr_t* jsonwr = MIO_NULL;
|
|
|
|
mio_uch_t ddd[4] = { 'D', '\0', 'R', 'Q' };
|
|
|
|
mio_uch_t ddv[5] = { L'밝', L'혀', L'졌', L'는', L'데' };
|
|
|
|
|
|
|
|
jsonwr = mio_jsonwr_open (mio, 0);
|
|
|
|
|
2020-06-01 08:52:43 +00:00
|
|
|
mio_jsonwr_setwritecb (jsonwr, write_json_element, MIO_NULL);
|
2020-05-31 17:36:11 +00:00
|
|
|
|
2020-06-01 07:32:28 +00:00
|
|
|
mio_jsonwr_startarray (jsonwr);
|
|
|
|
|
|
|
|
mio_jsonwr_writestringwithbchars (jsonwr, "hello", 5);
|
|
|
|
mio_jsonwr_writestringwithbchars (jsonwr, "world", 5);
|
|
|
|
|
2020-06-07 14:44:54 +00:00
|
|
|
mio_jsonwr_startobject (jsonwr);
|
2020-06-01 07:32:28 +00:00
|
|
|
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);
|
2020-06-07 14:44:54 +00:00
|
|
|
mio_jsonwr_endobject (jsonwr);
|
2020-06-01 07:32:28 +00:00
|
|
|
|
|
|
|
mio_jsonwr_writestringwithbchars (jsonwr, "tyler", 5);
|
|
|
|
|
|
|
|
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_endarray (jsonwr);
|
2020-05-31 17:36:11 +00:00
|
|
|
|
|
|
|
mio_jsonwr_close (jsonwr);
|
2020-05-30 06:58:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mio_close (mio);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|