13template <
typename Type>
14concept NumericT = std::integral<Type> || std::floating_point<Type>;
17template <
typename Type>
18concept StringyT = std::convertible_to<Type, std::string_view>;
21template <
typename Type>
25template <
typename Type>
31using Result = std::expected<Json, Error>;
90 explicit(
false)
Json(std::nullptr_t)
noexcept {}
92 template <SettableT Type>
93 explicit(
false)
Json(
Type const& value) {
123 [[nodiscard]]
auto as_bool(
bool fallback = {})
const ->
bool;
124 [[nodiscard]]
auto as_double(
double fallback = {})
const ->
double;
125 [[nodiscard]]
auto as_u64(std::uint64_t fallback = {})
const -> std::uint64_t;
126 [[nodiscard]]
auto as_i64(std::int64_t fallback = {})
const -> std::int64_t;
128 [[nodiscard]]
auto as_string_view(std::string_view fallback = {})
const -> std::string_view;
129 [[nodiscard]]
auto as_string(std::string_view
const fallback = {})
const -> std::string {
return std::string{
as_string_view(fallback)}; }
131 template <NumericT Type>
133 if constexpr (std::signed_integral<Type>) {
134 return static_cast<Type>(
as_i64(
static_cast<std::int64_t
>(fallback)));
135 }
else if constexpr (std::unsigned_integral<Type>) {
136 return static_cast<Type>(
as_u64(
static_cast<std::uint64_t
>(fallback)));
138 return static_cast<Type>(
as_double(
static_cast<double>(fallback)));
142 template <GettableT Type>
143 [[nodiscard]]
auto as(
Type const& fallback = {})
const ->
Type {
144 if constexpr (std::same_as<Type, bool>) {
146 }
else if constexpr (NumericT<Type>) {
148 }
else if constexpr (std::same_as<Type, std::string_view>) {
168 if constexpr (std::signed_integral<Type>) {
170 }
else if constexpr (std::unsigned_integral<Type>) {
171 set_number(
static_cast<std::uint64_t
>(value));
177 template <SettableT Type>
179 if constexpr (std::same_as<Type, std::nullptr_t>) {
181 }
else if constexpr (std::same_as<Type, bool>) {
235 friend void swap(
Json& a,
Json& b)
noexcept { std::swap(a.m_value, b.m_value); }
237 explicit operator bool()
const {
return m_value !=
nullptr; }
243 void operator()(detail::Value* ptr)
const noexcept;
248 std::unique_ptr<detail::Value, Deleter> m_value;
259template <GettableT Type>
261 value = json.
as<Type>(fallback);
265template <SettableT Type>
273struct std::formatter<
dj::Json> {
274 template <
typename FormatParseContext>
275 constexpr auto parse(FormatParseContext& pc)
const {
279 static auto format(
dj::Json const& json, std::format_context& fc) -> std::format_context::iterator;
Library interface, represents a valid JSON value.
Definition json.hpp:77
auto as_double(double fallback={}) const -> double
auto as_i64(std::int64_t fallback={}) const -> std::int64_t
auto as_object() const -> StringTable< dj::Json > const &
auto is_boolean() const -> bool
Definition json.hpp:117
static auto from_file(std::string_view path, ParseMode mode=ParseMode::Auto) -> Result
Parse JSON from a file.
static auto parse(std::string_view text, ParseMode mode=ParseMode::Auto) -> Result
Parse JSON text.
auto as_array() const -> std::span< dj::Json const >
auto operator=(Json &&) -> Json &=default
JsonType Type
Definition json.hpp:79
auto as_u64(std::uint64_t fallback={}) const -> std::uint64_t
friend class detail::Parser
Definition json.hpp:250
auto operator[](std::string_view key) -> Json &
Obtain the value associated with the passed key.
void set(Type const &value)
Definition json.hpp:178
auto operator[](std::string_view key) const -> Json const &
Obtain the value associated with the passed key.
auto is_number() const -> bool
Definition json.hpp:118
auto is_null() const -> bool
Definition json.hpp:116
auto serialize(SerializeOptions const &options={}) const -> std::string
Serialize value as a string.
void set_boolean(bool value)
friend void swap(Json &a, Json &b) noexcept
Definition json.hpp:235
auto to_file(std::string_view path, SerializeOptions const &options={}) const -> bool
Write serialized string to a file.
auto is_array() const -> bool
Definition json.hpp:120
auto insert_or_assign(std::string key, Json value) -> Json &
Insert value associated with key into the Object. Converts to empty Object value first if not already...
auto as_number(Type const fallback={}) const -> Type
Definition json.hpp:132
auto as_bool(bool fallback={}) const -> bool
void set_value(Json value)
auto as_string(std::string_view const fallback={}) const -> std::string
Definition json.hpp:129
auto operator[](std::size_t index) -> Json &
Obtain the value at the passed index.
void set_array()
Set value to empty Array.
auto operator=(Json const &other) -> Json &
void set_number(std::int64_t value)
void set_string(std::string_view value)
auto get_type() const -> Type
Obtain the value type of this Json.
static auto empty_array() -> Json const &
Obtain a Json representing an empty Array value.
auto as_string_view(std::string_view fallback={}) const -> std::string_view
static auto empty_object() -> Json const &
Obtain a Json representing an empty Object value.
auto operator[](std::size_t index) const -> Json const &
Obtain the value at the passed index.
auto is_object() const -> bool
Definition json.hpp:121
auto push_back(Json value={}) -> Json &
Insert value at the end of the Array. Converts to empty Array value first if not already one.
void set_object()
Set value to empty Object.
auto as(Type const &fallback={}) const -> Type
Definition json.hpp:143
auto is_string() const -> bool
Definition json.hpp:119
Type to obtain JSON value as.
Definition json.hpp:22
Numeric type.
Definition json.hpp:14
Type to set JSON value to.
Definition json.hpp:26
Stringy type.
Definition json.hpp:18
decltype(std::to_underlying(SerializeFlag::None)) SerializeFlags
Definition json.hpp:48
auto to_string(Error const &error) -> std::string
Obtain print-friendly error string.
ParseMode
Parse mode.
Definition json.hpp:62
@ Auto
Automatic: Strict unless first line specifies JSONC mode.
@ Jsonc
Allow JSONC extensions.
void from_json(Json const &json, Type &value, Type const fallback={})
Assign JSON as value.
Definition json.hpp:260
constexpr auto serialize_flags_v
Default serialize flags.
Definition json.hpp:50
void to_json(Json &json, Type const &value)
Assign value to JSON.
Definition json.hpp:266
JsonType
JSON value type.
Definition json.hpp:34
std::expected< Json, Error > Result
Parse result type.
Definition json.hpp:31
auto make_escaped(std::string_view text) -> std::string
Convert input text to escaped string.
std::unordered_map< std::string, Value, StringHash, std::equal_to<> > StringTable
Heterogeneous string map.
Definition string_table.hpp:13
Bit flags for serialization options.
Definition json.hpp:37
@ None
Definition json.hpp:39
@ TrailingNewline
Append newline at the end. Ignored if NoSpaces is set.
Definition json.hpp:43
@ NoSpaces
No whitespace. Ignores TrailingNewLine and other whitespace options.
Definition json.hpp:45
@ SortKeys
Sort keys lexicographically.
Definition json.hpp:41
Serialization options.
Definition json.hpp:53
std::string_view indent
Indentation string. Ignored if SerializeFlag::NoSpaces is set.
Definition json.hpp:55
std::string_view newline
Newline string. Ignored if SerializeFlag::NoSpaces is set.
Definition json.hpp:57
SerializeFlags flags
Definition json.hpp:58