Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Comments and cleanup #1553

Merged
merged 1 commit into from
Jan 6, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 32 additions & 24 deletions include/glaze/json/json_t.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,17 @@ namespace glz
using val_t = std::variant<null_t, double, std::string, bool, array_t, object_t>;
val_t data{};

// Dump the value to JSON, returns an expected that will contain a std::string if valid
/**
* @brief Converts the JSON data to a string representation.
* @return An `expected` containing a JSON string if successful, or an error context.
*/
expected<std::string, error_ctx> dump() const { return write_json(data); }

/**
* @brief Gets the value as the specified type.
* @tparam T The type to get the value as.
* @return Reference to the value of the specified type.
*/
template <class T>
[[nodiscard]] T& get()
{
Expand All @@ -59,6 +67,29 @@ namespace glz
{
return std::get_if<T>(&data);
}

template <class T>
[[nodiscard]] T as() const
{
// Prefer get becuase it returns a reference
return get<T>();
}

template <class T>
requires std::convertible_to<double, T>
[[nodiscard]] T as() const
{
// Can be used for int and the like
return static_cast<T>(get<double>());
}

template <class T>
requires std::convertible_to<std::string, T>
[[nodiscard]] T as() const
{
// Can be used for string_view and the like
return get<std::string>();
}

template <class T>
[[nodiscard]] bool holds() const noexcept
Expand Down Expand Up @@ -212,29 +243,6 @@ namespace glz
data.emplace<array_t>(std::move(arr));
}

template <class T>
[[nodiscard]] T as() const
{
// Prefer get becuase it returns a reference
return get<T>();
}

template <class T>
requires std::convertible_to<double, T>
[[nodiscard]] T as() const
{
// Can be used for int and the like
return static_cast<T>(get<double>());
}

template <class T>
requires std::convertible_to<std::string, T>
[[nodiscard]] T as() const
{
// Can be used for string_view and the like
return get<std::string>();
}

[[nodiscard]] bool is_array() const noexcept { return holds<json_t::array_t>(); }

[[nodiscard]] bool is_object() const noexcept { return holds<json_t::object_t>(); }
Expand Down