Permalink
Browse files

Merge pull request #8 from steinwurf/added-to-and-is

added to and is member functions
  • Loading branch information...
jpihl committed Mar 24, 2017
2 parents d2f4b0d + 27dc6b5 commit c337bb076dc4016d45ec92a15846941bda8452f0
Showing with 60 additions and 1 deletion.
  1. +2 −0 NEWS.rst
  2. +7 −1 examples/example.cpp
  3. +51 −0 src/bourne/json.hpp
View
@@ -6,6 +6,8 @@ every change, see the Git log.
Latest
------
* Minor: Added templated ``to`` and ``is`` function for checking and getting
fields of certain types.
* Major: Removed length function, use size instead.
* Major: Values are no longer silently converted to array values if append is
used.
View
@@ -64,7 +64,13 @@ int main()
"key1", "Value",
"key2", true,
"key3", {
"nested array", bourne::json::array("look", "an", "array", 6, true),
"nested array", bourne::json::array(
"look",
"an",
"array",
6,
true,
bourne::json({"key", "value"})),
"nested objects", {
"is_cool", true
}
View
@@ -206,6 +206,29 @@ class json
/// Returns the type of this object.
class_type json_type() const;
/// Returns true if this object matches the given type
template <class T>
bool is() const
{
if (std::is_same<T, bool>::value)
{
return m_type == class_type::boolean;
}
else if (std::is_integral<T>::value)
{
return m_type == class_type::integral;
}
else if (std::is_floating_point<T>::value)
{
return m_type == class_type::floating;
}
else if (std::is_convertible<T, std::string>::value)
{
return m_type == class_type::string;
}
return false;
}
/// Returns true if this object is a null value
bool is_null() const;
@@ -227,6 +250,34 @@ class json
/// Returns true if this object is a array value
bool is_array() const;
/// Converter for extracting the boolean value.
template <typename T>
void to(T& t, typename check_is_bool<T>::type* = 0) const
{
t = to_bool();
}
/// Converter for extracting the integral value.
template <typename T>
void to(T& t, typename check_is_integral<T>::type* = 0) const
{
t = to_int();
}
/// Converter for extracting the floating point value.
template <typename T>
void to(T& t, typename check_is_floating_point<T>::type* = 0) const
{
t = to_float();
}
/// Converter for extracting the string value.
template <typename T>
void to(T& t, typename check_is_string<T>::type* = 0) const
{
t = to_string();
}
/// Returns the underlying boolean value of this object. If this is not a
/// boolean value an assert is triggered.
bool to_bool() const;

0 comments on commit c337bb0

Please sign in to comment.