changeset 320:b67b2da45bb0

Json: * Remove uniform initialization * Add JsonObject::erase, JsonObject::clear * Add JsonArray::erase, JsonArray::clear * Fix operator== in iterators * Add JsonValue::JsonValue(std::nullptr_t)
author David Demelier <markand@malikania.fr>
date Thu, 26 Feb 2015 14:21:29 +0100
parents 4c3019385769
children 0eb59106f700
files C++/Json.cpp C++/Json.h C++/Tests/Json/main.cpp
diffstat 3 files changed, 230 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/C++/Json.cpp	Wed Feb 25 13:53:41 2015 +0100
+++ b/C++/Json.cpp	Thu Feb 26 14:21:29 2015 +0100
@@ -47,7 +47,7 @@
 	auto value = json_array_get(m_handle.get(), index);
 
 	if (value == nullptr)
-		throw JsonError{"index out of bounds"};
+		throw JsonError("index out of bounds");
 
 	json_incref(value);
 
--- a/C++/Json.h	Wed Feb 25 13:53:41 2015 +0100
+++ b/C++/Json.h	Thu Feb 26 14:21:29 2015 +0100
@@ -79,7 +79,7 @@
 	 * @param error the error message
 	 */
 	inline JsonError(std::string error)
-		: m_text{std::move(error)}
+		: m_text(std::move(error))
 	{
 	}
 
@@ -89,11 +89,11 @@
 	 * @param error the error
 	 */
 	inline JsonError(const json_error_t &error)
-		: m_text{error.text}
-		, m_source{error.source}
-		, m_line{error.line}
-		, m_column{error.column}
-		, m_position{error.position}
+		: m_text(error.text)
+		, m_source(error.source)
+		, m_line(error.line)
+		, m_column(error.column)
+		, m_position(error.position)
 	{
 	}
 
@@ -181,23 +181,12 @@
 
 public:
 	/**
-	 * Create a JsonValue from a native Jansson type. This function
-	 * will increment the json_t reference count.
-	 *
-	 * @param json the value
-	 */
-	inline JsonValue(json_t *json) noexcept
-		: m_handle{json, json_decref}
-	{
-	}
-
-	/**
 	 * Deep copy of that element.
 	 *
 	 * @param value the other value
 	 */
 	inline JsonValue(const JsonValue &value)
-		: m_handle{json_deep_copy(value.m_handle.get()), json_decref}
+		: m_handle(json_deep_copy(value.m_handle.get()), json_decref)
 	{
 	}
 
@@ -228,6 +217,25 @@
 	JsonValue &operator=(JsonValue &&) = default;
 
 	/**
+	 * Create a JsonValue from a native Jansson type. This function
+	 * will increment the json_t reference count.
+	 *
+	 * @param json the value
+	 */
+	inline JsonValue(json_t *json) noexcept
+		: m_handle(json, json_decref)
+	{
+	}
+
+	/**
+	 * Construct a null value from a nullptr argument.
+	 */
+	inline JsonValue(std::nullptr_t) noexcept
+		: m_handle(json_null(), json_decref)
+	{
+	}
+
+	/**
 	 * Create an empty value (JsonType::Null).
 	 */
 	inline JsonValue()
@@ -291,7 +299,7 @@
 	 */
 	template <size_t Size>
 	inline JsonValue(char (&value)[Size])
-		: m_handle{json_string(value), json_decref}
+		: m_handle(json_string(value), json_decref)
 	{
 	}
 
@@ -615,6 +623,8 @@
 		using reference = Ref;
 		using pointer = Ptr;
 
+		friend class JsonArray;
+
 	private:
 		JsonArray &m_array;
 		int m_index;
@@ -741,6 +751,8 @@
 		using reference = JsonValue;
 		using pointer = Ptr;
 
+		friend class JsonArray;
+
 	private:
 		const JsonArray &m_array;
 		int m_index;
