changeset 502:26ce6a7ba695

Json: switch to json::from(String|File)
author David Demelier <markand@malikania.fr>
date Thu, 28 Jan 2016 09:50:34 +0100
parents 4c786b58eacd
children 964b8b700087
files modules/json/json.cpp modules/json/json.h modules/json/test/main.cpp
diffstat 3 files changed, 53 insertions(+), 85 deletions(-) [+]
line wrap: on
line diff
--- a/modules/json/json.cpp	Thu Jan 28 09:25:19 2016 +0100
+++ b/modules/json/json.cpp	Thu Jan 28 09:50:34 2016 +0100
@@ -1,7 +1,7 @@
 /*
  * json.cpp -- C++14 JSON manipulation using jansson parser
  *
- * Copyright (c) 2013-2015 David Demelier <markand@malikania.fr>
+ * Copyright (c) 2013-2016 David Demelier <markand@malikania.fr>
  *
  * Permission to use, copy, modify, and/or distribute this software for any
  * purpose with or without fee is hereby granted, provided that the above
@@ -215,31 +215,7 @@
 	}
 }
 
-bool Value::toBool() const noexcept
-{
-	if (m_type != Type::Boolean)
-		return false;
-
-	return m_boolean;
-}
-
-double Value::toReal() const noexcept
-{
-	if (m_type != Type::Real)
-		return 0;
-
-	return m_number;
-}
-
-int Value::toInt() const noexcept
-{
-	if (m_type != Type::Int)
-		return 0;
-
-	return m_integer;
-}
-
-std::string Value::toString(bool coerce) const noexcept
+std::string Value::toString(bool coerce) const
 {
 	std::string result;
 
@@ -251,16 +227,6 @@
 	return result;
 }
 
-Value::Value(const Buffer &buffer)
-{
-	*this = convert(json_loads, buffer.text.c_str(), 0);
-}
-
-Value::Value(const File &file)
-{
-	*this = convert(json_load_file, file.path.c_str(), 0);
-}
-
 std::string Value::toJson(int level, int current) const
 {
 	std::ostringstream oss;
@@ -362,4 +328,14 @@
 	return result;
 }
 
+Value fromString(const std::string &buffer)
+{
+	return convert(json_loads, buffer.c_str(), 0);
+}
+
+Value fromFile(const std::string &path)
+{
+	return convert(json_load_file, path.c_str(), 0);
+}
+
 } // !json
--- a/modules/json/json.h	Thu Jan 28 09:25:19 2016 +0100
+++ b/modules/json/json.h	Thu Jan 28 09:50:34 2016 +0100
@@ -1,7 +1,7 @@
 /*
  * json.h -- C++14 JSON manipulation using jansson parser
  *
- * Copyright (c) 2013-2015 David Demelier <markand@malikania.fr>
+ * Copyright (c) 2013-2016 David Demelier <markand@malikania.fr>
  *
  * Permission to use, copy, modify, and/or distribute this software for any
  * purpose with or without fee is hereby granted, provided that the above
@@ -148,24 +148,6 @@
 };
 
 /**
- * @class Buffer
- * @brief Open JSON document from text.
- */
-class Buffer {
-public:
-	std::string text;	//!< The JSON text
-};
-
-/**
- * @class File
- * @brief Open JSON document from a file.
- */
-class File {
-public:
-	std::string path;	//!< The path to the file
-};
-
-/**
  * @class Value
  * @brief Generic JSON value wrapper.
  */
@@ -462,22 +444,6 @@
 	}
 
 	/**
-	 * Construct a value from a buffer.
-	 *
-	 * @param buffer the text
-	 * @throw Error on errors
-	 */
-	Value(const Buffer &buffer);
-
-	/**
-	 * Construct a value from a file.
-	 *
-	 * @param file the file
-	 * @throw Error on errors
-	 */
-	Value(const File &file);
-
-	/**
 	 * Move constructor.
 	 *
 	 * @param other the value to move from
@@ -620,21 +586,30 @@
 	 *
 	 * @return the value or false if not a boolean
 	 */
