JSON.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "Types.h"
  25. #include "List.h"
  26. #include "Map.h"
  27. namespace crown
  28. {
  29. enum JSONType
  30. {
  31. JT_NIL,
  32. JT_OBJECT,
  33. JT_ARRAY,
  34. JT_STRING,
  35. JT_NUMBER,
  36. JT_BOOL
  37. };
  38. struct JSONPair
  39. {
  40. const char* key;
  41. const char* val;
  42. };
  43. class JSON
  44. {
  45. public:
  46. static char next(const char* s, const char c = 0, bool force_reset = false);
  47. static bool is_escapee(char c);
  48. static JSONType type(const char* s);
  49. static void parse_string(const char* s, List<char>& str);
  50. static double parse_number(const char* s);
  51. static bool parse_bool(const char* s);
  52. static int32_t parse_int(const char* s);
  53. static float parse_float(const char* s);
  54. static void parse_array(const char* s, List<const char*>& array);
  55. static void parse_object(const char* s, List<JSONPair> & map);
  56. private:
  57. static char skip_whites(const char* s);
  58. static char skip_string(const char* s);
  59. static char skip_number(const char* s);
  60. static char skip_object(const char* s);
  61. static char skip_array(const char* s);
  62. static char skip_bool(const char* s);
  63. };
  64. } // namespace crown