diff --git a/README.md b/README.md index 06da87dd..38ad5812 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ There are myriads of [JSON](http://json.org) libraries out there, and each may e - **Trivial integration**. Our whole code consists of just two files: A header file `JSON.h` and a source file `JSON.cc`. That's it. No library, no subproject, no dependencies. The class is written in vanilla C++98 and -- if possible -- uses some features of C++11 such as move constructors. All in all, the class should require no adjustment of your compiler flags or project settings. -- **Intiuitve syntax**. In languages such as Python, JSON feels like a first class data type. We used all the operator magic of C++ to achieve the same feeling in your code. Check out the examples below and you know, what I mean. +- **Intiuitve syntax**. In languages such as Python, JSON feels like a first class data type. We used all the operator magic of C++ to achieve the same feeling in your code. Check out the [examples below](#examples) and you know, what I mean. Other aspects were not so important to us: @@ -52,6 +52,37 @@ j["further"]["entry"] = 42; j["list"] = { 1, 0, 2 }; ``` -## Input / Output +### Input / Output -## STL-like access +```cpp +// create object from stream +JSON j; +j << "{ \"pi\": 3.141, \"happy\": true }"; + +// write string representation to stream +std::cout << j; +``` + +### STL-like access + +```cpp +// create an array +JSON j; +j.push_back("foo"); +j.push_back(1); +j.push_back(true); + +// iterate the array +for (JSON::iterator it = j.begin(); it != j.end(); ++it) { + std::cout << *it << '\n'; +} + +// getter/setter +std::string tmp = j[0]; +j[1] = 42; + +// other stuff +j.size(); // 3 +j.empty(); // false +j.type(); // JSON::array +``` diff --git a/src/JSON.cc b/src/JSON.cc index 0ef00dc9..a9b9a4e5 100644 --- a/src/JSON.cc +++ b/src/JSON.cc @@ -413,7 +413,7 @@ JSON& JSON::operator[](int index) { array_t* array = static_cast(_payload); - if (index >= array->size()) { + if (index >= (int)array->size()) { throw std::runtime_error("cannot access element at index " + to_string(index)); } @@ -428,7 +428,7 @@ const JSON& JSON::operator[](const int index) const { array_t* array = static_cast(_payload); - if (index >= array->size()) { + if (index >= (int)array->size()) { throw std::runtime_error("cannot access element at index " + to_string(index)); } diff --git a/src/JSON.h b/src/JSON.h index 7fe305c6..f2750ebd 100644 --- a/src/JSON.h +++ b/src/JSON.h @@ -283,7 +283,7 @@ class JSON { private: bool next(); - void error(std::string = ""); + void error(std::string = "") __attribute__((noreturn)); std::string parseString(); void parseTrue(); void parseFalse();