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);
// other stuff
j.size(); // 3 entries
j.empty(); // false
j.type(); // json::value_type::array
j.type(); // json::value_t::array
j.clear(); // the array is empty again
// comparison
......
This diff is collapsed.
This diff is collapsed.
......@@ -2,7 +2,7 @@
@file
@copyright The code is licensed under the MIT License
<http://opensource.org/licenses/MIT>,
Copyright (c) 2013-2014 Niels Lohmann.
Copyright (c) 2013-2015 Niels Lohmann.
@author Niels Lohmann <http://nlohmann.me>
......@@ -18,6 +18,7 @@
#include <vector> // std::vector
#include <iterator> // std::iterator
#include <limits> // std::numeric_limits
#include <functional> // std::hash
namespace nlohmann
{
......@@ -41,30 +42,21 @@ due to alignment.
*/
class json
{
// forward declaration to friend this class
public:
// forward declaration to friend this class
class iterator;
class const_iterator;
public:
/// possible types of a JSON object
enum class value_type : 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
};
// container types
using value_type = json;
using reference = json&;
using const_reference = const json&;
using pointer = json*;
using const_pointer = const json*;
using iterator = json::iterator;
using const_iterator = json::const_iterator;
using difference_type = std::ptrdiff_t;
using size_type = std::size_t;
/// a type for an object
using object_t = std::map<std::string, json>;
......@@ -113,9 +105,28 @@ class json
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:
/// create an object according to given type
json(const value_type);
json(const value_t);
/// create a null object
json() = default;
/// create a null object
......@@ -140,15 +151,23 @@ class json
json(list_init_t);
/// 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
: 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)
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
: 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
......@@ -284,27 +303,29 @@ class json
/// operator to get an element in an array
const json& operator[](const int) const;
/// operator to get an element in an array
json& at(const int);
reference at(const int);
/// 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
json& operator[](const std::string&);
reference operator[](const std::string&);
/// operator to set an element in an object
json& operator[](const char*);
reference operator[](const char*);
/// 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
json& at(const std::string&);
reference at(const std::string&);
/// operator to set an element in an object
json& at(const char*);
reference at(const char*);
/// 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
const json& at(const char*) const;
const_reference at(const char*) const;
/// 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
bool empty() const noexcept;
/// removes all elements from compounds and resets values to default
......@@ -314,7 +335,7 @@ class json
void swap(json&) noexcept;
/// 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)
iterator find(const std::string&);
......@@ -345,7 +366,7 @@ class json
private:
/// the type of this object
value_type type_ = value_type::null;
value_t type_ = value_t::null;
/// the payload
value value_ {};
......@@ -475,15 +496,29 @@ class json
/// user-defined literal operator to create JSON objects from strings
nlohmann::json operator "" _json(const char*, std::size_t);
// specialization of std::swap
// specialization of std::swap, and std::hash
namespace std
{
template <>
/// swaps the values of two JSON objects
inline void swap(nlohmann::json& j1,
nlohmann::json& j2) noexcept(is_nothrow_move_constructible<nlohmann::json>::value and
is_nothrow_move_assignable<nlohmann::json>::value)
nlohmann::json& j2) noexcept(
is_nothrow_move_constructible<nlohmann::json>::value and
is_nothrow_move_assignable<nlohmann::json>::value
)
{
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