Permalink
Browse files

Allow ints to be floats

  • Loading branch information...
jpihl committed Mar 27, 2017
1 parent 0731160 commit 481aaacb2f63780fa06be424111cf5ef5870c05c
Showing with 13 additions and 8 deletions.
  1. +1 −1 NEWS.rst
  2. +6 −2 src/bourne/json.cpp
  3. +4 −4 src/bourne/json.hpp
  4. +2 −1 test/src/test_json.cpp
View
@@ -6,7 +6,7 @@ every change, see the Git log.
Latest
------
* tbd
* Minor: Allow integers to be converted implicitly to floating point values.
4.0.0
-----
View
@@ -253,7 +253,8 @@ bool json::is_int() const
bool json::is_float() const
{
return m_type == class_type::floating;
return m_type == class_type::floating ||
m_type == class_type::integral;
}
bool json::is_string() const
@@ -286,7 +287,10 @@ int64_t json::to_int() const
double json::to_float() const
{
assert(is_float());
return m_internal.m_float;
if (m_type == class_type::floating)
return m_internal.m_float;
else if (m_type == class_type::integral)
return (double) m_internal.m_int;
}
std::string json::to_string() const
View
@@ -212,19 +212,19 @@ class json
{
if (std::is_same<T, bool>::value)
{
return m_type == class_type::boolean;
return is_bool();
}
else if (std::is_integral<T>::value)
{
return m_type == class_type::integral;
return is_int();
}
else if (std::is_floating_point<T>::value)
{
return m_type == class_type::floating;
return is_float();
}
else if (std::is_convertible<T, std::string>::value)
{
return m_type == class_type::string;
return is_string();
}
return false;
}
View
@@ -67,12 +67,13 @@ TEST(test_json, test_retrival_of_primitives)
element = 42;
EXPECT_FALSE(element.is_bool());
EXPECT_TRUE(element.is_int());
EXPECT_FALSE(element.is_float());
EXPECT_TRUE(element.is_float());
EXPECT_FALSE(element.is_string());
EXPECT_FALSE(element.is_object());
EXPECT_FALSE(element.is_array());
EXPECT_FALSE(element.is_null());
EXPECT_EQ(42, element.to_int());
EXPECT_EQ(42, element.to_float());
element = 13.37;
EXPECT_FALSE(element.is_bool());

0 comments on commit 481aaac

Please sign in to comment.