JSONParser.h 3.5 KB

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