json/src/json.h

443 lines
13 KiB
C
Raw Normal View History

2014-12-30 10:47:28 +00:00
/*!
@file
@copyright The code is licensed under the MIT License
<http://opensource.org/licenses/MIT>,
Copyright (c) 2013-2014 Niels Lohmann.
@author Niels Lohmann <http://nlohmann.me>
@see https://github.com/nlohmann/json
*/
2013-07-04 08:49:03 +00:00
#pragma once
2014-12-28 08:11:01 +00:00
#include <initializer_list> // std::initializer_list
#include <iostream> // std::istream, std::ostream
#include <map> // std::map
#include <string> // std::string
#include <vector> // std::vector
namespace nlohmann
{
2014-12-28 08:11:01 +00:00
/*!
2014-12-30 10:47:28 +00:00
@brief JSON for Modern C++
2014-12-28 08:11:01 +00:00
The size of a JSON object is 16 bytes: 8 bytes for the value union whose
largest item is a pointer type and another 8 byte for an element of the
type union. The latter only needs 1 byte - the remaining 7 bytes are wasted
due to alignment.
@see http://stackoverflow.com/questions/7758580/writing-your-own-stl-container/7759622#7759622
@bug Numbers are currently handled too generously. There are several formats
that are forbidden by the standard, but are accepted by the parser.
2015-01-04 19:43:25 +00:00
@todo Implement json::swap()
@todo Implement json::insert(), json::emplace(), json::emplace_back, json::erase
@todo Implement json::reverse_iterator, json::const_reverse_iterator,
json::rbegin(), json::rend(), json::crbegin(), json::crend()?
2014-12-28 08:11:01 +00:00
*/
2015-01-04 19:43:25 +00:00
class json
2014-12-28 08:11:01 +00:00
{
// forward declaration to friend this class
2014-12-28 08:11:01 +00:00
public:
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
};
/// a type for an object
2015-01-04 19:43:25 +00:00
using object_t = std::map<std::string, json>;
2014-12-28 08:11:01 +00:00
/// a type for an array
2015-01-04 19:43:25 +00:00
using array_t = std::vector<json>;
2014-12-28 08:11:01 +00:00
/// a type for a string
using string_t = std::string;
/// a type for a Boolean
using boolean_t = bool;
/// a type for an integer number
using number_t = int;
/// a type for a floating point number
using number_float_t = double;
/// a type for list initialization
2015-01-04 19:43:25 +00:00
using list_init_t = std::initializer_list<json>;
2014-12-28 08:11:01 +00:00
/// a JSON value
union value
{
/// array as pointer to array_t
array_t* array;
/// object as pointer to object_t
object_t* object;
/// string as pointer to string_t
string_t* string;
/// Boolean
boolean_t boolean;
/// number (integer)
number_t number;
/// number (float)
number_float_t number_float;
/// default constructor
value() = default;
/// constructor for arrays
value(array_t*);
/// constructor for objects
value(object_t*);
/// constructor for strings
value(string_t*);
/// constructor for Booleans
value(boolean_t);
/// constructor for numbers (integer)
value(number_t);
/// constructor for numbers (float)
value(number_float_t);
};
public:
/// create an object according to given type
json(const value_type);
2014-12-28 08:11:01 +00:00
/// create a null object
2015-01-04 19:43:25 +00:00
json() = default;
2014-12-28 08:11:01 +00:00
/// create a null object
2015-01-04 19:43:25 +00:00
json(std::nullptr_t) noexcept;
2014-12-28 08:11:01 +00:00
/// create a string object from a C++ string
json(const std::string&);
2014-12-28 08:11:01 +00:00
/// create a string object from a C++ string (move)
json(std::string&&);
2014-12-28 08:11:01 +00:00
/// create a string object from a C string
json(const char*);
2014-12-28 08:11:01 +00:00
/// create a Boolean object
2015-01-04 19:43:25 +00:00
json(const bool) noexcept;
2014-12-28 08:11:01 +00:00
/// create a number object
2015-01-04 19:43:25 +00:00
json(const int) noexcept;
2014-12-28 08:11:01 +00:00
/// create a number object
2015-01-04 19:43:25 +00:00
json(const double) noexcept;
2014-12-28 08:11:01 +00:00
/// create an array
json(const array_t&);
2014-12-28 08:11:01 +00:00
/// create an array (move)
json(array_t&&);
2014-12-28 08:11:01 +00:00
/// create an object
json(const object_t&);
2014-12-28 08:11:01 +00:00
/// create an object (move)
json(object_t&&);
2014-12-28 08:11:01 +00:00
/// create from an initializer list (to an array or object)
json(list_init_t);
2014-12-28 08:11:01 +00:00
/// copy constructor
json(const json&);
2014-12-28 08:11:01 +00:00
/// move constructor
2015-01-04 19:43:25 +00:00
json(json&&) noexcept;
2014-12-28 08:11:01 +00:00
/// copy assignment
2015-01-04 19:43:25 +00:00
json& operator=(json) noexcept;
2014-12-28 08:11:01 +00:00
/// destructor
2015-01-04 19:43:25 +00:00
~json() noexcept;
2014-12-28 08:11:01 +00:00
/// create from string representation
2015-01-04 19:43:25 +00:00
static json parse(const std::string&);
2014-12-28 08:11:01 +00:00
/// create from string representation
2015-01-04 19:43:25 +00:00
static json parse(const char*);
2014-12-28 08:11:01 +00:00
private:
/// return the type as string
2015-01-04 22:09:30 +00:00
const std::string type_name() const noexcept;
2014-12-28 08:11:01 +00:00
/// dump the object (with pretty printer)
const std::string dump(const bool, const unsigned int, unsigned int = 0) const noexcept;
2014-12-28 08:11:01 +00:00
public:
/// explicit value conversion
template<typename T>
T get() const;
/// implicit conversion to string representation
operator const std::string() const;
/// implicit conversion to integer (only for numbers)
operator int() const;
/// implicit conversion to double (only for numbers)
operator double() const;
/// implicit conversion to Boolean (only for Booleans)
operator bool() const;
/// implicit conversion to JSON vector (not for objects)
operator array_t() const;
/// implicit conversion to JSON map (only for objects)
operator object_t() const;
/// serialize to stream
2015-01-04 19:43:25 +00:00
friend std::ostream& operator<<(std::ostream& o, const json& j)
2014-12-28 08:11:01 +00:00
{
o << j.dump();
2014-12-28 08:11:01 +00:00
return o;
}
/// serialize to stream
2015-01-04 19:43:25 +00:00
friend std::ostream& operator>>(const json& j, std::ostream& o)
2014-12-28 08:11:01 +00:00
{
o << j.dump();
2014-12-28 08:11:01 +00:00
return o;
}
/// deserialize from stream
2015-01-04 19:43:25 +00:00
friend std::istream& operator>>(std::istream& i, json& j)
2014-12-28 08:11:01 +00:00
{
2015-01-04 19:43:25 +00:00
j = parser(i).parse();
2014-12-28 08:11:01 +00:00
return i;
}
/// deserialize from stream
2015-01-04 19:43:25 +00:00
friend std::istream& operator<<(json& j, std::istream& i)
2014-12-28 08:11:01 +00:00
{
2015-01-04 19:43:25 +00:00
j = parser(i).parse();
2014-12-28 08:11:01 +00:00
return i;
}
/// explicit serialization
const std::string dump(int = -1) const noexcept;
2014-12-28 08:11:01 +00:00
/// add an object/array to an array
2015-01-04 19:43:25 +00:00
json& operator+=(const json&);
2014-12-28 08:11:01 +00:00
/// add a string to an array
2015-01-04 19:43:25 +00:00
json& operator+=(const std::string&);
2014-12-28 08:11:01 +00:00
/// add a null object to an array
2015-01-04 19:43:25 +00:00
json& operator+=(const std::nullptr_t);
2014-12-28 08:11:01 +00:00
/// add a string to an array
2015-01-04 19:43:25 +00:00
json& operator+=(const char*);
2014-12-28 08:11:01 +00:00
/// add a Boolean to an array
2015-01-04 19:43:25 +00:00
json& operator+=(bool);
2014-12-28 08:11:01 +00:00
/// add a number to an array
2015-01-04 19:43:25 +00:00
json& operator+=(int);
2014-12-28 08:11:01 +00:00
/// add a number to an array
2015-01-04 19:43:25 +00:00
json& operator+=(double);
2014-12-28 08:11:01 +00:00
/// add a pair to an object
2015-01-04 19:43:25 +00:00
json& operator+=(const object_t::value_type&);
2014-12-28 08:11:01 +00:00
/// add a list of elements to array or list of pairs to object
2015-01-04 19:43:25 +00:00
json& operator+=(list_init_t);
2014-12-28 08:11:01 +00:00
/// add an object/array to an array
2015-01-04 19:43:25 +00:00
void push_back(const json&);
2014-12-28 08:11:01 +00:00
/// add an object/array to an array (move)
2015-01-04 19:43:25 +00:00
void push_back(json&&);
2014-12-28 08:11:01 +00:00
/// add a string to an array
void push_back(const std::string&);
/// add a null object to an array
void push_back(const std::nullptr_t);
/// add a string to an array
void push_back(const char*);
/// add a Boolean to an array
void push_back(bool);
/// add a number to an array
void push_back(int);
/// add a number to an array
void push_back(double);
/// add a pair to an object
void push_back(const object_t::value_type&);
/// add a list of elements to array or list of pairs to object
void push_back(list_init_t);
/// operator to set an element in an array
2015-01-04 19:43:25 +00:00
json& operator[](const int);
2014-12-28 08:11:01 +00:00
/// operator to get an element in an array
2015-01-04 19:43:25 +00:00
const json& operator[](const int) const;
2014-12-28 08:11:01 +00:00
/// operator to get an element in an array
2015-01-04 19:43:25 +00:00
json& at(const int);
2014-12-28 08:11:01 +00:00
/// operator to get an element in an array
2015-01-04 19:43:25 +00:00
const json& at(const int) const;
2014-12-28 08:11:01 +00:00
/// operator to set an element in an object
2015-01-04 19:43:25 +00:00
json& operator[](const std::string&);
2014-12-28 08:11:01 +00:00
/// operator to set an element in an object
2015-01-04 19:43:25 +00:00
json& operator[](const char*);
2014-12-28 08:11:01 +00:00
/// operator to get an element in an object
2015-01-04 19:43:25 +00:00
const json& operator[](const std::string&) const;
2014-12-28 08:11:01 +00:00
/// operator to set an element in an object
2015-01-04 19:43:25 +00:00
json& at(const std::string&);
2014-12-28 08:11:01 +00:00
/// operator to set an element in an object
2015-01-04 19:43:25 +00:00
json& at(const char*);
2014-12-28 08:11:01 +00:00
/// operator to get an element in an object
2015-01-04 19:43:25 +00:00
const json& at(const std::string&) const;
2014-12-28 13:57:21 +00:00
/// operator to get an element in an object
2015-01-04 19:43:25 +00:00
const json& at(const char*) const;
2014-12-28 08:11:01 +00:00
/// return the number of stored values
std::size_t size() const noexcept;
2014-12-28 08:11:01 +00:00
/// checks whether object is empty
bool empty() const noexcept;
/// removes all elements from compounds and resets values to default
void clear() noexcept;
/// return the type of the object
value_type type() const noexcept;
/// find an element in an object (returns end() iterator otherwise)
iterator find(const std::string&);
/// find an element in an object (returns end() iterator otherwise)
const_iterator find(const std::string&) const;
/// find an element in an object (returns end() iterator otherwise)
iterator find(const char*);
/// find an element in an object (returns end() iterator otherwise)
const_iterator find(const char*) const;
/// lexicographically compares the values
2015-01-04 19:43:25 +00:00
bool operator==(const json&) const noexcept;
2014-12-28 08:11:01 +00:00
/// lexicographically compares the values
2015-01-04 19:43:25 +00:00
bool operator!=(const json&) const noexcept;
2014-12-28 08:11:01 +00:00
/// returns an iterator to the beginning (array/object)
iterator begin() noexcept;
/// returns an iterator to the end (array/object)
iterator end() noexcept;
/// returns an iterator to the beginning (array/object)
const_iterator begin() const noexcept;
/// returns an iterator to the end (array/object)
const_iterator end() const noexcept;
/// returns an iterator to the beginning (array/object)
const_iterator cbegin() const noexcept;
/// returns an iterator to the end (array/object)
const_iterator cend() const noexcept;
private:
/// the type of this object
2015-01-04 22:09:30 +00:00
value_type type_ = value_type::null;
2014-12-28 08:11:01 +00:00
/// the payload
2015-01-04 22:09:30 +00:00
value value_ {};
2014-12-28 08:11:01 +00:00
public:
/// an iterator
class iterator
{
2015-01-04 19:43:25 +00:00
friend class json;
friend class json::const_iterator;
2014-12-28 08:11:01 +00:00
public:
iterator() = default;
2015-01-04 19:43:25 +00:00
iterator(json*);
2014-12-28 08:11:01 +00:00
iterator(const iterator&);
~iterator();
iterator& operator=(iterator);
bool operator==(const iterator&) const;
bool operator!=(const iterator&) const;
iterator& operator++();
2015-01-04 19:43:25 +00:00
json& operator*() const;
json* operator->() const;
2014-12-28 08:11:01 +00:00
/// getter for the key (in case of objects)
std::string key() const;
/// getter for the value
2015-01-04 19:43:25 +00:00
json& value() const;
2014-12-28 08:11:01 +00:00
private:
/// a JSON value
2015-01-04 22:09:30 +00:00
json* object_ = nullptr;
2014-12-28 08:11:01 +00:00
/// an iterator for JSON arrays
2015-01-04 22:09:30 +00:00
array_t::iterator* vi_ = nullptr;
2014-12-28 08:11:01 +00:00
/// an iterator for JSON objects
2015-01-04 22:09:30 +00:00
object_t::iterator* oi_ = nullptr;
2014-12-28 08:11:01 +00:00
};
/// a const iterator
class const_iterator
{
2015-01-04 19:43:25 +00:00
friend class json;
2014-12-28 08:11:01 +00:00
public:
const_iterator() = default;
2015-01-04 19:43:25 +00:00
const_iterator(const json*);
2014-12-28 08:11:01 +00:00
const_iterator(const const_iterator&);
const_iterator(const iterator&);
~const_iterator();
const_iterator& operator=(const_iterator);
bool operator==(const const_iterator&) const;
bool operator!=(const const_iterator&) const;
const_iterator& operator++();
2015-01-04 19:43:25 +00:00
const json& operator*() const;
const json* operator->() const;
2014-12-28 08:11:01 +00:00
/// getter for the key (in case of objects)
std::string key() const;
/// getter for the value
2015-01-04 19:43:25 +00:00
const json& value() const;
2014-12-28 08:11:01 +00:00
private:
2013-07-10 12:33:17 +00:00
/// a JSON value
2015-01-04 22:09:30 +00:00
const json* object_ = nullptr;
2014-12-28 08:11:01 +00:00
/// an iterator for JSON arrays
2015-01-04 22:09:30 +00:00
array_t::const_iterator* vi_ = nullptr;
2014-12-28 08:11:01 +00:00
/// an iterator for JSON objects
2015-01-04 22:09:30 +00:00
object_t::const_iterator* oi_ = nullptr;
2014-12-28 08:11:01 +00:00
};
private:
/// a helper class to parse a JSON object
2015-01-04 19:43:25 +00:00
class parser
2014-12-28 08:11:01 +00:00
{
public:
/// a parser reading from a C string
2015-01-04 19:43:25 +00:00
parser(const char*);
2014-12-28 08:11:01 +00:00
/// a parser reading from a C++ string
2015-01-04 19:43:25 +00:00
parser(const std::string&);
2014-12-28 08:11:01 +00:00
/// a parser reading from an input stream
2015-01-04 19:43:25 +00:00
parser(std::istream&);
2014-12-28 08:11:01 +00:00
/// destructor of the parser
2015-01-04 19:43:25 +00:00
~parser() = default;
2014-12-28 08:11:01 +00:00
// no copy constructor
2015-01-04 19:43:25 +00:00
parser(const parser&) = delete;
2014-12-28 08:11:01 +00:00
// no copy assignment
2015-01-04 19:43:25 +00:00
parser& operator=(parser) = delete;
2014-12-28 08:11:01 +00:00
2015-01-02 16:19:40 +00:00
/// parse and return a JSON object
2015-01-04 19:43:25 +00:00
json parse();
2014-12-28 08:11:01 +00:00
private:
/// read the next character, stripping whitespace
bool next();
/// raise an exception with an error message
2015-01-02 14:29:36 +00:00
inline void error(const std::string&) __attribute__((noreturn));
2014-12-28 08:11:01 +00:00
/// parse a quoted string
2015-01-03 16:18:17 +00:00
inline std::string parseString();
2014-12-28 08:11:01 +00:00
/// parse a Boolean "true"
2015-01-02 14:29:36 +00:00
inline void parseTrue();
2014-12-28 08:11:01 +00:00
/// parse a Boolean "false"
2015-01-02 14:29:36 +00:00
inline void parseFalse();
2014-12-28 08:11:01 +00:00
/// parse a null object
2015-01-02 14:29:36 +00:00
inline void parseNull();
2014-12-28 08:11:01 +00:00
/// a helper function to expect a certain character
2015-01-03 16:18:17 +00:00
inline void expect(const char);
2014-12-28 08:11:01 +00:00
private:
/// a buffer of the input
2015-01-04 22:09:30 +00:00
std::string buffer_ {};
2014-12-28 08:11:01 +00:00
/// the current character
2015-01-04 22:09:30 +00:00
char current_ {};
2014-12-28 08:11:01 +00:00
/// the position inside the input buffer
std::size_t pos_ = 0;
2014-12-28 08:11:01 +00:00
};
2013-07-04 08:49:03 +00:00
};
2014-12-28 08:11:01 +00:00
}
2014-12-28 08:11:01 +00:00
/// user-defined literal operator to create JSON objects from strings
nlohmann::json operator "" _json(const char*, std::size_t);