changeset 312:ea1a73a7d468

Json: use uniform initializion everywhere
author David Demelier <markand@malikania.fr>
date Fri, 13 Feb 2015 18:02:07 +0100
parents ed3cc10761e4
children cd490a8ab82a
files C++/Json.cpp C++/Json.h C++/Tests/Json/main.cpp
diffstat 3 files changed, 45 insertions(+), 57 deletions(-) [+]
line wrap: on
line diff
--- a/C++/Json.cpp	Fri Feb 13 13:42:21 2015 +0100
+++ b/C++/Json.cpp	Fri Feb 13 18:02:07 2015 +0100
@@ -23,47 +23,47 @@
  * -------------------------------------------------------- */
 
 JsonValue::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}
 {
 }
 
 JsonValue &JsonValue::operator=(const JsonValue &value)
 {
-	m_handle = Handle(json_deep_copy(value.m_handle.get()), json_decref);
+	m_handle = {json_deep_copy(value.m_handle.get()), json_decref};
 }
 
 JsonValue::JsonValue(json_t *json)
-	: m_handle(json, json_decref)
+	: m_handle{json, json_decref}
 {
 }
 
 JsonValue::JsonValue()
-	: m_handle(json_null(), json_decref)
+	: m_handle{json_null(), json_decref}
 {
 }
 
 JsonValue::JsonValue(bool value)
-	: m_handle(json_boolean(value), json_decref)
+	: m_handle{json_boolean(value), json_decref}
 {
 }
 
 JsonValue::JsonValue(int value)
-	: m_handle(json_integer(value), json_decref)
+	: m_handle{json_integer(value), json_decref}
 {
 }
 
 JsonValue::JsonValue(double value)
-	: m_handle(json_real(value), json_decref)
+	: m_handle{json_real(value), json_decref}
 {
 }
 
 JsonValue::JsonValue(std::string value)
-	: m_handle(json_string(value.c_str()), json_decref)
+	: m_handle{json_string(value.c_str()), json_decref}
 {
 }
 
 JsonValue::JsonValue(const char *value)
-	: m_handle(json_string(value), json_decref)
+	: m_handle{json_string(value), json_decref}
 {
 }
 
@@ -143,14 +143,14 @@
 {
 	json_incref(m_handle.get());
 
-	return JsonObject(m_handle.get());
+	return JsonObject{m_handle.get()};
 }
 
 JsonArray JsonValue::toArray() const
 {
 	json_incref(m_handle.get());
 
-	return JsonArray(m_handle.get());
+	return JsonArray{m_handle.get()};
 }
 
 /* --------------------------------------------------------
@@ -158,7 +158,7 @@
  * -------------------------------------------------------- */
 
 JsonArray::JsonArray()
-	: JsonValue(json_array())
+	: JsonValue{json_array()}
 {
 }
 
@@ -185,16 +185,16 @@
 JsonValue JsonArray::operator[](int index) const
 {
 	if (typeOf() != JsonType::Array)
-		throw JsonError("not an array");
+		throw JsonError{"not an array"};
 
 	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);
 
-	return JsonValue(value);
+	return JsonValue{value};
 }
 
 /* --------------------------------------------------------
@@ -202,23 +202,23 @@
  * -------------------------------------------------------- */
 
 JsonObject::JsonObject()
-	: JsonValue(json_object())
+	: JsonValue{json_object()}
 {
 }
 
 JsonValue JsonObject::operator[](const std::string &name) const
 {
 	if (typeOf() != JsonType::Object)
-		throw JsonError("not an object");
+		throw JsonError{"not an object"};
 
 	auto value = json_object_get(m_handle.get(), name.c_str());
 
 	if (value == nullptr)
-		throw JsonError("key " + name + +" not found");
+		throw JsonError{"key " + name + +" not found"};
 
 	json_incref(value);
 
-	return JsonValue(value);
+	return JsonValue{value};
 }
 
 void JsonObject::set(const std::string &key, const JsonValue &value)
@@ -231,7 +231,7 @@
  * -------------------------------------------------------- */
 
 JsonReaderFile::JsonReaderFile(std::string path)
-	: m_path(std::move(path))
+	: m_path{std::move(path)}
 {
 }
 
@@ -243,7 +243,7 @@
 	if (handle == nullptr)
 		throw JsonError{error};
 
-	return JsonValue(handle);
+	return JsonValue{handle};
 }
 
 /* --------------------------------------------------------
@@ -251,12 +251,12 @@
  * -------------------------------------------------------- */
 
 JsonWriterFile::JsonWriterFile(std::string path)
-	: m_path(std::move(path))
+	: m_path{std::move(path)}
 {
 }
 
 void JsonWriterFile::write(const JsonValue &value)
 {
 	if (json_dump_file(value, m_path.c_str(), 0) < 0)
-		throw JsonError("Failed to write file: " + m_path);
+		throw JsonError{"Failed to write file: " + m_path};
 }
--- a/C++/Json.h	Fri Feb 13 13:42:21 2015 +0100
+++ b/C++/Json.h	Fri Feb 13 18:02:07 2015 +0100
@@ -19,12 +19,10 @@
 #ifndef _JSON_H_
 #define _JSON_H_
 
-#include <initializer_list>
 #include <memory>
 #include <stdexcept>
 #include <string>
 #include <utility>
-#include <vector>
 
 #include <jansson.h>
 
@@ -163,7 +161,7 @@
 	 */
 	template <size_t Size>
 	inline JsonValue(char (&value)[Size])
-		: m_handle(json_string(value), json_decref)
+		: m_handle{json_string(value), json_decref}
 	{
 	}
 
