Add is<T> member functions to json::value.
This commit is contained in:
@@ -154,10 +154,11 @@ namespace json {
|
||||
float as_float_or(float def) const;
|
||||
|
||||
double as_double() const;
|
||||
|
||||
double as_double_or(double def) const;
|
||||
|
||||
int as_int() const;
|
||||
int as_int_or(int def) const;
|
||||
int as_int() const { return number.value(); }
|
||||
int as_int_or(int def) const {return number.value_or(def);}
|
||||
|
||||
|
||||
|
||||
@@ -168,21 +169,6 @@ namespace json {
|
||||
bool as_bool() const { return boolean.value(); }
|
||||
bool as_bool_or(bool def) const { return boolean.value_or(def);}
|
||||
|
||||
template <typename T>
|
||||
T as_or(T def) const;
|
||||
|
||||
|
||||
template<> float as_or(float def) const
|
||||
{ return number.value_or(def); }
|
||||
|
||||
template<> int as_or(int def) const
|
||||
{ return number.value_or(def); }
|
||||
|
||||
template<> double as_or(double def) const
|
||||
{ return number.value_or(def); }
|
||||
|
||||
template <typename T>
|
||||
T as() const;
|
||||
|
||||
/// Converts all JSON values contained in this object / array to the derived json value types.
|
||||
void convert_descendants();
|
||||
@@ -205,15 +191,23 @@ namespace json {
|
||||
value& operator[] (int index);
|
||||
/// @throws std::runtime_error if value_type != array
|
||||
const value& operator[] (int index) const;
|
||||
|
||||
/// @return True if a key-value pair in this object has a key that matches the given value.
|
||||
/// @throws std::runtime_error if value_type != array
|
||||
bool contains(const std::string& key);
|
||||
|
||||
bool is(value_type type) const;
|
||||
bool is_number() const;
|
||||
|
||||
bool is(value_type type) const
|
||||
{ return type == value_type::object; }
|
||||
bool is_string() const;
|
||||
|
||||
bool has_correct_value_type(value_type type) const
|
||||
{ }
|
||||
bool is_array() const;
|
||||
|
||||
bool is_object() const;
|
||||
|
||||
bool is_boolean() const;
|
||||
|
||||
bool is_null() const;
|
||||
#pragma endregion
|
||||
};
|
||||
|
||||
|
10
main.cpp
10
main.cpp
@@ -1,4 +1,4 @@
|
||||
#include <JSON.hpp>
|
||||
#include <json.hpp>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
|
||||
@@ -50,9 +50,9 @@ json::value vector3_to_json(const Vector3& v) {
|
||||
/// Creates Vector3 type out of json array object.
|
||||
Vector3 json_to_vector3(const json::value& val) {
|
||||
Vector3 value;
|
||||
auto arr = val.as_array();
|
||||
auto arr = val.as_array_value();
|
||||
|
||||
value.x = arr.at(0).as_float_or(0);
|
||||
value.x = arr[0].as_float_or(0);
|
||||
value.y = arr.at(1).as_float_or(0);
|
||||
value.z = arr.at(2).as_float_or(0);
|
||||
|
||||
@@ -153,6 +153,10 @@ json::value product_info::review::serialize() const {
|
||||
product_info::review::review(const json::value &jv) {
|
||||
if (jv.type != json::value_type::object)
|
||||
throw std::runtime_error("Malformed JSON for review!");
|
||||
|
||||
rating = jv["rating"].as_int();
|
||||
review_text = jv["review_text"].as_string();
|
||||
user = jv["user"].as_string();
|
||||
}
|
||||
|
||||
void test_product_info()
|
||||
|
24
src/json.cpp
24
src/json.cpp
@@ -1,7 +1,7 @@
|
||||
#include <json.hpp>
|
||||
#include <lexer.hpp>
|
||||
#include <sstream>
|
||||
#include <__msvc_ranges_tuple_formatter.hpp>
|
||||
#include <format>
|
||||
|
||||
namespace json {
|
||||
|
||||
@@ -146,12 +146,18 @@ namespace json {
|
||||
float value::as_float_or(float def) const
|
||||
{ return number.value_or(def); }
|
||||
|
||||
double value::as_double() const { return number.value();}
|
||||
|
||||
double value::as_double_or(double def) const { return number.value_or(def); }
|
||||
|
||||
std::string value::as_string() const
|
||||
{ return string.value();}
|
||||
|
||||
std::string value::as_string_or(const std::string& def) const
|
||||
{ return string.value_or(def);}
|
||||
|
||||
|
||||
|
||||
void value::convert_descendants() {
|
||||
if (type == value_type::object)
|
||||
{
|
||||
@@ -509,8 +515,22 @@ namespace json {
|
||||
return this->value::object->contains(key);
|
||||
}
|
||||
|
||||
bool value::is(value_type type) const { return type == this->type; }
|
||||
|
||||
bool value::is_number() const { return is(value_type::number);}
|
||||
|
||||
bool value::is_string() const { return is(value_type::string);}
|
||||
|
||||
bool value::is_array() const { return is(value_type::array); }
|
||||
|
||||
bool value::is_object() const { return is(value_type::object); }
|
||||
|
||||
bool value::is_boolean() const { return is(value_type::boolean); }
|
||||
|
||||
bool value::is_null() const { return is(value_type::null);}
|
||||
|
||||
value &value::operator[](int index) {
|
||||
assert(type == value_type::array);
|
||||
assert(is_array());
|
||||
return value::array->operator[](index);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user