1
0
mirror of https://github.com/nlohmann/json synced 2024-11-25 21:30:06 +00:00

- further adjustments

This commit is contained in:
Niels 2013-07-05 16:15:29 +02:00
parent 6abf140b2b
commit cb2da141ec
4 changed files with 292 additions and 190 deletions

View File

@ -55,4 +55,3 @@ j["list"] = { 1, 0, 2 };
## Input / Output ## Input / Output
## STL-like access ## STL-like access

View File

@ -35,44 +35,82 @@ std::mutex JSON::_token;
*******************************/ *******************************/
JSON::JSON() : _type(null), _payload(nullptr) {} 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 std::string& s) : _type(string), _payload(new std::string(s)) {}
JSON::JSON(const char* 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(char* s) : _type(string), _payload(new std::string(s)) {}
JSON::JSON(const bool b) : _type(boolean), _payload(new bool(b)) {} JSON::JSON(const bool b) : _type(boolean), _payload(new bool(b)) {}
JSON::JSON(const int i) : _type(number_int), _payload(new int(i)) {} 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(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 #ifdef __cplusplus11
JSON::JSON(array_t a) : _type(array), _payload(new std::vector<JSON>(a)) {} JSON::JSON(array_init_t a) : _type(array), _payload(new array_t(a)) {}
JSON::JSON(object_t o) : _type(object), _payload(new std::map<std::string, JSON>) {
(*this)[std::get<0>(o)] = std::get<1>(o);
}
#endif #endif
/// copy constructor /// copy constructor
JSON::JSON(const JSON& o) : _type(o._type) { JSON::JSON(const JSON& o) : _type(o._type) {
switch (_type) { switch (_type) {
case (array): case (array): {
_payload = new std::vector<JSON>(*static_cast<std::vector<JSON>*>(o._payload)); _payload = new array_t(*static_cast<array_t*>(o._payload));
break; break;
case (object): }
_payload = new std::map<std::string, JSON>(*static_cast<std::map<std::string, JSON>*>(o._payload)); case (object): {
_payload = new object_t(*static_cast<object_t*>(o._payload));
break; break;
case (string): }
case (string): {
_payload = new std::string(*static_cast<std::string*>(o._payload)); _payload = new std::string(*static_cast<std::string*>(o._payload));
break; break;
case (boolean): }
case (boolean): {
_payload = new bool(*static_cast<bool*>(o._payload)); _payload = new bool(*static_cast<bool*>(o._payload));
break; break;
case (number_int): }
case (number): {
_payload = new int(*static_cast<int*>(o._payload)); _payload = new int(*static_cast<int*>(o._payload));
break; break;
case (number_float): }
case (number_float): {
_payload = new double(*static_cast<double*>(o._payload)); _payload = new double(*static_cast<double*>(o._payload));
break; break;
case (null): }
case (null): {
break; break;
}
} }
} }
@ -97,11 +135,11 @@ JSON& JSON::operator=(const JSON& o) {
switch (_type) { switch (_type) {
case (array): { case (array): {
delete static_cast<std::vector<JSON>*>(_payload); delete static_cast<array_t*>(_payload);
break; break;
} }
case (object): { case (object): {
delete static_cast<std::map<std::string, JSON>*>(_payload); delete static_cast<object_t*>(_payload);
break; break;
} }
case (string): { case (string): {
@ -112,7 +150,7 @@ JSON& JSON::operator=(const JSON& o) {
delete static_cast<bool*>(_payload); delete static_cast<bool*>(_payload);
break; break;
} }
case (number_int): { case (number): {
delete static_cast<int*>(_payload); delete static_cast<int*>(_payload);
break; break;
} }
@ -128,11 +166,11 @@ JSON& JSON::operator=(const JSON& o) {
_type = o._type; _type = o._type;
switch (_type) { switch (_type) {
case (array): { case (array): {
_payload = new std::vector<JSON>(*static_cast<std::vector<JSON>*>(o._payload)); _payload = new array_t(*static_cast<array_t*>(o._payload));
break; break;
} }
case (object): { case (object): {
_payload = new std::map<std::string, JSON>(*static_cast<std::map<std::string, JSON>*>(o._payload)); _payload = new object_t(*static_cast<object_t*>(o._payload));
break; break;
} }
case (string): { case (string): {
@ -143,7 +181,7 @@ JSON& JSON::operator=(const JSON& o) {
_payload = new bool(*static_cast<bool*>(o._payload)); _payload = new bool(*static_cast<bool*>(o._payload));
break; break;
} }
case (number_int): { case (number): {
_payload = new int(*static_cast<int*>(o._payload)); _payload = new int(*static_cast<int*>(o._payload));
break; break;
} }
@ -164,11 +202,11 @@ JSON& JSON::operator=(const JSON& o) {
JSON::~JSON() { JSON::~JSON() {
switch (_type) { switch (_type) {
case (array): { case (array): {
delete static_cast<std::vector<JSON>*>(_payload); delete static_cast<array_t*>(_payload);
break; break;
} }
case (object): { case (object): {
delete static_cast<std::map<std::string, JSON>*>(_payload); delete static_cast<object_t*>(_payload);
break; break;
} }
case (string): { case (string): {
@ -179,7 +217,7 @@ JSON::~JSON() {
delete static_cast<bool*>(_payload); delete static_cast<bool*>(_payload);
break; break;
} }
case (number_int): { case (number): {
delete static_cast<int*>(_payload); delete static_cast<int*>(_payload);
break; break;
} }
@ -210,7 +248,7 @@ JSON::operator const std::string() const {
JSON::operator int() const { JSON::operator int() const {
switch (_type) { switch (_type) {
case (number_int): case (number):
return *static_cast<int*>(_payload); return *static_cast<int*>(_payload);
case (number_float): case (number_float):
return static_cast<int>(*static_cast<double*>(_payload)); return static_cast<int>(*static_cast<double*>(_payload));
@ -221,7 +259,7 @@ JSON::operator int() const {
JSON::operator double() const { JSON::operator double() const {
switch (_type) { switch (_type) {
case (number_int): case (number):
return static_cast<double>(*static_cast<int*>(_payload)); return static_cast<double>(*static_cast<int*>(_payload));
case (number_float): case (number_float):
return *static_cast<double*>(_payload); return *static_cast<double*>(_payload);
@ -253,7 +291,7 @@ const std::string JSON::toString() const {
return *static_cast<bool*>(_payload) ? "true" : "false"; return *static_cast<bool*>(_payload) ? "true" : "false";
} }
case (number_int): { case (number): {
return to_string(*static_cast<int*>(_payload)); return to_string(*static_cast<int*>(_payload));
} }
@ -264,8 +302,8 @@ const std::string JSON::toString() const {
case (array): { case (array): {
std::string result; std::string result;
const std::vector<JSON>* array = static_cast<std::vector<JSON>*>(_payload); const array_t* array = static_cast<array_t*>(_payload);
for (std::vector<JSON>::const_iterator i = array->begin(); i != array->end(); ++i) { for (array_t::const_iterator i = array->begin(); i != array->end(); ++i) {
if (i != array->begin()) { if (i != array->begin()) {
result += ", "; result += ", ";
} }
@ -278,8 +316,8 @@ const std::string JSON::toString() const {
case (object): { case (object): {
std::string result; std::string result;
const std::map<std::string, JSON>* object = static_cast<std::map<std::string, JSON>*>(_payload); const object_t* object = static_cast<object_t*>(_payload);
for (std::map<std::string, JSON>::const_iterator i = object->begin(); i != object->end(); ++i) { for (object_t::const_iterator i = object->begin(); i != object->end(); ++i) {
if (i != object->begin()) { if (i != object->begin()) {
result += ", "; result += ", ";
} }
@ -297,6 +335,36 @@ const std::string JSON::toString() const {
*****************************************/ *****************************************/
JSON& JSON::operator+=(const JSON& o) { 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 #ifdef __cplusplus11
std::lock_guard<std::mutex> lg(_token); std::lock_guard<std::mutex> lg(_token);
#endif #endif
@ -307,42 +375,30 @@ JSON& JSON::operator+=(const JSON& o) {
if (_type == null) { if (_type == null) {
_type = array; _type = array;
_payload = new std::vector<JSON>; _payload = new array_t;
} }
static_cast<std::vector<JSON>*>(_payload)->push_back(o); static_cast<array_t*>(_payload)->push_back(o);
return *this;
} }
JSON& JSON::operator+=(const std::string& s) { void JSON::push_back(const std::string& s) {
JSON tmp(s); push_back(JSON(s));
operator+=(tmp);
return *this;
} }
JSON& JSON::operator+=(const char* s) { void JSON::push_back(const char* s) {
JSON tmp(s); push_back(JSON(s));
operator+=(tmp);
return *this;
} }
JSON& JSON::operator+=(bool b) { void JSON::push_back(bool b) {
JSON tmp(b); push_back(JSON(b));
operator+=(tmp);
return *this;
} }
JSON& JSON::operator+=(int i) { void JSON::push_back(int i) {
JSON tmp(i); push_back(JSON(i));
operator+=(tmp);
return *this;
} }
JSON& JSON::operator+=(double f) { void JSON::push_back(double f) {
JSON tmp(f); push_back(JSON(f));
operator+=(tmp);
return *this;
} }
/// operator to set an element in an object /// operator to set an element in an object
@ -355,7 +411,7 @@ JSON& JSON::operator[](int index) {
throw std::runtime_error("cannot add entry with index " + to_string(index) + " to " + _typename()); throw std::runtime_error("cannot add entry with index " + to_string(index) + " to " + _typename());
} }
std::vector<JSON>* array = static_cast<std::vector<JSON>*>(_payload); array_t* array = static_cast<array_t*>(_payload);
if (index >= array->size()) { if (index >= array->size()) {
throw std::runtime_error("cannot access element at index " + to_string(index)); throw std::runtime_error("cannot access element at index " + to_string(index));
@ -370,7 +426,7 @@ const JSON& JSON::operator[](const int index) const {
throw std::runtime_error("cannot get entry with index " + to_string(index) + " from " + _typename()); throw std::runtime_error("cannot get entry with index " + to_string(index) + " from " + _typename());
} }
std::vector<JSON>* array = static_cast<std::vector<JSON>*>(_payload); array_t* array = static_cast<array_t*>(_payload);
if (index >= array->size()) { if (index >= array->size()) {
throw std::runtime_error("cannot access element at index " + to_string(index)); throw std::runtime_error("cannot access element at index " + to_string(index));
@ -387,14 +443,14 @@ JSON& JSON::operator[](const std::string& key) {
if (_type == null) { if (_type == null) {
_type = object; _type = object;
_payload = new std::map<std::string, JSON>; _payload = new object_t;
} }
if (_type != object) { if (_type != object) {
throw std::runtime_error("cannot add entry with key " + std::string(key) + " to " + _typename()); throw std::runtime_error("cannot add entry with key " + std::string(key) + " to " + _typename());
} }
std::map<std::string, JSON>* object = static_cast<std::map<std::string, JSON>*>(_payload); object_t* object = static_cast<object_t*>(_payload);
if (object->find(key) == object->end()) { if (object->find(key) == object->end()) {
(*object)[key] = JSON(); (*object)[key] = JSON();
} }
@ -410,14 +466,14 @@ JSON& JSON::operator[](const char* key) {
if (_type == null) { if (_type == null) {
_type = object; _type = object;
_payload = new std::map<std::string, JSON>; _payload = new object_t;
} }
if (_type != object) { if (_type != object) {
throw std::runtime_error("cannot add entry with key " + std::string(key) + " to " + _typename()); throw std::runtime_error("cannot add entry with key " + std::string(key) + " to " + _typename());
} }
std::map<std::string, JSON>* object = static_cast<std::map<std::string, JSON>*>(_payload); object_t* object = static_cast<object_t*>(_payload);
if (object->find(key) == object->end()) { if (object->find(key) == object->end()) {
(*object)[key] = JSON(); (*object)[key] = JSON();
} }
@ -431,7 +487,7 @@ const JSON& JSON::operator[](const std::string& key) const {
throw std::runtime_error("cannot get entry with key " + std::string(key) + " from " + _typename()); throw std::runtime_error("cannot get entry with key " + std::string(key) + " from " + _typename());
} }
const std::map<std::string, JSON>* object = static_cast<std::map<std::string, JSON>*>(_payload); const object_t* object = static_cast<object_t*>(_payload);
if (object->find(key) == object->end()) { if (object->find(key) == object->end()) {
throw std::runtime_error("key " + key + " not found"); throw std::runtime_error("key " + key + " not found");
} else { } else {
@ -443,16 +499,16 @@ const JSON& JSON::operator[](const std::string& key) const {
size_t JSON::size() const { size_t JSON::size() const {
switch (_type) { switch (_type) {
case (array): case (array):
return static_cast<std::vector<JSON>*>(_payload)->size(); return static_cast<array_t*>(_payload)->size();
case (object): case (object):
return static_cast<std::map<std::string, JSON>*>(_payload)->size(); return static_cast<object_t*>(_payload)->size();
case (null): case (null):
return 0; return 0;
case (string): case (string):
return 1; return 1;
case (boolean): case (boolean):
return 1; return 1;
case (number_int): case (number):
return 1; return 1;
case (number_float): case (number_float):
return 1; return 1;
@ -463,16 +519,16 @@ size_t JSON::size() const {
bool JSON::empty() const { bool JSON::empty() const {
switch (_type) { switch (_type) {
case (array): case (array):
return static_cast<std::vector<JSON>*>(_payload)->empty(); return static_cast<array_t*>(_payload)->empty();
case (object): case (object):
return static_cast<std::map<std::string, JSON>*>(_payload)->empty(); return static_cast<object_t*>(_payload)->empty();
case (null): case (null):
return true; return true;
case (string): case (string):
return false; return false;
case (boolean): case (boolean):
return false; return false;
case (number_int): case (number):
return false; return false;
case (number_float): case (number_float):
return false; return false;
@ -496,12 +552,12 @@ JSON::iterator JSON::find(const char* key) {
if (_type != object) { if (_type != object) {
return end(); return end();
} else { } else {
std::map<std::string, JSON>* o = static_cast<std::map<std::string, JSON>*>(_payload); object_t* o = static_cast<object_t*>(_payload);
const std::map<std::string, JSON>::iterator i = o->find(key); const object_t::iterator i = o->find(key);
if (i != o->end()) { if (i != o->end()) {
JSON::iterator result; JSON::iterator result;
result._object = this; result._object = this;
result._oi = new std::map<std::string, JSON>::iterator(i); result._oi = new object_t::iterator(i);
return result; return result;
} else { } else {
return end(); return end();
@ -513,12 +569,12 @@ JSON::const_iterator JSON::find(const char* key) const {
if (_type != object) { if (_type != object) {
return end(); return end();
} else { } else {
std::map<std::string, JSON>* o = static_cast<std::map<std::string, JSON>*>(_payload); object_t* o = static_cast<object_t*>(_payload);
const std::map<std::string, JSON>::const_iterator i = o->find(key); const object_t::const_iterator i = o->find(key);
if (i != o->end()) { if (i != o->end()) {
JSON::const_iterator result; JSON::const_iterator result;
result._object = this; result._object = this;
result._oi = new std::map<std::string, JSON>::const_iterator(i); result._oi = new object_t::const_iterator(i);
return result; return result;
} else { } else {
return end(); return end();
@ -541,15 +597,15 @@ bool JSON::operator==(const JSON& o) const {
switch (_type) { switch (_type) {
case (array): { case (array): {
if (o._type == array) { if (o._type == array) {
std::vector<JSON>* a = static_cast<std::vector<JSON>*>(_payload); array_t* a = static_cast<array_t*>(_payload);
std::vector<JSON>* b = static_cast<std::vector<JSON>*>(o._payload); array_t* b = static_cast<array_t*>(o._payload);
return *a == *b; return *a == *b;
} }
} }
case (object): { case (object): {
if (o._type == object) { if (o._type == object) {
std::map<std::string, JSON>* a = static_cast<std::map<std::string, JSON>*>(_payload); object_t* a = static_cast<object_t*>(_payload);
std::map<std::string, JSON>* b = static_cast<std::map<std::string, JSON>*>(o._payload); object_t* b = static_cast<object_t*>(o._payload);
return *a == *b; return *a == *b;
} }
} }
@ -572,15 +628,15 @@ bool JSON::operator==(const JSON& o) const {
return a == b; return a == b;
} }
} }
case (number_int): { case (number): {
if (o._type == number_int or o._type == number_float) { if (o._type == number or o._type == number_float) {
int a = *this; int a = *this;
int b = o; int b = o;
return a == b; return a == b;
} }
} }
case (number_float): { case (number_float): {
if (o._type == number_int or o._type == number_float) { if (o._type == number or o._type == number_float) {
double a = *this; double a = *this;
double b = o; double b = o;
return a == b; return a == b;
@ -606,7 +662,7 @@ std::string JSON::_typename() const {
return "string"; return "string";
case (boolean): case (boolean):
return "boolean"; return "boolean";
case (number_int): case (number):
return "number"; return "number";
case (number_float): case (number_float):
return "number"; return "number";
@ -761,7 +817,7 @@ void JSON::parser::parse(JSON& result) {
case ('{'): { case ('{'): {
// explicitly set result to object to cope with {} // explicitly set result to object to cope with {}
result._type = object; result._type = object;
result._payload = new std::map<std::string, JSON>; result._payload = new object_t;
next(); next();
@ -788,7 +844,7 @@ void JSON::parser::parse(JSON& result) {
case ('['): { case ('['): {
// explicitly set result to array to cope with [] // explicitly set result to array to cope with []
result._type = array; result._type = array;
result._payload = new std::vector<JSON>; result._payload = new array_t;
next(); next();
@ -845,7 +901,7 @@ void JSON::parser::parse(JSON& result) {
if (tmp.find(".") == std::string::npos) { if (tmp.find(".") == std::string::npos) {
// integer (we use atof, because it can cope with e) // integer (we use atof, because it can cope with e)
result._type = number_int; result._type = number;
result._payload = new int(std::atof(tmp.c_str())); result._payload = new int(std::atof(tmp.c_str()));
} else { } else {
// float // float
@ -874,33 +930,35 @@ JSON::iterator JSON::end() {
JSON::iterator::iterator() : _object(nullptr), _vi(nullptr), _oi(nullptr) {} JSON::iterator::iterator() : _object(nullptr), _vi(nullptr), _oi(nullptr) {}
JSON::iterator::iterator(JSON* j) : _object(j), _vi(nullptr), _oi(nullptr) { JSON::iterator::iterator(JSON* j) : _object(j), _vi(nullptr), _oi(nullptr) {
switch (_object->_type) { if (_object != nullptr)
case (array): { switch (_object->_type) {
_vi = new std::vector<JSON>::iterator(static_cast<std::vector<JSON>*>(_object->_payload)->begin()); case (array): {
break; _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;
} }
case (object): {
_oi = new std::map<std::string, JSON>::iterator(static_cast<std::map<std::string, JSON>*>(_object->_payload)->begin());
break;
}
default:
break;
}
} }
JSON::iterator::iterator(const JSON::iterator& o) : _object(o._object), _vi(nullptr), _oi(nullptr) { JSON::iterator::iterator(const JSON::iterator& o) : _object(o._object), _vi(nullptr), _oi(nullptr) {
switch (_object->_type) { if (_object != nullptr)
case (array): { switch (_object->_type) {
_vi = new std::vector<JSON>::iterator(*(o._vi)); case (array): {
break; _vi = new array_t::iterator(*(o._vi));
break;
}
case (object): {
_oi = new object_t::iterator(*(o._oi));
break;
}
default:
break;
} }
case (object): {
_oi = new std::map<std::string, JSON>::iterator(*(o._oi));
break;
}
default:
break;
}
} }
JSON::iterator::~iterator() { JSON::iterator::~iterator() {
@ -910,18 +968,19 @@ JSON::iterator::~iterator() {
JSON::iterator& JSON::iterator::operator=(const JSON::iterator& o) { JSON::iterator& JSON::iterator::operator=(const JSON::iterator& o) {
_object = o._object; _object = o._object;
switch (_object->_type) { if (_object != nullptr)
case (array): { switch (_object->_type) {
_vi = new std::vector<JSON>::iterator(*(o._vi)); case (array): {
break; _vi = new array_t::iterator(*(o._vi));
break;
}
case (object): {
_oi = new object_t::iterator(*(o._oi));
break;
}
default:
break;
} }
case (object): {
_oi = new std::map<std::string, JSON>::iterator(*(o._oi));
break;
}
default:
break;
}
return *this; return *this;
} }
@ -942,13 +1001,13 @@ JSON::iterator& JSON::iterator::operator++() {
switch (_object->_type) { switch (_object->_type) {
case (array): { case (array): {
if (++(*_vi) == static_cast<std::vector<JSON>*>(_object->_payload)->end()) { if (++(*_vi) == static_cast<array_t*>(_object->_payload)->end()) {
_object = nullptr; _object = nullptr;
} }
break; break;
} }
case (object): { case (object): {
if (++(*_oi) == static_cast<std::map<std::string, JSON>*>(_object->_payload)->end()) { if (++(*_oi) == static_cast<object_t*>(_object->_payload)->end()) {
_object = nullptr; _object = nullptr;
} }
break; break;
@ -1033,48 +1092,51 @@ JSON::const_iterator JSON::cend() const {
JSON::const_iterator::const_iterator() : _object(nullptr), _vi(nullptr), _oi(nullptr) {} 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) { JSON::const_iterator::const_iterator(const JSON* j) : _object(j), _vi(nullptr), _oi(nullptr) {
switch (_object->_type) { if (_object != nullptr)
case (array): { switch (_object->_type) {
_vi = new std::vector<JSON>::const_iterator(static_cast<std::vector<JSON>*>(_object->_payload)->begin()); case (array): {
break; _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;
} }
case (object): {
_oi = new std::map<std::string, JSON>::const_iterator(static_cast<std::map<std::string, JSON>*>(_object->_payload)->begin());
break;
}
default:
break;
}
} }
JSON::const_iterator::const_iterator(const JSON::const_iterator& o) : _object(o._object), _vi(nullptr), _oi(nullptr) { JSON::const_iterator::const_iterator(const JSON::const_iterator& o) : _object(o._object), _vi(nullptr), _oi(nullptr) {
switch (_object->_type) { if (_object != nullptr)
case (array): { switch (_object->_type) {
_vi = new std::vector<JSON>::const_iterator(*(o._vi)); case (array): {
break; _vi = new array_t::const_iterator(*(o._vi));
break;
}
case (object): {
_oi = new object_t::const_iterator(*(o._oi));
break;
}
default:
break;
} }
case (object): {
_oi = new std::map<std::string, JSON>::const_iterator(*(o._oi));
break;
}
default:
break;
}
} }
JSON::const_iterator::const_iterator(const JSON::iterator& o) : _object(o._object), _vi(nullptr), _oi(nullptr) { JSON::const_iterator::const_iterator(const JSON::iterator& o) : _object(o._object), _vi(nullptr), _oi(nullptr) {
switch (_object->_type) { if (_object != nullptr)
case (array): { switch (_object->_type) {
_vi = new std::vector<JSON>::const_iterator(*(o._vi)); case (array): {
break; _vi = new array_t::const_iterator(*(o._vi));
break;
}
case (object): {
_oi = new object_t::const_iterator(*(o._oi));
break;
}
default:
break;
} }
case (object): {
_oi = new std::map<std::string, JSON>::const_iterator(*(o._oi));
break;
}
default:
break;
}
} }
JSON::const_iterator::~const_iterator() { JSON::const_iterator::~const_iterator() {
@ -1084,18 +1146,19 @@ JSON::const_iterator::~const_iterator() {
JSON::const_iterator& JSON::const_iterator::operator=(const JSON::const_iterator& o) { JSON::const_iterator& JSON::const_iterator::operator=(const JSON::const_iterator& o) {
_object = o._object; _object = o._object;
switch (_object->_type) { if (_object != nullptr)
case (array): { switch (_object->_type) {
_vi = new std::vector<JSON>::const_iterator(*(o._vi)); case (array): {
break; _vi = new array_t::const_iterator(*(o._vi));
break;
}
case (object): {
_oi = new object_t::const_iterator(*(o._oi));
break;
}
default:
break;
} }
case (object): {
_oi = new std::map<std::string, JSON>::const_iterator(*(o._oi));
break;
}
default:
break;
}
return *this; return *this;
} }
@ -1116,13 +1179,13 @@ JSON::const_iterator& JSON::const_iterator::operator++() {
switch (_object->_type) { switch (_object->_type) {
case (array): { case (array): {
if (++(*_vi) == static_cast<std::vector<JSON>*>(_object->_payload)->end()) { if (++(*_vi) == static_cast<array_t*>(_object->_payload)->end()) {
_object = nullptr; _object = nullptr;
} }
break; break;
} }
case (object): { case (object): {
if (++(*_oi) == static_cast<std::map<std::string, JSON>*>(_object->_payload)->end()) { if (++(*_oi) == static_cast<object_t*>(_object->_payload)->end()) {
_object = nullptr; _object = nullptr;
} }
break; break;

View File

@ -22,7 +22,7 @@
#endif #endif
class JSON { class JSON {
// forward declaration to friend this class // forward declaration to friend this class
public: public:
class iterator; class iterator;
class const_iterator; class const_iterator;
@ -36,7 +36,7 @@ class JSON {
public: public:
/// possible types of a JSON object /// possible types of a JSON object
typedef enum { typedef enum {
array, object, null, string, boolean, number_int, number_float array, object, null, string, boolean, number, number_float
} json_t; } json_t;
private: private:
@ -47,16 +47,21 @@ class JSON {
void* _payload; void* _payload;
public: public:
/// a type for an object
typedef std::map<std::string, JSON> object_t;
/// a type for an array
typedef std::vector<JSON> array_t;
#ifdef __cplusplus11 #ifdef __cplusplus11
/// a type for objects /// a type for array initialization
typedef std::tuple<std::string, JSON> object_t; typedef std::initializer_list<JSON> array_init_t;
/// a type for arrays
typedef std::initializer_list<JSON> array_t;
#endif #endif
public: public:
/// create an empty (null) object /// create an empty (null) object
JSON(); JSON();
/// create an empty object according to given type
JSON(json_t);
/// create a string object from C++ string /// create a string object from C++ string
JSON(const std::string&); JSON(const std::string&);
/// create a string object from C string /// create a string object from C string
@ -69,11 +74,13 @@ class JSON {
JSON(const int); JSON(const int);
/// create a number object /// create a number object
JSON(const double); JSON(const double);
/// create an array
JSON(array_t);
/// create an object
JSON(object_t);
#ifdef __cplusplus11 #ifdef __cplusplus11
/// create from an initializer list (to an array) /// create from an initializer list (to an array)
JSON(array_t); JSON(array_init_t);
/// create from a mapping (to an object)
JSON(object_t);
#endif #endif
/// copy constructor /// copy constructor
@ -143,6 +150,19 @@ class JSON {
/// add a number to an array /// add a number to an array
JSON& operator+=(double); JSON& operator+=(double);
/// add an object/array to an array
void push_back(const JSON&);
/// add a string to an array
void push_back(const std::string&);
/// 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);
/// operator to set an element in an array /// operator to set an element in an array
JSON& operator[](int); JSON& operator[](int);
/// operator to get an element in an array /// operator to get an element in an array
@ -166,8 +186,8 @@ class JSON {
/// 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&);
const_iterator find(const std::string&) const; const_iterator find(const std::string&) const;
iterator find(const char *); iterator find(const char*);
const_iterator find(const char *) const; const_iterator find(const char*) const;
/// direct access to the underlying payload /// direct access to the underlying payload
void* data(); void* data();
@ -184,8 +204,8 @@ class JSON {
public: public:
/// an iterator /// an iterator
class iterator { class iterator {
friend class JSON; friend class JSON;
friend class JSON::const_iterator; friend class JSON::const_iterator;
public: public:
iterator(); iterator();
iterator(JSON*); iterator(JSON*);
@ -208,14 +228,14 @@ class JSON {
/// a JSON value /// a JSON value
JSON* _object; JSON* _object;
/// an iterator for JSON arrays /// an iterator for JSON arrays
std::vector<JSON>::iterator* _vi; array_t::iterator* _vi;
/// an iterator for JSON objects /// an iterator for JSON objects
std::map<std::string, JSON>::iterator* _oi; object_t::iterator* _oi;
}; };
/// a const iterator /// a const iterator
class const_iterator { class const_iterator {
friend class JSON; friend class JSON;
public: public:
const_iterator(); const_iterator();
const_iterator(const JSON*); const_iterator(const JSON*);
@ -239,9 +259,9 @@ class JSON {
/// a JSON value /// a JSON value
const JSON* _object; const JSON* _object;
/// an iterator for JSON arrays /// an iterator for JSON arrays
std::vector<JSON>::const_iterator* _vi; array_t::const_iterator* _vi;
/// an iterator for JSON objects /// an iterator for JSON objects
std::map<std::string, JSON>::const_iterator* _oi; object_t::const_iterator* _oi;
}; };
public: public:

View File

@ -74,7 +74,6 @@ void test_null() {
} }
} }
void test_bool() { void test_bool() {
JSON True = true; JSON True = true;
JSON False = false; JSON False = false;
@ -212,14 +211,12 @@ void test_array() {
} }
{ {
/*
size_t count = 0; size_t count = 0;
for (JSON::const_iterator i = a.begin(); i != a.end(); ++i) { for (JSON::const_iterator i = a.begin(); i != a.end(); ++i) {
std::cerr << *i << '\n'; std::cerr << *i << '\n';
count++; count++;
} }
assert(count == a.size()); assert(count == a.size());
*/
} }
{ {
@ -253,7 +250,6 @@ void test_array() {
} }
{ {
/*
JSON::const_iterator i; JSON::const_iterator i;
size_t count = 0; size_t count = 0;
for (i = a.begin(); i != a.end(); ++i) { for (i = a.begin(); i != a.end(); ++i) {
@ -261,7 +257,6 @@ void test_array() {
count++; count++;
} }
assert(count == a.size()); assert(count == a.size());
*/
} }
{ {
@ -282,6 +277,30 @@ void test_array() {
} }
} }
void test_object() {
// check find()
{
JSON o;
o["foo"] = "bar";
JSON::iterator i1 = o.find("foo");
assert(i1 != o.end());
assert(i1.value() == "bar");
assert(i1.key() == "foo");
assert(*i1 == "bar");
JSON::iterator i2 = o.find("baz");
assert(i2 == o.end());
JSON a;
a += "foo";
a += "bar";
JSON::iterator i;
i = a.find("foo");
assert(i == a.end());
}
}
void test_streaming() { void test_streaming() {
// stream text representation into stream // stream text representation into stream
std::stringstream i; std::stringstream i;
@ -335,6 +354,7 @@ int main() {
test_bool(); test_bool();
test_string(); test_string();
test_array(); test_array();
test_object();
test_streaming(); test_streaming();
return 0; return 0;