Json.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated January 1, 2020. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2020, Esoteric Software LLC
  6. *
  7. * Integration of the Spine Runtimes into software or otherwise creating
  8. * derivative works of the Spine Runtimes is permitted under the terms and
  9. * conditions of Section 2 of the Spine Editor License Agreement:
  10. * http://esotericsoftware.com/spine-editor-license
  11. *
  12. * Otherwise, it is permitted to integrate the Spine Runtimes into software
  13. * or otherwise create derivative works of the Spine Runtimes (collectively,
  14. * "Products"), provided that each user of the Products must obtain their own
  15. * Spine Editor license and redistribution of the Products in any form must
  16. * include this license and copyright notice.
  17. *
  18. * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
  24. * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. #ifndef Spine_Json_h
  30. #define Spine_Json_h
  31. #include <spine/SpineObject.h>
  32. #ifndef SPINE_JSON_HAVE_PREV
  33. /* spine doesn't use the "prev" link in the Json sibling lists. */
  34. #define SPINE_JSON_HAVE_PREV 0
  35. #endif
  36. namespace spine {
  37. class SP_API Json : public SpineObject {
  38. friend class SkeletonJson;
  39. public:
  40. /* Json Types: */
  41. static const int JSON_FALSE;
  42. static const int JSON_TRUE;
  43. static const int JSON_NULL;
  44. static const int JSON_NUMBER;
  45. static const int JSON_STRING;
  46. static const int JSON_ARRAY;
  47. static const int JSON_OBJECT;
  48. /* Get item "string" from object. Case insensitive. */
  49. static Json *getItem(Json *object, const char *string);
  50. static const char *getString(Json *object, const char *name, const char *defaultValue);
  51. static float getFloat(Json *object, const char *name, float defaultValue);
  52. static int getInt(Json *object, const char *name, int defaultValue);
  53. static bool getBoolean(Json *object, const char *name, bool defaultValue);
  54. /* 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. */
  55. static const char *getError();
  56. /* Supply a block of JSON, and this returns a Json object you can interrogate. Call Json_dispose when finished. */
  57. explicit Json(const char *value);
  58. ~Json();
  59. private:
  60. static const char *_error;
  61. Json *_next;
  62. #if SPINE_JSON_HAVE_PREV
  63. Json* _prev; /* next/prev allow you to walk array/object chains. Alternatively, use getSize/getItem */
  64. #endif
  65. Json *_child; /* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */
  66. int _type; /* The type of the item, as above. */
  67. int _size; /* The number of children. */
  68. const char *_valueString; /* The item's string, if type==JSON_STRING */
  69. int _valueInt; /* The item's number, if type==JSON_NUMBER */
  70. float _valueFloat; /* The item's number, if type==JSON_NUMBER */
  71. 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. */
  72. /* Utility to jump whitespace and cr/lf */
  73. static const char *skip(const char *inValue);
  74. /* Parser core - when encountering text, process appropriately. */
  75. static const char *parseValue(Json *item, const char *value);
  76. /* Parse the input text into an unescaped cstring, and populate item. */
  77. static const char *parseString(Json *item, const char *str);
  78. /* Parse the input text to generate a number, and populate the result into item. */
  79. static const char *parseNumber(Json *item, const char *num);
  80. /* Build an array from input text. */
  81. static const char *parseArray(Json *item, const char *value);
  82. /* Build an object from the text. */
  83. static const char *parseObject(Json *item, const char *value);
  84. static int json_strcasecmp(const char *s1, const char *s2);
  85. };
  86. }
  87. #endif /* Spine_Json_h */