123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- /*
- * 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>
- */
-
- //#include "contiki.h"
- #include "jsontree.h"
- #include "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 FUNCTION_ATTRIBUTE
- jsontree_write_atom(const struct jsontree_context *js_ctx, const char *text)
- {
- if(text == NULL) {
- js_ctx->putinchar('0');
- } else {
- while(*text != '\0') {
- js_ctx->putinchar(*text++);
- }
- }
- }
- /*---------------------------------------------------------------------------*/
- void FUNCTION_ATTRIBUTE
- jsontree_write_string(const struct jsontree_context *js_ctx, const char *text)
- {
- js_ctx->putinchar('"');
- if(text != NULL) {
- while(*text != '\0') {
- if(*text == '"') {
- js_ctx->putinchar('\\');
- }
- js_ctx->putinchar(*text++);
- }
- }
- js_ctx->putinchar('"');
- }
- /*---------------------------------------------------------------------------*/
- void FUNCTION_ATTRIBUTE
- jsontree_write_int(const struct jsontree_context *js_ctx, int value)
- {
- char buf[10];
- int l;
- if(value < 0) {
- js_ctx->putinchar('-');
- 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->putinchar(buf[l]);
- }
- }
- /*---------------------------------------------------------------------------*/
- void FUNCTION_ATTRIBUTE
- jsontree_write_int_array(const struct jsontree_context *js_ctx, const int *text, uint32_t length)
- {
- uint32_t i = 0;
- if(text == NULL) {
- js_ctx->putinchar('0');
- } else {
- for (i = 0; i < length - 1; i ++) {
- jsontree_write_int(js_ctx, *text++);
- js_ctx->putinchar(',');
- }
- jsontree_write_int(js_ctx, *text);
- }
- }
- /*---------------------------------------------------------------------------*/
- void FUNCTION_ATTRIBUTE
- jsontree_setup(struct jsontree_context *js_ctx, struct jsontree_value *root,
- int (* putchar)(int))
- {
- js_ctx->values[0] = root;
- js_ctx->putinchar = putchar;
- js_ctx->path = 0;
- jsontree_reset(js_ctx);
- }
- /*---------------------------------------------------------------------------*/
- void FUNCTION_ATTRIBUTE
- jsontree_reset(struct jsontree_context *js_ctx)
- {
- js_ctx->depth = 0;
- js_ctx->index[0] = 0;
- }
- /*---------------------------------------------------------------------------*/
- const char *FUNCTION_ATTRIBUTE
- 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 FUNCTION_ATTRIBUTE
- 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->putinchar(v->type);
- js_ctx->putinchar('\n');
- }
- if(index >= o->count) {
- js_ctx->putinchar('\n');
- js_ctx->putinchar(v->type + 2);
- /* Default operation: back up one level! */
- break;
- }
- if(index > 0) {
- js_ctx->putinchar(',');
- js_ctx->putinchar('\n');
- }
- if(v->type == JSON_TYPE_OBJECT) {
- jsontree_write_string(js_ctx,
- ((struct jsontree_object *)o)->pairs[index].name);
- js_ctx->putinchar(':');
- 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 *FUNCTION_ATTRIBUTE
- 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 *FUNCTION_ATTRIBUTE
- 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;
- }
- /*---------------------------------------------------------------------------*/
|