jsontree.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright (c) 2011-2012, Swedish Institute of Computer Science.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the Institute nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. *
  29. * This file is part of the Contiki operating system.
  30. */
  31. /**
  32. * \file
  33. * JSON output generation
  34. * \author
  35. * Niclas Finne <nfi@sics.se>
  36. * Joakim Eriksson <joakime@sics.se>
  37. */
  38. #ifndef __JSONTREE_H__
  39. #define __JSONTREE_H__
  40. #include "pando_types.h"
  41. #include "json.h"
  42. #ifdef JSONTREE_CONF_MAX_DEPTH
  43. #define JSONTREE_MAX_DEPTH JSONTREE_CONF_MAX_DEPTH
  44. #else
  45. #define JSONTREE_MAX_DEPTH 10
  46. #endif /* JSONTREE_CONF_MAX_DEPTH */
  47. struct jsontree_context {
  48. struct jsontree_value *values[JSONTREE_MAX_DEPTH];
  49. uint16_t index[JSONTREE_MAX_DEPTH];
  50. int (* putinchar)(int);
  51. uint8_t depth;
  52. uint8_t path;
  53. int callback_state;
  54. };
  55. struct jsontree_value {
  56. uint8_t type;
  57. /* followed by a value */
  58. };
  59. struct jsontree_string {
  60. uint8_t type;
  61. const char *value;
  62. };
  63. struct jsontree_int {
  64. uint8_t type;
  65. int value;
  66. };
  67. /* NOTE: the jsontree_callback set will receive a jsonparse state */
  68. struct jsonparse_state;
  69. struct jsontree_callback {
  70. uint8_t type;
  71. int (* output)(struct jsontree_context *js_ctx);
  72. int (* set)(struct jsontree_context *js_ctx, struct jsonparse_state *parser);
  73. };
  74. struct jsontree_pair {
  75. const char *name;
  76. struct jsontree_value *value;
  77. };
  78. struct jsontree_object {
  79. uint8_t type;
  80. uint8_t count;
  81. struct jsontree_pair *pairs;
  82. };
  83. struct jsontree_array {
  84. uint8_t type;
  85. uint8_t count;
  86. struct jsontree_value **values;
  87. };
  88. #define JSONTREE_INT(num) {JSON_TYPE_INT, (num)}
  89. #define JSONTREE_STRING(text) {JSON_TYPE_STRING, (text)}
  90. #define JSONTREE_PAIR(name, value) {(name), (struct jsontree_value *)(value)}
  91. #define JSONTREE_CALLBACK(output, set) {JSON_TYPE_CALLBACK, (output), (set)}
  92. #define JSONTREE_OBJECT(name, ...) \
  93. static struct jsontree_pair jsontree_pair_##name[] = {__VA_ARGS__}; \
  94. static struct jsontree_object name = { \
  95. JSON_TYPE_OBJECT, \
  96. sizeof(jsontree_pair_##name)/sizeof(struct jsontree_pair), \
  97. jsontree_pair_##name }
  98. #define JSONTREE_PAIR_ARRAY(value) (struct jsontree_value *)(value)
  99. #define JSONTREE_ARRAY(name, ...) \
  100. static struct jsontree_value* jsontree_value_##name[] = {__VA_ARGS__}; \
  101. static struct jsontree_array name = { \
  102. JSON_TYPE_ARRAY, \
  103. sizeof(jsontree_value_##name)/sizeof(struct jsontree_value*), \
  104. jsontree_value_##name }
  105. #define JSONTREE_OBJECT_EXT(name, ...) \
  106. struct jsontree_pair jsontree_pair_##name[] = {__VA_ARGS__}; \
  107. struct jsontree_object name = { \
  108. JSON_TYPE_OBJECT, \
  109. sizeof(jsontree_pair_##name)/sizeof(struct jsontree_pair), \
  110. jsontree_pair_##name }
  111. void jsontree_setup(struct jsontree_context *js_ctx,
  112. struct jsontree_value *root, int (* putchar)(int));
  113. void jsontree_reset(struct jsontree_context *js_ctx);
  114. const char *jsontree_path_name(const struct jsontree_context *js_ctx,
  115. int depth);
  116. void jsontree_write_int(const struct jsontree_context *js_ctx, int value);
  117. void jsontree_write_int_array(const struct jsontree_context *js_ctx, const int *text, uint32_t length);
  118. void jsontree_write_atom(const struct jsontree_context *js_ctx,
  119. const char *text);
  120. void jsontree_write_string(const struct jsontree_context *js_ctx,
  121. const char *text);
  122. int jsontree_print_next(struct jsontree_context *js_ctx);
  123. struct jsontree_value *jsontree_find_next(struct jsontree_context *js_ctx,
  124. int type);
  125. #endif /* __JSONTREE_H__ */