Tyler Wilson / cJSON
Committer:
tylerwilson
Date:
Tue Feb 19 23:10:00 2013 +0000
Revision:
1:042a8cb7a643
Parent:
0:881733436890
Not sure

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tylerwilson 0:881733436890 1 /*
tylerwilson 0:881733436890 2 Copyright (c) 2009 Dave Gamble
tylerwilson 0:881733436890 3
tylerwilson 0:881733436890 4 Permission is hereby granted, free of charge, to any person obtaining a copy
tylerwilson 0:881733436890 5 of this software and associated documentation files (the "Software"), to deal
tylerwilson 0:881733436890 6 in the Software without restriction, including without limitation the rights
tylerwilson 0:881733436890 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
tylerwilson 0:881733436890 8 copies of the Software, and to permit persons to whom the Software is
tylerwilson 0:881733436890 9 furnished to do so, subject to the following conditions:
tylerwilson 0:881733436890 10
tylerwilson 0:881733436890 11 The above copyright notice and this permission notice shall be included in
tylerwilson 0:881733436890 12 all copies or substantial portions of the Software.
tylerwilson 0:881733436890 13
tylerwilson 0:881733436890 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
tylerwilson 0:881733436890 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
tylerwilson 0:881733436890 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
tylerwilson 0:881733436890 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
tylerwilson 0:881733436890 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
tylerwilson 0:881733436890 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
tylerwilson 0:881733436890 20 THE SOFTWARE.
tylerwilson 0:881733436890 21 */
tylerwilson 0:881733436890 22
tylerwilson 0:881733436890 23 #ifndef cJSON__h
tylerwilson 0:881733436890 24 #define cJSON__h
tylerwilson 0:881733436890 25
tylerwilson 0:881733436890 26 #ifdef __cplusplus
tylerwilson 0:881733436890 27 extern "C"
tylerwilson 0:881733436890 28 {
tylerwilson 0:881733436890 29 #endif
tylerwilson 0:881733436890 30
tylerwilson 0:881733436890 31 /* cJSON Types: */
tylerwilson 0:881733436890 32 #define cJSON_False 0
tylerwilson 0:881733436890 33 #define cJSON_True 1
tylerwilson 0:881733436890 34 #define cJSON_NULL 2
tylerwilson 0:881733436890 35 #define cJSON_Number 3
tylerwilson 0:881733436890 36 #define cJSON_String 4
tylerwilson 0:881733436890 37 #define cJSON_Array 5
tylerwilson 0:881733436890 38 #define cJSON_Object 6
tylerwilson 0:881733436890 39
tylerwilson 0:881733436890 40 #define cJSON_IsReference 256
tylerwilson 0:881733436890 41
tylerwilson 0:881733436890 42 /* The cJSON structure: */
tylerwilson 0:881733436890 43 typedef struct cJSON {
tylerwilson 0:881733436890 44 struct cJSON *next,*prev; /* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */
tylerwilson 0:881733436890 45 struct cJSON *child; /* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */
tylerwilson 0:881733436890 46
tylerwilson 0:881733436890 47 int type; /* The type of the item, as above. */
tylerwilson 0:881733436890 48
tylerwilson 0:881733436890 49 char *valuestring; /* The item's string, if type==cJSON_String */
tylerwilson 0:881733436890 50 int valueint; /* The item's number, if type==cJSON_Number */
tylerwilson 0:881733436890 51 double valuedouble; /* The item's number, if type==cJSON_Number */
tylerwilson 0:881733436890 52
tylerwilson 0:881733436890 53 char *string; /* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
tylerwilson 0:881733436890 54 } cJSON;
tylerwilson 0:881733436890 55
tylerwilson 0:881733436890 56 typedef struct cJSON_Hooks {
tylerwilson 0:881733436890 57 void *(*malloc_fn)(size_t sz);
tylerwilson 0:881733436890 58 void (*free_fn)(void *ptr);
tylerwilson 0:881733436890 59 } cJSON_Hooks;
tylerwilson 0:881733436890 60
tylerwilson 0:881733436890 61 /* Supply malloc, realloc and free functions to cJSON */
tylerwilson 0:881733436890 62 extern void cJSON_InitHooks(cJSON_Hooks* hooks);
tylerwilson 0:881733436890 63
tylerwilson 0:881733436890 64
tylerwilson 0:881733436890 65 /* Supply a block of JSON, and this returns a cJSON object you can interrogate. Call cJSON_Delete when finished. */
tylerwilson 0:881733436890 66 extern cJSON *cJSON_Parse(const char *value);
tylerwilson 0:881733436890 67 /* Render a cJSON entity to text for transfer/storage. Free the char* when finished. */
tylerwilson 0:881733436890 68 extern char *cJSON_Print(cJSON *item);
tylerwilson 0:881733436890 69 /* Render a cJSON entity to text for transfer/storage without any formatting. Free the char* when finished. */
tylerwilson 0:881733436890 70 extern char *cJSON_PrintUnformatted(cJSON *item);
tylerwilson 0:881733436890 71 /* Delete a cJSON entity and all subentities. */
tylerwilson 0:881733436890 72 extern void cJSON_Delete(cJSON *c);
tylerwilson 0:881733436890 73
tylerwilson 0:881733436890 74 /* Returns the number of items in an array (or object). */
tylerwilson 0:881733436890 75 extern int cJSON_GetArraySize(cJSON *array);
tylerwilson 0:881733436890 76 /* Retrieve item number "item" from array "array". Returns NULL if unsuccessful. */
tylerwilson 0:881733436890 77 extern cJSON *cJSON_GetArrayItem(cJSON *array,int item);
tylerwilson 0:881733436890 78 /* Get item "string" from object. Case insensitive. */
tylerwilson 0:881733436890 79 extern cJSON *cJSON_GetObjectItem(cJSON *object,const char *string);
tylerwilson 0:881733436890 80
tylerwilson 0:881733436890 81 /* For analysing failed parses. This returns a pointer to the parse error. You'll probably need to look a few chars back to make sense of it. Defined when cJSON_Parse() returns 0. 0 when cJSON_Parse() succeeds. */
tylerwilson 1:042a8cb7a643 82 extern const char *cJSON_GetErrorPtr(void);
tylerwilson 0:881733436890 83
tylerwilson 0:881733436890 84 /* These calls create a cJSON item of the appropriate type. */
tylerwilson 1:042a8cb7a643 85 extern cJSON *cJSON_CreateNull(void);
tylerwilson 1:042a8cb7a643 86 extern cJSON *cJSON_CreateTrue(void);
tylerwilson 1:042a8cb7a643 87 extern cJSON *cJSON_CreateFalse(void);
tylerwilson 0:881733436890 88 extern cJSON *cJSON_CreateBool(int b);
tylerwilson 0:881733436890 89 extern cJSON *cJSON_CreateNumber(double num);
tylerwilson 0:881733436890 90 extern cJSON *cJSON_CreateString(const char *string);
tylerwilson 1:042a8cb7a643 91 extern cJSON *cJSON_CreateArray(void);
tylerwilson 1:042a8cb7a643 92 extern cJSON *cJSON_CreateObject(void);
tylerwilson 0:881733436890 93
tylerwilson 0:881733436890 94 /* These utilities create an Array of count items. */
tylerwilson 0:881733436890 95 extern cJSON *cJSON_CreateIntArray(int *numbers,int count);
tylerwilson 0:881733436890 96 extern cJSON *cJSON_CreateFloatArray(float *numbers,int count);
tylerwilson 0:881733436890 97 extern cJSON *cJSON_CreateDoubleArray(double *numbers,int count);
tylerwilson 0:881733436890 98 extern cJSON *cJSON_CreateStringArray(const char **strings,int count);
tylerwilson 0:881733436890 99
tylerwilson 0:881733436890 100 /* Append item to the specified array/object. */
tylerwilson 0:881733436890 101 extern void cJSON_AddItemToArray(cJSON *array, cJSON *item);
tylerwilson 0:881733436890 102 extern void cJSON_AddItemToObject(cJSON *object,const char *string,cJSON *item);
tylerwilson 0:881733436890 103 /* Append reference to item to the specified array/object. Use this when you want to add an existing cJSON to a new cJSON, but don't want to corrupt your existing cJSON. */
tylerwilson 0:881733436890 104 extern void cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item);
tylerwilson 0:881733436890 105 extern void cJSON_AddItemReferenceToObject(cJSON *object,const char *string,cJSON *item);
tylerwilson 0:881733436890 106
tylerwilson 0:881733436890 107 /* Remove/Detatch items from Arrays/Objects. */
tylerwilson 0:881733436890 108 extern cJSON *cJSON_DetachItemFromArray(cJSON *array,int which);
tylerwilson 0:881733436890 109 extern void cJSON_DeleteItemFromArray(cJSON *array,int which);
tylerwilson 0:881733436890 110 extern cJSON *cJSON_DetachItemFromObject(cJSON *object,const char *string);
tylerwilson 0:881733436890 111 extern void cJSON_DeleteItemFromObject(cJSON *object,const char *string);
tylerwilson 0:881733436890 112
tylerwilson 0:881733436890 113 /* Update array items. */
tylerwilson 0:881733436890 114 extern void cJSON_ReplaceItemInArray(cJSON *array,int which,cJSON *newitem);
tylerwilson 0:881733436890 115 extern void cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem);
tylerwilson 0:881733436890 116
tylerwilson 0:881733436890 117 #define cJSON_AddNullToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateNull())
tylerwilson 0:881733436890 118 #define cJSON_AddTrueToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateTrue())
tylerwilson 0:881733436890 119 #define cJSON_AddFalseToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateFalse())
tylerwilson 0:881733436890 120 #define cJSON_AddNumberToObject(object,name,n) cJSON_AddItemToObject(object, name, cJSON_CreateNumber(n))
tylerwilson 0:881733436890 121 #define cJSON_AddStringToObject(object,name,s) cJSON_AddItemToObject(object, name, cJSON_CreateString(s))
tylerwilson 0:881733436890 122
tylerwilson 0:881733436890 123 #ifdef __cplusplus
tylerwilson 0:881733436890 124 }
tylerwilson 0:881733436890 125 #endif
tylerwilson 0:881733436890 126
tylerwilson 0:881733436890 127 #endif