| 128 | namespace fl { |
| 129 | |
| 130 | class json { |
| 131 | private: |
| 132 | fl::shared_ptr<json_value> mValue; |
| 133 | |
| 134 | public: |
| 135 | // Constructors |
| 136 | json() FL_NOEXCEPT : mValue() {} // Default initialize to nullptr |
| 137 | json(fl::nullptr_t) FL_NOEXCEPT : mValue(fl::make_shared<json_value>(nullptr)) {} |
| 138 | json(bool b) FL_NOEXCEPT : mValue(fl::make_shared<json_value>(b)) {} |
| 139 | json(int i) FL_NOEXCEPT : mValue(fl::make_shared<json_value>(static_cast<i64>(i))) {} |
| 140 | json(i64 i) FL_NOEXCEPT : mValue(fl::make_shared<json_value>(i)) {} |
| 141 | json(float f) FL_NOEXCEPT : mValue(fl::make_shared<json_value>(f)) {} // Use float directly |
| 142 | json(double d) FL_NOEXCEPT : mValue(fl::make_shared<json_value>(static_cast<float>(d))) {} // Convert double to float |
| 143 | json(const fl::string& s) FL_NOEXCEPT : mValue(fl::make_shared<json_value>(s)) {} |
| 144 | json(const char* s) FL_NOEXCEPT : json(fl::string(s)) {} |
| 145 | json(json_array a) FL_NOEXCEPT : mValue(fl::make_shared<json_value>(fl::move(a))) {} |
| 146 | json(json_object o) FL_NOEXCEPT : mValue(fl::make_shared<json_value>(fl::move(o))) {} |
| 147 | // Constructor from shared_ptr<json_value> |
| 148 | json(const fl::shared_ptr<json_value>& value) FL_NOEXCEPT : mValue(value) {} |
| 149 | |
| 150 | // Factory method to create a json from a json_value |
| 151 | static json fromValue(const json_value& value) FL_NOEXCEPT { |
| 152 | json result; |
| 153 | result.mValue = fl::make_shared<json_value>(value); |
| 154 | return result; |
| 155 | } |
| 156 | |
| 157 | // Constructor for fl::vector<float> - converts to JSON array |
| 158 | json(const fl::vector<float>& vec) FL_NOEXCEPT : mValue(fl::make_shared<json_value>(json_array{})) { |
| 159 | auto ptr = mValue->data.ptr<json_array>(); |
| 160 | if (ptr) { |
| 161 | for (const auto& item : vec) { |
| 162 | ptr->push_back(fl::make_shared<json_value>(item)); // Use float directly |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | // Special constructor for char values |
| 168 | static json from_char(char c) FL_NOEXCEPT { |
| 169 | json result; |
| 170 | auto value = fl::make_shared<json_value>(fl::string(1, c)); |
| 171 | //FL_WARN("Created json_value with string: " << value->is_string() << ", int: " << value->is_int()); |
| 172 | result.mValue = value; |
| 173 | //FL_WARN("json has string: " << result.is_string() << ", int: " << result.is_int()); |
| 174 | return result; |
| 175 | } |
| 176 | |
| 177 | // Copy constructor |
| 178 | json(const json& other) FL_NOEXCEPT : mValue(other.mValue) {} |
| 179 | |
| 180 | // Assignment operator |
| 181 | json& operator=(const json& other) FL_NOEXCEPT { |
| 182 | //FL_WARN("json& operator=(const json& other): " << (other.mValue ? other.mValue.get() : 0)); |
| 183 | if (this != &other) { |
| 184 | mValue = other.mValue; |
| 185 | } |
| 186 | return *this; |
| 187 | } |
no test coverage detected