48#if defined(_MSC_VER) || __has_include(<charconv>)
50#define SMTG_HAS_CHARCONV
58template <
typename JsonT>
61 explicit Base (JsonT* o) : object_ (o) {}
62 explicit Base (
const Base& o) : object_ (o.object_) {}
64 Base& operator= (
const Base& o) =
default;
66 operator JsonT* ()
const {
return object_; }
67 JsonT* jsonValue ()
const {
return object_; }
70 Base () : object_ (
nullptr) {}
76template <
typename JsonElement>
79 explicit Iterator (JsonElement el) : el (el) {}
81 bool operator== (
const Iterator& other)
const {
return other.el == el; }
82 bool operator!= (
const Iterator& other)
const {
return other.el != el; }
84 const JsonElement& operator* ()
const {
return el; }
85 const JsonElement& operator-> ()
const {
return el; }
156 Boolean (
size_t type) : value (type == json_type_true) {}
158 operator bool ()
const {
return value; }
169 std::string_view text ()
const {
return {jsonValue ()->string, jsonValue ()->string_size}; }
179 std::string_view text ()
const {
return {jsonValue ()->number, jsonValue ()->number_size}; }
190 String name ()
const {
return String (jsonValue ()->name); }
191 Value value ()
const {
return Value (jsonValue ()->value); }
202 size_t size ()
const {
return jsonValue ()->length; }
213 Value value ()
const {
return Value (jsonValue ()->value); }
224 size_t size ()
const {
return jsonValue ()->length; }
235 auto allocate = [] (
void*,
size_t allocSize) {
return std::malloc (allocSize); };
237 auto value = json_parse_ex (data.data (), data.size (),
238 json_parse_flags_allow_json5 |
239 json_parse_flags_allow_location_information,
240 allocate,
nullptr, &parse_result);
265 if (type () != Type::Object)
267 return Object (json_value_as_object (jsonValue ()));
273 if (type () != Type::Array)
275 return Array (json_value_as_array (jsonValue ()));
281 if (type () != Type::String)
283 return String (json_value_as_string (jsonValue ()));
289 if (type () != Type::Number)
291 return Number (json_value_as_number (jsonValue ()));
297 if (type () == Type::True || type () == Type::False)
298 return Boolean (jsonValue ()->type);
305 if (type () != Type::Null)
311inline Type Value::type ()
const
313 switch (jsonValue ()->type)
315 case json_type_string:
return Type::String;
316 case json_type_number:
return Type::Number;
317 case json_type_object:
return Type::Object;
318 case json_type_array:
return Type::Array;
319 case json_type_true:
return Type::True;
320 case json_type_false:
return Type::False;
321 case json_type_null:
return Type::Null;
328inline Value::VariantT Value::asVariant ()
const
332 case Type::String:
return *asString ();
333 case Type::Number:
return *asNumber ();
334 case Type::Object:
return *asObject ();
335 case Type::Array:
return *asArray ();
336 case Type::True:
return *asBoolean ();
337 case Type::False:
return *asBoolean ();
338 case Type::Null:
return *asNull ();
345inline SourceLocation Value::getSourceLocation ()
const
348 return {exValue->offset, exValue->line_no, exValue->row_no};
352inline SourceLocation String::getSourceLocation ()
const
355 return {exValue->offset, exValue->line_no, exValue->row_no};
361#if defined(SMTG_HAS_CHARCONV)
364 jsonValue ()->number + jsonValue ()->number_size, result);
370 std::string str (jsonValue ()->number, jsonValue ()->number + jsonValue ()->number_size);
371 if (
std::sscanf (str.data (),
"%lld", &result) != 1)
388 jsonValue ()->number + jsonValue ()->number_size, result);
400 case json_parse_error_e::json_parse_error_none:
return {};
401 case json_parse_error_e::json_parse_error_expected_comma_or_closing_bracket:
402 return "json_parse_error_expected_comma_or_closing_bracket";
403 case json_parse_error_e::json_parse_error_expected_colon:
404 return "json_parse_error_expected_colon";
405 case json_parse_error_e::json_parse_error_expected_opening_quote:
406 return "json_parse_error_expected_opening_quote";
407 case json_parse_error_e::json_parse_error_invalid_string_escape_sequence:
408 return "json_parse_error_invalid_string_escape_sequence";
409 case json_parse_error_e::json_parse_error_invalid_number_format:
410 return "json_parse_error_invalid_number_format";
411 case json_parse_error_e::json_parse_error_invalid_value:
412 return "json_parse_error_invalid_value";
413 case json_parse_error_e::json_parse_error_premature_end_of_buffer:
414 return "json_parse_error_premature_end_of_buffer";
415 case json_parse_error_e::json_parse_error_invalid_string:
416 return "json_parse_error_invalid_string";
417 case json_parse_error_e::json_parse_error_allocator_failed:
418 return "json_parse_error_allocator_failed";
419 case json_parse_error_e::json_parse_error_unexpected_trailing_characters:
420 return "json_parse_error_unexpected_trailing_characters";
421 case json_parse_error_e::json_parse_error_unknown:
return "json_parse_error_unknown";