JSONParser.h 3.4 KB

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