Commit cb2da141 authored by Niels's avatar Niels

- further adjustments

parent 6abf140b
......@@ -55,4 +55,3 @@ j["list"] = { 1, 0, 2 };
## Input / Output
## STL-like access
This diff is collapsed.
......@@ -22,7 +22,7 @@
#endif
class JSON {
// forward declaration to friend this class
// forward declaration to friend this class
public:
class iterator;
class const_iterator;
......@@ -36,7 +36,7 @@ class JSON {
public:
/// possible types of a JSON object
typedef enum {
array, object, null, string, boolean, number_int, number_float
array, object, null, string, boolean, number, number_float
} json_t;
private:
......@@ -47,16 +47,21 @@ class JSON {
void* _payload;
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
/// a type for objects
typedef std::tuple<std::string, JSON> object_t;
/// a type for arrays
typedef std::initializer_list<JSON> array_t;
/// a type for array initialization
typedef std::initializer_list<JSON> array_init_t;
#endif
public:
/// create an empty (null) object
JSON();
/// create an empty object according to given type
JSON(json_t);
/// create a string object from C++ string
JSON(const std::string&);
/// create a string object from C string
......@@ -69,11 +74,13 @@ class JSON {
JSON(const int);
/// create a number object
JSON(const double);
#ifdef __cplusplus11
/// create from an initializer list (to an array)
/// create an array
JSON(array_t);
/// create from a mapping (to an object)
/// create an object
JSON(object_t);
#ifdef __cplusplus11
/// create from an initializer list (to an array)
JSON(array_init_t);
#endif
/// copy constructor
......@@ -143,6 +150,19 @@ class JSON {
/// add a number to an array
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
JSON& operator[](int);
/// operator to get an element in an array
......@@ -166,8 +186,8 @@ class JSON {
/// find an element in an object (returns end() iterator otherwise)
iterator find(const std::string&);
const_iterator find(const std::string&) const;
iterator find(const char *);
const_iterator find(const char *) const;
iterator find(const char*);
const_iterator find(const char*) const;
/// direct access to the underlying payload
void* data();
......@@ -184,8 +204,8 @@ class JSON {
public:
/// an iterator
class iterator {
friend class JSON;
friend class JSON::const_iterator;
friend class JSON;
friend class JSON::const_iterator;
public:
iterator();
iterator(JSON*);
......@@ -208,14 +228,14 @@ class JSON {
/// a JSON value
JSON* _object;
/// an iterator for JSON arrays
std::vector<JSON>::iterator* _vi;
array_t::iterator* _vi;
/// an iterator for JSON objects
std::map<std::string, JSON>::iterator* _oi;
object_t::iterator* _oi;
};
/// a const iterator
class const_iterator {
friend class JSON;
friend class JSON;
public:
const_iterator();
const_iterator(const JSON*);
......@@ -239,9 +259,9 @@ class JSON {
/// a JSON value
const JSON* _object;
/// an iterator for JSON arrays
std::vector<JSON>::const_iterator* _vi;
array_t::const_iterator* _vi;
/// an iterator for JSON objects
std::map<std::string, JSON>::const_iterator* _oi;
object_t::const_iterator* _oi;
};
public:
......
......@@ -74,7 +74,6 @@ void test_null() {
}
}
void test_bool() {
JSON True = true;
JSON False = false;
......@@ -212,14 +211,12 @@ void test_array() {
}
{
/*
size_t count = 0;
for (JSON::const_iterator i = a.begin(); i != a.end(); ++i) {
std::cerr << *i << '\n';
count++;
}
assert(count == a.size());
*/
}
{
......@@ -253,7 +250,6 @@ void test_array() {
}
{
/*
JSON::const_iterator i;
size_t count = 0;
for (i = a.begin(); i != a.end(); ++i) {
......@@ -261,7 +257,6 @@ void test_array() {
count++;
}
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() {
// stream text representation into stream
std::stringstream i;
......@@ -335,6 +354,7 @@ int main() {
test_bool();
test_string();
test_array();
test_object();
test_streaming();
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