json/src/JSON.cc
2013-07-05 16:15:29 +02:00

1251 lines
30 KiB
C++

#include "JSON.h"
#include <utility>
#include <stdexcept>
#include <fstream>
#include <cctype>
#include <iostream>
#include <streambuf>
#include <sstream>
#include <cstring>
#include <cstdlib>
#ifdef __cplusplus11
using std::to_string;
#else
inline std::string to_string(double f) {
std::stringstream s;
s << f;
return s.str();
}
#endif
/******************
* STATIC MEMBERS *
******************/
#ifdef __cplusplus11
/// a mutex to ensure thread safety
std::mutex JSON::_token;
#endif
/*******************************
* CONSTRUCTORS AND DESTRUCTOR *
*******************************/
JSON::JSON() : _type(null), _payload(nullptr) {}
JSON::JSON(json_t type) : _type(type) {
switch (_type) {
case (array): {
_payload = new array_t();
break;
}
case (object): {
_payload = new object_t();
break;
}
case (string): {
_payload = new std::string();
break;
}
case (boolean): {
_payload = new bool();
break;
}
case (number): {
_payload = new int(0);
break;
}
case (number_float): {
_payload = new double(0.0);
break;
}
case (null): {
break;
}
}
}
JSON::JSON(const std::string& s) : _type(string), _payload(new std::string(s)) {}
JSON::JSON(const char* s) : _type(string), _payload(new std::string(s)) {}
JSON::JSON(char* s) : _type(string), _payload(new std::string(s)) {}
JSON::JSON(const bool b) : _type(boolean), _payload(new bool(b)) {}
JSON::JSON(const int i) : _type(number), _payload(new int(i)) {}
JSON::JSON(const double f) : _type(number_float), _payload(new double(f)) {}
JSON::JSON(array_t a) : _type(array), _payload(new array_t(a)) {}
JSON::JSON(object_t o) : _type(object), _payload(new object_t(o)) {}
#ifdef __cplusplus11
JSON::JSON(array_init_t a) : _type(array), _payload(new array_t(a)) {}
#endif
/// copy constructor
JSON::JSON(const JSON& o) : _type(o._type) {
switch (_type) {
case (array): {
_payload = new array_t(*static_cast<array_t*>(o._payload));
break;
}
case (object): {
_payload = new object_t(*static_cast<object_t*>(o._payload));
break;
}
case (string): {
_payload = new std::string(*static_cast<std::string*>(o._payload));
break;
}
case (boolean): {
_payload = new bool(*static_cast<bool*>(o._payload));
break;
}
case (number): {
_payload = new int(*static_cast<int*>(o._payload));
break;
}
case (number_float): {
_payload = new double(*static_cast<double*>(o._payload));
break;
}
case (null): {
break;
}
}
}
#ifdef __cplusplus11
/// move constructor
JSON::JSON(JSON&& o) : _type(std::move(o._type)), _payload(std::move(o._payload)) {}
#endif
/// copy assignment
#ifdef __cplusplus11
JSON& JSON::operator=(JSON o) {
std::swap(_type, o._type);
std::swap(_payload, o._payload);
return *this;
}
#else
JSON& JSON::operator=(const JSON& o) {
// check for self-assignment
if (&o == this) {
return *this;
}
switch (_type) {
case (array): {
delete static_cast<array_t*>(_payload);
break;
}
case (object): {
delete static_cast<object_t*>(_payload);
break;
}
case (string): {
delete static_cast<std::string*>(_payload);
break;
}
case (boolean): {
delete static_cast<bool*>(_payload);
break;
}
case (number): {
delete static_cast<int*>(_payload);
break;
}
case (number_float): {
delete static_cast<double*>(_payload);
break;
}
case (null): {
break;
}
}
_type = o._type;
switch (_type) {
case (array): {
_payload = new array_t(*static_cast<array_t*>(o._payload));
break;
}
case (object): {
_payload = new object_t(*static_cast<object_t*>(o._payload));
break;
}
case (string): {
_payload = new std::string(*static_cast<std::string*>(o._payload));
break;
}
case (boolean): {
_payload = new bool(*static_cast<bool*>(o._payload));
break;
}
case (number): {
_payload = new int(*static_cast<int*>(o._payload));
break;
}
case (number_float): {
_payload = new double(*static_cast<double*>(o._payload));
break;
}
case (null): {
break;
}
}
return *this;
}
#endif
/// destructor
JSON::~JSON() {
switch (_type) {
case (array): {
delete static_cast<array_t*>(_payload);
break;
}
case (object): {
delete static_cast<object_t*>(_payload);
break;
}
case (string): {
delete static_cast<std::string*>(_payload);
break;
}
case (boolean): {
delete static_cast<bool*>(_payload);
break;
}
case (number): {
delete static_cast<int*>(_payload);
break;
}
case (number_float): {
delete static_cast<double*>(_payload);
break;
}
case (null): {
break;
}
}
}
/*****************************
* OPERATORS AND CONVERSIONS *
*****************************/
JSON::operator const std::string() const {
switch (_type) {
case (string):
return *static_cast<std::string*>(_payload);
default:
throw std::runtime_error("cannot cast " + _typename() + " to JSON string");
}
}
JSON::operator int() const {
switch (_type) {
case (number):
return *static_cast<int*>(_payload);
case (number_float):
return static_cast<int>(*static_cast<double*>(_payload));
default:
throw std::runtime_error("cannot cast " + _typename() + " to JSON number");
}
}
JSON::operator double() const {
switch (_type) {
case (number):
return static_cast<double>(*static_cast<int*>(_payload));
case (number_float):
return *static_cast<double*>(_payload);
default:
throw std::runtime_error("cannot cast " + _typename() + " to JSON number");
}
}
JSON::operator bool() const {
switch (_type) {
case (boolean):
return *static_cast<bool*>(_payload);
default:
throw std::runtime_error("cannot cast " + _typename() + " to JSON Boolean");
}
}
const std::string JSON::toString() const {
switch (_type) {
case (null): {
return "null";
}
case (string): {
return std::string("\"") + *static_cast<std::string*>(_payload) + "\"";
}
case (boolean): {
return *static_cast<bool*>(_payload) ? "true" : "false";
}
case (number): {
return to_string(*static_cast<int*>(_payload));
}
case (number_float): {
return to_string(*static_cast<double*>(_payload));
}
case (array): {
std::string result;
const array_t* array = static_cast<array_t*>(_payload);
for (array_t::const_iterator i = array->begin(); i != array->end(); ++i) {
if (i != array->begin()) {
result += ", ";
}
result += (*i).toString();
}
return "[" + result + "]";
}
case (object): {
std::string result;
const object_t* object = static_cast<object_t*>(_payload);
for (object_t::const_iterator i = object->begin(); i != object->end(); ++i) {
if (i != object->begin()) {
result += ", ";
}
result += "\"" + i->first + "\": " + (i->second).toString();
}
return "{" + result + "}";
}
}
}
/*****************************************
* ADDING ELEMENTS TO OBJECTS AND ARRAYS *
*****************************************/
JSON& JSON::operator+=(const JSON& o) {
push_back(o);
return *this;
}
JSON& JSON::operator+=(const std::string& s) {
push_back(JSON(s));
return *this;
}
JSON& JSON::operator+=(const char* s) {
push_back(JSON(s));
return *this;
}
JSON& JSON::operator+=(bool b) {
push_back(JSON(b));
return *this;
}
JSON& JSON::operator+=(int i) {
push_back(JSON(i));
return *this;
}
JSON& JSON::operator+=(double f) {
push_back(JSON(f));
return *this;
}
void JSON::push_back(const JSON& o) {
#ifdef __cplusplus11
std::lock_guard<std::mutex> lg(_token);
#endif
if (not(_type == null or _type == array)) {
throw std::runtime_error("cannot add element to " + _typename());
}
if (_type == null) {
_type = array;
_payload = new array_t;
}
static_cast<array_t*>(_payload)->push_back(o);
}
void JSON::push_back(const std::string& s) {
push_back(JSON(s));
}
void JSON::push_back(const char* s) {
push_back(JSON(s));
}
void JSON::push_back(bool b) {
push_back(JSON(b));
}
void JSON::push_back(int i) {
push_back(JSON(i));
}
void JSON::push_back(double f) {
push_back(JSON(f));
}
/// operator to set an element in an object
JSON& JSON::operator[](int index) {
#ifdef __cplusplus11
std::lock_guard<std::mutex> lg(_token);
#endif
if (_type != array) {
throw std::runtime_error("cannot add entry with index " + to_string(index) + " to " + _typename());
}
array_t* array = static_cast<array_t*>(_payload);
if (index >= array->size()) {
throw std::runtime_error("cannot access element at index " + to_string(index));
}
return array->at(index);
}
/// operator to get an element in an object
const JSON& JSON::operator[](const int index) const {
if (_type != array) {
throw std::runtime_error("cannot get entry with index " + to_string(index) + " from " + _typename());
}
array_t* array = static_cast<array_t*>(_payload);
if (index >= array->size()) {
throw std::runtime_error("cannot access element at index " + to_string(index));
}
return array->at(index);
}
/// operator to set an element in an object
JSON& JSON::operator[](const std::string& key) {
#ifdef __cplusplus11
std::lock_guard<std::mutex> lg(_token);
#endif
if (_type == null) {
_type = object;
_payload = new object_t;
}
if (_type != object) {
throw std::runtime_error("cannot add entry with key " + std::string(key) + " to " + _typename());
}
object_t* object = static_cast<object_t*>(_payload);
if (object->find(key) == object->end()) {
(*object)[key] = JSON();
}
return (*object)[key];
}
/// operator to set an element in an object
JSON& JSON::operator[](const char* key) {
#ifdef __cplusplus11
std::lock_guard<std::mutex> lg(_token);
#endif
if (_type == null) {
_type = object;
_payload = new object_t;
}
if (_type != object) {
throw std::runtime_error("cannot add entry with key " + std::string(key) + " to " + _typename());
}
object_t* object = static_cast<object_t*>(_payload);
if (object->find(key) == object->end()) {
(*object)[key] = JSON();
}
return (*object)[key];
}
/// operator to get an element in an object
const JSON& JSON::operator[](const std::string& key) const {
if (_type != object) {
throw std::runtime_error("cannot get entry with key " + std::string(key) + " from " + _typename());
}
const object_t* object = static_cast<object_t*>(_payload);
if (object->find(key) == object->end()) {
throw std::runtime_error("key " + key + " not found");
} else {
return object->find(key)->second;
}
}
/// return the number of stored values
size_t JSON::size() const {
switch (_type) {
case (array):
return static_cast<array_t*>(_payload)->size();
case (object):
return static_cast<object_t*>(_payload)->size();
case (null):
return 0;
case (string):
return 1;
case (boolean):
return 1;
case (number):
return 1;
case (number_float):
return 1;
}
}
/// checks whether object is empty
bool JSON::empty() const {
switch (_type) {
case (array):
return static_cast<array_t*>(_payload)->empty();
case (object):
return static_cast<object_t*>(_payload)->empty();
case (null):
return true;
case (string):
return false;
case (boolean):
return false;
case (number):
return false;
case (number_float):
return false;
}
}
/// return the type of the object
JSON::json_t JSON::type() const {
return _type;
}
JSON::iterator JSON::find(const std::string& key) {
return find(key.c_str());
}
JSON::const_iterator JSON::find(const std::string& key) const {
return find(key.c_str());
}
JSON::iterator JSON::find(const char* key) {
if (_type != object) {
return end();
} else {
object_t* o = static_cast<object_t*>(_payload);
const object_t::iterator i = o->find(key);
if (i != o->end()) {
JSON::iterator result;
result._object = this;
result._oi = new object_t::iterator(i);
return result;
} else {
return end();
}
}
}
JSON::const_iterator JSON::find(const char* key) const {
if (_type != object) {
return end();
} else {
object_t* o = static_cast<object_t*>(_payload);
const object_t::const_iterator i = o->find(key);
if (i != o->end()) {
JSON::const_iterator result;
result._object = this;
result._oi = new object_t::const_iterator(i);
return result;
} else {
return end();
}
}
}
/// direct access to the underlying payload
void* JSON::data() {
return _payload;
}
/// direct access to the underlying payload
const void* JSON::data() const {
return _payload;
}
/// lexicographically compares the values
bool JSON::operator==(const JSON& o) const {
switch (_type) {
case (array): {
if (o._type == array) {
array_t* a = static_cast<array_t*>(_payload);
array_t* b = static_cast<array_t*>(o._payload);
return *a == *b;
}
}
case (object): {
if (o._type == object) {
object_t* a = static_cast<object_t*>(_payload);
object_t* b = static_cast<object_t*>(o._payload);
return *a == *b;
}
}
case (null): {
if (o._type == null) {
return true;
}
}
case (string): {
if (o._type == string) {
const std::string a = *this;
const std::string b = o;
return a == b;
}
}
case (boolean): {
if (o._type == boolean) {
bool a = *this;
bool b = o;
return a == b;
}
}
case (number): {
if (o._type == number or o._type == number_float) {
int a = *this;
int b = o;
return a == b;
}
}
case (number_float): {
if (o._type == number or o._type == number_float) {
double a = *this;
double b = o;
return a == b;
}
}
}
return false;
}
/// return the type as string
std::string JSON::_typename() const {
switch (_type) {
case (array):
return "array";
case (object):
return "object";
case (null):
return "null";
case (string):
return "string";
case (boolean):
return "boolean";
case (number):
return "number";
case (number_float):
return "number";
}
}
JSON::parser::parser(char* s) : _pos(0) {
_length = std::strlen(s);
_buffer = new char[_length + 1];
std::strcpy(_buffer, s);
// read first character
next();
}
JSON::parser::parser(std::string& s) : _pos(0) {
_length = s.length();
_buffer = new char[_length + 1];
std::strcpy(_buffer, s.c_str());
// read first character
next();
}
JSON::parser::parser(std::istream& _is) : _pos(0) {
// determine length of input stream
_is.seekg(0, std::ios::end);
_length = _is.tellg();
_is.seekg(0, std::ios::beg);
// copy stream to buffer
_buffer = new char[_length + 1];
_is.read(_buffer, _length);
// read first character
next();
}
JSON::parser::~parser() {
delete [] _buffer;
}
void JSON::parser::error(std::string msg) {
throw std::runtime_error("parse error at position " + to_string(_pos) + ": " + msg + ", last read: '" + _current + "'");
}
bool JSON::parser::next() {
if (_pos == _length) {
return false;
}
_current = _buffer[_pos++];
// skip trailing whitespace
while (std::isspace(_current)) {
if (_pos == _length) {
return false;
}
_current = _buffer[_pos++];
}
return true;
}
std::string JSON::parser::parseString() {
// get position of closing quotes
char* p = std::strchr(_buffer + _pos, '\"');
// if the closing quotes are escaped (viz. *(p-1) is '\\'),
// we continue looking for the "right" quotes
while (p != nullptr and * (p - 1) == '\\') {
// length of the string so far
const size_t length = p - _buffer - _pos;
// continue checking after escaped quote
p = std::strchr(_buffer + _pos + length + 1, '\"');
}
// check if closing quotes were found
if (p == nullptr) {
error("expected '\"'");
}
// copy string to return value
const size_t length = p - _buffer - _pos;
char* tmp = new char[length + 1];
std::strncpy(tmp, _buffer + _pos, length);
tmp[length] = 0;
std::string result(tmp);
delete [] tmp;
// +1 to eat closing quote
_pos += length + 1;
// read next character
next();
return result;
}
void JSON::parser::parseTrue() {
if (std::strncmp(_buffer + _pos, "rue", 3)) {
error("expected true");
}
_pos += 3;
// read next character
next();
}
void JSON::parser::parseFalse() {
if (std::strncmp(_buffer + _pos, "alse", 4)) {
error("expected false");
}
_pos += 4;
// read next character
next();
}
void JSON::parser::parseNull() {
if (std::strncmp(_buffer + _pos, "ull", 3)) {
error("expected null");
}
_pos += 3;
// read next character (optional?)
next();
}
void JSON::parser::expect(char c) {
if (_current != c) {
std::string msg = "expected '";
msg.append(1, c);
msg += "'";
error(msg.c_str());
} else {
next();
}
}
void JSON::parser::parse(JSON& result) {
if (!_buffer) {
error("unexpected end of file");
}
switch (_current) {
case ('{'): {
// explicitly set result to object to cope with {}
result._type = object;
result._payload = new object_t;
next();
// process nonempty object
if (_current != '}') {
do {
// key
const std::string key = parseString();
// colon
expect(':');
// value
parse(result[key]);
} while (_current == ',' && next());
}
// closing brace
expect('}');
break;
}
case ('['): {
// explicitly set result to array to cope with []
result._type = array;
result._payload = new array_t;
next();
// process nonempty array
if (_current != ']') {
size_t element_count = 0;
do {
// add a dummy value and continue parsing at its position
result += JSON();
parse(result[element_count++]);
} while (_current == ',' && next());
}
// closing bracket
expect(']');
break;
}
case ('\"'): {
result._type = string;
result._payload = new std::string(parseString());
break;
}
case ('t'): {
parseTrue();
result._type = boolean;
result._payload = new bool(true);
break;
}
case ('f'): {
parseFalse();
result._type = boolean;
result._payload = new bool(false);
break;
}
case ('n'): {
parseNull();
// nothing to do with result: is null by default
break;
}
default: {
if (std::isdigit(_current) || _current == '-') {
// collect number in tmp string
std::string tmp;
do {
tmp += _current;
next();
} while (std::isdigit(_current) || _current == '.' || _current == 'e' || _current == 'E' || _current == '+' || _current == '-');
if (tmp.find(".") == std::string::npos) {
// integer (we use atof, because it can cope with e)
result._type = number;
result._payload = new int(std::atof(tmp.c_str()));
} else {
// float
result._type = number_float;
result._payload = new double(std::atof(tmp.c_str()));
}
break;
} else {
error("unexpected character");
}
}
}
}
// http://stackoverflow.com/questions/7758580/writing-your-own-stl-container/7759622#7759622
JSON::iterator JSON::begin() {
return JSON::iterator(this);
}
JSON::iterator JSON::end() {
return JSON::iterator();
}
JSON::iterator::iterator() : _object(nullptr), _vi(nullptr), _oi(nullptr) {}
JSON::iterator::iterator(JSON* j) : _object(j), _vi(nullptr), _oi(nullptr) {
if (_object != nullptr)
switch (_object->_type) {
case (array): {
_vi = new array_t::iterator(static_cast<array_t*>(_object->_payload)->begin());
break;
}
case (object): {
_oi = new object_t::iterator(static_cast<object_t*>(_object->_payload)->begin());
break;
}
default:
break;
}
}
JSON::iterator::iterator(const JSON::iterator& o) : _object(o._object), _vi(nullptr), _oi(nullptr) {
if (_object != nullptr)
switch (_object->_type) {
case (array): {
_vi = new array_t::iterator(*(o._vi));
break;
}
case (object): {
_oi = new object_t::iterator(*(o._oi));
break;
}
default:
break;
}
}
JSON::iterator::~iterator() {
delete _vi;
delete _oi;
}
JSON::iterator& JSON::iterator::operator=(const JSON::iterator& o) {
_object = o._object;
if (_object != nullptr)
switch (_object->_type) {
case (array): {
_vi = new array_t::iterator(*(o._vi));
break;
}
case (object): {
_oi = new object_t::iterator(*(o._oi));
break;
}
default:
break;
}
return *this;
}
bool JSON::iterator::operator==(const JSON::iterator& o) const {
return _object == o._object;
}
bool JSON::iterator::operator!=(const JSON::iterator& o) const {
return _object != o._object;
}
JSON::iterator& JSON::iterator::operator++() {
// iterator cannot be incremented
if (_object == nullptr) {
return *this;
}
switch (_object->_type) {
case (array): {
if (++(*_vi) == static_cast<array_t*>(_object->_payload)->end()) {
_object = nullptr;
}
break;
}
case (object): {
if (++(*_oi) == static_cast<object_t*>(_object->_payload)->end()) {
_object = nullptr;
}
break;
}
default: {
_object = nullptr;
}
}
return *this;
}
JSON& JSON::iterator::operator*() const {
if (_object == nullptr) {
throw std::runtime_error("cannot get value");
}
switch (_object->_type) {
case (array):
return **_vi;
case (object):
return (*_oi)->second;
default:
return *_object;
}
}
JSON* JSON::iterator::operator->() const {
if (_object == nullptr) {
throw std::runtime_error("cannot get value");
}
switch (_object->_type) {
case (array):
return &(**_vi);
case (object):
return &((*_oi)->second);
default:
return _object;
}
}
std::string JSON::iterator::key() const {
if (_object != nullptr and _object->_type == object) {
return (*_oi)->first;
} else {
throw std::runtime_error("cannot get key");
}
}
JSON& JSON::iterator::value() const {
if (_object == nullptr) {
throw std::runtime_error("cannot get value");
}
switch (_object->_type) {
case (array):
return **_vi;
case (object):
return (*_oi)->second;
default:
return *_object;
}
}
JSON::const_iterator JSON::begin() const {
return JSON::const_iterator(this);
}
JSON::const_iterator JSON::end() const {
return JSON::const_iterator();
}
JSON::const_iterator JSON::cbegin() const {
return JSON::const_iterator(this);
}
JSON::const_iterator JSON::cend() const {
return JSON::const_iterator();
}
JSON::const_iterator::const_iterator() : _object(nullptr), _vi(nullptr), _oi(nullptr) {}
JSON::const_iterator::const_iterator(const JSON* j) : _object(j), _vi(nullptr), _oi(nullptr) {
if (_object != nullptr)
switch (_object->_type) {
case (array): {
_vi = new array_t::const_iterator(static_cast<array_t*>(_object->_payload)->begin());
break;
}
case (object): {
_oi = new object_t::const_iterator(static_cast<object_t*>(_object->_payload)->begin());
break;
}
default:
break;
}
}
JSON::const_iterator::const_iterator(const JSON::const_iterator& o) : _object(o._object), _vi(nullptr), _oi(nullptr) {
if (_object != nullptr)
switch (_object->_type) {
case (array): {
_vi = new array_t::const_iterator(*(o._vi));
break;
}
case (object): {
_oi = new object_t::const_iterator(*(o._oi));
break;
}
default:
break;
}
}
JSON::const_iterator::const_iterator(const JSON::iterator& o) : _object(o._object), _vi(nullptr), _oi(nullptr) {
if (_object != nullptr)
switch (_object->_type) {
case (array): {
_vi = new array_t::const_iterator(*(o._vi));
break;
}
case (object): {
_oi = new object_t::const_iterator(*(o._oi));
break;
}
default:
break;
}
}
JSON::const_iterator::~const_iterator() {
delete _vi;
delete _oi;
}
JSON::const_iterator& JSON::const_iterator::operator=(const JSON::const_iterator& o) {
_object = o._object;
if (_object != nullptr)
switch (_object->_type) {
case (array): {
_vi = new array_t::const_iterator(*(o._vi));
break;
}
case (object): {
_oi = new object_t::const_iterator(*(o._oi));
break;
}
default:
break;
}
return *this;
}
bool JSON::const_iterator::operator==(const JSON::const_iterator& o) const {
return _object == o._object;
}
bool JSON::const_iterator::operator!=(const JSON::const_iterator& o) const {
return _object != o._object;
}
JSON::const_iterator& JSON::const_iterator::operator++() {
// iterator cannot be incremented
if (_object == nullptr) {
return *this;
}
switch (_object->_type) {
case (array): {
if (++(*_vi) == static_cast<array_t*>(_object->_payload)->end()) {
_object = nullptr;
}
break;
}
case (object): {
if (++(*_oi) == static_cast<object_t*>(_object->_payload)->end()) {
_object = nullptr;
}
break;
}
default: {
_object = nullptr;
}
}
return *this;
}
const JSON& JSON::const_iterator::operator*() const {
if (_object == nullptr) {
throw std::runtime_error("cannot get value");
}
switch (_object->_type) {
case (array):
return **_vi;
case (object):
return (*_oi)->second;
default:
return *_object;
}
}
const JSON* JSON::const_iterator::operator->() const {
if (_object == nullptr) {
throw std::runtime_error("cannot get value");
}
switch (_object->_type) {
case (array):
return &(**_vi);
case (object):
return &((*_oi)->second);
default:
return _object;
}
}
std::string JSON::const_iterator::key() const {
if (_object != nullptr and _object->_type == object) {
return (*_oi)->first;
} else {
throw std::runtime_error("cannot get key");
}
}
const JSON& JSON::const_iterator::value() const {
if (_object == nullptr) {
throw std::runtime_error("cannot get value");
}
switch (_object->_type) {
case (array):
return **_vi;
case (object):
return (*_oi)->second;
default:
return *_object;
}
}