Remove app/json. (#1312)
This commit is contained in:
parent
2dacec156a
commit
37411da06d
|
@ -24,7 +24,6 @@ SPECIAL_MKTARGETS=$(APP_MKTARGETS)
|
||||||
SUBDIRS= \
|
SUBDIRS= \
|
||||||
user \
|
user \
|
||||||
driver \
|
driver \
|
||||||
json \
|
|
||||||
platform \
|
platform \
|
||||||
libc \
|
libc \
|
||||||
lua \
|
lua \
|
||||||
|
@ -70,7 +69,6 @@ LD_FILE = $(LDDIR)/nodemcu.ld
|
||||||
COMPONENTS_eagle.app.v6 = \
|
COMPONENTS_eagle.app.v6 = \
|
||||||
user/libuser.a \
|
user/libuser.a \
|
||||||
driver/libdriver.a \
|
driver/libdriver.a \
|
||||||
json/libjson.a \
|
|
||||||
platform/libplatform.a \
|
platform/libplatform.a \
|
||||||
task/libtask.a \
|
task/libtask.a \
|
||||||
libc/liblibc.a \
|
libc/liblibc.a \
|
||||||
|
@ -116,7 +114,6 @@ LINKFLAGS_eagle.app.v6 = \
|
||||||
-lwpa \
|
-lwpa \
|
||||||
-lwpa2 \
|
-lwpa2 \
|
||||||
-lmain \
|
-lmain \
|
||||||
-ljson \
|
|
||||||
-lsmartconfig \
|
-lsmartconfig \
|
||||||
-lssl \
|
-lssl \
|
||||||
-lcrypto \
|
-lcrypto \
|
||||||
|
|
|
@ -1,46 +0,0 @@
|
||||||
|
|
||||||
#############################################################
|
|
||||||
# Required variables for each makefile
|
|
||||||
# Discard this section from all parent makefiles
|
|
||||||
# Expected variables (with automatic defaults):
|
|
||||||
# CSRCS (all "C" files in the dir)
|
|
||||||
# SUBDIRS (all subdirs with a Makefile)
|
|
||||||
# GEN_LIBS - list of libs to be generated ()
|
|
||||||
# GEN_IMAGES - list of images to be generated ()
|
|
||||||
# COMPONENTS_xxx - a list of libs/objs in the form
|
|
||||||
# subdir/lib to be extracted and rolled up into
|
|
||||||
# a generated lib/image xxx.a ()
|
|
||||||
#
|
|
||||||
ifndef PDIR
|
|
||||||
|
|
||||||
GEN_LIBS = libjson.a
|
|
||||||
|
|
||||||
endif
|
|
||||||
|
|
||||||
|
|
||||||
#############################################################
|
|
||||||
# Configuration i.e. compile options etc.
|
|
||||||
# Target specific stuff (defines etc.) goes in here!
|
|
||||||
# Generally values applying to a tree are captured in the
|
|
||||||
# makefile at its root level - these are then overridden
|
|
||||||
# for a subtree within the makefile rooted therein
|
|
||||||
#
|
|
||||||
#DEFINES +=
|
|
||||||
|
|
||||||
#############################################################
|
|
||||||
# Recursion Magic - Don't touch this!!
|
|
||||||
#
|
|
||||||
# Each subtree potentially has an include directory
|
|
||||||
# corresponding to the common APIs applicable to modules
|
|
||||||
# rooted at that subtree. Accordingly, the INCLUDE PATH
|
|
||||||
# of a module can only contain the include directories up
|
|
||||||
# its parent path, and not its siblings
|
|
||||||
#
|
|
||||||
# Required for each makefile to inherit from the parent
|
|
||||||
#
|
|
||||||
|
|
||||||
INCLUDES := $(INCLUDES) -I $(PDIR)include
|
|
||||||
INCLUDES += -I ./
|
|
||||||
PDIR := ../$(PDIR)
|
|
||||||
sinclude $(PDIR)Makefile
|
|
||||||
|
|
|
@ -1,281 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (c) 2011-2012, Swedish Institute of Computer Science.
|
|
||||||
* 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.
|
|
||||||
* 3. Neither the name of the Institute nor the names of its contributors
|
|
||||||
* may be used to endorse or promote products derived from this software
|
|
||||||
* without specific prior written permission.
|
|
||||||
*
|
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
||||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
||||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
||||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS 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.
|
|
||||||
*
|
|
||||||
* This file is part of the Contiki operating system.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifdef JSON_FORMAT
|
|
||||||
|
|
||||||
#include "json/jsonparse.h"
|
|
||||||
#include "osapi.h"
|
|
||||||
//#include <stdlib.h>
|
|
||||||
//#include <string.h>
|
|
||||||
|
|
||||||
/*--------------------------------------------------------------------*/
|
|
||||||
static int ICACHE_FLASH_ATTR
|
|
||||||
push(struct jsonparse_state *state, char c)
|
|
||||||
{
|
|
||||||
state->stack[state->depth] = c;
|
|
||||||
state->depth++;
|
|
||||||
state->vtype = 0;
|
|
||||||
return state->depth < JSONPARSE_MAX_DEPTH;
|
|
||||||
}
|
|
||||||
/*--------------------------------------------------------------------*/
|
|
||||||
static char ICACHE_FLASH_ATTR
|
|
||||||
pop(struct jsonparse_state *state)
|
|
||||||
{
|
|
||||||
if(state->depth == 0) {
|
|
||||||
return JSON_TYPE_ERROR;
|
|
||||||
}
|
|
||||||
state->depth--;
|
|
||||||
return state->stack[state->depth];
|
|
||||||
}
|
|
||||||
/*--------------------------------------------------------------------*/
|
|
||||||
/* will pass by the value and store the start and length of the value for
|
|
||||||
atomic types */
|
|
||||||
/*--------------------------------------------------------------------*/
|
|
||||||
static void ICACHE_FLASH_ATTR
|
|
||||||
atomic(struct jsonparse_state *state, char type)
|
|
||||||
{
|
|
||||||
char c;
|
|
||||||
|
|
||||||
state->vstart = state->pos;
|
|
||||||
state->vtype = type;
|
|
||||||
if(type == JSON_TYPE_STRING || type == JSON_TYPE_PAIR_NAME) {
|
|
||||||
while((c = state->json[state->pos++]) && c != '"') {
|
|
||||||
if(c == '\\') {
|
|
||||||
state->pos++; /* skip current char */
|
|
||||||
}
|
|
||||||
}
|
|
||||||
state->vlen = state->pos - state->vstart - 1;
|
|
||||||
} else if(type == JSON_TYPE_NUMBER) {
|
|
||||||
do {
|
|
||||||
c = state->json[state->pos];
|
|
||||||
if((c < '0' || c > '9') && c != '.') {
|
|
||||||
c = 0;
|
|
||||||
} else {
|
|
||||||
state->pos++;
|
|
||||||
}
|
|
||||||
} while(c);
|
|
||||||
/* need to back one step since first char is already gone */
|
|
||||||
state->vstart--;
|
|
||||||
state->vlen = state->pos - state->vstart;
|
|
||||||
}
|
|
||||||
/* no other types for now... */
|
|
||||||
}
|
|
||||||
/*--------------------------------------------------------------------*/
|
|
||||||
static void ICACHE_FLASH_ATTR
|
|
||||||
skip_ws(struct jsonparse_state *state)
|
|
||||||
{
|
|
||||||
char c;
|
|
||||||
|
|
||||||
while(state->pos < state->len &&
|
|
||||||
((c = state->json[state->pos]) == ' ' || c == '\n')) {
|
|
||||||
state->pos++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/*--------------------------------------------------------------------*/
|
|
||||||
void ICACHE_FLASH_ATTR
|
|
||||||
jsonparse_setup(struct jsonparse_state *state, const char *json, int len)
|
|
||||||
{
|
|
||||||
state->json = json;
|
|
||||||
state->len = len;
|
|
||||||
state->pos = 0;
|
|
||||||
state->depth = 0;
|
|
||||||
state->error = 0;
|
|
||||||
state->stack[0] = 0;
|
|
||||||
}
|
|
||||||
/*--------------------------------------------------------------------*/
|
|
||||||
int ICACHE_FLASH_ATTR
|
|
||||||
jsonparse_next(struct jsonparse_state *state)
|
|
||||||
{
|
|
||||||
char c;
|
|
||||||
char s;
|
|
||||||
|
|
||||||
skip_ws(state);
|
|
||||||
c = state->json[state->pos];
|
|
||||||
s = jsonparse_get_type(state);
|
|
||||||
state->pos++;
|
|
||||||
|
|
||||||
switch(c) {
|
|
||||||
case '{':
|
|
||||||
push(state, c);
|
|
||||||
return c;
|
|
||||||
case '}':
|
|
||||||
if(s == ':' && state->vtype != 0) {
|
|
||||||
/* printf("Popping vtype: '%c'\n", state->vtype); */
|
|
||||||
pop(state);
|
|
||||||
s = jsonparse_get_type(state);
|
|
||||||
}
|
|
||||||
if(s == '{') {
|
|
||||||
pop(state);
|
|
||||||
} else {
|
|
||||||
state->error = JSON_ERROR_SYNTAX;
|
|
||||||
return JSON_TYPE_ERROR;
|
|
||||||
}
|
|
||||||
return c;
|
|
||||||
case ']':
|
|
||||||
if(s == '[') {
|
|
||||||
pop(state);
|
|
||||||
} else {
|
|
||||||
state->error = JSON_ERROR_UNEXPECTED_END_OF_ARRAY;
|
|
||||||
return JSON_TYPE_ERROR;
|
|
||||||
}
|
|
||||||
return c;
|
|
||||||
case ':':
|
|
||||||
push(state, c);
|
|
||||||
return c;
|
|
||||||
case ',':
|
|
||||||
/* if x:y ... , */
|
|
||||||
if(s == ':' && state->vtype != 0) {
|
|
||||||
pop(state);
|
|
||||||
} else if(s == '[') {
|
|
||||||
/* ok! */
|
|
||||||
} else {
|
|
||||||
state->error = JSON_ERROR_SYNTAX;
|
|
||||||
return JSON_TYPE_ERROR;
|
|
||||||
}
|
|
||||||
return c;
|
|
||||||
case '"':
|
|
||||||
if(s == '{' || s == '[' || s == ':') {
|
|
||||||
atomic(state, c = (s == '{' ? JSON_TYPE_PAIR_NAME : c));
|
|
||||||
} else {
|
|
||||||
state->error = JSON_ERROR_UNEXPECTED_STRING;
|
|
||||||
return JSON_TYPE_ERROR;
|
|
||||||
}
|
|
||||||
return c;
|
|
||||||
case '[':
|
|
||||||
if(s == '{' || s == '[' || s == ':') {
|
|
||||||
push(state, c);
|
|
||||||
} else {
|
|
||||||
state->error = JSON_ERROR_UNEXPECTED_ARRAY;
|
|
||||||
return JSON_TYPE_ERROR;
|
|
||||||
}
|
|
||||||
return c;
|
|
||||||
default:
|
|
||||||
if(s == ':' || s == '[') {
|
|
||||||
if(c <= '9' && c >= '0') {
|
|
||||||
atomic(state, JSON_TYPE_NUMBER);
|
|
||||||
return JSON_TYPE_NUMBER;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
/*--------------------------------------------------------------------*/
|
|
||||||
/* get the json value of the current position
|
|
||||||
* works only on "atomic" values such as string, number, null, false, true
|
|
||||||
*/
|
|
||||||
/*--------------------------------------------------------------------*/
|
|
||||||
int ICACHE_FLASH_ATTR
|
|
||||||
jsonparse_copy_value(struct jsonparse_state *state, char *str, int size)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
char z = 0;
|
|
||||||
char y = 0;
|
|
||||||
|
|
||||||
if(state->vtype == 0) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
size = size <= state->vlen ? (size - 1) : state->vlen;
|
|
||||||
for(i = 0; i < size; i++) {
|
|
||||||
if (y == 0 && state->json[state->vstart + i] == '\\') {
|
|
||||||
y = 1;
|
|
||||||
z++;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
y = 0;
|
|
||||||
str[i - z] = state->json[state->vstart + i];
|
|
||||||
}
|
|
||||||
str[i - z] = 0;
|
|
||||||
return state->vtype;
|
|
||||||
}
|
|
||||||
/*--------------------------------------------------------------------*/
|
|
||||||
int ICACHE_FLASH_ATTR
|
|
||||||
jsonparse_get_value_as_int(struct jsonparse_state *state)
|
|
||||||
{
|
|
||||||
if(state->vtype != JSON_TYPE_NUMBER) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
return atoi(&state->json[state->vstart]);
|
|
||||||
}
|
|
||||||
/*--------------------------------------------------------------------*/
|
|
||||||
long ICACHE_FLASH_ATTR
|
|
||||||
jsonparse_get_value_as_long(struct jsonparse_state *state)
|
|
||||||
{
|
|
||||||
if(state->vtype != JSON_TYPE_NUMBER) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
return atol(&state->json[state->vstart]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*--------------------------------------------------------------------*/
|
|
||||||
unsigned long ICACHE_FLASH_ATTR
|
|
||||||
jsonparse_get_value_as_ulong(struct jsonparse_state *state)
|
|
||||||
{
|
|
||||||
if(state->vtype != JSON_TYPE_NUMBER) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
return strtoul(&state->json[state->vstart], '\0', 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*--------------------------------------------------------------------*/
|
|
||||||
/* strcmp - assume no strange chars that needs to be stuffed in string... */
|
|
||||||
/*--------------------------------------------------------------------*/
|
|
||||||
int ICACHE_FLASH_ATTR
|
|
||||||
jsonparse_strcmp_value(struct jsonparse_state *state, const char *str)
|
|
||||||
{
|
|
||||||
if(state->vtype == 0) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
return os_strncmp(str, &state->json[state->vstart], state->vlen);
|
|
||||||
}
|
|
||||||
/*--------------------------------------------------------------------*/
|
|
||||||
int ICACHE_FLASH_ATTR
|
|
||||||
jsonparse_get_len(struct jsonparse_state *state)
|
|
||||||
{
|
|
||||||
return state->vlen;
|
|
||||||
}
|
|
||||||
/*--------------------------------------------------------------------*/
|
|
||||||
int ICACHE_FLASH_ATTR
|
|
||||||
jsonparse_get_type(struct jsonparse_state *state)
|
|
||||||
{
|
|
||||||
if(state->depth == 0) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
return state->stack[state->depth - 1];
|
|
||||||
}
|
|
||||||
/*--------------------------------------------------------------------*/
|
|
||||||
int ICACHE_FLASH_ATTR
|
|
||||||
jsonparse_has_next(struct jsonparse_state *state)
|
|
||||||
{
|
|
||||||
return state->pos < state->len;
|
|
||||||
}
|
|
||||||
/*--------------------------------------------------------------------*/
|
|
||||||
#endif
|
|
||||||
|
|
|
@ -1,296 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (c) 2011-2012, Swedish Institute of Computer Science.
|
|
||||||
* 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.
|
|
||||||
* 3. Neither the name of the Institute nor the names of its contributors
|
|
||||||
* may be used to endorse or promote products derived from this software
|
|
||||||
* without specific prior written permission.
|
|
||||||
*
|
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
||||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
||||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
||||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS 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.
|
|
||||||
*
|
|
||||||
* This file is part of the Contiki operating system.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \file
|
|
||||||
* JSON output generation
|
|
||||||
* \author
|
|
||||||
* Niclas Finne <nfi@sics.se>
|
|
||||||
* Joakim Eriksson <joakime@sics.se>
|
|
||||||
*/
|
|
||||||
#ifdef JSON_FORMAT
|
|
||||||
//#include "contiki.h"
|
|
||||||
#include "json/jsontree.h"
|
|
||||||
#include "json/jsonparse.h"
|
|
||||||
#include "osapi.h"
|
|
||||||
//#include <string.h>
|
|
||||||
|
|
||||||
#define DEBUG 0
|
|
||||||
#if DEBUG
|
|
||||||
//#include <stdio.h>
|
|
||||||
#define PRINTF(...) os_printf(__VA_ARGS__)
|
|
||||||
#else
|
|
||||||
#define PRINTF(...)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/*---------------------------------------------------------------------------*/
|
|
||||||
void ICACHE_FLASH_ATTR
|
|
||||||
jsontree_write_atom(const struct jsontree_context *js_ctx, const char *text)
|
|
||||||
{
|
|
||||||
if(text == NULL) {
|
|
||||||
js_ctx->putchar('0');
|
|
||||||
} else {
|
|
||||||
while(*text != '\0') {
|
|
||||||
js_ctx->putchar(*text++);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/*---------------------------------------------------------------------------*/
|
|
||||||
void ICACHE_FLASH_ATTR
|
|
||||||
jsontree_write_string(const struct jsontree_context *js_ctx, const char *text)
|
|
||||||
{
|
|
||||||
js_ctx->putchar('"');
|
|
||||||
if(text != NULL) {
|
|
||||||
while(*text != '\0') {
|
|
||||||
if(*text == '"') {
|
|
||||||
js_ctx->putchar('\\');
|
|
||||||
}
|
|
||||||
js_ctx->putchar(*text++);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
js_ctx->putchar('"');
|
|
||||||
}
|
|
||||||
/*---------------------------------------------------------------------------*/
|
|
||||||
void ICACHE_FLASH_ATTR
|
|
||||||
jsontree_write_int(const struct jsontree_context *js_ctx, int value)
|
|
||||||
{
|
|
||||||
char buf[10];
|
|
||||||
int l;
|
|
||||||
|
|
||||||
if(value < 0) {
|
|
||||||
js_ctx->putchar('-');
|
|
||||||
value = -value;
|
|
||||||
}
|
|
||||||
|
|
||||||
l = sizeof(buf) - 1;
|
|
||||||
do {
|
|
||||||
buf[l--] = '0' + (value % 10);
|
|
||||||
value /= 10;
|
|
||||||
} while(value > 0 && l >= 0);
|
|
||||||
|
|
||||||
while(++l < sizeof(buf)) {
|
|
||||||
js_ctx->putchar(buf[l]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*---------------------------------------------------------------------------*/
|
|
||||||
void ICACHE_FLASH_ATTR
|
|
||||||
jsontree_write_int_array(const struct jsontree_context *js_ctx, const int *text, uint32 length)
|
|
||||||
{
|
|
||||||
uint32 i = 0;
|
|
||||||
if(text == NULL) {
|
|
||||||
js_ctx->putchar('0');
|
|
||||||
} else {
|
|
||||||
for (i = 0; i < length - 1; i ++) {
|
|
||||||
jsontree_write_int(js_ctx, *text++);
|
|
||||||
js_ctx->putchar(',');
|
|
||||||
}
|
|
||||||
jsontree_write_int(js_ctx, *text);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*---------------------------------------------------------------------------*/
|
|
||||||
void ICACHE_FLASH_ATTR
|
|
||||||
jsontree_setup(struct jsontree_context *js_ctx, struct jsontree_value *root,
|
|
||||||
int (* putchar)(int))
|
|
||||||
{
|
|
||||||
js_ctx->values[0] = root;
|
|
||||||
js_ctx->putchar = putchar;
|
|
||||||
js_ctx->path = 0;
|
|
||||||
jsontree_reset(js_ctx);
|
|
||||||
}
|
|
||||||
/*---------------------------------------------------------------------------*/
|
|
||||||
void ICACHE_FLASH_ATTR
|
|
||||||
jsontree_reset(struct jsontree_context *js_ctx)
|
|
||||||
{
|
|
||||||
js_ctx->depth = 0;
|
|
||||||
js_ctx->index[0] = 0;
|
|
||||||
}
|
|
||||||
/*---------------------------------------------------------------------------*/
|
|
||||||
const char *ICACHE_FLASH_ATTR
|
|
||||||
jsontree_path_name(const struct jsontree_context *js_ctx, int depth)
|
|
||||||
{
|
|
||||||
if(depth < js_ctx->depth && js_ctx->values[depth]->type == JSON_TYPE_OBJECT) {
|
|
||||||
return ((struct jsontree_object *)js_ctx->values[depth])->
|
|
||||||
pairs[js_ctx->index[depth]].name;
|
|
||||||
}
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
/*---------------------------------------------------------------------------*/
|
|
||||||
int ICACHE_FLASH_ATTR
|
|
||||||
jsontree_print_next(struct jsontree_context *js_ctx)
|
|
||||||
{
|
|
||||||
struct jsontree_value *v;
|
|
||||||
int index;
|
|
||||||
|
|
||||||
v = js_ctx->values[js_ctx->depth];
|
|
||||||
|
|
||||||
/* Default operation after switch is to back up one level */
|
|
||||||
switch(v->type) {
|
|
||||||
case JSON_TYPE_OBJECT:
|
|
||||||
case JSON_TYPE_ARRAY: {
|
|
||||||
struct jsontree_array *o = (struct jsontree_array *)v;
|
|
||||||
struct jsontree_value *ov;
|
|
||||||
|
|
||||||
index = js_ctx->index[js_ctx->depth];
|
|
||||||
if(index == 0) {
|
|
||||||
js_ctx->putchar(v->type);
|
|
||||||
js_ctx->putchar('\n');
|
|
||||||
}
|
|
||||||
if(index >= o->count) {
|
|
||||||
js_ctx->putchar('\n');
|
|
||||||
js_ctx->putchar(v->type + 2);
|
|
||||||
/* Default operation: back up one level! */
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(index > 0) {
|
|
||||||
js_ctx->putchar(',');
|
|
||||||
js_ctx->putchar('\n');
|
|
||||||
}
|
|
||||||
if(v->type == JSON_TYPE_OBJECT) {
|
|
||||||
jsontree_write_string(js_ctx,
|
|
||||||
((struct jsontree_object *)o)->pairs[index].name);
|
|
||||||
js_ctx->putchar(':');
|
|
||||||
ov = ((struct jsontree_object *)o)->pairs[index].value;
|
|
||||||
} else {
|
|
||||||
ov = o->values[index];
|
|
||||||
}
|
|
||||||
/* TODO check max depth */
|
|
||||||
js_ctx->depth++; /* step down to value... */
|
|
||||||
js_ctx->index[js_ctx->depth] = 0; /* and init index */
|
|
||||||
js_ctx->values[js_ctx->depth] = ov;
|
|
||||||
/* Continue on this new level */
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
case JSON_TYPE_STRING:
|
|
||||||
jsontree_write_string(js_ctx, ((struct jsontree_string *)v)->value);
|
|
||||||
/* Default operation: back up one level! */
|
|
||||||
break;
|
|
||||||
case JSON_TYPE_INT:
|
|
||||||
jsontree_write_int(js_ctx, ((struct jsontree_int *)v)->value);
|
|
||||||
/* Default operation: back up one level! */
|
|
||||||
break;
|
|
||||||
case JSON_TYPE_CALLBACK: { /* pre-formatted json string currently */
|
|
||||||
struct jsontree_callback *callback;
|
|
||||||
|
|
||||||
callback = (struct jsontree_callback *)v;
|
|
||||||
if(js_ctx->index[js_ctx->depth] == 0) {
|
|
||||||
/* First call: reset the callback status */
|
|
||||||
js_ctx->callback_state = 0;
|
|
||||||
}
|
|
||||||
if(callback->output == NULL) {
|
|
||||||
jsontree_write_string(js_ctx, "");
|
|
||||||
} else if(callback->output(js_ctx)) {
|
|
||||||
/* The callback wants to output more */
|
|
||||||
js_ctx->index[js_ctx->depth]++;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
/* Default operation: back up one level! */
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
PRINTF("\nError: Illegal json type:'%c'\n", v->type);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
/* Done => back up one level! */
|
|
||||||
if(js_ctx->depth > 0) {
|
|
||||||
js_ctx->depth--;
|
|
||||||
js_ctx->index[js_ctx->depth]++;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
/*---------------------------------------------------------------------------*/
|
|
||||||
static struct jsontree_value *ICACHE_FLASH_ATTR
|
|
||||||
find_next(struct jsontree_context *js_ctx)
|
|
||||||
{
|
|
||||||
struct jsontree_value *v;
|
|
||||||
int index;
|
|
||||||
|
|
||||||
do {
|
|
||||||
v = js_ctx->values[js_ctx->depth];
|
|
||||||
|
|
||||||
/* Default operation after switch is to back up one level */
|
|
||||||
switch(v->type) {
|
|
||||||
case JSON_TYPE_OBJECT:
|
|
||||||
case JSON_TYPE_ARRAY: {
|
|
||||||
struct jsontree_array *o = (struct jsontree_array *)v;
|
|
||||||
struct jsontree_value *ov;
|
|
||||||
|
|
||||||
index = js_ctx->index[js_ctx->depth];
|
|
||||||
if(index >= o->count) {
|
|
||||||
/* Default operation: back up one level! */
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(v->type == JSON_TYPE_OBJECT) {
|
|
||||||
ov = ((struct jsontree_object *)o)->pairs[index].value;
|
|
||||||
} else {
|
|
||||||
ov = o->values[index];
|
|
||||||
}
|
|
||||||
/* TODO check max depth */
|
|
||||||
js_ctx->depth++; /* step down to value... */
|
|
||||||
js_ctx->index[js_ctx->depth] = 0; /* and init index */
|
|
||||||
js_ctx->values[js_ctx->depth] = ov;
|
|
||||||
/* Continue on this new level */
|
|
||||||
return ov;
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
/* Default operation: back up one level! */
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
/* Done => back up one level! */
|
|
||||||
if(js_ctx->depth > 0) {
|
|
||||||
js_ctx->depth--;
|
|
||||||
js_ctx->index[js_ctx->depth]++;
|
|
||||||
} else {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
} while(1);
|
|
||||||
}
|
|
||||||
/*---------------------------------------------------------------------------*/
|
|
||||||
struct jsontree_value *ICACHE_FLASH_ATTR
|
|
||||||
jsontree_find_next(struct jsontree_context *js_ctx, int type)
|
|
||||||
{
|
|
||||||
struct jsontree_value *v;
|
|
||||||
|
|
||||||
while((v = find_next(js_ctx)) != NULL && v->type != type &&
|
|
||||||
js_ctx->path < js_ctx->depth) {
|
|
||||||
/* search */
|
|
||||||
}
|
|
||||||
js_ctx->callback_state = 0;
|
|
||||||
return js_ctx->path < js_ctx->depth ? v : NULL;
|
|
||||||
}
|
|
||||||
/*---------------------------------------------------------------------------*/
|
|
||||||
#endif
|
|
||||||
|
|
Loading…
Reference in New Issue