Mercurial > code
changeset 404:9ab878fb9fa2
Json: bring classes into json namespace
author | David Demelier <markand@malikania.fr> |
---|---|
date | Mon, 05 Oct 2015 14:39:57 +0200 |
parents | d5ec1174b707 |
children | f81478065901 |
files | C++/modules/Json/Json.cpp C++/modules/Json/Json.h C++/tests/Json/main.cpp |
diffstat | 3 files changed, 290 insertions(+), 282 deletions(-) [+] |
line wrap: on
line diff
--- a/C++/modules/Json/Json.cpp Mon Oct 05 14:27:19 2015 +0200 +++ b/C++/modules/Json/Json.cpp Mon Oct 05 14:39:57 2015 +0200 @@ -20,53 +20,55 @@ #include "Json.h" +namespace json { + /* -------------------------------------------------------- - * JsonObject + * Object * -------------------------------------------------------- */ -JsonObject JsonValue::toObject() const noexcept +Object Value::toObject() const noexcept { json_incref(m_handle.get()); - return JsonObject(m_handle.get()); + return Object(m_handle.get()); } -JsonArray JsonValue::toArray() const noexcept +Array Value::toArray() const noexcept { json_incref(m_handle.get()); - return JsonArray(m_handle.get()); + return Array(m_handle.get()); } /* -------------------------------------------------------- - * JsonArray + * Array * -------------------------------------------------------- */ -JsonValue JsonArray::at(int index) const +Value Array::at(int index) const { auto value = json_array_get(m_handle.get(), index); if (value == nullptr) - throw JsonError("index out of bounds"); + throw Error("index out of bounds"); json_incref(value); - return JsonValue{value}; + return Value{value}; } -JsonValue JsonArray::operator[](int index) const noexcept +Value Array::operator[](int index) const noexcept { auto value = json_array_get(m_handle.get(), index); if (value == nullptr) - return JsonValue(); + return Value(); json_incref(value); - return JsonValue(value); + return Value(value); } -JsonArray::Ref JsonArray::operator[](int index) noexcept +Array::Ref Array::operator[](int index) noexcept { auto value = json_array_get(m_handle.get(), index); @@ -79,13 +81,13 @@ } /* -------------------------------------------------------- - * JsonObject + * Object * -------------------------------------------------------- */ -JsonObject::Ref JsonObject::operator[](const std::string &name) +Object::Ref Object::operator[](const std::string &name) { - if (typeOf() != JsonType::Object) - return Ref(JsonValue(), *this, name); + if (typeOf() != Type::Object) + return Ref(Value(), *this, name); auto value = json_object_get(m_handle.get(), name.c_str()); @@ -94,40 +96,40 @@ return Ref(value, *this, name); } -JsonValue JsonObject::operator[](const std::string &name) const +Value Object::operator[](const std::string &name) const { - if (typeOf() != JsonType::Object) - return JsonValue(); + if (typeOf() != Type::Object) + return Value(); auto value = json_object_get(m_handle.get(), name.c_str()); if (value == nullptr) - return JsonValue(); + return Value(); json_incref(value); - return JsonValue(value); + return Value(value); } /* -------------------------------------------------------- - * JsonDocument + * Document * -------------------------------------------------------- */ -JsonValue JsonDocument::read(std::string content, int flags) const +Value Document::read(std::string content, int flags) const { json_error_t error; json_t *json = json_loads(content.c_str(), flags, &error); if (json == nullptr) - throw JsonError(error); + throw Error(error); - return JsonValue(json); + return Value(json); } -JsonValue JsonDocument::read(std::ifstream &stream, int flags) const +Value Document::read(std::ifstream &stream, int flags) const { if (!stream.is_open()) - throw JsonError("File not opened"); + throw Error("File not opened"); stream.seekg(0, stream.end); auto length = stream.tellg(); @@ -142,17 +144,19 @@ return read(std::move(buffer), flags); } -JsonDocument::JsonDocument(std::ifstream &stream, int flags) +Document::Document(std::ifstream &stream, int flags) { m_value = read(stream, flags); } -JsonDocument::JsonDocument(std::ifstream &&stream, int flags) +Document::Document(std::ifstream &&stream, int flags) { m_value = read(stream, flags); } -JsonDocument::JsonDocument(std::string content, int flags) +Document::Document(std::string content, int flags) { m_value = read(std::move(content), flags); -} \ No newline at end of file +} + +} // !json \ No newline at end of file
--- a/C++/modules/Json/Json.h Mon Oct 05 14:27:19 2015 +0200 +++ b/C++/modules/Json/Json.h Mon Oct 05 14:39:57 2015 +0200 @@ -41,17 +41,19 @@ * * This means that you can't set any value to an existing value as it would * change a value which may be used somewhere else, instead you must set - * or replace elements in JsonObject and JsonArray respectively. + * or replace elements in Object and Array respectively. * * However, copy constructors are implemented as deep copy so take care of * not copying values mistakenly. */ +namespace json { + /** - * @class JsonType + * @class Type * @brief Json value type */ -enum class JsonType { +enum class Type { Object = JSON_OBJECT, //!< Object Array = JSON_ARRAY, //!< Array String = JSON_STRING, //!< String @@ -63,10 +65,10 @@ }; /** - * @class JsonError + * @class Error * @brief Error thrown for any error */ -class JsonError final : public std::exception { +class Error final : public std::exception { private: std::string m_text; std::string m_source; @@ -80,7 +82,7 @@ * * @param error the error message */ - inline JsonError(std::string error) + inline Error(std::string error) : m_text(std::move(error)) { } @@ -90,7 +92,7 @@ * * @param error the error */ - inline JsonError(const json_error_t &error) + inline Error(const json_error_t &error) : m_text(error.text) , m_source(error.source) , m_line(error.line) @@ -160,19 +162,19 @@ } }; -class JsonObject; -class JsonArray; +class Object; +class Array; /** - * @class JsonValue + * @class Value * @brief Encapsulate any JSON value */ -class JsonValue { +class Value { public: using Handle = std::unique_ptr<json_t, void (*)(json_t *)>; - friend class JsonObject; - friend class JsonArray; + friend class Object; + friend class Array; protected: /** @@ -184,7 +186,7 @@ inline void check() const { if (m_handle == nullptr) - throw JsonError(std::strerror(errno)); + throw Error(std::strerror(errno)); } public: @@ -192,9 +194,9 @@ * Deep copy of that element. * * @param value the other value - * @throw JsonError on allocation error + * @throw Error on allocation error */ - inline JsonValue(const JsonValue &value) + inline Value(const Value &value) : m_handle(json_deep_copy(value.m_handle.get()), json_decref) { check(); @@ -204,9 +206,9 @@ * Assign a deep copy of the other element. * * @return *this - * @throw JsonError on allocation error + * @throw Error on allocation error */ - inline JsonValue &operator=(const JsonValue &value) + inline Value &operator=(const Value &value) { m_handle = Handle(json_deep_copy(value.m_handle.get()), json_decref); @@ -216,22 +218,22 @@ } /** - * Move constructor, the other value is left empty (JsonType::Null). + * Move constructor, the other value is left empty (Type::Null). * * @param other the other value */ - inline JsonValue(JsonValue &&other) noexcept + inline Value(Value &&other) noexcept : m_handle(std::move(other.m_handle)) { other.m_handle = Handle(json_null(), json_decref); } /** - * Move assignment, the other value is left empty (JsonType::Null). + * Move assignment, the other value is left empty (Type::Null). * * @param other the other value */ - inline JsonValue &operator=(JsonValue &&other) noexcept + inline Value &operator=(Value &&other) noexcept { m_handle = std::move(other.m_handle); other.m_handle = Handle(json_null(), json_decref); @@ -240,12 +242,12 @@ } /** - * Create a JsonValue from a native Jansson type. This function + * Create a Value from a native Jansson type. This function * will increment the json_t reference count. * * @param json the value */ - inline JsonValue(json_t *json) noexcept + inline Value(json_t *json) noexcept : m_handle(json, json_decref) { } @@ -253,15 +255,15 @@ /** * Construct a null value from a nullptr argument. */ - inline JsonValue(std::nullptr_t) noexcept + inline Value(std::nullptr_t) noexcept : m_handle(json_null(), json_decref) { } /** - * Create an empty value (JsonType::Null). + * Create an empty value (Type::Null). */ - inline JsonValue() noexcept + inline Value() noexcept : m_handle(json_null(), json_decref) { } @@ -271,67 +273,67 @@ * * @param value the value */ - inline JsonValue(bool value) noexcept + inline Value(bool value) noexcept : m_handle(json_boolean(value), json_decref) { } /** - * Create a integer value (JsonType::Integer). + * Create a integer value (Type::Integer). * * @param value the value - * @throw JsonError on allocation error + * @throw Error on allocation error */ - inline JsonValue(int value) + inline Value(int value) : m_handle(json_integer(value), json_decref) { check(); } /** - * Create a real value (JsonType::Real). + * Create a real value (Type::Real). * * @param value the value - * @throw JsonError on allocation error + * @throw Error on allocation error */ - inline JsonValue(double value) + inline Value(double value) : m_handle(json_real(value), json_decref) { check(); } /** - * Create a string value (JsonType::String). + * Create a string value (Type::String). * * @param value the value - * @throw JsonError on allocation error + * @throw Error on allocation error */ - inline JsonValue(std::string value) + inline Value(std::string value) : m_handle(json_string(value.c_str()), json_decref) { check(); } /** - * Create from a C string (JsonType::String). + * Create from a C string (Type::String). * * @param value the string - * @throw JsonError on allocation error + * @throw Error on allocation error */ - inline JsonValue(const char *value) + inline Value(const char *value) : m_handle(json_string(value), json_decref) { check(); } /** - * Create from a string literal (JsonType::String). + * Create from a string literal (Type::String). * * @param value the value - * @throw JsonError on allocation error + * @throw Error on allocation error */ template <size_t Size> - inline JsonValue(char (&value)[Size]) + inline Value(char (&value)[Size]) : m_handle(json_string(value), json_decref) { check(); @@ -340,16 +342,16 @@ /** * Default destructor. */ - virtual ~JsonValue() = default; + virtual ~Value() = default; /** * Get the type of value. * * @return the type */ - inline JsonType typeOf() const noexcept + inline Type typeOf() const noexcept { - return static_cast<JsonType>(json_typeof(m_handle.get())); + return static_cast<Type>(json_typeof(m_handle.get())); } /** @@ -489,14 +491,14 @@ * * @return an object */ - JsonObject toObject() const noexcept; + Object toObject() const noexcept; /** * Convert to array. * * @return an array */ - JsonArray toArray() const noexcept; + Array toArray() const noexcept; /** * Write to a stream. @@ -568,58 +570,58 @@ /** * Equality operator. */ - inline bool operator==(const JsonValue &other) const noexcept + inline bool operator==(const Value &other) const noexcept { return json_equal(m_handle.get(), other.m_handle.get()); } }; /** - * @class JsonArray + * @class Array * @brief Manipulate JSON arrays */ -class JsonArray final : public JsonValue { +class Array final : public Value { public: /** * @class Ref * @brief Reference wrapper to be assignable */ - class Ref final : public JsonValue { + class Ref final : public Value { private: - JsonArray &m_array; + Array &m_array; int m_index; public: - explicit inline Ref(JsonValue value, JsonArray &array, int index) - : JsonValue(std::move(value)) + explicit inline Ref(Value value, Array &array, int index) + : Value(std::move(value)) , m_array(array) , m_index(index) { } - inline operator JsonValue() const noexcept + inline operator Value() const noexcept { return *this; } - inline JsonValue &operator*() noexcept + inline Value &operator*() noexcept { return *this; } - inline JsonValue *operator->() noexcept + inline Value *operator->() noexcept { return this; } - inline Ref &operator=(const JsonValue &value) + inline Ref &operator=(const Value &value) { m_array.replace(value, m_index); return *this; } - inline Ref &operator=(JsonValue &&value) + inline Ref &operator=(Value &&value) { m_array.replace(std::move(value), m_index); @@ -629,21 +631,21 @@ /** * @class Ptr - * @brief Pointer wrapper for JsonValue iterators + * @brief Pointer wrapper for Value iterators */ - class Ptr final : public JsonValue { + class Ptr final : public Value { public: - explicit Ptr(JsonValue value) noexcept - : JsonValue(std::move(value)) + explicit Ptr(Value value) noexcept + : Value(std::move(value)) { } - inline JsonValue &operator*() noexcept + inline Value &operator*() noexcept { return *this; } - inline JsonValue *operator->() noexcept + inline Value *operator->() noexcept { return this; } @@ -653,18 +655,18 @@ public: using iterator_category = std::random_access_iterator_tag; using difference_type = int; - using value_type = JsonValue; + using value_type = Value; using reference = Ref; using pointer = Ptr; - friend class JsonArray; + friend class Array; private: - JsonArray &m_array; + Array &m_array; int m_index; public: - explicit inline iterator(JsonArray &array, int index = 0) noexcept + explicit inline iterator(Array &array, int index = 0) noexcept : m_array(array) , m_index(index) { @@ -781,24 +783,24 @@ public: using iterator_category = std::random_access_iterator_tag; using difference_type = int; - using value_type = JsonValue; - using reference = JsonValue; + using value_type = Value; + using reference = Value; using pointer = Ptr; - friend class JsonArray; + friend class Array; private: - const JsonArray &m_array; + const Array &m_array; int m_index; public: - explicit inline const_iterator(const JsonArray &array, int index = 0) noexcept + explicit inline const_iterator(const Array &array, int index = 0) noexcept : m_array(array) , m_index(index) { } - inline JsonValue operator*() const + inline Value operator*() const { return m_array.at(m_index); } @@ -808,7 +810,7 @@ return Ptr(m_array.at(m_index)); } - inline JsonValue operator[](int nindex) const noexcept + inline Value operator[](int nindex) const noexcept { return m_array.at(m_index + nindex); } @@ -906,23 +908,23 @@ }; using size_type = int; - using value_type = JsonValue; + using value_type = Value; using const_pointer = const value_type *; - using reference = JsonValue &; - using const_reference = const JsonValue &; + using reference = Value &; + using const_reference = const Value &; using difference_type = int; protected: - using JsonValue::JsonValue; + using Value::Value; public: /** * Create an empty array. * - * @throw JsonError on allocation error + * @throw Error on allocation error */ - inline JsonArray() - : JsonValue(json_array()) + inline Array() + : Value(json_array()) { check(); } @@ -931,10 +933,10 @@ * Create an array from a list of values. * * @param list the list - * @throw JsonError on allocation error + * @throw Error on allocation error */ - inline JsonArray(std::initializer_list<value_type> list) - : JsonArray() + inline Array(std::initializer_list<value_type> list) + : Array() { for (auto &v : list) append(std::move(v)); @@ -1004,9 +1006,9 @@ * Get a value. * * @param index the index - * @throw JsonError on error + * @throw Error on error */ - JsonValue at(int index) const; + Value at(int index) const; /** * Erase the array content. @@ -1061,7 +1063,7 @@ * * @param value the value */ - inline void push(const JsonValue &value) noexcept + inline void push(const Value &value) noexcept { json_array_insert(m_handle.get(), 0, value.m_handle.get()); } @@ -1071,7 +1073,7 @@ * * @param value the value to insert */ - inline void append(const JsonValue &value) + inline void append(const Value &value) { json_array_append(m_handle.get(), value.m_handle.get()); } @@ -1082,7 +1084,7 @@ * @param value the value to insert * @param index the position */ - inline void insert(const JsonValue &value, int index) + inline void insert(const Value &value, int index) { json_array_insert(m_handle.get(), index, value.m_handle.get()); } @@ -1093,7 +1095,7 @@ * @param value the value * @param index the index */ - inline void replace(const JsonValue &value, int index) + inline void replace(const Value &value, int index) { json_array_set(m_handle.get(), index, value.m_handle.get()); } @@ -1104,7 +1106,7 @@ * @param index the position * @return the value */ - JsonValue operator[](int index) const noexcept; + Value operator[](int index) const noexcept; /** * Access a value as a reference wrapper. @@ -1116,64 +1118,64 @@ }; /** - * @class JsonObject + * @class Object * @brief Object wrapper */ -class JsonObject final : public JsonValue { +class Object final : public Value { public: using key_type = std::string; - using mapped_type = JsonValue; + using mapped_type = Value; using size_type = int; using value_type = std::pair<key_type, mapped_type>; using const_pointer = const value_type *; - using reference = JsonValue &; - using const_reference = const JsonValue &; + using reference = Value &; + using const_reference = const Value &; using difference_type = int; /** * @class Ref - * @brief Wrapper for updating JsonObject + * @brief Wrapper for updating Object * * This class is only used for the following functions: * - * JsonObject::operator[] + * Object::operator[] */ - class Ref final : public JsonValue { + class Ref final : public Value { private: - JsonObject &m_object; + Object &m_object; std::string m_key; public: - explicit inline Ref(JsonValue value, JsonObject &object, std::string key) - : JsonValue(std::move(value)) + explicit inline Ref(Value value, Object &object, std::string key) + : Value(std::move(value)) , m_object(object) , m_key(std::move(key)) { } - inline operator JsonValue() const noexcept + inline operator Value() const noexcept { return *this; } - inline JsonValue &operator*() noexcept + inline Value &operator*() noexcept { return *this; } - inline JsonValue *operator->() noexcept + inline Value *operator->() noexcept { return this; } - inline Ref &operator=(const JsonValue &value) + inline Ref &operator=(const Value &value) { m_object.set(m_key, value); return *this; } - inline Ref &operator=(JsonValue &&value) + inline Ref &operator=(Value &&value) { m_object.set(m_key, std::move(value)); @@ -1183,9 +1185,9 @@ /** * @class Ptr - * @brief Pointer wrapper for JsonValue iterators + * @brief Pointer wrapper for Value iterators * - * For const iterators, the real type is a JsonValue, for non const + * For const iterators, the real type is a Value, for non const * iterators it's a ref so that user can edit it. */ template <typename Type> @@ -1218,10 +1220,10 @@ public: using value_type = std::pair<std::string, Ref>; - friend class JsonObject; + friend class Object; private: - JsonObject &m_object; + Object &m_object; void *m_keyIt; inline std::string key() const noexcept @@ -1230,7 +1232,7 @@ } public: - explicit inline iterator(JsonObject &object, void *keyIt) noexcept + explicit inline iterator(Object &object, void *keyIt) noexcept : m_object(object) , m_keyIt(keyIt) { @@ -1283,12 +1285,12 @@ */ class const_iterator { public: - using value_type = std::pair<std::string, JsonValue>; + using value_type = std::pair<std::string, Value>; - friend class JsonObject; + friend class Object; private: - const JsonObject &m_object; + const Object &m_object; void *m_keyIt; inline std::string key() const noexcept @@ -1297,7 +1299,7 @@ } public: - explicit inline const_iterator(const JsonObject &object, void *keyIt) noexcept + explicit inline const_iterator(const Object &object, void *keyIt) noexcept : m_object(object) , m_keyIt(keyIt) { @@ -1310,11 +1312,11 @@ return value_type(k, m_object[k]); } - inline Ptr<JsonValue> operator->() const + inline Ptr<Value> operator->() const { auto k = key(); - return Ptr<JsonValue>({k, m_object[k]}); + return Ptr<Value>({k, m_object[k]}); } inline const_iterator &operator++() noexcept @@ -1345,28 +1347,28 @@ }; protected: - using JsonValue::JsonValue; + using Value::Value; public: /** * Create empty object. * - * @throw JsonError on allocation error + * @throw Error on allocation error */ - inline JsonObject() - : JsonValue(json_object()) + inline Object() + : Value(json_object()) { check(); } /** - * Create a JsonObject from an initializer_list. + * Create a Object from an initializer_list. * * @param list the list of key-value pairs - * @throw JsonError on allocation error + * @throw Error on allocation error */ - inline JsonObject(std::initializer_list<value_type> list) - : JsonObject() + inline Object(std::initializer_list<value_type> list) + : Object() { for (auto &v : list) set(v.first, std::move(v.second)); @@ -1497,7 +1499,7 @@ * @param key the key * @param value the value */ - inline void set(const std::string &key, const JsonValue &value) noexcept + inline void set(const std::string &key, const Value &value) noexcept { json_object_set(m_handle.get(), key.c_str(), value.m_handle.get()); } @@ -1517,19 +1519,19 @@ * @param key the key * @return the value */ - JsonValue operator[](const std::string &key) const; + Value operator[](const std::string &key) const; }; /** - * @class JsonDocument + * @class Document * @brief Read files and strings to create Json values */ -class JsonDocument final { +class Document final { private: - JsonValue m_value; + Value m_value; - JsonValue read(std::string, int flags) const; - JsonValue read(std::ifstream &stream, int flags) const; + Value read(std::string, int flags) const; + Value read(std::ifstream &stream, int flags) const; public: /** @@ -1537,27 +1539,27 @@ * * @param stream the stream * @param flags the optional Jansson flags - * @throw JsonError on errors + * @throw Error on errors */ - JsonDocument(std::ifstream &fstream, int flags = 0); + Document(std::ifstream &fstream, int flags = 0); /** * Construct a document from a file. * * @param stream the stream * @param flags the optional Jansson flags - * @throw JsonError on errors + * @throw Error on errors */ - JsonDocument(std::ifstream &&stream, int flags = 0); + Document(std::ifstream &&stream, int flags = 0); /** * Construct a document from a file. * * @param stream the stream * @param flags the optional Jansson flags - * @throw JsonError on errors + * @throw Error on errors */ - JsonDocument(std::string content, int flags = 0); + Document(std::string content, int flags = 0); /** * Check if the document contains an object. @@ -1584,7 +1586,7 @@ * * @return the value as object */ - inline JsonObject toObject() const noexcept + inline Object toObject() const noexcept { return m_value.toObject(); } @@ -1594,10 +1596,12 @@ * * @return the value as array */ - inline JsonArray toArray() const noexcept + inline Array toArray() const noexcept { return m_value.toArray(); } }; +} // !json + #endif // !_JSON_H_
--- a/C++/tests/Json/main.cpp Mon Oct 05 14:27:19 2015 +0200 +++ b/C++/tests/Json/main.cpp Mon Oct 05 14:39:57 2015 +0200 @@ -29,12 +29,12 @@ TEST(Misc, copy) { - JsonObject object; + json::Object object; object.set("integer", 123); object.set("true", true); - JsonObject object2{object}; + json::Object object2{object}; ASSERT_TRUE(object2.isObject()); ASSERT_EQ(123, object2["integer"].toInteger()); @@ -43,10 +43,10 @@ TEST(Misc, copyAssign) { - JsonObject object; + json::Object object; { - JsonObject tmp; + json::Object tmp; tmp.set("integer", 123); tmp.set("true", true); @@ -61,8 +61,8 @@ TEST(Misc, move) { - JsonObject object(123); - JsonObject object2(std::move(object)); + json::Object object(123); + json::Object object2(std::move(object)); ASSERT_TRUE(object.isNull()); ASSERT_TRUE(object2.isInteger()); @@ -71,8 +71,8 @@ TEST(Misc, moveAssign) { - JsonObject object(123); - JsonObject object2; + json::Object object(123); + json::Object object2; object2 = std::move(object); @@ -82,16 +82,16 @@ } /* -------------------------------------------------------- - * JsonValue constructors + * json::Value constructors * -------------------------------------------------------- */ TEST(Constructors, null) { try { - JsonValue value; + json::Value value; ASSERT_TRUE(value.isNull()); - } catch (const JsonError &ex) { + } catch (const json::Error &ex) { FAIL() << ex.what(); } } @@ -99,12 +99,12 @@ TEST(Constructors, boolean) { try { - JsonValue value{true}; + json::Value value{true}; ASSERT_TRUE(value.isTrue()); ASSERT_TRUE(value.isBoolean()); ASSERT_FALSE(value.isFalse()); - } catch (const JsonError &ex) { + } catch (const json::Error &ex) { FAIL() << ex.what(); } } @@ -112,13 +112,13 @@ TEST(Constructors, integer) { try { - JsonValue value{123}; + json::Value value{123}; ASSERT_TRUE(value.isInteger()); ASSERT_TRUE(value.isNumber()); ASSERT_FALSE(value.isReal()); ASSERT_EQ(123, value.toInteger()); - } catch (const JsonError &ex) { + } catch (const json::Error &ex) { FAIL() << ex.what(); } } @@ -126,13 +126,13 @@ TEST(Constructors, real) { try { - JsonValue value{9.2}; + json::Value value{9.2}; ASSERT_TRUE(value.isReal()); ASSERT_TRUE(value.isNumber()); ASSERT_FALSE(value.isInteger()); ASSERT_EQ(9.2, value.toReal()); - } catch (const JsonError &ex) { + } catch (const json::Error &ex) { FAIL() << ex.what(); } } @@ -140,11 +140,11 @@ TEST(Constructors, string) { try { - JsonValue value("hello"); + json::Value value("hello"); ASSERT_TRUE(value.isString()); ASSERT_EQ("hello", value.toString()); - } catch (const JsonError &ex) { + } catch (const json::Error &ex) { FAIL() << ex.what(); } } @@ -156,7 +156,7 @@ TEST(Object, set) { try { - JsonObject object; + json::Object object; object.set("integer", 123); object.set("string", "hello"); @@ -165,7 +165,7 @@ ASSERT_EQ(123, object["integer"].toInteger()); ASSERT_EQ("hello", object["string"].toString()); ASSERT_TRUE(object["true"].isTrue()); - } catch (const JsonError &ex) { + } catch (const json::Error &ex) { FAIL() << ex.what(); } } @@ -173,7 +173,7 @@ TEST(Object, clear) { try { - JsonObject object; + json::Object object; object.set("integer", 123); object.set("string", "hello"); @@ -185,7 +185,7 @@ ASSERT_FALSE(object.contains("integer")); ASSERT_FALSE(object.contains("string")); ASSERT_FALSE(object.contains("true")); - } catch (const JsonError &ex) { + } catch (const json::Error &ex) { FAIL() << ex.what(); } } @@ -193,7 +193,7 @@ TEST(Object, erase) { try { - JsonObject object; + json::Object object; object.set("integer", 123); object.set("string", "hello"); @@ -205,7 +205,7 @@ ASSERT_FALSE(object.contains("integer")); ASSERT_TRUE(object.contains("string")); ASSERT_TRUE(object.contains("true")); - } catch (const JsonError &ex) { + } catch (const json::Error &ex) { FAIL() << ex.what(); } } @@ -213,7 +213,7 @@ TEST(ObjectInitializer, simple) { try { - JsonObject object{ + json::Object object{ { "username", "jean" }, { "age", 99 } }; @@ -229,10 +229,10 @@ TEST(ObjectInitializer, deep) { try { - JsonObject object{ + json::Object object{ { "username", "jean" }, { "age", 99 }, - { "network", JsonObject{ + { "network", json::Object{ { "port", 9999 }, { "host", "localhost" } } @@ -245,7 +245,7 @@ ASSERT_EQ(99, object["age"].toInteger()); // Second - JsonObject network = object["network"].toObject(); + json::Object network = object["network"].toObject(); ASSERT_TRUE(network.isObject()); ASSERT_EQ(2, static_cast<int>(network.size())); ASSERT_EQ(9999, network["port"].toInteger()); @@ -262,7 +262,7 @@ TEST(Array, push) { try { - JsonArray array; + json::Array array; ASSERT_TRUE(array.isArray()); @@ -274,7 +274,7 @@ ASSERT_TRUE(array[0].isTrue()); ASSERT_EQ("hello", array[1].toString()); ASSERT_EQ(1, array[2].toInteger()); - } catch (const JsonError &ex) { + } catch (const json::Error &ex) { FAIL() << ex.what(); } } @@ -282,7 +282,7 @@ TEST(Array, append) { try { - JsonArray array; + json::Array array; ASSERT_TRUE(array.isArray()); @@ -294,7 +294,7 @@ ASSERT_EQ(1, array[0].toInteger()); ASSERT_EQ("hello", array[1].toString()); ASSERT_TRUE(array[2].isTrue()); - } catch (const JsonError &ex) { + } catch (const json::Error &ex) { FAIL() << ex.what(); } } @@ -302,7 +302,7 @@ TEST(Array, insert) { try { - JsonArray array; + json::Array array; ASSERT_TRUE(array.isArray()); @@ -314,7 +314,7 @@ ASSERT_TRUE(array[0].isTrue()); ASSERT_EQ(1, array[1].toInteger()); ASSERT_EQ("hello", array[2].toString()); - } catch (const JsonError &ex) { + } catch (const json::Error &ex) { FAIL() << ex.what(); } } @@ -322,7 +322,7 @@ TEST(Array, clear) { try { - JsonArray array; + json::Array array; array.append(1); array.append("hello"); @@ -331,7 +331,7 @@ array.clear(); ASSERT_EQ(0, static_cast<int>(array.size())); - } catch (const JsonError &ex) { + } catch (const json::Error &ex) { FAIL() << ex.what(); } } @@ -339,7 +339,7 @@ TEST(Array, erase) { try { - JsonArray array; + json::Array array; array.append(1); array.append("hello"); @@ -350,7 +350,7 @@ ASSERT_EQ(2, static_cast<int>(array.size())); ASSERT_EQ("hello", array[0].toString()); ASSERT_TRUE(array[1].isTrue()); - } catch (const JsonError &ex) { + } catch (const json::Error &ex) { FAIL() << ex.what(); } } @@ -358,7 +358,7 @@ TEST(Array, eraseIterator) { try { - JsonArray array; + json::Array array; array.append(1); array.append("hello"); @@ -369,7 +369,7 @@ ASSERT_EQ(2, static_cast<int>(array.size())); ASSERT_EQ("hello", array[0].toString()); ASSERT_TRUE(array[1].isTrue()); - } catch (const JsonError &ex) { + } catch (const json::Error &ex) { FAIL() << ex.what(); } } @@ -377,7 +377,7 @@ TEST(Array, eraseConstIterator) { try { - JsonArray array; + json::Array array; array.append(1); array.append("hello"); @@ -388,7 +388,7 @@ ASSERT_EQ(2, static_cast<int>(array.size())); ASSERT_EQ("hello", array[0].toString()); ASSERT_TRUE(array[1].isTrue()); - } catch (const JsonError &ex) { + } catch (const json::Error &ex) { FAIL() << ex.what(); } } @@ -396,13 +396,13 @@ TEST(ArrayInitializer, simple) { try { - JsonArray array{123, true, "hello"}; + json::Array array{123, true, "hello"}; ASSERT_EQ(3, static_cast<int>(array.size())); ASSERT_EQ(123, array[0].toInteger()); ASSERT_TRUE(array[1].isTrue()); ASSERT_EQ("hello", array[2].toString()); - } catch (const JsonError &ex) { + } catch (const json::Error &ex) { FAIL() << ex.what(); } } @@ -410,11 +410,11 @@ TEST(ArrayInitializer, deep) { try { - JsonArray array{ + json::Array array{ 123, true, "hello", - JsonArray{ + json::Array{ 321, false, "olleh" @@ -428,13 +428,13 @@ ASSERT_EQ("hello", array[2].toString()); // Second - JsonArray array2 = array[3].toArray(); + json::Array array2 = array[3].toArray(); ASSERT_TRUE(array.isArray()); ASSERT_EQ(3, static_cast<int>(array2.size())); ASSERT_EQ(321, array2[0].toInteger()); ASSERT_TRUE(array2[1].isFalse()); ASSERT_EQ("olleh", array2[2].toString()); - } catch (const JsonError &ex) { + } catch (const json::Error &ex) { FAIL() << ex.what(); } } @@ -446,17 +446,17 @@ TEST(FileRead, simple) { try { - JsonDocument doc(std::ifstream("Json/simple.json")); + json::Document doc(std::ifstream("Json/simple.json")); ASSERT_TRUE(doc.isObject()); ASSERT_FALSE(doc.isArray()); - JsonObject object = doc.toObject(); - JsonArray array = doc.toArray(); + json::Object object = doc.toObject(); + json::Array array = doc.toArray(); ASSERT_TRUE(object.isObject()); ASSERT_FALSE(array.isArray()); - } catch (const JsonError &ex) { + } catch (const json::Error &ex) { FAIL() << ex.what(); } } @@ -464,27 +464,27 @@ TEST(FileRead, fail) { try { - JsonDocument(std::ifstream("Json/notexist.json")); + json::Document(std::ifstream("Json/notexist.json")); FAIL() << "Exception expected"; - } catch (const JsonError &) { + } catch (const json::Error &) { } } TEST(FileWrite, simple) { try { - JsonObject object{ + json::Object object{ { "name", "jean" }, { "age", 99 } }; object.write(std::ofstream("object-write.json")); - JsonObject object2 = JsonDocument(std::ifstream("object-write.json")).toObject(); + json::Object object2 = json::Document(std::ifstream("object-write.json")).toObject(); ASSERT_EQ(object2, object); - } catch (const JsonError &ex) { + } catch (const json::Error &ex) { FAIL() << ex.what(); } } @@ -492,17 +492,17 @@ TEST(StringRead, simple) { try { - JsonDocument doc("{ \"license\": \"ISC\" }"); + json::Document doc("{ \"license\": \"ISC\" }"); ASSERT_TRUE(doc.isObject()); ASSERT_FALSE(doc.isArray()); - JsonObject object = doc.toObject(); - JsonArray array = doc.toArray(); + json::Object object = doc.toObject(); + json::Array array = doc.toArray(); ASSERT_TRUE(object.isObject()); ASSERT_FALSE(array.isArray()); - } catch (const JsonError &ex) { + } catch (const json::Error &ex) { FAIL() << ex.what(); } } @@ -510,25 +510,25 @@ TEST(StringRead, fail) { try { - JsonDocument("{ \"license\": ISC }"); + json::Document("{ \"license\": ISC }"); FAIL() << "Exception expected"; - } catch (const JsonError &ex) { + } catch (const json::Error &ex) { } } TEST(StringWrite, simple) { try { - JsonObject object{ + json::Object object{ { "name", "jean" }, { "age", 99 } }; - JsonObject object2 = JsonDocument(object.dump()).toObject(); + json::Object object2 = json::Document(object.dump()).toObject(); ASSERT_EQ(object2, object); - } catch (const JsonError &ex) { + } catch (const json::Error &ex) { FAIL() << ex.what(); } } @@ -539,28 +539,28 @@ class ObjectRead : public testing::Test { protected: - JsonObject m_object; - JsonObject m_objectAll; + json::Object m_object; + json::Object m_objectAll; public: ObjectRead() { - m_object = JsonDocument(std::ifstream("Json/object.json")).toObject(); - m_objectAll = JsonDocument(std::ifstream("Json/object-all.json")).toObject(); + m_object = json::Document(std::ifstream("Json/object.json")).toObject(); + m_objectAll = json::Document(std::ifstream("Json/object-all.json")).toObject(); } }; TEST_F(ObjectRead, simple) { try { - JsonValue name = m_object["name"]; - JsonValue description = m_object["description"]; + json::Value name = m_object["name"]; + json::Value description = m_object["description"]; ASSERT_TRUE(name.isString()); ASSERT_TRUE(description.isString()); ASSERT_EQ("simple", name.toString()); ASSERT_EQ("basic JSON file", description.toString()); - } catch (const JsonError &ex) { + } catch (const json::Error &ex) { FAIL() << ex.what(); } } @@ -591,7 +591,7 @@ ASSERT_TRUE(m_objectAll["object"].isObject()); ASSERT_TRUE(m_objectAll["array"].isArray()); - } catch (const JsonError &ex) { + } catch (const json::Error &ex) { FAIL() << ex.what(); } } @@ -602,14 +602,14 @@ class ArrayRead : public testing::Test { protected: - JsonArray m_array; - JsonArray m_arrayAll; + json::Array m_array; + json::Array m_arrayAll; public: ArrayRead() { - m_array = JsonDocument(std::ifstream("Json/array.json")).toArray(); - m_arrayAll = JsonDocument(std::ifstream("Json/array-all.json")).toArray(); + m_array = json::Document(std::ifstream("Json/array.json")).toArray(); + m_arrayAll = json::Document(std::ifstream("Json/array-all.json")).toArray(); } }; @@ -620,7 +620,7 @@ ASSERT_EQ(1, m_array[0].toInteger()); ASSERT_EQ(2, m_array[1].toInteger()); ASSERT_EQ(3, m_array[2].toInteger()); - } catch (const JsonError &ex) { + } catch (const json::Error &ex) { FAIL() << ex.what(); } } @@ -648,7 +648,7 @@ ASSERT_TRUE(m_arrayAll[5].isObject()); ASSERT_TRUE(m_arrayAll[6].isArray()); - } catch (const JsonError &ex) { + } catch (const json::Error &ex) { FAIL() << ex.what(); } } @@ -659,7 +659,7 @@ class ObjectIteratorsTest : public testing::Test { protected: - JsonObject m_object; + json::Object m_object; public: ObjectIteratorsTest() @@ -676,9 +676,9 @@ { std::set<std::string> expected{"boolean", "integer", "string"}; std::set<std::string> result; - std::unordered_map<std::string, JsonValue> values; - JsonObject::iterator it = m_object.begin(); - JsonObject::iterator end = m_object.end(); + std::unordered_map<std::string, json::Value> values; + json::Object::iterator it = m_object.begin(); + json::Object::iterator end = m_object.end(); while (it != end) { values.insert({it->first, it->second}); @@ -695,9 +695,9 @@ { std::set<std::string> expected{"boolean", "integer", "string"}; std::set<std::string> result; - std::unordered_map<std::string, JsonValue> values; - JsonObject::const_iterator it = m_object.cbegin(); - JsonObject::const_iterator end = m_object.cend(); + std::unordered_map<std::string, json::Value> values; + json::Object::const_iterator it = m_object.cbegin(); + json::Object::const_iterator end = m_object.cend(); while (it != end) { values.insert({it->first, it->second}); @@ -715,10 +715,10 @@ { // Assign (non const) { - JsonObject::iterator it = m_object.begin(); + json::Object::iterator it = m_object.begin(); std::string key = it->first; - it->second = JsonValue("CHANGED"); + it->second = json::Value("CHANGED"); ASSERT_EQ("CHANGED", m_object[key].toString()); ASSERT_EQ("CHANGED", it->second.toString()); @@ -730,11 +730,11 @@ { // Assign (const) { - JsonObject::const_iterator it = m_object.cbegin(); + json::Object::const_iterator it = m_object.cbegin(); std::string key = it->first; - JsonValue orig = it->second; + json::Value orig = it->second; - it->second = JsonValue("CHANGED"); + it->second = json::Value("CHANGED"); ASSERT_TRUE(m_object.contains(key)); ASSERT_EQ(orig, m_object[key]); @@ -748,7 +748,7 @@ class ArrayIteratorsTest : public testing::Test { protected: - JsonArray m_array; + json::Array m_array; public: ArrayIteratorsTest() @@ -763,19 +763,19 @@ { // Read only (non const) { - JsonArray::iterator it = m_array.begin(); + json::Array::iterator it = m_array.begin(); ASSERT_EQ(1, (*it).toInteger()); ASSERT_EQ(1, it->toInteger()); ASSERT_EQ("hello", it[1].toString()); ASSERT_TRUE(it[2].isTrue()); - JsonArray::iterator it2 = it + 1; + json::Array::iterator it2 = it + 1; ASSERT_EQ(1, it2[-1].toInteger()); ASSERT_EQ("hello", it2->toString()); ASSERT_TRUE(it2[1].isTrue()); - JsonArray::iterator it3 = it; + json::Array::iterator it3 = it; ASSERT_TRUE(it2 != it); ASSERT_FALSE(it3 != it); @@ -797,19 +797,19 @@ // Read only (const) { - JsonArray::const_iterator it = m_array.cbegin(); + json::Array::const_iterator it = m_array.cbegin(); ASSERT_EQ(1, (*it).toInteger()); ASSERT_EQ(1, it->toInteger()); ASSERT_EQ("hello", it[1].toString()); ASSERT_TRUE(it[2].isTrue()); - JsonArray::const_iterator it2 = it + 1; + json::Array::const_iterator it2 = it + 1; ASSERT_EQ(1, it2[-1].toInteger()); ASSERT_EQ("hello", it2->toString()); ASSERT_TRUE(it2[1].isTrue()); - JsonArray::const_iterator it3 = it; + json::Array::const_iterator it3 = it; ASSERT_TRUE(it2 != it); ASSERT_FALSE(it3 != it); @@ -834,9 +834,9 @@ { // Assign (non const) { - JsonArray::iterator it = m_array.begin(); + json::Array::iterator it = m_array.begin(); - *it = JsonValue(9999); + *it = json::Value(9999); ASSERT_EQ(3, static_cast<int>(m_array.size())); ASSERT_EQ(9999, it->toInteger()); @@ -848,9 +848,9 @@ { // Assign (const) { - JsonArray::const_iterator it = m_array.cbegin(); + json::Array::const_iterator it = m_array.cbegin(); - *it = JsonValue(9999); + *it = json::Value(9999); ASSERT_EQ(3, static_cast<int>(m_array.size())); ASSERT_EQ(1, it->toInteger()); @@ -860,10 +860,10 @@ TEST_F(ArrayIteratorsTest, castToRef) { - JsonArray array{1, 2, 3}; + json::Array array{1, 2, 3}; int i = 1; - for (const JsonValue &v : array) + for (const json::Value &v : array) { ASSERT_EQ(i++, v.toInteger()); }