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

Dependents:   WIZwikiREST20

Committer:
mimil
Date:
Mon Aug 27 15:15:45 2012 +0000
Revision:
0:428cf9a51873
Child:
1:6df1d1f1b372
[mbed] converted /Trash/aJSON

Who changed what in which revision?

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