Json.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. Copyright (c) 2009 Dave Gamble
  3. Permission is hereby granted, dispose 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. /* Esoteric Software: Removed everything except parsing, shorter method names, more get methods, double to float, formatted. */
  20. #ifndef SPINE_JSON_H_
  21. #define SPINE_JSON_H_
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. /* Json Types: */
  26. #define Json_False 0
  27. #define Json_True 1
  28. #define Json_NULL 2
  29. #define Json_Number 3
  30. #define Json_String 4
  31. #define Json_Array 5
  32. #define Json_Object 6
  33. #ifndef SPINE_JSON_HAVE_PREV
  34. /* Spine doesn't use the "prev" link in the Json sibling lists. */
  35. #define SPINE_JSON_HAVE_PREV 0
  36. #endif
  37. /* The Json structure: */
  38. typedef struct Json {
  39. struct Json* next;
  40. #if SPINE_JSON_HAVE_PREV
  41. struct Json* prev; /* next/prev allow you to walk array/object chains. Alternatively, use getSize/getItem */
  42. #endif
  43. struct Json* child; /* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */
  44. int type; /* The type of the item, as above. */
  45. int size; /* The number of children. */
  46. const char* valueString; /* The item's string, if type==Json_String */
  47. int valueInt; /* The item's number, if type==Json_Number */
  48. float valueFloat; /* The item's number, if type==Json_Number */
  49. const char* name; /* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
  50. } Json;
  51. /* Supply a block of JSON, and this returns a Json object you can interrogate. Call Json_dispose when finished. */
  52. Json* Json_create (const char* value);
  53. /* Delete a Json entity and all subentities. */
  54. void Json_dispose (Json* json);
  55. /* Get item "string" from object. Case insensitive. */
  56. Json* Json_getItem (Json* json, const char* string);
  57. const char* Json_getString (Json* json, const char* name, const char* defaultValue);
  58. float Json_getFloat (Json* json, const char* name, float defaultValue);
  59. int Json_getInt (Json* json, const char* name, int defaultValue);
  60. /* 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 Json_create() returns 0. 0 when Json_create() succeeds. */
  61. const char* Json_getError (void);
  62. #ifdef __cplusplus
  63. }
  64. #endif
  65. #endif /* SPINE_JSON_H_ */