Skip to content

Commit

Permalink
chore: add functions to access private member variables
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbacsur committed Oct 11, 2024
1 parent 2210297 commit 89d6af8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
13 changes: 13 additions & 0 deletions include/cppx/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ class JSON {
std::string stringify() const;
static JSON parse(const std::string& s);

Type type() const;

template <typename T>
const T& value() const;

private:
Type type_;
std::variant<Null, Boolean, Integer, Floating, String, Array, Object, Callable> value_;
Expand All @@ -85,3 +90,11 @@ class JSON {
static JSON parseArray(const std::string& s, size_t& pos);
static JSON parseObject(const std::string& s, size_t& pos);
};

template <typename T>
const T& JSON::value() const {
if (!std::holds_alternative<T>(value_)) {
throw std::runtime_error("Type mismatch when accessing JSON value.");
}
return std::get<T>(value_);
}
2 changes: 2 additions & 0 deletions src/cppx/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -594,3 +594,5 @@ JSON JSON::parseObject(const std::string& s, size_t& pos) {
}
return JSON(obj);
}

JSON::Type JSON::type() const { return type_; }

0 comments on commit 89d6af8

Please sign in to comment.