/aJSON.h

http://github.com/interactive-matter/aJson · C Header · 254 lines · 136 code · 41 blank · 77 comment · 0 complexity · b180082e910372878210ce2d6ec33732 MD5 · raw file

  1. /*
  2. Copyright (c) 2001, Interactive Matter, Marcus Nowotny
  3. Based on the cJSON Library, Copyright (C) 2009 Dave Gamble
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. #ifndef aJson__h
  21. #define aJson__h
  22. #include <Print.h>
  23. #include <Stream.h>
  24. #include <Client.h>
  25. #include <Arduino.h> // To get access to the Arduino millis() function
  26. /******************************************************************************
  27. * Definitions
  28. ******************************************************************************/
  29. // aJson Types:
  30. #define aJson_NULL 0
  31. #define aJson_Boolean 1
  32. #define aJson_Int 2
  33. #define aJson_Float 3
  34. #define aJson_String 4
  35. #define aJson_Array 5
  36. #define aJson_Object 6
  37. #define aJson_IsReference 128
  38. #ifndef EOF
  39. #define EOF -1
  40. #endif
  41. #define PRINT_BUFFER_LEN 256
  42. // The aJson structure:
  43. typedef struct aJsonObject {
  44. char *name; // The item's name string, if this item is the child of, or is in the list of subitems of an object.
  45. struct aJsonObject *next, *prev; // next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem
  46. struct aJsonObject *child; // An array or object item will have a child pointer pointing to a chain of the items in the array/object.
  47. char type; // The type of the item, as above.
  48. union {
  49. char *valuestring; // The item's string, if type==aJson_String
  50. char valuebool; //the items value for true & false
  51. int valueint; // The item's value, if type==aJson_Int
  52. double valuefloat; // The item's value, if type==aJson_Float
  53. };
  54. } aJsonObject;
  55. /* aJsonStream is stream representation of aJson for its internal use;
  56. * it is meant to abstract out differences between Stream (e.g. serial
  57. * stream) and Client (which may or may not be connected) or provide even
  58. * stream-ish interface to string buffers. */
  59. class aJsonStream : public Print {
  60. public:
  61. aJsonStream(Stream *stream_)
  62. : stream_obj(stream_), bucket(EOF)
  63. {}
  64. /* Use this to check if more data is available, as aJsonStream
  65. * can read some more data than really consumed and automatically
  66. * skips separating whitespace if you use this method. */
  67. virtual bool available();
  68. int parseNumber(aJsonObject *item);
  69. int printInt(aJsonObject *item);
  70. int printFloat(aJsonObject *item);
  71. int parseString(aJsonObject *item);
  72. int printStringPtr(const char *str);
  73. int printString(aJsonObject *item);
  74. int skip();
  75. int flush();
  76. int parseValue(aJsonObject *item, char** filter);
  77. int printValue(aJsonObject *item);
  78. int parseArray(aJsonObject *item, char** filter);
  79. int printArray(aJsonObject *item);
  80. int parseObject(aJsonObject *item, char** filter);
  81. int printObject(aJsonObject *item);
  82. protected:
  83. /* Blocking load of character, returning EOF if the stream
  84. * is exhausted. */
  85. /* Base implementation just looks at bucket, returns EOF
  86. * otherwise; descendats take care of the real reading. */
  87. virtual int getch();
  88. virtual size_t readBytes(uint8_t *buffer, size_t len);
  89. /* Return the character back to the front of the stream
  90. * after loading it with getch(). Only returning a single
  91. * character is supported. */
  92. virtual void ungetch(char ch);
  93. /* Inherited from class Print. */
  94. virtual size_t write(uint8_t ch);
  95. /* stream attribute is used only from virtual functions,
  96. * therefore an object inheriting aJsonStream may avoid
  97. * using streams completely. */
  98. Stream *stream_obj;
  99. /* Use this accessor for stream retrieval; some subclasses
  100. * may use their own stream subclass. */
  101. virtual inline Stream *stream() { return stream_obj; }
  102. /* bucket is EOF by default. Otherwise, it is a character
  103. * to be returned by next getch() - returned by a call
  104. * to ungetch(). */
  105. int bucket;
  106. };
  107. /* JSON stream that consumes data from a connection (usually
  108. * Ethernet client) until the connection is closed. */
  109. class aJsonClientStream : public aJsonStream {
  110. public:
  111. aJsonClientStream(Client *stream_)
  112. : aJsonStream(NULL), client_obj(stream_)
  113. {}
  114. private:
  115. virtual int getch();
  116. Client *client_obj;
  117. virtual inline Client *stream() { return client_obj; }
  118. };
  119. /* JSON stream that is bound to input and output string buffer. This is
  120. * for internal usage by string-based aJsonClass methods. */
  121. /* TODO: Elastic output buffer support. */
  122. class aJsonStringStream : public aJsonStream {
  123. public:
  124. /* Either of inbuf, outbuf can be NULL if you do not care about
  125. * particular I/O direction. */
  126. aJsonStringStream(char *inbuf_, char *outbuf_ = NULL, size_t outbuf_len_ = 0)
  127. : aJsonStream(NULL), inbuf(inbuf_), outbuf(outbuf_), outbuf_len(outbuf_len_)
  128. {
  129. inbuf_len = inbuf ? strlen(inbuf) : 0;
  130. }
  131. virtual bool available();
  132. private:
  133. virtual int getch();
  134. virtual size_t write(uint8_t ch);
  135. char *inbuf, *outbuf;
  136. size_t inbuf_len, outbuf_len;
  137. };
  138. class aJsonClass {
  139. /******************************************************************************
  140. * Constructors
  141. ******************************************************************************/
  142. /******************************************************************************
  143. * User API
  144. ******************************************************************************/
  145. public:
  146. // Supply a block of JSON, and this returns a aJson object you can interrogate. Call aJson.deleteItem when finished.
  147. aJsonObject* parse(aJsonStream* stream); //Reads from a stream
  148. aJsonObject* parse(aJsonStream* stream,char** filter_values); //Read from a file, but only return values include in the char* array filter_values
  149. aJsonObject* parse(char *value); //Reads from a string
  150. // Render a aJsonObject entity to text for transfer/storage. Free the char* when finished.
  151. int print(aJsonObject *item, aJsonStream* stream);
  152. char* print(aJsonObject* item);
  153. //Renders a aJsonObject directly to a output stream
  154. char stream(aJsonObject *item, aJsonStream* stream);
  155. // Delete a aJsonObject entity and all sub-entities.
  156. void deleteItem(aJsonObject *c);
  157. // Returns the number of items in an array (or object).
  158. unsigned char getArraySize(aJsonObject *array);
  159. // Retrieve item number "item" from array "array". Returns NULL if unsuccessful.
  160. aJsonObject* getArrayItem(aJsonObject *array, unsigned char item);
  161. // Get item "string" from object. Case insensitive.
  162. aJsonObject* getObjectItem(aJsonObject *object, const char *string);
  163. // These calls create a aJsonObject item of the appropriate type.
  164. aJsonObject* createNull();
  165. aJsonObject* createItem(bool b);
  166. aJsonObject* createItem(char b);
  167. aJsonObject* createItem(int num);
  168. aJsonObject* createItem(double num);
  169. aJsonObject* createItem(const char *string);
  170. aJsonObject* createArray();
  171. aJsonObject* createObject();
  172. // These utilities create an Array of count items.
  173. aJsonObject* createIntArray(int *numbers, unsigned char count);
  174. aJsonObject* createFloatArray(double *numbers, unsigned char count);
  175. aJsonObject* createDoubleArray(double *numbers, unsigned char count);
  176. aJsonObject* createStringArray(const char **strings, unsigned char count);
  177. // Append item to the specified array/object.
  178. void addItemToArray(aJsonObject *array, aJsonObject *item);
  179. void addItemToObject(aJsonObject *object, const char *string,
  180. aJsonObject *item);
  181. // Append reference to item to the specified array/object. Use this when you want to add an existing aJsonObject to a new aJsonObject, but don't want to corrupt your existing aJsonObject.
  182. void addItemReferenceToArray(aJsonObject *array, aJsonObject *item);
  183. void addItemReferenceToObject(aJsonObject *object, const char *string,
  184. aJsonObject *item);
  185. // Remove/Detach items from Arrays/Objects.
  186. aJsonObject* detachItemFromArray(aJsonObject *array, unsigned char which);
  187. void deleteItemFromArray(aJsonObject *array, unsigned char which);
  188. aJsonObject* detachItemFromObject(aJsonObject *object, const char *string);
  189. void deleteItemFromObject(aJsonObject *object, const char *string);
  190. // Update array items.
  191. void replaceItemInArray(aJsonObject *array, unsigned char which,
  192. aJsonObject *newitem);
  193. void replaceItemInObject(aJsonObject *object, const char *string,
  194. aJsonObject *newitem);
  195. void addNullToObject(aJsonObject* object, const char* name);
  196. void addBooleanToObject(aJsonObject* object, const char* name, bool b);
  197. void addNumberToObject(aJsonObject* object, const char* name, int n);
  198. void addNumberToObject(aJsonObject* object, const char* name, double n);
  199. void addStringToObject(aJsonObject* object, const char* name,
  200. const char* s);
  201. protected:
  202. friend class aJsonStream;
  203. static aJsonObject* newItem();
  204. private:
  205. void suffixObject(aJsonObject *prev, aJsonObject *item);
  206. aJsonObject* createReference(aJsonObject *item);
  207. };
  208. extern aJsonClass aJson;
  209. #endif