srjson.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. Copyright (c) 2009 Dave Gamble
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #ifndef _srjson__h_
  20. #define _srjson__h_
  21. #ifdef __cplusplus
  22. extern "C"
  23. {
  24. #endif
  25. #include "../../str.h"
  26. /* srjson Types: */
  27. #define srjson_False 0
  28. #define srjson_True 1
  29. #define srjson_NULL 2
  30. #define srjson_Number 3
  31. #define srjson_String 4
  32. #define srjson_Array 5
  33. #define srjson_Object 6
  34. #define srjson_IsReference 256
  35. /* The srjson node structure: */
  36. typedef struct srjson {
  37. struct srjson *parent;
  38. struct srjson *next;
  39. struct srjson *prev; /* next/prev allow you to
  40. * walk array/object chains.
  41. * Alternatively, use
  42. * GetArraySize/GetArrayItem/G
  43. * etObjectItem */
  44. struct srjson *child; /* An array or object item will have
  45. * a child pointer pointing to a
  46. * chain of the items in the
  47. * array/object. */
  48. int type; /* The type of the item, as above. */
  49. char *valuestring; /* The item's string, if
  50. * type==srjson_String */
  51. int valueint; /* The item's number, if
  52. * type==srjson_Number */
  53. double valuedouble; /* The item's number, if
  54. * type==srjson_Number */
  55. char *string; /* The item's name string, if this
  56. * item is the child of, or is in the
  57. * list of subitems of an object. */
  58. } srjson_t;
  59. typedef struct srjson_doc {
  60. srjson_t *root;
  61. int flags;
  62. str buf;
  63. void *(*malloc_fn) (size_t sz);
  64. void (*free_fn) (void *ptr);
  65. } srjson_doc_t;
  66. typedef struct srjson_Hooks {
  67. void *(*malloc_fn) (size_t sz);
  68. void (*free_fn) (void *ptr);
  69. } srjson_Hooks;
  70. extern srjson_doc_t *srjson_NewDoc(srjson_Hooks *hooks);
  71. extern int srjson_InitDoc(srjson_doc_t *doc, srjson_Hooks *hooks);
  72. extern void srjson_DeleteDoc(srjson_doc_t *doc);
  73. extern void srjson_DestroyDoc(srjson_doc_t *doc);
  74. /*
  75. * Supply a block of JSON, and this returns a srjson object you can
  76. * interrogate. Call srjson_Delete when finished.
  77. */
  78. extern srjson_t *srjson_Parse(srjson_doc_t *doc, const char *value);
  79. /*
  80. * Render a srjson entity to text for transfer/storage. Free the char*
  81. * when finished.
  82. */
  83. extern char *srjson_Print(srjson_doc_t *doc, srjson_t *item);
  84. /*
  85. * Render a srjson entity to text for transfer/storage without any
  86. * formatting. Free the char* when finished.
  87. */
  88. extern char *srjson_PrintUnformatted(srjson_doc_t *doc, srjson_t *item);
  89. /* Delete a srjson entity and all subentities. */
  90. extern void srjson_Delete(srjson_doc_t *doc, srjson_t *c);
  91. /* Returns the number of items in an array (or object). */
  92. extern int srjson_GetArraySize(srjson_doc_t *doc, srjson_t *array);
  93. /*
  94. * Retrieve item number "item" from array "array". Returns NULL if
  95. * unsuccessful.
  96. */
  97. extern srjson_t *srjson_GetArrayItem(srjson_doc_t *doc, srjson_t *array, int item);
  98. /* Get item "string" from object. Case insensitive. */
  99. extern srjson_t *srjson_GetObjectItem(srjson_doc_t *doc, srjson_t *object, const char *string);
  100. /*
  101. * For analysing failed parses. This returns a pointer to the parse
  102. * error. You'll probably need to look a few chars back to make sense
  103. * of it. Defined when srjson_Parse() returns 0. 0 when srjson_Parse()
  104. * succeeds.
  105. */
  106. extern const char *srjson_GetErrorPtr();
  107. /* These calls create a srjson item of the appropriate type. */
  108. extern srjson_t *srjson_CreateNull(srjson_doc_t *doc);
  109. extern srjson_t *srjson_CreateTrue(srjson_doc_t *doc);
  110. extern srjson_t *srjson_CreateFalse(srjson_doc_t *doc);
  111. extern srjson_t *srjson_CreateBool(srjson_doc_t *doc, int b);
  112. extern srjson_t *srjson_CreateNumber(srjson_doc_t *doc, double num);
  113. extern srjson_t *srjson_CreateString(srjson_doc_t *doc, const char *string);
  114. extern srjson_t *srjson_CreateStr(srjson_doc_t *doc, const char *string, int len);
  115. extern srjson_t *srjson_CreateArray(srjson_doc_t *doc);
  116. extern srjson_t *srjson_CreateObject(srjson_doc_t *doc);
  117. /* These utilities create an Array of count items. */
  118. extern srjson_t *srjson_CreateIntArray(srjson_doc_t *doc, int *numbers, int count);
  119. extern srjson_t *srjson_CreateFloatArray(srjson_doc_t *doc, float *numbers, int count);
  120. extern srjson_t *srjson_CreateDoubleArray(srjson_doc_t *doc, double *numbers, int count);
  121. extern srjson_t *srjson_CreateStringArray(srjson_doc_t *doc, const char **strings, int count);
  122. /* Append item to the specified array/object. */
  123. extern void srjson_AddItemToArray(srjson_doc_t *doc, srjson_t *array, srjson_t *item);
  124. extern void srjson_AddItemToObject(srjson_doc_t *doc, srjson_t *object, const char *string, srjson_t *item);
  125. extern void srjson_AddStrItemToObject(srjson_doc_t *doc, srjson_t *object, const char *string, int len, srjson_t *item);
  126. /*
  127. * Append reference to item to the specified array/object. Use this
  128. * when you want to add an existing srjson to a new srjson, but don't
  129. * want to corrupt your existing srjson.
  130. */
  131. extern void srjson_AddItemReferenceToArray(srjson_doc_t *doc, srjson_t *array, srjson_t *item);
  132. extern void srjson_AddItemReferenceToObject(srjson_doc_t *doc, srjson_t *object, const char *string, srjson_t *item);
  133. /* Remove/Detatch items from Arrays/Objects. */
  134. extern srjson_t *srjson_UnlinkItemFromObj(srjson_doc_t *doc, srjson_t *obj, srjson_t *item);
  135. extern srjson_t *srjson_DetachItemFromArray(srjson_doc_t *doc, srjson_t *array, int which);
  136. extern void srjson_DeleteItemFromArray(srjson_doc_t *doc, srjson_t *array, int which);
  137. extern srjson_t *srjson_DetachItemFromObject(srjson_doc_t *doc, srjson_t *object, const char *string);
  138. extern void srjson_DeleteItemFromObject(srjson_doc_t *doc, srjson_t *object, const char *string);
  139. /* Update array items. */
  140. extern void srjson_ReplaceItemInArray(srjson_doc_t *doc, srjson_t *array, int which, srjson_t *newitem);
  141. extern void srjson_ReplaceItemInObject(srjson_doc_t *doc, srjson_t *object, const char *string, srjson_t *newitem);
  142. #define srjson_AddNullToObject(doc, object,name) srjson_AddItemToObject(doc, object, name, srjson_CreateNull(doc))
  143. #define srjson_AddTrueToObject(doc, object,name) srjson_AddItemToObject(doc, object, name, srjson_CreateTrue(doc))
  144. #define srjson_AddFalseToObject(doc, object,name) srjson_AddItemToObject(doc, object, name, srjson_CreateFalse(doc))
  145. #define srjson_AddNumberToObject(doc, object,name,n) srjson_AddItemToObject(doc, object, name, srjson_CreateNumber(doc,n))
  146. #define srjson_AddStringToObject(doc, object,name,s) srjson_AddItemToObject(doc, object, name, srjson_CreateString(doc,s))
  147. #define srjson_AddStrToObject(doc, object,name,s,l) srjson_AddItemToObject(doc, object, name, srjson_CreateStr(doc,s,l))
  148. #define srjson_AddStrStrToObject(doc, object,name,ln,s,l) srjson_AddStrItemToObject(doc, object, name, ln, srjson_CreateStr(doc,s,l))
  149. #ifdef __cplusplus
  150. }
  151. #endif
  152. #endif