renamed class from "JSON" to "son"

This commit is contained in:
Niels 2015-01-04 20:43:25 +01:00
parent 4d00105e5c
commit f63ff7727e
8 changed files with 698 additions and 698 deletions

View File

@ -2,16 +2,16 @@ noinst_PROGRAMS = json_unit json_parser
FLAGS = -Wall -Wextra -pedantic -Weffc++ -Wcast-align -Wcast-qual -Wctor-dtor-privacy -Wdisabled-optimization -Wformat=2 -Winit-self -Wmissing-declarations -Wmissing-include-dirs -Wold-style-cast -Woverloaded-virtual -Wredundant-decls -Wshadow -Wsign-conversion -Wsign-promo -Wstrict-overflow=5 -Wswitch -Wundef -Wno-unused -Wnon-virtual-dtor -Wreorder
json_unit_SOURCES = src/JSON.cc src/JSON.h test/catch.hpp test/JSON_unit.cc
json_unit_SOURCES = src/json.cc src/json.h test/catch.hpp test/json_unit.cc
json_unit_CXXFLAGS = $(FLAGS) -std=c++11
json_unit_CPPFLAGS = -I$(top_srcdir)/src -I$(top_srcdir)/test -Dprivate=public
json_parser_SOURCES = src/JSON.cc src/JSON.h benchmark/parse.cc
json_parser_SOURCES = src/json.cc src/json.h benchmark/parse.cc
json_parser_CXXFLAGS = -O3 -std=c++11 -flto
json_parser_CPPFLAGS = -I$(top_srcdir)/src -I$(top_srcdir)/benchmark
cppcheck:
cppcheck --enable=all --inconclusive --std=c++11 src/JSON.*
cppcheck --enable=all --inconclusive --std=c++11 src/json.*
svn-clean: maintainer-clean
rm -fr configure INSTALL aclocal.m4 build-aux depcomp install-sh missing test-driver

View File

