Commit 76be1ae1 authored by Niels's avatar Niels

+ more adjustments toward a real container

parent 3c780f0f
...@@ -184,7 +184,7 @@ bool foo = j.at(2); ...@@ -184,7 +184,7 @@ bool foo = j.at(2);
// other stuff // other stuff
j.size(); // 3 entries j.size(); // 3 entries
j.empty(); // false j.empty(); // false
j.type(); // json::value_type::array j.type(); // json::value_t::array
j.clear(); // the array is empty again j.clear(); // the array is empty again
// comparison // comparison
......
This diff is collapsed.
This diff is collapsed.
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
@file @file
@copyright The code is licensed under the MIT License @copyright The code is licensed under the MIT License
<http://opensource.org/licenses/MIT>, <http://opensource.org/licenses/MIT>,
Copyright (c) 2013-2014 Niels Lohmann. Copyright (c) 2013-2015 Niels Lohmann.
@author Niels Lohmann <http://nlohmann.me> @author Niels Lohmann <http://nlohmann.me>
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
#include <vector> // std::vector #include <vector> // std::vector
#include <iterator> // std::iterator #include <iterator> // std::iterator
#include <limits> // std::numeric_limits #include <limits> // std::numeric_limits
#include <functional> // std::hash
namespace nlohmann namespace nlohmann
{ {
...@@ -41,30 +42,21 @@ due to alignment. ...@@ -41,30 +42,21 @@ due to alignment.
*/ */
class json class json
{ {
// forward declaration to friend this class
public: public:
// forward declaration to friend this class
class iterator; class iterator;
class const_iterator; class const_iterator;
public: // container types
/// possible types of a JSON object using value_type = json;
enum class value_type : uint8_t using reference = json&;
{ using const_reference = const json&;
/// ordered collection of values using pointer = json*;
array = 0, using const_pointer = const json*;
/// unordered set of name/value pairs using iterator = json::iterator;
object, using const_iterator = json::const_iterator;
/// null value using difference_type = std::ptrdiff_t;
null, using size_type = std::size_t;
/// string value
string,
/// Boolean value
boolean,
/// number value (integer)
number,
/// number value (float)
number_float
};
/// a type for an object /// a type for an object
using object_t = std::map<std::string, json>; using object_t = std::map<std::string, json>;
...@@ -113,9 +105,28 @@ class json ...@@ -113,9 +105,28 @@ class json
value(number_float_t); value(number_float_t);
}; };
/// possible types of a JSON object
enum class value_t : uint8_t
{
/// ordered collection of values
array = 0,
/// unordered set of name/value pairs
object,
/// null value
null,
/// string value
string,
/// Boolean value
boolean,
/// number value (integer)
number,
/// number value (float)
number_float
};
public: public:
/// create an object according to given type /// create an object according to given type
json(const value_type); json(const value_t);
/// create a null object /// create a null object
json() = default; json() = default;
/// create a null object /// create a null object
...@@ -140,15 +151,23 @@ class json ...@@ -140,15 +151,23 @@ class json
json(list_init_t); json(list_init_t);
/// create a number object (integer) /// create a number object (integer)
template<typename T, typename std::enable_if<std::numeric_limits<T>::is_integer, T>::type = 0> template<typename T, typename
std::enable_if<
std::numeric_limits<T>::is_integer, T>::type
= 0>
json(const T n) noexcept json(const T n) noexcept
: type_(value_type::number), value_(static_cast<number_t>(n)) : type_(value_t::number),
value_(static_cast<number_t>(n))
{} {}
/// create a number object (float) /// create a number object (float)
template<typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type> template<typename T, typename = typename
std::enable_if<
std::is_floating_point<T>::value>::type
>
json(const T n) noexcept json(const T n) noexcept
: type_(value_type::number_float), value_(static_cast<number_float_t>(n)) : type_(value_t::number_float),
value_(static_cast<number_float_t>(n))
{} {}
/// create an array object /// create an array object
...@@ -284,27 +303,29 @@ class json ...@@ -284,27 +303,29 @@ class json
/// operator to get an element in an array /// operator to get an element in an array
const json& operator[](const int) const; const json& operator[](const int) const;
/// operator to get an element in an array /// operator to get an element in an array
json& at(const int); reference at(const int);
/// operator to get an element in an array /// operator to get an element in an array
const json& at(const int) const; const_reference at(const int) const;
/// operator to set an element in an object /// operator to set an element in an object
json& operator[](const std::string&); reference operator[](const std::string&);
/// operator to set an element in an object /// operator to set an element in an object
json& operator[](const char*); reference operator[](const char*);
/// operator to get an element in an object /// operator to get an element in an object
const json& operator[](const std::string&) const; const_reference operator[](const std::string&) const;
/// operator to set an element in an object /// operator to set an element in an object
json& at(const std::string&); reference at(const std::string&);
/// operator to set an element in an object /// operator to set an element in an object
json& at(const char*); reference at(const char*);
/// operator to get an element in an object /// operator to get an element in an object
const json& at(const std::string&) const; const_reference at(const std::string&) const;
/// operator to get an element in an object /// operator to get an element in an object
const json& at(const char*) const; const_reference at(const char*) const;
/// return the number of stored values /// return the number of stored values
std::size_t size() const noexcept; size_type size() const noexcept;
/// return the maximal number of values that can be stored
size_type max_size() const noexcept;
/// checks whether object is empty /// checks whether object is empty
bool empty() const noexcept; bool empty() const noexcept;
/// removes all elements from compounds and resets values to default /// removes all elements from compounds and resets values to default
...@@ -314,7 +335,7 @@ class json ...@@ -314,7 +335,7 @@ class json
void swap(json&) noexcept; void swap(json&) noexcept;
/// return the type of the object /// return the type of the object
value_type type() const noexcept; value_t type() const noexcept;
/// find an element in an object (returns end() iterator otherwise) /// find an element in an object (returns end() iterator otherwise)
iterator find(const std::string&); iterator find(const std::string&);
...@@ -345,7 +366,7 @@ class json ...@@ -345,7 +366,7 @@ class json
private: private:
/// the type of this object /// the type of this object
value_type type_ = value_type::null; value_t type_ = value_t::null;
/// the payload /// the payload
value value_ {}; value value_ {};
...@@ -475,15 +496,29 @@ class json ...@@ -475,15 +496,29 @@ class json
/// user-defined literal operator to create JSON objects from strings /// user-defined literal operator to create JSON objects from strings
nlohmann::json operator "" _json(const char*, std::size_t); nlohmann::json operator "" _json(const char*, std::size_t);
// specialization of std::swap // specialization of std::swap, and std::hash
namespace std namespace std
{ {
template <> template <>
/// swaps the values of two JSON objects /// swaps the values of two JSON objects
inline void swap(nlohmann::json& j1, inline void swap(nlohmann::json& j1,
nlohmann::json& j2) noexcept(is_nothrow_move_constructible<nlohmann::json>::value and nlohmann::json& j2) noexcept(
is_nothrow_move_assignable<nlohmann::json>::value) is_nothrow_move_constructible<nlohmann::json>::value and
is_nothrow_move_assignable<nlohmann::json>::value
)
{ {
j1.swap(j2); j1.swap(j2);
} }
template <>
/// hash value for JSON objects
struct hash<nlohmann::json>
{
size_t operator()(const nlohmann::json& j) const
{
// a naive hashing via the string representation
return hash<std::string>()(j.dump());
}
};
} }
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment