Mercurial > code
changeset 489:4376ec6e5329
Json:
- Add optional coerce parameter in toString(),
- Forgot to add null in toJson(),
- Reorder tests while here.
author | David Demelier <markand@malikania.fr> |
---|---|
date | Fri, 13 Nov 2015 23:08:34 +0100 |
parents | b280d7aca160 |
children | d2b2071a9b85 |
files | modules/json/json.cpp modules/json/json.h modules/json/test/main.cpp |
diffstat | 3 files changed, 77 insertions(+), 21 deletions(-) [+] |
line wrap: on
line diff
--- a/modules/json/json.cpp Fri Nov 13 19:53:38 2015 +0100 +++ b/modules/json/json.cpp Fri Nov 13 23:08:34 2015 +0100 @@ -251,13 +251,17 @@ return m_integer; } -std::string Value::toString() const noexcept +std::string Value::toString(bool coerce) const noexcept { - if (m_type != Type::String) { - return ""; + std::string result; + + if (m_type == Type::String) { + result = m_string; + } else if (coerce) { + result = toJson(); } - return m_string; + return result; } Value::Value(const Buffer &buffer) @@ -290,11 +294,14 @@ break; } case Type::Boolean: - oss << (m_boolean ? true : false); + oss << (m_boolean ? "true" : "false"); break; case Type::Int: oss << m_integer; break; + case Type::Null: + oss << "null"; + break; case Type::Object: { oss << '{' << (level != 0 ? "\n" : "");
--- a/modules/json/json.h Fri Nov 13 19:53:38 2015 +0100 +++ b/modules/json/json.h Fri Nov 13 23:08:34 2015 +0100 @@ -49,10 +49,10 @@ Array, //!< Value is an array [] Boolean, //!< Value is boolean Int, //!< Value is integer - Real, //!< Value is float + Null, //!< Value is defined to null Object, //!< Value is object {} - String, //!< Value is unicode string - Null //!< Value is defined to null + Real, //!< Value is float + String //!< Value is unicode string }; /** @@ -642,9 +642,10 @@ /** * Get the value as string. * - * @return the value or empty stirng if not a string + * @param coerce set to true to coerce the value if not a string + * @return the value or empty string if not a string */ - std::string toString() const noexcept; + std::string toString(bool coerce = false) const noexcept; /** * Check if the value is boolean type.
--- a/modules/json/test/main.cpp Fri Nov 13 19:53:38 2015 +0100 +++ b/modules/json/test/main.cpp Fri Nov 13 23:08:34 2015 +0100 @@ -93,17 +93,6 @@ * ------------------------------------------------------------------ */ -TEST(Constructors, null) -{ - try { - json::Value value{nullptr}; - - ASSERT_TRUE(value.isNull()); - } catch (const json::Error &ex) { - FAIL() << ex.what(); - } -} - TEST(Constructors, boolean) { try { @@ -130,6 +119,17 @@ } } +TEST(Constructors, null) +{ + try { + json::Value value{nullptr}; + + ASSERT_TRUE(value.isNull()); + } catch (const json::Error &ex) { + FAIL() << ex.what(); + } +} + TEST(Constructors, real) { try { @@ -885,6 +885,54 @@ ASSERT_EQ(expected, result); } +/* + * Value::toString with coerce or not + * ------------------------------------------------------------------ + */ + +TEST(ToString, boolean) +{ + { + json::Value value(true); + ASSERT_EQ("", value.toString()); + ASSERT_EQ("true", value.toString(true)); + } + + { + json::Value value(false); + ASSERT_EQ("", value.toString()); + ASSERT_EQ("false", value.toString(true)); + } +} + +TEST(ToString, integer) +{ + json::Value value(123); + ASSERT_EQ("", value.toString()); + ASSERT_EQ("123", value.toString(true)); +} + +TEST(ToString, null) +{ + json::Value value(nullptr); + ASSERT_EQ("", value.toString()); + ASSERT_EQ("null", value.toString(true)); +} + +TEST(ToString, real) +{ + json::Value value(20.5); + ASSERT_EQ("", value.toString()); + ASSERT_EQ("20.5", value.toString(true)); +} + +TEST(ToString, string) +{ + json::Value value("hello"); + ASSERT_EQ("hello", value.toString()); + ASSERT_EQ("hello", value.toString(true)); +} + int main(int argc, char **argv) { testing::InitGoogleTest(&argc, argv);