Mercurial > code
diff C++/modules/Json/Json.cpp @ 475:b681299e6987
Json: removal of json::Object, json::Array, json::Document
author | David Demelier <markand@malikania.fr> |
---|---|
date | Mon, 09 Nov 2015 16:52:38 +0100 |
parents | 902b034df6e3 |
children | c7825b8c6145 |
line wrap: on
line diff
--- a/C++/modules/Json/Json.cpp Fri Nov 06 15:09:00 2015 +0100 +++ b/C++/modules/Json/Json.cpp Mon Nov 09 16:52:38 2015 +0100 @@ -45,14 +45,14 @@ return Value{json_boolean_value(v)}; } if (json_is_object(v)) { - Object object; + Value object{Type::Object}; readObject(object, v); return object; } if (json_is_array(v)) { - Array array; + Value array{Type::Array}; readArray(array, v); @@ -68,7 +68,7 @@ json_t *value; json_object_foreach(object, key, value) { - static_cast<Object &>(parent).insert(key, readValue(value)); + parent.insert(key, readValue(value)); } } @@ -78,7 +78,7 @@ json_t *value; json_array_foreach(array, index, value) { - static_cast<Array &>(parent).append(readValue(value)); + parent.append(readValue(value)); } } @@ -95,10 +95,10 @@ Value value; if (json_is_object(json)) { - value = Object{}; + value = Value{Type::Object}; readObject(value, json); } else { - value = Array{}; + value = Value{Type::Array}; readArray(value, json); } @@ -109,6 +109,109 @@ } // !namespace +void Value::copy(const Value &other) +{ + switch (other.m_type) { + case Type::String: + new (&m_string) std::string(); + m_string = std::move(other.m_string); + break; + case Type::Real: + m_number = other.m_number; + break; + case Type::Int: + m_integer = other.m_integer; + break; + case Type::Boolean: + m_boolean = other.m_boolean; + break; + case Type::Object: + new (&m_object) std::map<std::string, Value>(); + m_object = std::move(other.m_object); + break; + case Type::Array: + new (&m_array) std::deque<Value>(); + m_array = std::move(other.m_array); + break; + default: + break; + } + + m_type = other.m_type; +} + +void Value::move(Value &&other) +{ + switch (other.m_type) { + case Type::String: + new (&m_string) std::string(std::move(other.m_string)); + break; + case Type::Real: + m_number = other.m_number; + break; + case Type::Int: + m_integer = other.m_integer; + break; + case Type::Boolean: + m_boolean = other.m_boolean; + break; + case Type::Object: + new (&m_object) std::map<std::string, Value>(std::move(other.m_object)); + break; + case Type::Array: + new (&m_array) std::deque<Value>(std::move(other.m_array)); + break; + default: + break; + } + + m_type = other.m_type; +} + +Value::Value(Type type) + : m_type{type} +{ + switch (type) { + case Type::String: + new (&m_string) std::string(); + break; + case Type::Array: + new (&m_array) std::deque<Value>(); + break; + case Type::Object: + new (&m_object) std::map<std::string, Value>(); + break; + case Type::Real: + m_number = 0; + break; + case Type::Int: + m_integer = 0; + break; + case Type::Boolean: + m_boolean = false; + break; + default: + break; + } +} + +Value::~Value() +{ + switch (m_type) { + case Type::String: + m_string.~basic_string(); + break; + case Type::Object: + m_object.~map<std::string, Value>(); + break; + case Type::Array: + m_array.~deque<Value>(); + break; + default: + break; + } +} + bool Value::toBool() const noexcept { if (m_type != Type::Boolean) { @@ -145,32 +248,14 @@ return m_string; } -Object Value::toObject() const noexcept +Value::Value(const Buffer &buffer) { - if (m_type != Type::Object) { - return Object{}; - } - - return Object(*this); + *this = convert(json_loads, buffer.text.c_str(), 0); } -Array Value::toArray() const noexcept +Value::Value(const File &file) { - if (m_type != Type::Array) { - return Array{}; - } - - return Array(*this); -} - -Document::Document(Buffer buffer) -{ - m_value = convert(json_loads, buffer.text.c_str(), 0); -} - -Document::Document(File file) -{ - m_value = convert(json_load_file, file.path.c_str(), 0); + *this = convert(json_load_file, file.path.c_str(), 0); } std::string escape(const std::string &value)