@@ -377,7 +375,7 @@
 
 		json_array_foreach(m_handle.get(), index, value) {
 			json_incref(value);
-			function(static_cast<int>(index), JsonValue(value));
+			function(static_cast<int>(index), JsonValue{value});
 		}
 	}
 };
@@ -429,7 +427,7 @@
 
 		json_object_foreach(m_handle.get(), key, value) {
 			json_incref(value);
-			function(std::string(key), JsonValue(value));
+			function(std::string(key), JsonValue{value});
 		}
 	}
 };
@@ -446,7 +444,7 @@
 	std::string	m_source;
 	int		m_line{};
 	int		m_column{};
-	unsigned	m_position{};
+	int		m_position{};
 
 public:
 	/**
@@ -455,7 +453,7 @@
 	 * @param error the error message
 	 */
 	inline JsonError(std::string error)
-		: m_text(std::move(error))
+		: m_text{std::move(error)}
 	{
 	}
 
@@ -465,11 +463,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}
 	{
 	}
 
--- a/C++/Tests/Json/main.cpp	Fri Feb 13 13:42:21 2015 +0100
+++ b/C++/Tests/Json/main.cpp	Fri Feb 13 18:02:07 2015 +0100
@@ -31,7 +31,7 @@
 	object.set("integer", 123);
 	object.set("true", true);
 
-	JsonObject object2(object);
+	JsonObject object2{object};
 
 	ASSERT_TRUE(object2.isObject());
 	ASSERT_EQ(123, object2["integer"].toInteger());
@@ -131,7 +131,7 @@
 TEST(Open, simple)
 {
 	try {
-		JsonReaderFile reader(SOURCE "/data/simple.json");
+		JsonReaderFile reader{SOURCE "/data/simple.json"};
 
 		(void)reader.read();
 	} catch (const JsonError &ex) {
@@ -149,12 +149,12 @@
 			array.append(123);
 			array.append(true);
 
-			JsonWriterFile(BINARY "/write-array.json").write(array);
+			JsonWriterFile{BINARY "/write-array.json"}.write(array);
 		}
 
 		// Read
 		{
-			JsonArray array = JsonReaderFile(BINARY "/write-array.json").read().toArray();
+			JsonArray array = JsonReaderFile{BINARY "/write-array.json"}.read().toArray();
 
 			ASSERT_TRUE(array.isArray());
 			ASSERT_EQ(123, array[0].toInteger());
@@ -175,12 +175,12 @@
 			object.set("integer", 123);
 			object.set("true", true);
 
-			JsonWriterFile(BINARY "/write-object.json").write(object);
+			JsonWriterFile{BINARY "/write-object.json"}.write(object);
 		}
 
 		// Read
 		{
-			JsonObject object = JsonReaderFile(BINARY "/write-object.json").read().toObject();
+			JsonObject object = JsonReaderFile{BINARY "/write-object.json"}.read().toObject();
 
 			ASSERT_TRUE(object.isObject());
 			ASSERT_EQ(123, object["integer"].toInteger());
@@ -198,8 +198,7 @@
 TEST(Object, simple)
 {
 	try {
-		JsonReaderFile reader(SOURCE "/data/object.json");
-		JsonValue value = reader.read();
+		JsonValue value = JsonReaderFile{SOURCE "/data/object.json"}.read();
 
 		ASSERT_TRUE(value.isObject());
 		ASSERT_FALSE(value.isArray());
@@ -223,9 +222,7 @@
 TEST(Object, all)
 {
 	try {
-		JsonReaderFile reader(SOURCE "/data/object-all.json");
-		JsonValue value = reader.read();
-		JsonObject object = value.toObject();
+		JsonObject object = JsonReaderFile{SOURCE "/data/object-all.json"}.read().toObject();
 
 		ASSERT_TRUE(object["integer"].isInteger());
 		ASSERT_TRUE(object["integer"].isNumber());
@@ -255,9 +252,7 @@
 TEST(Object, forAll)
 {
 	try {
-		JsonReaderFile reader{SOURCE "/data/object-all.json"};
-		JsonValue value = reader.read();
-		JsonObject object = value.toObject();
+		JsonObject object = JsonReaderFile{SOURCE "/data/object-all.json"}.read().toObject();
 
 		object.forAll([] (const auto &key, const auto &value) {
 			if (key == "integer") {
@@ -307,8 +302,7 @@
 TEST(Array, simple)
 {
 	try {
-		JsonReaderFile reader{SOURCE "/data/array.json"};
-		JsonValue value = reader.read();
+		JsonValue value = JsonReaderFile{SOURCE "/data/array.json"}.read();
 
 		ASSERT_TRUE(value.isArray());
 		ASSERT_FALSE(value.isObject());
@@ -329,9 +323,7 @@
 TEST(Array, all)
 {
 	try {
-		JsonReaderFile reader{SOURCE "/data/array-all.json"};
-		JsonValue value = reader.read();
-		JsonArray array = value.toArray();
+		JsonArray array = JsonReaderFile{SOURCE "/data/array-all.json"}.read().toArray();
 
 		ASSERT_TRUE(array[0].isInteger());
 		ASSERT_TRUE(array[0].isNumber());
@@ -361,9 +353,7 @@
 TEST(Array, forAll)
 {
 	try {
-		JsonReaderFile reader{SOURCE "/data/array-all.json"};
-		JsonValue value = reader.read();
-		JsonArray array = value.toArray();
+		JsonArray array = JsonReaderFile{SOURCE "/data/array-all.json"}.read().toArray();
 
 		array.forAll([] (auto index, const auto &value) {
 			if (index == 0) {