2 * Lightweight Embedded JSON Parser
4 * Copyright (C) 2013-2017 Andy Green <andy@warmcat.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation:
9 * version 2.1 of the License.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22 #include <libwebsockets.h>
26 static const char * const reason_names[] = {
37 "LEJPCB_VAL_NUM_FLOAT",
38 "LEJPCB_VAL_STR_START",
39 "LEJPCB_VAL_STR_CHUNK",
43 "LEJPCB_OBJECT_START",
45 "LEJPCB_OBJECT_END_PRE",
48 static const char * const tok[] = {
53 cb(struct lejp_ctx *ctx, char reason)
55 char buf[1024], *p = buf, *end = &buf[sizeof(buf)];
58 for (n = 0; n < ctx->sp; n++)
62 if (reason & LEJP_FLAG_CB_IS_VALUE) {
63 p += lws_snprintf(p, p - end, " value '%s' ", ctx->buf);
67 p += lws_snprintf(p, p - end, "(array indexes: ");
68 for (n = 0; n < ctx->ipos; n++)
69 p += lws_snprintf(p, p - end, "%d ", ctx->i[n]);
70 p += lws_snprintf(p, p - end, ") ");
72 lwsl_notice("%s (%s)\r\n", buf,
73 reason_names[(unsigned int)
74 (reason) & (LEJP_FLAG_CB_IS_VALUE - 1)]);
76 (void)reason_names; /* NO_LOGS... */
82 lwsl_notice("%sParsing Completed (LEJPCB_COMPLETE)\n", buf);
84 case LEJPCB_PAIR_NAME:
85 lwsl_notice("%spath: '%s' (LEJPCB_PAIR_NAME)\n", buf, ctx->path);
89 lwsl_notice("%s%s: path %s match %d statckp %d\r\n", buf, reason_names[(unsigned int)
90 (reason) & (LEJP_FLAG_CB_IS_VALUE - 1)], ctx->path,
91 ctx->path_match, ctx->pst[ctx->pst_sp].ppos);
97 main(int argc, char *argv[])
99 int fd, n = 1, ret = 1, m;
103 lws_set_log_level(7, NULL);
105 lwsl_notice("libwebsockets-test-lejp (C) 2017 - 2018 andy@warmcat.com\n");
106 lwsl_notice(" usage: cat my.json | libwebsockets-test-lejp\n\n");
108 lejp_construct(&ctx, cb, NULL, tok, LWS_ARRAY_SIZE(tok));
113 n = read(fd, buf, sizeof(buf));
117 m = lejp_parse(&ctx, (uint8_t *)buf, n);
118 if (m < 0 && m != LEJP_CONTINUE) {
119 lwsl_err("parse failed %d\n", m);
123 lwsl_notice("okay\n");