Commit f63ff772 authored by Niels's avatar Niels

renamed class from "JSON" to "son"

parent 4d00105e
......@@ -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
......
......@@ -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.
......
#include "JSON.h"
#include "json.h"
int main()
{
JSON j;
json j;
j << std::cin;
return 0;
}
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])
......
......@@ -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
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
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