@ -12,9 +12,9 @@ There are myriads of [JSON](http://json.org) libraries out there, and each may e
- **Intuitive syntax**. In languages such as Python, JSON feels like a first class data type. We used all the operator magic of modern C++ to achieve the same feeling in your code. Check out the [examples below](#examples) and the [reference](https://github.com/nlohmann/json/blob/master/doc/Reference.md), and you know, what I mean.
- **Trivial integration**. Our whole code consists of a class in 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++11. All in all, everything should require no adjustment of your compiler flags or project settings.
- **Trivial integration**. Our whole code consists of a class in 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++11. All in all, everything should require no adjustment of your compiler flags or project settings.
- **Serious testing**. Our class is heavily [unit-tested](https://github.com/nlohmann/json/blob/master/test/JSON_unit.cc) and covers [100%](https://coveralls.io/r/nlohmann/json) of the code, including all exceptional behavior. Furthermore, we checked with [Valgrind](http://valgrind.org) that there are no memory leaks.
- **Serious testing**. Our class is heavily [unit-tested](https://github.com/nlohmann/json/blob/master/test/json_unit.cc) and covers [100%](https://coveralls.io/r/nlohmann/json) of the code, including all exceptional behavior. Furthermore, we checked with [Valgrind](http://valgrind.org) that there are no memory leaks.
Other aspects were not so important to us:
@ -29,10 +29,10 @@ Other aspects were not so important to us:
All you need to do is add
```cpp
#include "JSON.h"
#include "json.h"
```
to the files you want to use JSON objects. Furthermore, you need to compile the file `JSON.cc` and link it to your binaries. Do not forget to set the necessary switches to enable C++11 (e.g., `-std=c++11` for GCC and Clang).
to the files you want to use JSON objects. Furthermore, you need to compile the file `json.cc` and link it to your binaries. Do not forget to set the necessary switches to enable C++11 (e.g., `-std=c++11` for GCC and Clang).
## Examples
@ -61,7 +61,7 @@ With the JSON class, you could write:
```cpp
// create an empty structure (null)
JSON j;
json j;
// add a number that is stored as double (note the implicit conversion of j to an object)
j["pi"] = 3.141;
@ -85,7 +85,7 @@ j["list"] = { 1, 0, 2 };
j["object"] = { {"currency", "USD"}, {"value", "42.99"} };
// instead, you could also write (which looks very similar to the JSON above)
JSON j2 = {
json j2 = {
{"pi", 3.141},
{"happy", true},
{"name", "Niels"},
@ -109,7 +109,7 @@ You can create an object (deserialization) by appending `_json` to a string lite
```cpp
// create object from string literal
JSON j = "{ \"pi\": 3.141, \"happy\": true }"_json;
json j = "{ \"pi\": 3.141, \"happy\": true }"_json;
```
You can also get a string representation (serialize):
@ -125,7 +125,7 @@ You can also use streams to serialize and deserialize:
```cpp
// deserialize from standard input
JSON j;
json j;
j << std::cin;
// serialize to standard output
@ -140,13 +140,13 @@ We designed the JSON class to behave just like an STL container:
```cpp
// create an array using push_back
JSON j;
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) {
for (json::iterator it = j.begin(); it != j.end(); ++it) {
std::cout << *it << '\n';
}
@ -163,14 +163,14 @@ bool foo = j.at(2);
// other stuff
j.size(); // 3 entries
j.empty(); // false
j.type(); // JSON::value_type::array
j.type(); // json::value_type::array
j.clear(); // the array is empty again
// comparison
j == "[\"foo\", 1, true]"_json; // true
// create an object
JSON o;
json o;
o["foo"] = 23;
o["bar"] = false;
o["baz"] = 3.141;
@ -181,7 +181,7 @@ if (o.find("foo") != o.end()) {
}
// iterate the object
for (JSON::iterator it = o.begin(); it != o.end(); ++it) {
for (json::iterator it = o.begin(); it != o.end(); ++it) {
std::cout << it.key() << ':' << it.value() << '\n';
}
```
@ -193,17 +193,17 @@ The type of the JSON object is determined automatically by the expression to sto
```cpp
/// strings
std::string s1 = "Hello, world!";
JSON js = s1;
json js = s1;
std::string s2 = js;
// Booleans
bool b1 = true;
JSON jb = b1;
json jb = b1;
bool b2 = jb;
// numbers
int i = 42;
JSON jn = i;
json jn = i;
double f = jn;
// etc.

View File

@ -1,8 +1,8 @@
#include "JSON.h"
#include "json.h"
int main()
{
JSON j;
json j;
j << std::cin;
return 0;
}

View File

@ -1,5 +1,5 @@
AC_INIT([JSON], [2.0], [mail@nlohmann.me])
AC_CONFIG_SRCDIR([src/JSON.cc])
AC_CONFIG_SRCDIR([src/json.h])
AM_INIT_AUTOMAKE([foreign subdir-objects])
AM_SILENT_RULES([yes])

View File

@ -2,7 +2,7 @@
## Nomenclature
We use the term "JSON" when we mean the [JavaScript Object Notation](http://json.org); that is, the file format. When we talk about the class implementing our library, we use "`JSON`" (typewriter font). Instances of this class are called "`JSON` values" to differentiate them from "JSON objects"; that is, unordered mappings, hashes, and whatnot.
We use the term "JSON" when we mean the [JavaScript Object Notation](http://json.org); that is, the file format. When we talk about the class implementing our library, we use "`json`" (typewriter font). Instances of this class are called "`json` values" to differentiate them from "JSON objects"; that is, unordered mappings, hashes, and whatnot.
## Types and default values
@ -11,13 +11,13 @@ This table describes how JSON values are mapped to C++ types.
| JSON type | value_type | C++ type | type alias | default value |
| ----------------------- | -------------------------- | ----------------------------- | ---------------------- | --------------
| null | `value_type::null` | `nullptr_t` | - | `nullptr` |
| string | `value_type::string` | `std::string` | `JSON::string_t` | `""` |
| number (integer) | `value_type::number` | `int` | `JSON::number_t` | `0` |
| number (floating point) | `value_type::number_float` | `double` | `JSON::number_float_t` | `0.0` |
| array | `value_type::array ` | `std::array<JSON>` | `JSON::array_t` | `{}` |
| object | `value_type::object` | `std::map<std::string, JSON>` | `JSON::object_t` | `{}` |
| string | `value_type::string` | `std::string` | `json::string_t` | `""` |
| number (integer) | `value_type::number` | `int` | `json::number_t` | `0` |
| number (floating point) | `value_type::number_float` | `double` | `json::number_float_t` | `0.0` |
| array | `value_type::array ` | `std::array<json>` | `json::array_t` | `{}` |
| object | `value_type::object` | `std::map<std::string, json>` | `json::object_t` | `{}` |
The second column list entries of an enumeration `value_type` which can be queried by calling `type()` on a `JSON` value. The column "C++ types" list the internal type that is used to represent the respective JSON value. The "type alias" column furthermore lists type aliases that are used in the `JSON` class to allow for more flexibility. The last column list the default value; that is, the value that is set if none is passed to the constructor or that is set if `clear()` is called.
The second column list entries of an enumeration `value_type` which can be queried by calling `type()` on a `json` value. The column "C++ types" list the internal type that is used to represent the respective JSON value. The "type alias" column furthermore lists type aliases that are used in the `json` class to allow for more flexibility. The last column list the default value; that is, the value that is set if none is passed to the constructor or that is set if `clear()` is called.
## Type conversions
@ -28,21 +28,21 @@ There are only a few type conversions possible:
- Any value (i.e., boolean, string, number, null) but JSON objects can be translated into an array. The result is a singleton array that consists of the value before.
- Any other conversion will throw a `std::logic_error` exception.
When compatible, `JSON` values **implicitly convert** to `std::string`, `int`, `double`, `JSON::array_t`, and `JSON::object_t`. Furthermore, **explicit type conversion** is possible using the `get<>()` function with the aforementioned types.
When compatible, `json` values **implicitly convert** to `std::string`, `int`, `double`, `json::array_t`, and `json::object_t`. Furthermore, **explicit type conversion** is possible using the `get<>()` function with the aforementioned types.
## Initialization
`JSON` values can be created from many literals and variable types:
`json` values can be created from many literals and variable types:
| JSON type | literal/variable types | examples |
| --------- | ---------------------- | -------- |
| none | null pointer literal, `nullptr_t` type, no value | `nullptr` |
| boolean | boolean literals, `bool` type, `JSON::boolean_t` type | `true`, `false` |
| string | string literal, `char*` type, `std::string` type, `std::string&&` rvalue reference, `JSON::string_t` type | `"Hello"` |
| number (integer) | integer literal, `short int` type, `int` type, `JSON_number_t` type | `42` |
| number (floating point) | floating point literal, `float` type, `double` type, `JSON::number_float_t` type | `3.141529`
| array | initializer list whose elements are `JSON` values (or can be translated into `JSON` values using the rules above), `std::vector<JSON>` type, `JSON::array_t` type, `JSON::array_t&&` rvalue reference | `{1, 2, 3, true, "foo"}` |
| object | initializer list whose elements are pairs of a string literal and a `JSON` value (or can be translated into `JSON` values using the rules above), `std::map<std::string, JSON>` type, `JSON::object_t` type, `JSON::object_t&&` rvalue reference | `{ {"key1", 42}, {"key2", false} }` |
| boolean | boolean literals, `bool` type, `json::boolean_t` type | `true`, `false` |
| string | string literal, `char*` type, `std::string` type, `std::string&&` rvalue reference, `json::string_t` type | `"Hello"` |
| number (integer) | integer literal, `short int` type, `int` type, `json_number_t` type | `42` |
| number (floating point) | floating point literal, `float` type, `double` type, `json::number_float_t` type | `3.141529`
| array | initializer list whose elements are `json` values (or can be translated into `json` values using the rules above), `std::vector<json>` type, `json::array_t` type, `json::array_t&&` rvalue reference | `{1, 2, 3, true, "foo"}` |
| object | initializer list whose elements are pairs of a string literal and a `json` value (or can be translated into `json` values using the rules above), `std::map<std::string, json>` type, `json::object_t` type, `json::object_t&&` rvalue reference | `{ {"key1", 42}, {"key2", false} }` |
## Number types

File diff suppressed because it is too large Load Diff

View File

@ -31,12 +31,12 @@ due to alignment.
@bug Numbers are currently handled too generously. There are several formats
that are forbidden by the standard, but are accepted by the parser.
@todo Implement JSON::swap()
@todo Implement JSON::insert(), JSON::emplace(), JSON::emplace_back, JSON::erase
@todo Implement JSON::reverse_iterator, JSON::const_reverse_iterator,
JSON::rbegin(), JSON::rend(), JSON::crbegin(), JSON::crend()?
@todo Implement json::swap()
@todo Implement json::insert(), json::emplace(), json::emplace_back, json::erase
@todo Implement json::reverse_iterator, json::const_reverse_iterator,
json::rbegin(), json::rend(), json::crbegin(), json::crend()?
*/
class JSON
class json
{
// forward declaration to friend this class
public:
@ -64,9 +64,9 @@ class JSON
};
/// a type for an object
using object_t = std::map<std::string, JSON>;
using object_t = std::map<std::string, json>;
/// a type for an array
using array_t = std::vector<JSON>;
using array_t = std::vector<json>;
/// a type for a string
using string_t = std::string;
/// a type for a Boolean
@ -76,7 +76,7 @@ class JSON
/// a type for a floating point number
using number_float_t = double;
/// a type for list initialization
using list_init_t = std::initializer_list<JSON>;
using list_init_t = std::initializer_list<json>;
/// a JSON value
union value
@ -112,49 +112,49 @@ class JSON
public:
/// create an object according to given type
JSON(const value_type) noexcept;
json(const value_type) noexcept;
/// create a null object
JSON() = default;
json() = default;
/// create a null object
JSON(std::nullptr_t) noexcept;
json(std::nullptr_t) noexcept;
/// create a string object from a C++ string
JSON(const std::string&) noexcept;
json(const std::string&) noexcept;
/// create a string object from a C++ string (move)
JSON(std::string&&) noexcept;
json(std::string&&) noexcept;
/// create a string object from a C string
JSON(const char*) noexcept;
json(const char*) noexcept;
/// create a Boolean object
JSON(const bool) noexcept;
json(const bool) noexcept;
/// create a number object
JSON(const int) noexcept;
json(const int) noexcept;
/// create a number object
JSON(const double) noexcept;
json(const double) noexcept;
/// create an array
JSON(const array_t&) noexcept;
json(const array_t&) noexcept;
/// create an array (move)
JSON(array_t&&) noexcept;
json(array_t&&) noexcept;
/// create an object
JSON(const object_t&) noexcept;
json(const object_t&) noexcept;
/// create an object (move)
JSON(object_t&&) noexcept;
json(object_t&&) noexcept;
/// create from an initializer list (to an array or object)
JSON(list_init_t) noexcept;
json(list_init_t) noexcept;
/// copy constructor
JSON(const JSON&) noexcept;
json(const json&) noexcept;
/// move constructor
JSON(JSON&&) noexcept;
json(json&&) noexcept;
/// copy assignment
JSON& operator=(JSON) noexcept;
json& operator=(json) noexcept;
/// destructor
~JSON() noexcept;
~json() noexcept;
/// create from string representation
static JSON parse(const std::string&);
static json parse(const std::string&);
/// create from string representation
static JSON parse(const char*);
static json parse(const char*);
private:
/// return the type as string
@ -179,28 +179,28 @@ class JSON
operator object_t() const;
/// write to stream
friend std::ostream& operator<<(std::ostream& o, const JSON& j)
friend std::ostream& operator<<(std::ostream& o, const json& j)
{
o << j.toString();
return o;
}
/// write to stream
friend std::ostream& operator>>(const JSON& j, std::ostream& o)
friend std::ostream& operator>>(const json& j, std::ostream& o)
{
o << j.toString();
return o;
}
/// read from stream
friend std::istream& operator>>(std::istream& i, JSON& j)
friend std::istream& operator>>(std::istream& i, json& j)
{
j = Parser(i).parse();
j = parser(i).parse();
return i;
}
/// read from stream
friend std::istream& operator<<(JSON& j, std::istream& i)
friend std::istream& operator<<(json& j, std::istream& i)
{
j = Parser(i).parse();
j = parser(i).parse();
return i;
}
@ -208,29 +208,29 @@ class JSON
const std::string toString() const noexcept;
/// add an object/array to an array
JSON& operator+=(const JSON&);
json& operator+=(const json&);
/// add a string to an array
JSON& operator+=(const std::string&);
json& operator+=(const std::string&);
/// add a null object to an array
JSON& operator+=(const std::nullptr_t);
json& operator+=(const std::nullptr_t);
/// add a string to an array
JSON& operator+=(const char*);
json& operator+=(const char*);
/// add a Boolean to an array
JSON& operator+=(bool);
json& operator+=(bool);
/// add a number to an array
JSON& operator+=(int);
json& operator+=(int);
/// add a number to an array
JSON& operator+=(double);
json& operator+=(double);
/// add a pair to an object
JSON& operator+=(const object_t::value_type&);
json& operator+=(const object_t::value_type&);
/// add a list of elements to array or list of pairs to object
JSON& operator+=(list_init_t);
json& operator+=(list_init_t);
/// add an object/array to an array
void push_back(const JSON&);
void push_back(const json&);
/// add an object/array to an array (move)
void push_back(JSON&&);
void push_back(json&&);
/// add a string to an array
void push_back(const std::string&);
/// add a null object to an array
@ -250,28 +250,28 @@ class JSON
void push_back(list_init_t);
/// operator to set an element in an array
JSON& operator[](const int);
json& operator[](const int);
/// operator to get an element in an array
const JSON& operator[](const int) const;
const json& operator[](const int) const;
/// operator to get an element in an array
JSON& at(const int);
json& at(const int);
/// operator to get an element in an array
const JSON& at(const int) const;
const json& at(const int) const;
/// operator to set an element in an object
JSON& operator[](const std::string&);
json& operator[](const std::string&);
/// operator to set an element in an object
JSON& operator[](const char*);
json& operator[](const char*);
/// operator to get an element in an object
const JSON& operator[](const std::string&) const;
const json& operator[](const std::string&) const;
/// operator to set an element in an object
JSON& at(const std::string&);
json& at(const std::string&);
/// operator to set an element in an object
JSON& at(const char*);
json& at(const char*);
/// operator to get an element in an object
const JSON& at(const std::string&) const;
const json& at(const std::string&) const;
/// operator to get an element in an object
const JSON& at(const char*) const;
const json& at(const char*) const;
/// return the number of stored values
size_t size() const noexcept;
@ -293,9 +293,9 @@ class JSON
const_iterator find(const char*) const;
/// lexicographically compares the values
bool operator==(const JSON&) const noexcept;
bool operator==(const json&) const noexcept;
/// lexicographically compares the values
bool operator!=(const JSON&) const noexcept;
bool operator!=(const json&) const noexcept;
/// returns an iterator to the beginning (array/object)
iterator begin() noexcept;
@ -325,11 +325,11 @@ class JSON
/// an iterator
class iterator
{
friend class JSON;
friend class JSON::const_iterator;
friend class json;
friend class json::const_iterator;
public:
iterator() = default;
iterator(JSON*);
iterator(json*);
iterator(const iterator&);
~iterator();
@ -337,17 +337,17 @@ class JSON
bool operator==(const iterator&) const;
bool operator!=(const iterator&) const;
iterator& operator++();
JSON& operator*() const;
JSON* operator->() const;
json& operator*() const;
json* operator->() const;
/// getter for the key (in case of objects)
std::string key() const;
/// getter for the value
JSON& value() const;
json& value() const;
private:
/// a JSON value
JSON* _object = nullptr;
json* _object = nullptr;
/// an iterator for JSON arrays
array_t::iterator* _vi = nullptr;
/// an iterator for JSON objects
@ -357,11 +357,11 @@ class JSON
/// a const iterator
class const_iterator
{
friend class JSON;
friend class json;
public:
const_iterator() = default;
const_iterator(const JSON*);
const_iterator(const json*);
const_iterator(const const_iterator&);
const_iterator(const iterator&);
~const_iterator();
@ -370,17 +370,17 @@ class JSON
bool operator==(const const_iterator&) const;
bool operator!=(const const_iterator&) const;
const_iterator& operator++();
const JSON& operator*() const;
const JSON* operator->() const;
const json& operator*() const;
const json* operator->() const;
/// getter for the key (in case of objects)
std::string key() const;
/// getter for the value
const JSON& value() const;
const json& value() const;
private:
/// a JSON value
const JSON* _object = nullptr;
const json* _object = nullptr;
/// an iterator for JSON arrays
array_t::const_iterator* _vi = nullptr;
/// an iterator for JSON objects
@ -389,25 +389,25 @@ class JSON
private:
/// a helper class to parse a JSON object
class Parser
class parser
{
public:
/// a parser reading from a C string
Parser(const char*);
parser(const char*);
/// a parser reading from a C++ string
Parser(const std::string&);
parser(const std::string&);
/// a parser reading from an input stream
Parser(std::istream&);
parser(std::istream&);
/// destructor of the parser
~Parser() = default;
~parser() = default;
// no copy constructor
Parser(const Parser&) = delete;
parser(const parser&) = delete;
// no copy assignment
Parser& operator=(Parser) = delete;
parser& operator=(parser) = delete;
/// parse and return a JSON object
JSON parse();
json parse();
private:
/// read the next character, stripping whitespace
@ -436,4 +436,4 @@ class JSON
};
/// user-defined literal operator to create JSON objects from strings
JSON operator "" _json(const char*, size_t);
json operator "" _json(const char*, size_t);

File diff suppressed because it is too large Load Diff