aJson is the attempt to port a complete JSON implementation to Arduino. It is based on the cJSON implementation, reduced in size and removing one or two feature. The current mbed implementation only supports FILE* as input so you will have to use a temporary file for parsing your json input. https://github.com/interactive-matter/aJson
aJSON.h@2:ece3b5c4afed, 2012-09-07 (annotated)
- Committer:
- mimil
- Date:
- Fri Sep 07 09:30:33 2012 +0000
- Revision:
- 2:ece3b5c4afed
- Parent:
- 1:6df1d1f1b372
update to last git version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mimil | 1:6df1d1f1b372 | 1 | /* |
mimil | 1:6df1d1f1b372 | 2 | Copyright (c) 2001, Interactive Matter, Marcus Nowotny |
mimil | 1:6df1d1f1b372 | 3 | |
mimil | 1:6df1d1f1b372 | 4 | Based on the cJSON Library, Copyright (C) 2009 Dave Gamble |
mimil | 1:6df1d1f1b372 | 5 | |
mimil | 1:6df1d1f1b372 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy |
mimil | 1:6df1d1f1b372 | 7 | of this software and associated documentation files (the "Software"), to deal |
mimil | 1:6df1d1f1b372 | 8 | in the Software without restriction, including without limitation the rights |
mimil | 1:6df1d1f1b372 | 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
mimil | 1:6df1d1f1b372 | 10 | copies of the Software, and to permit persons to whom the Software is |
mimil | 1:6df1d1f1b372 | 11 | furnished to do so, subject to the following conditions: |
mimil | 1:6df1d1f1b372 | 12 | |
mimil | 1:6df1d1f1b372 | 13 | The above copyright notice and this permission notice shall be included in |
mimil | 1:6df1d1f1b372 | 14 | all copies or substantial portions of the Software. |
mimil | 1:6df1d1f1b372 | 15 | |
mimil | 1:6df1d1f1b372 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
mimil | 1:6df1d1f1b372 | 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
mimil | 1:6df1d1f1b372 | 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
mimil | 1:6df1d1f1b372 | 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
mimil | 1:6df1d1f1b372 | 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
mimil | 1:6df1d1f1b372 | 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
mimil | 1:6df1d1f1b372 | 22 | THE SOFTWARE. |
mimil | 1:6df1d1f1b372 | 23 | */ |
mimil | 1:6df1d1f1b372 | 24 | |
mimil | 1:6df1d1f1b372 | 25 | #ifndef aJson__h |
mimil | 1:6df1d1f1b372 | 26 | #define aJson__h |
mimil | 1:6df1d1f1b372 | 27 | |
mimil | 1:6df1d1f1b372 | 28 | /****************************************************************************** |
mimil | 1:6df1d1f1b372 | 29 | * Includes |
mimil | 1:6df1d1f1b372 | 30 | ******************************************************************************/ |
mimil | 1:6df1d1f1b372 | 31 | #include <stdio.h> |
mimil | 1:6df1d1f1b372 | 32 | |
mimil | 1:6df1d1f1b372 | 33 | |
mimil | 1:6df1d1f1b372 | 34 | /****************************************************************************** |
mimil | 1:6df1d1f1b372 | 35 | * Definitions |
mimil | 1:6df1d1f1b372 | 36 | ******************************************************************************/ |
mimil | 1:6df1d1f1b372 | 37 | // aJson Types: |
mimil | 1:6df1d1f1b372 | 38 | #define aJson_False 0 |
mimil | 1:6df1d1f1b372 | 39 | #define aJson_True 1 |
mimil | 1:6df1d1f1b372 | 40 | #define aJson_NULL 2 |
mimil | 1:6df1d1f1b372 | 41 | #define aJson_Int 3 |
mimil | 1:6df1d1f1b372 | 42 | #define aJson_Float 4 |
mimil | 1:6df1d1f1b372 | 43 | #define aJson_String 5 |
mimil | 1:6df1d1f1b372 | 44 | #define aJson_Array 6 |
mimil | 1:6df1d1f1b372 | 45 | #define aJson_Object 7 |
mimil | 1:6df1d1f1b372 | 46 | |
mimil | 1:6df1d1f1b372 | 47 | #define aJson_IsReference 128 |
mimil | 1:6df1d1f1b372 | 48 | |
mimil | 1:6df1d1f1b372 | 49 | // The aJson structure: |
mimil | 1:6df1d1f1b372 | 50 | typedef struct aJsonObject { |
mimil | 1:6df1d1f1b372 | 51 | char *name; // The item's name string, if this item is the child of, or is in the list of subitems of an object. |
mimil | 1:6df1d1f1b372 | 52 | struct aJsonObject *next, *prev; // next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem |
mimil | 1:6df1d1f1b372 | 53 | struct aJsonObject *child; // An array or object item will have a child pointer pointing to a chain of the items in the array/object. |
mimil | 1:6df1d1f1b372 | 54 | |
mimil | 1:6df1d1f1b372 | 55 | char type; // The type of the item, as above. |
mimil | 1:6df1d1f1b372 | 56 | |
mimil | 1:6df1d1f1b372 | 57 | union { |
mimil | 1:6df1d1f1b372 | 58 | char *valuestring; // The item's string, if type==aJson_String |
mimil | 1:6df1d1f1b372 | 59 | char valuebool; //the items value for true & false |
mimil | 1:6df1d1f1b372 | 60 | int valueint; // The item's number, if type==aJson_Number |
mimil | 1:6df1d1f1b372 | 61 | double valuefloat; // The item's number, if type==aJson_Number |
mimil | 1:6df1d1f1b372 | 62 | }; |
mimil | 1:6df1d1f1b372 | 63 | } aJsonObject; |
mimil | 1:6df1d1f1b372 | 64 | |
mimil | 1:6df1d1f1b372 | 65 | class aJsonClass { |
mimil | 1:6df1d1f1b372 | 66 | /****************************************************************************** |
mimil | 1:6df1d1f1b372 | 67 | * Constructors |
mimil | 1:6df1d1f1b372 | 68 | ******************************************************************************/ |
mimil | 1:6df1d1f1b372 | 69 | |
mimil | 1:6df1d1f1b372 | 70 | /****************************************************************************** |
mimil | 1:6df1d1f1b372 | 71 | * User API |
mimil | 1:6df1d1f1b372 | 72 | ******************************************************************************/ |
mimil | 1:6df1d1f1b372 | 73 | public: |
mimil | 1:6df1d1f1b372 | 74 | // Supply a block of JSON, and this returns a aJson object you can interrogate. Call aJson.deleteItem when finished. |
mimil | 1:6df1d1f1b372 | 75 | aJsonObject* parse(FILE* stream); //Reads from a stream |
mimil | 1:6df1d1f1b372 | 76 | aJsonObject* parse(FILE* stream,char** filter_values); //Read from a file, but only return values include in the char* array filter_values |
mimil | 1:6df1d1f1b372 | 77 | aJsonObject* parse(char *value); //Reads from a string |
mimil | 1:6df1d1f1b372 | 78 | // Render a aJsonObject entity to text for transfer/storage. Free the char* when finished. |
mimil | 1:6df1d1f1b372 | 79 | int print(aJsonObject *item, FILE* stream); |
mimil | 1:6df1d1f1b372 | 80 | char* print(aJsonObject* item); |
mimil | 1:6df1d1f1b372 | 81 | //Renders a aJsonObject directly to a output stream |
mimil | 1:6df1d1f1b372 | 82 | char stream(aJsonObject *item, FILE* stream); |
mimil | 1:6df1d1f1b372 | 83 | // Delete a aJsonObject entity and all sub-entities. |
mimil | 1:6df1d1f1b372 | 84 | void deleteItem(aJsonObject *c); |
mimil | 1:6df1d1f1b372 | 85 | |
mimil | 1:6df1d1f1b372 | 86 | // Returns the number of items in an array (or object). |
mimil | 1:6df1d1f1b372 | 87 | unsigned char getArraySize(aJsonObject *array); |
mimil | 1:6df1d1f1b372 | 88 | // Retrieve item number "item" from array "array". Returns NULL if unsuccessful. |
mimil | 1:6df1d1f1b372 | 89 | aJsonObject* getArrayItem(aJsonObject *array, unsigned char item); |
mimil | 1:6df1d1f1b372 | 90 | // Get item "string" from object. Case insensitive. |
mimil | 1:6df1d1f1b372 | 91 | aJsonObject* getObjectItem(aJsonObject *object, const char *string); |
mimil | 1:6df1d1f1b372 | 92 | |
mimil | 1:6df1d1f1b372 | 93 | // These calls create a aJsonObject item of the appropriate type. |
mimil | 1:6df1d1f1b372 | 94 | aJsonObject* createNull(); |
mimil | 1:6df1d1f1b372 | 95 | aJsonObject* createTrue(); |
mimil | 1:6df1d1f1b372 | 96 | aJsonObject* createFalse(); |
mimil | 1:6df1d1f1b372 | 97 | aJsonObject* createItem(char b); |
mimil | 1:6df1d1f1b372 | 98 | aJsonObject* createItem(int num); |
mimil | 1:6df1d1f1b372 | 99 | aJsonObject* createItem(double num); |
mimil | 1:6df1d1f1b372 | 100 | aJsonObject* createItem(const char *string); |
mimil | 1:6df1d1f1b372 | 101 | aJsonObject* createArray(); |
mimil | 1:6df1d1f1b372 | 102 | aJsonObject* createObject(); |
mimil | 1:6df1d1f1b372 | 103 | |
mimil | 1:6df1d1f1b372 | 104 | // These utilities create an Array of count items. |
mimil | 1:6df1d1f1b372 | 105 | aJsonObject* createIntArray(int *numbers, unsigned char count); |
mimil | 1:6df1d1f1b372 | 106 | aJsonObject* createFloatArray(double *numbers, unsigned char count); |
mimil | 1:6df1d1f1b372 | 107 | aJsonObject* createDoubleArray(double *numbers, unsigned char count); |
mimil | 1:6df1d1f1b372 | 108 | aJsonObject* createStringArray(const char **strings, unsigned char count); |
mimil | 1:6df1d1f1b372 | 109 | |
mimil | 1:6df1d1f1b372 | 110 | // Append item to the specified array/object. |
mimil | 1:6df1d1f1b372 | 111 | void addItemToArray(aJsonObject *array, aJsonObject *item); |
mimil | 1:6df1d1f1b372 | 112 | void addItemToObject(aJsonObject *object, const char *string, |
mimil | 1:6df1d1f1b372 | 113 | aJsonObject *item); |
mimil | 1:6df1d1f1b372 | 114 | // 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. |
mimil | 1:6df1d1f1b372 | 115 | void addItemReferenceToArray(aJsonObject *array, aJsonObject *item); |
mimil | 1:6df1d1f1b372 | 116 | void addItemReferenceToObject(aJsonObject *object, const char *string, |
mimil | 1:6df1d1f1b372 | 117 | aJsonObject *item); |
mimil | 1:6df1d1f1b372 | 118 | |
mimil | 1:6df1d1f1b372 | 119 | // Remove/Detach items from Arrays/Objects. |
mimil | 1:6df1d1f1b372 | 120 | aJsonObject* detachItemFromArray(aJsonObject *array, unsigned char which); |
mimil | 1:6df1d1f1b372 | 121 | void deleteItemFromArray(aJsonObject *array, unsigned char which); |
mimil | 1:6df1d1f1b372 | 122 | aJsonObject* detachItemFromObject(aJsonObject *object, const char *string); |
mimil | 1:6df1d1f1b372 | 123 | void deleteItemFromObject(aJsonObject *object, const char *string); |
mimil | 1:6df1d1f1b372 | 124 | |
mimil | 1:6df1d1f1b372 | 125 | // Update array items. |
mimil | 1:6df1d1f1b372 | 126 | void replaceItemInArray(aJsonObject *array, unsigned char which, |
mimil | 1:6df1d1f1b372 | 127 | aJsonObject *newitem); |
mimil | 1:6df1d1f1b372 | 128 | void replaceItemInObject(aJsonObject *object, const char *string, |
mimil | 1:6df1d1f1b372 | 129 | aJsonObject *newitem); |
mimil | 1:6df1d1f1b372 | 130 | |
mimil | 1:6df1d1f1b372 | 131 | void addNullToObject(aJsonObject* object, const char* name); |
mimil | 1:6df1d1f1b372 | 132 | void addTrueToObject(aJsonObject* object, const char* name); |
mimil | 1:6df1d1f1b372 | 133 | void addFalseToObject(aJsonObject* object, const char* name); |
mimil | 1:6df1d1f1b372 | 134 | void addNumberToObject(aJsonObject* object, const char* name, int n); |
mimil | 1:6df1d1f1b372 | 135 | void addNumberToObject(aJsonObject* object, const char* name, double n); |
mimil | 1:6df1d1f1b372 | 136 | void addStringToObject(aJsonObject* object, const char* name, |
mimil | 1:6df1d1f1b372 | 137 | const char* s); |
mimil | 1:6df1d1f1b372 | 138 | |
mimil | 1:6df1d1f1b372 | 139 | private: |
mimil | 1:6df1d1f1b372 | 140 | aJsonObject* newItem(); |
mimil | 1:6df1d1f1b372 | 141 | int parseNumber(aJsonObject *item, FILE* stream); |
mimil | 1:6df1d1f1b372 | 142 | int printInt(aJsonObject *item, FILE* stream); |
mimil | 1:6df1d1f1b372 | 143 | int printFloat(aJsonObject *item, FILE* stream); |
mimil | 1:6df1d1f1b372 | 144 | |
mimil | 1:6df1d1f1b372 | 145 | int parseString(aJsonObject *item, FILE* stream); |
mimil | 1:6df1d1f1b372 | 146 | int printStringPtr(const char *str, FILE* stream); |
mimil | 1:6df1d1f1b372 | 147 | int printString(aJsonObject *item, FILE* stream); |
mimil | 1:6df1d1f1b372 | 148 | |
mimil | 1:6df1d1f1b372 | 149 | int skip(FILE* stream); |
mimil | 1:6df1d1f1b372 | 150 | |
mimil | 1:6df1d1f1b372 | 151 | int parseValue(aJsonObject *item, FILE* stream, char** filter); |
mimil | 1:6df1d1f1b372 | 152 | int printValue(aJsonObject *item, FILE* stream); |
mimil | 1:6df1d1f1b372 | 153 | |
mimil | 1:6df1d1f1b372 | 154 | int parseArray(aJsonObject *item, FILE* stream, char** filter); |
mimil | 1:6df1d1f1b372 | 155 | int printArray(aJsonObject *item, FILE* stream); |
mimil | 1:6df1d1f1b372 | 156 | |
mimil | 1:6df1d1f1b372 | 157 | int parseObject(aJsonObject *item, FILE* stream, char** filter); |
mimil | 1:6df1d1f1b372 | 158 | int printObject(aJsonObject *item, FILE* stream); |
mimil | 1:6df1d1f1b372 | 159 | void suffixObject(aJsonObject *prev, aJsonObject *item); |
mimil | 1:6df1d1f1b372 | 160 | |
mimil | 1:6df1d1f1b372 | 161 | aJsonObject* createReference(aJsonObject *item); |
mimil | 1:6df1d1f1b372 | 162 | |
mimil | 1:6df1d1f1b372 | 163 | }; |
mimil | 1:6df1d1f1b372 | 164 | |
mimil | 1:6df1d1f1b372 | 165 | extern aJsonClass aJson; |
mimil | 1:6df1d1f1b372 | 166 | |
mimil | 1:6df1d1f1b372 | 167 | #endif |