-	bool toBool() const noexcept;
+	inline bool toBool() const noexcept
+	{
+		return m_type != Type::Boolean ? false : m_boolean;
+	}
 
 	/**
 	 * Get the value as integer.
 	 *
 	 * @return the value or 0 if not a integer
 	 */
-	int toInt() const noexcept;
+	inline int toInt() const noexcept
+	{
+		return m_type != Type::Int ? 0 : m_integer;
+	}
 
 	/**
 	 * Get the value as real.
 	 *
 	 * @return the value or 0 if not a real
 	 */
-	double toReal() const noexcept;
+	inline double toReal() const noexcept
+	{
+		return m_type != Type::Real ? 0 : m_number;
+	}
 
 	/**
 	 * Get the value as string.
@@ -642,7 +617,7 @@
 	 * @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(bool coerce = false) const noexcept;
+	std::string toString(bool coerce = false) const;
 
 	/**
 	 * Check if the value is boolean type.
@@ -1134,8 +1109,7 @@
 	/**
 	 * Return this value as JSon representation.
 	 *
-	 * @param indent, the indentation to use (0 == compact, < 0 == tabs, > 0 == number of spaces)
-	 * @param tabs, use tabs or not
+	 * @param indent the indentation to use (0 == compact, < 0 == tabs, > 0 == number of spaces)
 	 * @return the string
 	 */
 	inline std::string toJson(int indent = 2) const
@@ -1194,6 +1168,24 @@
 	return Value(std::map<std::string, Value>(values.begin(), values.end()));
 }
 
+/**
+ * Construct a value from a buffer.
+ *
+ * @param data the JSON data
+ * @return the parsed value
+ * @throw Error on errors
+ */
+Value fromString(const std::string &data);
+
+/**
+ * Construct a value from a file.
+ *
+ * @param path the path to the file
+ * @return the parsed value
+ * @throw Error on errors
+ */
+Value fromFile(const std::string &path);
+
 } // !json
 
 #endif // !_JSON_H_
--- a/modules/json/test/main.cpp	Thu Jan 28 09:25:19 2016 +0100
+++ b/modules/json/test/main.cpp	Thu Jan 28 09:50:34 2016 +0100
@@ -504,7 +504,7 @@
 TEST(FileRead, simple)
 {
 	try {
-		json::Value doc(json::File{"json/simple.json"});
+		json::Value doc = json::fromFile("json/simple.json");
 
 		ASSERT_TRUE(doc.isObject());
 		ASSERT_FALSE(doc.isArray());
@@ -516,7 +516,7 @@
 TEST(FileRead, fail)
 {
 	try {
-		json::Value(json::File{"json/notexist.json"});
+		json::fromFile("json/notexist.json");
 
 		FAIL() << "Exception expected";
 	} catch (const json::Error &) {
@@ -526,7 +526,7 @@
 TEST(StringRead, simple)
 {
 	try {
-		json::Value doc(json::Buffer{"{ \"license\": \"ISC\" }"});
+		json::Value doc = json::fromString("{ \"license\": \"ISC\" }");
 
 		ASSERT_TRUE(doc.isObject());
 		ASSERT_FALSE(doc.isArray());
@@ -538,7 +538,7 @@
 TEST(StringRead, fail)
 {
 	try {
-		json::Value(json::Buffer{"{ \"license\": ISC }"});
+		json::fromString("{ \"license\": ISC }");
 
 		FAIL() << "Exception expected";
 	} catch (const json::Error &ex) {
@@ -557,8 +557,8 @@
 
 public:
 	ObjectRead()
-		: m_object(json::File{"json/object.json"})
-		, m_objectAll(json::File{"json/object-all.json"})
+		: m_object(json::fromFile("json/object.json"))
+		, m_objectAll(json::fromFile("json/object-all.json"))
 	{
 	}
 };
@@ -621,8 +621,8 @@
 
 public:
 	ArrayRead()
-		: m_array(json::File{"json/array.json"})
-		, m_arrayAll(json::File{"json/array-all.json"})
+		: m_array(json::fromFile("json/array.json"))
+		, m_arrayAll(json::fromFile("json/array-all.json"))
 	{
 	}
 };