Commit cb2da141 authored by Niels's avatar Niels

- further adjustments

parent 6abf140b
...@@ -55,4 +55,3 @@ j["list"] = { 1, 0, 2 }; ...@@ -55,4 +55,3 @@ j["list"] = { 1, 0, 2 };
## Input / Output ## Input / Output
## STL-like access ## STL-like access
This diff is collapsed.
...@@ -22,7 +22,7 @@ ...@@ -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 { ...@@ -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 { ...@@ -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 { ...@@ -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);
#ifdef __cplusplus11 /// create an array
/// create from an initializer list (to an array)
JSON(array_t); JSON(array_t);
/// create from a mapping (to an object) /// create an object
JSON(object_t); JSON(object_t);
#ifdef __cplusplus11
/// create from an initializer list (to an array)
JSON(array_init_t);
#endif #endif
/// copy constructor /// copy constructor
...@@ -143,6 +150,19 @@ class JSON { ...@@ -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 { ...@@ -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 { ...@@ -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 { ...@@ -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 { ...@@ -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:
......
...@@ -74,7 +74,6 @@ void test_null() { ...@@ -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() { ...@@ -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() { ...@@ -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() { ...@@ -261,7 +257,6 @@ void test_array() {
count++; count++;
} }
assert(count == a.size()); assert(count == a.size());
*/
} }
{ {
...@@ -282,6 +277,30 @@ void test_array() { ...@@ -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() { ...@@ -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;
......
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