JSONParser.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #pragma once
  2. #include <cstdarg>
  3. #include "Types.h"
  4. #include "OS.h"
  5. #include "File.h"
  6. #include "List.h"
  7. #include "Dictionary.h"
  8. #include "Allocator.h"
  9. namespace crown
  10. {
  11. /// JSON Token types
  12. enum JSONType
  13. {
  14. JSON_OBJECT = 0, // Object
  15. JSON_ARRAY = 1, // Array
  16. JSON_STRING = 2, // String
  17. JSON_NUMBER = 3, // Number
  18. JSON_BOOL = 4 // Boolean
  19. };
  20. /// JSON error typology
  21. enum JSONError
  22. {
  23. JSON_NO_MEMORY = 0, // Not enough token provided
  24. JSON_INV_CHAR = 1, // Invalid character inside JSON string
  25. JSON_INV_PART = 2, // JSON string is incompleted
  26. JSON_SUCCESS = 3 // Everything OK!
  27. };
  28. /// JSONToken is a container which have pointer to a single json entity
  29. /// (primitive, object, array or string) of a json file.
  30. struct JSONToken
  31. {
  32. static const uint32_t MAX_TOKEN_LEN = 1024;
  33. JSONType m_type; // Token's type
  34. int32_t m_id; // Token's id
  35. char m_value[MAX_TOKEN_LEN]; // Token's value
  36. int32_t m_start; // Starting byte
  37. int32_t m_end; // Ending byte
  38. size_t m_size; // Token's dimension
  39. int32_t m_parent; // Token's parent
  40. inline void print()
  41. {
  42. os::printf("Id:\t%d\n", m_id);
  43. os::printf("Value:\t%s\n", m_value);
  44. os::printf("Type:\t%d\n", m_type);
  45. os::printf("Start:\t%d\n", m_start);
  46. os::printf("End:\t%d\n", m_end);
  47. os::printf("Parent:\t%d\n", m_parent);
  48. os::printf("Size:\t%d\n", m_size);
  49. os::printf("\n");
  50. }
  51. inline bool has_parent()
  52. {
  53. return m_parent != -1;
  54. }
  55. };
  56. struct JSONNode
  57. {
  58. int32_t m_id;
  59. JSONType m_type;
  60. inline void print()
  61. {
  62. os::printf("----------------\n");
  63. os::printf("Id:\t%d\n", m_id);
  64. os::printf("----------------\n");
  65. }
  66. };
  67. /// JSONParser parses JSON file and stores all relative tokens.
  68. /// It is designed to be robust (it should work with erroneus data)
  69. /// and fast (data parsing on fly).
  70. class JSONParser
  71. {
  72. public:
  73. /// Constructor
  74. JSONParser(Allocator& allocator, File* file, size_t size = 1024);
  75. /// Destructor
  76. ~JSONParser();
  77. JSONParser& get_root();
  78. JSONParser& get_object(const char* key);
  79. JSONParser& get_array(const char* key, uint32_t element);
  80. JSONParser& get_string(const char* key);
  81. JSONParser& get_number(const char* key);
  82. JSONParser& get_bool(const char* key);
  83. void to_string(char* value);
  84. void to_float(float& value);
  85. void to_int(int& value);
  86. void to_bool(bool& value);
  87. private:
  88. /// Parse JSON data and fill tokens
  89. void parse();
  90. /// Parse string in JSON data
  91. void parse_string();
  92. /// Parse boolean in JSON data
  93. void parse_bool();
  94. /// Parse float in JSON data
  95. void parse_number();
  96. /// Allocate token node
  97. JSONToken* allocate_token();
  98. /// Fill token and set boundaries
  99. void fill_token(JSONToken* token, JSONType type, int32_t start, int32_t end);
  100. /// Reset all JSON nodes
  101. void reset_nodes();
  102. Allocator& m_allocator;
  103. /// JSON data
  104. File* m_file;
  105. /// Next token to allocate
  106. int32_t m_next_token;
  107. /// Previous token e.g parent or array
  108. int32_t m_prev_token;
  109. /// JSON tokens list, used as default
  110. JSONToken m_tokens_list[1024];
  111. /// JSON tokens ptr (used only if we need more then 1024 tokens)
  112. JSONToken* m_tokens;
  113. /// m_tokens default size, default 1024
  114. size_t m_tokens_number;
  115. ///
  116. JSONNode* m_nodes;
  117. ///
  118. uint32_t m_nodes_count;
  119. };
  120. } // namespace crown