You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

json_loader.h 5.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #pragma once
  2. #include <cctype>
  3. #include <fstream>
  4. #include <functional>
  5. #include <iostream>
  6. #include <map>
  7. #include <memory>
  8. #include "megbrain/common.h"
  9. #include "megdnn/thin/small_vector.h"
  10. namespace mgb {
  11. /*!
  12. * \brief JSON format data loader for --input
  13. */
  14. class JsonLoader {
  15. public:
  16. // base class for different value format
  17. class Value {
  18. protected:
  19. enum struct Type : uint8_t { UNKNOWN, NUMBER, STRING, OBJECT, ARRAY, BOOL };
  20. Type m_type;
  21. public:
  22. template <typename T>
  23. T* safe_cast();
  24. Value() { m_type = Type::UNKNOWN; }
  25. Value(Type type) : m_type(type) {}
  26. virtual ~Value() {}
  27. bool is_array() { return Type::ARRAY == m_type; }
  28. bool is_object() { return Type::OBJECT == m_type; }
  29. bool is_number() { return Type::NUMBER == m_type; }
  30. bool is_str() { return Type::STRING == m_type; }
  31. bool is_bool() { return Type::BOOL == m_type; }
  32. std::unique_ptr<Value>& operator[](const std::string& key);
  33. std::unique_ptr<Value>& operator[](const size_t index);
  34. std::map<std::string, std::unique_ptr<Value>>& objects();
  35. std::vector<std::string>& keys();
  36. size_t len();
  37. megdnn::SmallVector<std::unique_ptr<Value>>& array();
  38. double number();
  39. std::string str();
  40. bool Bool();
  41. };
  42. void expect(char c);
  43. void skip_whitespace();
  44. std::unique_ptr<Value> parse_object();
  45. std::unique_ptr<Value> parse_array();
  46. std::unique_ptr<Value> parse_string();
  47. std::unique_ptr<Value> parse_number();
  48. std::unique_ptr<Value> parse_value();
  49. std::unique_ptr<Value> parse_bool();
  50. enum struct State : uint8_t {
  51. OK = 0,
  52. BAD_TYPE,
  53. BAD_DIGIT,
  54. BAD_ARRAY,
  55. MISS_COLON,
  56. MISS_BRACE,
  57. KEY_NOT_UNIQUE
  58. };
  59. JsonLoader() { m_state = State::OK; }
  60. std::unique_ptr<Value> load(const char* content, const size_t size);
  61. std::unique_ptr<Value> load(const char* path);
  62. class NumberValue final : public Value {
  63. friend std::unique_ptr<Value> JsonLoader::parse_number();
  64. double m_value;
  65. public:
  66. NumberValue() : Value(Type::NUMBER) {}
  67. double value() { return m_value; }
  68. };
  69. class StringValue final : public Value {
  70. std::string m_value;
  71. public:
  72. StringValue() : Value(Type::STRING) {}
  73. std::string value() { return m_value; }
  74. friend std::unique_ptr<Value> JsonLoader::parse_string();
  75. };
  76. class ArrayValue final : public Value {
  77. megdnn::SmallVector<std::unique_ptr<Value>> m_obj;
  78. public:
  79. ArrayValue() : Value(Type::ARRAY) {}
  80. ArrayValue(ArrayValue& arr) : Value(arr) {
  81. m_obj.clear();
  82. for (auto& item : arr.m_obj) {
  83. m_obj.emplace_back(item.get());
  84. item.release();
  85. }
  86. }
  87. ArrayValue(ArrayValue&& arr) : Value(arr) {
  88. m_obj.clear();
  89. for (auto& item : arr.m_obj) {
  90. m_obj.emplace_back(item.get());
  91. item.release();
  92. }
  93. }
  94. friend std::unique_ptr<Value> JsonLoader::parse_array();
  95. friend std::unique_ptr<JsonLoader::Value>& JsonLoader::Value::operator[](
  96. const size_t index);
  97. friend megdnn::SmallVector<std::unique_ptr<JsonLoader::Value>>& JsonLoader::
  98. Value::array();
  99. friend size_t JsonLoader::Value::len();
  100. };
  101. class ObjectValue final : public Value {
  102. std::map<std::string, std::unique_ptr<Value>> m_obj;
  103. std::vector<std::string> m_keys;
  104. public:
  105. ObjectValue() : Value(Type::OBJECT) {}
  106. ObjectValue(ObjectValue& arr) : Value(arr) {
  107. m_obj.clear();
  108. m_keys.clear();
  109. for (auto itra = arr.m_obj.begin(); itra != arr.m_obj.end(); ++itra) {
  110. m_obj.emplace(std::make_pair(itra->first, std::move(itra->second)));
  111. m_keys.push_back(itra->first);
  112. }
  113. }
  114. ObjectValue(ObjectValue&& arr) : Value(arr) {
  115. m_obj.clear();
  116. m_keys.clear();
  117. for (auto itra = arr.m_obj.begin(); itra != arr.m_obj.end(); ++itra) {
  118. m_obj.emplace(std::make_pair(itra->first, std::move(itra->second)));
  119. m_keys.push_back(itra->first);
  120. }
  121. }
  122. friend std::unique_ptr<Value> JsonLoader::parse_object();
  123. friend std::unique_ptr<JsonLoader::Value>& JsonLoader::Value::operator[](
  124. const std::string&);
  125. friend std::map<std::string, std::unique_ptr<JsonLoader::Value>>& JsonLoader::
  126. Value::objects();
  127. friend std::vector<std::string>& JsonLoader::Value::keys();
  128. friend size_t JsonLoader::Value::len();
  129. };
  130. class BoolValue final : public Value {
  131. bool m_value;
  132. public:
  133. BoolValue() : Value(Type::BOOL) {}
  134. bool value() { return m_value; }
  135. friend std::unique_ptr<Value> JsonLoader::parse_bool();
  136. };
  137. private:
  138. const char* m_buf;
  139. State m_state;
  140. };
  141. } // namespace mgb