JSONParser.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #pragma once
  2. #include "Types.h"
  3. #include "OS.h"
  4. #include "Stream.h"
  5. namespace crown
  6. {
  7. /// JSON Token types
  8. enum json_type
  9. {
  10. JSON_PRIMITIVE = 0, // Number, boolean or null
  11. JSON_OBJECT = 1, // Object
  12. JSON_ARRAY = 2, // Array
  13. JSON_STRING = 3 // String
  14. };
  15. /// JSON error typology
  16. enum json_error
  17. {
  18. JSON_NO_MEMORY = 0, // Not enough token provided
  19. JSON_INV_CHAR = 1, // Invalid character inside JSON string
  20. JSON_INV_PART = 2, // JSON string is incompleted
  21. JSON_NO_INIT = 3, // JSON parser not initialized
  22. JSON_SUCCESS = 4 // Everything OK!
  23. };
  24. /// JSONToken is a container which have pointer to a single json entity
  25. /// (primitive, object, array or string) of a json file.
  26. struct JSONToken
  27. {
  28. json_type m_type;
  29. int32_t m_start;
  30. int32_t m_end;
  31. size_t m_size;
  32. int32_t m_parent;
  33. inline void print()
  34. {
  35. os::printf("Type:\t%d\n",m_type);
  36. os::printf("Start:\t%d\n",m_start);
  37. os::printf("End:\t%d\n",m_end);
  38. os::printf("Parent:\t%d\n",m_parent);
  39. os::printf("Size:\t%d\n",m_size);
  40. os::printf("\n");
  41. }
  42. };
  43. /// JSONParser parses JSON file and stores all relative tokens.
  44. /// It is designed to be robust (it should work with erroneus data)
  45. /// and fast (data parsing on fly).
  46. class JSONParser
  47. {
  48. public:
  49. /// Constructor
  50. JSONParser(Stream* stream, size_t size = 1024);
  51. /// Destructor
  52. ~JSONParser();
  53. /// Init JSON parser, must be called for each different JSON string
  54. void init();
  55. /// Shutdown JSON parser
  56. void shutdown();
  57. /// Parse JSON data
  58. json_error parse();
  59. /// Get all tokens
  60. JSONToken* get_tokens();
  61. /// Get next token
  62. int32_t get_tokens_number();
  63. private:
  64. /// Parse string in JSON data
  65. json_error parse_string();
  66. /// Parse number or boolean in JSON data
  67. json_error parse_primitive();
  68. /// Allocate token node
  69. JSONToken* allocate_token();
  70. /// Fill token and set boundaries
  71. void fill_token(JSONToken* token, json_type type, int32_t start, int32_t end);
  72. /// JSON stream of data
  73. Stream* m_stream;
  74. /// JSON string offset
  75. uint32_t m_pos;
  76. /// Next token to allocate
  77. int32_t m_next_token;
  78. /// Previous token e.g parent or array
  79. int32_t m_prev_token;
  80. /// JSON tokens list, used as default
  81. JSONToken m_tokens_list[1024];
  82. /// JSON tokens ptr (used only if we need more then 1024 tokens)
  83. JSONToken* m_tokens;
  84. /// m_tokens default size, default 1024
  85. size_t m_size;
  86. // is JSON parser initilized
  87. bool is_init;
  88. };
  89. } // namespace crown