@@ -959,6 +971,44 @@
 	JsonValue at(int index) const;
 
 	/**
+	 * Erase the array content.
+	 */
+	inline void clear() noexcept
+	{
+		json_array_clear(m_handle.get());
+	}
+
+	/**
+	 * Remove the element at the specified index.
+	 *
+	 * @param index the index
+	 */
+	inline void erase(int index) noexcept
+	{
+		json_array_remove(m_handle.get(), index);
+	}
+
+	/**
+	 * Overloaded function.
+	 *
+	 * @param it the iterator
+	 */
+	inline void erase(iterator it) noexcept
+	{
+		erase(it.m_index);
+	}
+
+	/**
+	 * Overloaded function.
+	 *
+	 * @param it the iterator
+	 */
+	inline void erase(const_iterator it) noexcept
+	{
+		erase(it.m_index);
+	}
+
+	/**
 	 * Get the number of values in the array
 	 *
 	 * @return the number or 0
@@ -1130,6 +1180,8 @@
 	public:
 		using value_type = std::pair<std::string, Ref>;
 
+		friend class JsonObject;
+
 	private:
 		JsonObject &m_object;
 		void *m_keyIt;
@@ -1176,12 +1228,12 @@
 			return save;
 		}
 
-		inline operator==(iterator other) const noexcept
+		inline bool operator==(iterator other) const noexcept
 		{
 			return m_keyIt == other.m_keyIt;
 		}
 
-		inline operator!=(iterator other) const noexcept
+		inline bool operator!=(iterator other) const noexcept
 		{
 			return m_keyIt != other.m_keyIt;
 		}
@@ -1195,6 +1247,8 @@
 	public:
 		using value_type = std::pair<std::string, JsonValue>;
 
+		friend class JsonObject;
+
 	private:
 		const JsonObject &m_object;
 		void *m_keyIt;
@@ -1241,12 +1295,12 @@
 			return save;
 		}
 
-		inline operator==(const_iterator other) const noexcept
+		inline bool operator==(const_iterator other) const noexcept
 		{
 			return m_keyIt == other.m_keyIt;
 		}
 
-		inline operator!=(const_iterator other) const noexcept
+		inline bool operator!=(const_iterator other) const noexcept
 		{
 			return m_keyIt != other.m_keyIt;
 		}
@@ -1358,6 +1412,44 @@
 	}
 
 	/**
+	 * Remove all elements from the object.
+	 */
+	inline void clear() noexcept
+	{
+		json_object_clear(m_handle.get());
+	}
+
+	/**
+	 * Remove element `key' if exists.
+	 *
+	 * @param key the key
+	 */
+	inline void erase(const std::string &key) noexcept
+	{
+		json_object_del(m_handle.get(), key.c_str());
+	}
+
+	/**
+	 * Overloaded function.
+	 *
+	 * @param it the iterator
+	 */
+	inline void erase(iterator it) noexcept
+	{
+		erase(it.key());
+	}
+
+	/**
+	 * Overloaded function.
+	 *
+	 * @param it the iterator
+	 */
+	inline void erase(const_iterator it) noexcept
+	{
+		erase(it.key());
+	}
+
+	/**
 	 * Set the value as key in the object.
 	 *
 	 * @param key the key
--- a/C++/Tests/Json/main.cpp	Wed Feb 25 13:53:41 2015 +0100
+++ b/C++/Tests/Json/main.cpp	Thu Feb 26 14:21:29 2015 +0100
@@ -148,6 +148,46 @@
 	}
 }
 
+TEST(Object, clear)
+{
+	try {
+		JsonObject object;
+
+		object.set("integer", 123);
+		object.set("string", "hello");
+		object.set("true", true);
+
+		object.clear();
+
+		ASSERT_EQ(0, static_cast<int>(object.size()));
+		ASSERT_FALSE(object.contains("integer"));
+		ASSERT_FALSE(object.contains("string"));
+		ASSERT_FALSE(object.contains("true"));
+	} catch (const JsonError &ex) {
+		FAIL() << ex.what();
+	}
+}
+
+TEST(Object, erase)
+{
+	try {
+		JsonObject object;
+
+		object.set("integer", 123);
+		object.set("string", "hello");
+		object.set("true", true);
+
+		object.erase("integer");
+
+		ASSERT_EQ(2, static_cast<int>(object.size()));
+		ASSERT_FALSE(object.contains("integer"));
+		ASSERT_TRUE(object.contains("string"));
+		ASSERT_TRUE(object.contains("true"));
+	} catch (const JsonError &ex) {
+		FAIL() << ex.what();
+	}
+}
+
 TEST(ObjectInitializer, simple)
 {
 	try {
@@ -257,6 +297,80 @@
 	}
 }
 
+TEST(Array, clear)
+{
+	try {
+		JsonArray array;
+
+		array.append(1);
+		array.append("hello");
+		array.append(true);
+
+		array.clear();
+
+		ASSERT_EQ(0, static_cast<int>(array.size()));
+	} catch (const JsonError &ex) {
+		FAIL() << ex.what();
+	}
+}
+
+TEST(Array, erase)
+{
+	try {
+		JsonArray array;
+
+		array.append(1);
+		array.append("hello");
+		array.append(true);
+
+		array.erase(0);
+
+		ASSERT_EQ(2, static_cast<int>(array.size()));
+		ASSERT_EQ("hello", array[0].toString());
+		ASSERT_TRUE(array[1].isTrue());
+	} catch (const JsonError &ex) {
+		FAIL() << ex.what();
+	}
+}
+
+TEST(Array, eraseIterator)
+{
+	try {
+		JsonArray array;
+
+		array.append(1);
+		array.append("hello");
+		array.append(true);
+
+		array.erase(array.begin());
+
+		ASSERT_EQ(2, static_cast<int>(array.size()));
+		ASSERT_EQ("hello", array[0].toString());
+		ASSERT_TRUE(array[1].isTrue());
+	} catch (const JsonError &ex) {
+		FAIL() << ex.what();
+	}
+}
+
+TEST(Array, eraseConstIterator)
+{
+	try {
+		JsonArray 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].isTrue());
+	} catch (const JsonError &ex) {
+		FAIL() << ex.what();
+	}
+}
+
 TEST(ArrayInitializer, simple)
 {
 	try {