Mercurial > code
changeset 419:ee155fc84c56
Json: add valueOr for objects and arrays
author | David Demelier <markand@malikania.fr> |
---|---|
date | Thu, 08 Oct 2015 10:55:41 +0200 |
parents | 30e4a93f86c7 |
children | 0631c4bd56f9 |
files | C++/modules/Json/Json.h C++/tests/Json/main.cpp |
diffstat | 2 files changed, 57 insertions(+), 114 deletions(-) [+] |
line wrap: on
line diff
--- a/C++/modules/Json/Json.h Thu Oct 08 10:07:47 2015 +0200 +++ b/C++/modules/Json/Json.h Thu Oct 08 10:55:41 2015 +0200 @@ -603,6 +603,23 @@ } /** + * Get the value at the specified position or the defaultValue if position is out of bounds. + * + * @param position the position + * @param defaultValue the value replacement + * @return the value or defaultValue + */ + template <typename V> + inline Value valueOr(unsigned position, V &&defaultValue) const + { + if (position >= m_array.size()) { + return defaultValue; + } + + return m_array[position]; + } + + /** * Get a value at the specified index. * * @param position the position @@ -818,6 +835,25 @@ } /** + * Get the value at the specified key or the defaultValue if key is absent. + * + * @param name the name + * @param defaultValue the value replacement + * @return the value or defaultValue + */ + template <typename V> + inline Value valueOr(const std::string &name, V &&defaultValue) const + { + auto it = m_map.find(name); + + if (it == m_map.end()) { + return defaultValue; + } + + return it->second; + } + + /** * Get a value from the object. * * @param name the value key
--- a/C++/tests/Json/main.cpp Thu Oct 08 10:07:47 2015 +0200 +++ b/C++/tests/Json/main.cpp Thu Oct 08 10:55:41 2015 +0200 @@ -207,6 +207,22 @@ } } +TEST(Object, valueOr) +{ + try { + json::Object object{ + { "x", 10 }, + { "y", 20 } + }; + + ASSERT_EQ(10, object.valueOr("x", -9999).toInt()); + ASSERT_EQ(20, object.valueOr("y", -9999).toInt()); + ASSERT_EQ(-9999, object.valueOr("not-found", -9999).toInt()); + } catch (const json::Error &ex) { + FAIL() << ex.what(); + } +} + TEST(ObjectInitializer, simple) { try { @@ -352,48 +368,19 @@ } } -#if 0 - -TEST(Array, eraseIterator) +TEST(Array, valueOr) { try { - json::Array array; - - array.append(1); - array.append("hello"); - array.append(true); + json::Array array{-10, -20}; - array.erase(array.begin()); - - ASSERT_EQ(2, static_cast<int>(array.size())); - ASSERT_EQ("hello", array[0].toString()); - ASSERT_TRUE(array[1].toBool()); + ASSERT_EQ(-10, array.valueOr(0, -9999).toInt()); + ASSERT_EQ(-20, array.valueOr(1, -9999).toInt()); + ASSERT_EQ(-9999, array.valueOr(2, -9999).toInt()); } catch (const json::Error &ex) { FAIL() << ex.what(); } } -TEST(Array, eraseConstIterator) -{ - try { - json::Array array; - - array.append(1); - array.append("hello"); - array.append(true); - - array.erase(array.cbegin()); - - ASSERT_EQ(2, static_cast<int>(array.size())); - ASSERT_EQ("hello", array[0].toString()); - ASSERT_TRUE(array[1].toBool()); - } catch (const json::Error &ex) { - FAIL() << ex.what(); - } -} - -#endif - TEST(ArrayInitializer, simple) { try { @@ -466,28 +453,6 @@ } } -#if 0 - -TEST(FileWrite, simple) -{ - try { - json::Object object{ - { "name", "jean" }, - { "age", 99 } - }; - - object.write(std::ofstream("object-write.json")); - - json::Object object2 = json::Document(json::File("object-write.json")).toObject(); - - ASSERT_EQ(object2, object); - } catch (const json::Error &ex) { - FAIL() << ex.what(); - } -} - -#endif - TEST(StringRead, simple) { try { @@ -510,26 +475,6 @@ } } -#if 0 - -TEST(StringWrite, simple) -{ - try { - json::Object object{ - { "name", "jean" }, - { "age", 99 } - }; - - json::Object object2 = json::Document(object.dump()).toObject(); - - ASSERT_EQ(object2, object); - } catch (const json::Error &ex) { - FAIL() << ex.what(); - } -} - -#endif - /* -------------------------------------------------------- * Object read * -------------------------------------------------------- */ @@ -723,26 +668,6 @@ } } -#if 0 - -TEST_F(ObjectIteratorsTest, assignConst) -{ - // Assign (const) - { - auto it = m_object.cbegin(); - auto key = it->first; - auto orig = it->second; - - it->second = json::Value("CHANGED"); - - ASSERT_TRUE(m_object.contains(key)); - ASSERT_EQ(orig, m_object[key]); - ASSERT_EQ(3, static_cast<int>(m_object.size())); - } -} - -#endif - /* -------------------------------------------------------- * Array iterators * -------------------------------------------------------- */ @@ -845,24 +770,6 @@ } } -#if 0 - -TEST_F(ArrayIteratorsTest, assignConst) -{ - // Assign (const) - { - auto it = m_array.cbegin(); - - *it = json::Value(9999); - - ASSERT_EQ(3, static_cast<int>(m_array.size())); - ASSERT_EQ(1, it->toInt()); - ASSERT_EQ(1, m_array[0].toInt()); - } -} - -#endif - TEST_F(ArrayIteratorsTest, castToRef) { json::Array array{1, 2, 3};