Skip to content

Commit

Permalink
Comments and cleanup (#1553)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenberry authored Jan 6, 2025
1 parent cb8b3b8 commit 7a65db7
Showing 1 changed file with 32 additions and 24 deletions.
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

0 comments on commit 7a65db7

Please sign in to comment.