srjson.h

00001 /*
00002   Copyright (c) 2009 Dave Gamble
00003 
00004   Permission is hereby granted, free of charge, to any person obtaining a copy
00005   of this software and associated documentation files (the "Software"), to deal
00006   in the Software without restriction, including without limitation the rights
00007   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008   copies of the Software, and to permit persons to whom the Software is
00009   furnished to do so, subject to the following conditions:
00010 
00011   The above copyright notice and this permission notice shall be included in
00012   all copies or substantial portions of the Software.
00013 
00014   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020   THE SOFTWARE.
00021 */
00022 
00023 #ifndef _srjson__h_
00024 #define _srjson__h_
00025 
00026 #ifdef __cplusplus
00027 extern          "C"
00028 {
00029 #endif
00030 
00031 #include "../../str.h"
00032 
00033 /* srjson Types: */
00034 #define srjson_False   0
00035 #define srjson_True    1
00036 #define srjson_NULL    2
00037 #define srjson_Number  3
00038 #define srjson_String  4
00039 #define srjson_Array   5
00040 #define srjson_Object  6
00041 
00042 #define srjson_IsReference 256
00043 
00044 /* The srjson node structure: */
00045 typedef struct srjson {
00046         struct srjson *parent;
00047         struct srjson *next;
00048         struct srjson *prev;    /* next/prev allow you to
00049                                                  * walk array/object chains.
00050                                                  * Alternatively, use
00051                                                  * GetArraySize/GetArrayItem/G
00052                                                  * etObjectItem */
00053         struct srjson *child;   /* An array or object item will have
00054                                          * a child pointer pointing to a
00055                                          * chain of the items in the
00056                                          * array/object. */
00057 
00058         int             type;   /* The type of the item, as above. */
00059         char           *valuestring;    /* The item's string, if
00060                                                  * type==srjson_String */
00061         int             valueint;       /* The item's number, if
00062                                                  * type==srjson_Number */
00063         double          valuedouble;    /* The item's number, if
00064                                                  * type==srjson_Number */
00065         char           *string; /* The item's name string, if this
00066                                          * item is the child of, or is in the
00067                                          * list of subitems of an object. */
00068 } srjson_t;
00069 
00070 typedef struct srjson_doc {
00071         srjson_t  *root;
00072         int flags;
00073         str buf;
00074         void           *(*malloc_fn) (size_t sz);
00075         void            (*free_fn) (void *ptr);
00076 } srjson_doc_t;
00077 
00078 typedef struct srjson_Hooks {
00079         void           *(*malloc_fn) (size_t sz);
00080         void            (*free_fn) (void *ptr);
00081 } srjson_Hooks;
00082 
00083 
00084 extern srjson_doc_t *srjson_NewDoc(srjson_Hooks *hooks);
00085 extern int srjson_InitDoc(srjson_doc_t *doc, srjson_Hooks *hooks);
00086 
00087 extern void srjson_DeleteDoc(srjson_doc_t *doc);
00088 extern void srjson_DestroyDoc(srjson_doc_t *doc);
00089 
00090 /*
00091  * Supply a block of JSON, and this returns a srjson object you can
00092  * interrogate. Call srjson_Delete when finished.
00093  */
00094 extern srjson_t *srjson_Parse(srjson_doc_t *doc, const char *value);
00095 
00096 /*
00097  * Render a srjson entity to text for transfer/storage. Free the char*
00098  * when finished.
00099  */
00100 extern char *srjson_Print(srjson_doc_t *doc, srjson_t *item);
00101 
00102 /*
00103  * Render a srjson entity to text for transfer/storage without any
00104  * formatting. Free the char* when finished.
00105  */
00106 extern char *srjson_PrintUnformatted(srjson_doc_t *doc, srjson_t *item);
00107 
00108 /* Delete a srjson entity and all subentities. */
00109 extern void srjson_Delete(srjson_doc_t *doc, srjson_t *c);
00110 
00111 /* Returns the number of items in an array (or object). */
00112 extern int srjson_GetArraySize(srjson_doc_t *doc, srjson_t *array);
00113 
00114 /*
00115  * Retrieve item number "item" from array "array". Returns NULL if
00116  * unsuccessful.
00117  */
00118 extern srjson_t *srjson_GetArrayItem(srjson_doc_t *doc, srjson_t *array, int item);
00119 
00120 /* Get item "string" from object. Case insensitive. */
00121 extern srjson_t *srjson_GetObjectItem(srjson_doc_t *doc, srjson_t *object, const char *string);
00122 
00123 /*
00124  * For analysing failed parses. This returns a pointer to the parse
00125  * error. You'll probably need to look a few chars back to make sense
00126  * of it. Defined when srjson_Parse() returns 0. 0 when srjson_Parse()
00127  * succeeds.
00128  */
00129 extern const char *srjson_GetErrorPtr();
00130 
00131 /* These calls create a srjson item of the appropriate type. */
00132 extern srjson_t *srjson_CreateNull(srjson_doc_t *doc);
00133 extern srjson_t *srjson_CreateTrue(srjson_doc_t *doc);
00134 extern srjson_t *srjson_CreateFalse(srjson_doc_t *doc);
00135 extern srjson_t *srjson_CreateBool(srjson_doc_t *doc, int b);
00136 extern srjson_t *srjson_CreateNumber(srjson_doc_t *doc, double num);
00137 extern srjson_t *srjson_CreateString(srjson_doc_t *doc, const char *string);
00138 extern srjson_t *srjson_CreateStr(srjson_doc_t *doc, const char *string, int len);
00139 extern srjson_t *srjson_CreateArray(srjson_doc_t *doc);
00140 extern srjson_t *srjson_CreateObject(srjson_doc_t *doc);
00141 
00142 /* These utilities create an Array of count items. */
00143 extern srjson_t *srjson_CreateIntArray(srjson_doc_t *doc, int *numbers, int count);
00144 extern srjson_t *srjson_CreateFloatArray(srjson_doc_t *doc, float *numbers, int count);
00145 extern srjson_t *srjson_CreateDoubleArray(srjson_doc_t *doc, double *numbers, int count);
00146 extern srjson_t *srjson_CreateStringArray(srjson_doc_t *doc, const char **strings, int count);
00147 
00148 /* Append item to the specified array/object. */
00149 extern void srjson_AddItemToArray(srjson_doc_t *doc, srjson_t *array, srjson_t *item);
00150 extern void srjson_AddItemToObject(srjson_doc_t *doc, srjson_t *object, const char *string, srjson_t *item);
00151 extern void srjson_AddStrItemToObject(srjson_doc_t *doc, srjson_t *object, const char *string, int len, srjson_t *item);
00152 
00153 /*
00154  * Append reference to item to the specified array/object. Use this
00155  * when you want to add an existing srjson to a new srjson, but don't
00156  * want to corrupt your existing srjson.
00157  */
00158 extern void srjson_AddItemReferenceToArray(srjson_doc_t *doc, srjson_t *array, srjson_t *item);
00159 extern void srjson_AddItemReferenceToObject(srjson_doc_t *doc, srjson_t *object, const char *string, srjson_t *item);
00160 
00161 /* Remove/Detatch items from Arrays/Objects. */
00162 extern srjson_t *srjson_UnlinkItemFromObj(srjson_doc_t *doc, srjson_t *obj, srjson_t *item);
00163 extern srjson_t *srjson_DetachItemFromArray(srjson_doc_t *doc, srjson_t *array, int which);
00164 extern void srjson_DeleteItemFromArray(srjson_doc_t *doc, srjson_t *array, int which);
00165 extern srjson_t *srjson_DetachItemFromObject(srjson_doc_t *doc, srjson_t *object, const char *string);
00166 extern void srjson_DeleteItemFromObject(srjson_doc_t *doc, srjson_t *object, const char *string);
00167 
00168 /* Update array items. */
00169 extern void srjson_ReplaceItemInArray(srjson_doc_t *doc, srjson_t *array, int which, srjson_t *newitem);
00170 extern void srjson_ReplaceItemInObject(srjson_doc_t *doc, srjson_t *object, const char *string, srjson_t *newitem);
00171 
00172 #define srjson_AddNullToObject(doc, object,name)                srjson_AddItemToObject(doc, object, name, srjson_CreateNull(doc))
00173 #define srjson_AddTrueToObject(doc, object,name)                srjson_AddItemToObject(doc, object, name, srjson_CreateTrue(doc))
00174 #define srjson_AddFalseToObject(doc, object,name)               srjson_AddItemToObject(doc, object, name, srjson_CreateFalse(doc))
00175 #define srjson_AddNumberToObject(doc, object,name,n)    srjson_AddItemToObject(doc, object, name, srjson_CreateNumber(doc,n))
00176 #define srjson_AddStringToObject(doc, object,name,s)    srjson_AddItemToObject(doc, object, name, srjson_CreateString(doc,s))
00177 #define srjson_AddStrToObject(doc, object,name,s,l)             srjson_AddItemToObject(doc, object, name, srjson_CreateStr(doc,s,l))
00178 #define srjson_AddStrStrToObject(doc, object,name,ln,s,l)       srjson_AddStrItemToObject(doc, object, name, ln, srjson_CreateStr(doc,s,l))
00179 
00180 #ifdef __cplusplus
00181 }
00182 #endif
00183 
00184 #endif