This commit is contained in:
dariomt 2016-01-18 10:08:58 +01:00
commit e1645a09e9
23 changed files with 6245 additions and 2746 deletions

View file

@ -13,6 +13,12 @@ matrix:
packages: ['g++-4.9', 'valgrind', 'python-pip', 'python-yaml']
before_script:
- pip install --user git+git://github.com/eddyxu/cpp-coveralls.git
after_success:
- make clean
- touch src/json.hpp
- make json_unit CXXFLAGS="-fprofile-arcs -ftest-coverage -std=c++11 -lstdc++" CXX=$COMPILER
- ./json_unit "*"
- coveralls --exclude test/catch.hpp --exclude test/unit.cpp --include src/json.hpp --gcov-options '\-lp' --gcov 'gcov-4.9'
env: COMPILER=g++-4.9
- os: linux
@ -31,14 +37,22 @@ matrix:
packages: ['clang-3.6', 'valgrind']
env: COMPILER=clang++-3.6
- os: linux
compiler: clang
addons:
apt:
sources: ['ubuntu-toolchain-r-test', 'llvm-toolchain-precise-3.7']
packages: ['clang-3.7', 'valgrind']
env: COMPILER=clang++-3.7
# - os: osx
# compiler: clang
# env: COMPILER=clang
# before_install:
# - brew update
# - brew install valgrind
script:
- make CXX=$COMPILER CXXFLAGS="-lstdc++"
- ./json_unit "*"
- valgrind --error-exitcode=1 --leak-check=full ./json_unit
after_success:
- if [ "$COMPILER" = "g++-4.9" ]; then make clean ; fi
- if [ "$COMPILER" = "g++-4.9" ]; then touch src/json.hpp ; fi
- if [ "$COMPILER" = "g++-4.9" ]; then make json_unit CXXFLAGS="-fprofile-arcs -ftest-coverage -std=c++11 -lstdc++" CXX=$COMPILER ; fi
- if [ "$COMPILER" = "g++-4.9" ]; then ./json_unit "*" ; fi
- if [ "$COMPILER" = "g++-4.9" ]; then coveralls --exclude test/catch.hpp --exclude test/unit.cpp --include src/json.hpp --gcov-options '\-lp' --gcov 'gcov-4.9' ; fi

View file

@ -16,7 +16,7 @@ if(MSVC)
add_definitions(-D_SCL_SECURE_NO_WARNINGS)
else(MSVC)
set(CMAKE_CXX_FLAGS
"-std=c++11 -stdlib=libc++"
"-std=c++11"
)
endif(MSVC)

69
CONTRIBUTING.md Normal file
View file

@ -0,0 +1,69 @@
# How to contribute
This project started as a little excuse to exercise some of the cool new C++11 features. Over time, people actually started to use the JSON library (yey!) and started to help improve it by proposing features, finding bugs, or even fixing my mistakes. I am really [thankful](https://github.com/nlohmann/json/blob/master/README.md#thanks) for this and try to keep track of all the helpers.
To make it as easy as possible for you to contribute and for me to keep an overview, here are a few guidelines which should help us avoid all kinds of unnecessary work or disappointment. And of course, this document is subject to discussion, so please [create an issue](https://github.com/nlohmann/json/issues/new) or a pull request if you find a way to improve it!
## Prerequisites
Please [create an issue](https://github.com/nlohmann/json/issues/new), assuming one does not already exist, and describe your concern. Note you need a [GitHub account](https://github.com/signup/free) for this.
If you want propose changes to the code, you need to download the [`re2c`](http://re2c.org) tool.
## Describe your issue
Clearly describe the issue:
- If it is a bug, please describe how to **reproduce** it. If possible, attach a complete example which demonstrates the error. Please also state what you **expected** to happen instead of the error.
- If you propose a change or addition, try to give an **example** how the improved code could look like or how to use it.
- If you found a compilation error, please tell us which **compiler** (version and operating system) you used and paste the (relevant part of) the error messages to the ticket.
## Files to change
There are currently two files which need to be edited:
1. [`src/json.hpp.re2c`](https://github.com/nlohmann/json/blob/master/src/json.hpp.re2c) (note the `.re2c` suffix) - This file contains a comment section which describes the JSON lexic. This section is translated by [`re2c`](http://re2c.org) into file [`src/json.hpp`](https://github.com/nlohmann/json/blob/master/src/json.hpp) which is plain "vanilla" C++11 code. (In fact, the generated lexer consists of some hundred lines of `goto`s, which is a hint you never want to edit this file...).
If you only edit file `src/json.hpp` (without the `.re2c` suffix), your changes will be overwritten as soon as the lexer is touched again. To generate the `src/json.hpp` file which is actually used during compilation of the tests and all other code, please execute
```sh
make re2c
```
To run [`re2c`](http://re2c.org) and generate/overwrite file `src/json.hpp` with your changes in file `src/json.hpp.re2c`.
2. [`test/unit.cpp`](https://github.com/nlohmann/json/blob/master/test/unit.cpp) - This contains the [Catch](https://github.com/philsquared/Catch) unit tests which currently cover [100 %](https://coveralls.io/github/nlohmann/json) of the library's code.
If you add or change a feature, please also add a unit test to this file. The unit tests can be compiled with
```sh
make
```
and can be executed with
```sh
./json_unit
```
The test cases are also executed with several different compilers on [Travis](https://travis-ci.org/nlohmann/json) once you open a pull request.
Please understand that I cannot accept pull requests changing only file `src/json.hpp`.
## Please don't
- Only make changes to file `src/json.hpp` -- please read the paragraph above and understand why `src/json.hpp.re2c` exists.
- The C++11 support varies between different **compilers** and versions. Please note the [list of supported compilers](https://github.com/nlohmann/json/blob/master/README.md#supported-compilers). Some compilers like GCC 4.8 (and earlier), Clang 3.3 (and earlier), or Microsoft Visual Studio 13.0 and earlier are known not to work due to missing or incomplete C++11 support. Please refrain from proposing changes that work around these compiler's limitations with `#ifdef`s or other means.
- Specifically, I am aware of compilation problems with **Microsoft Visual Studio** (there even is an [issue label](https://github.com/nlohmann/json/issues?utf8=✓&q=label%3A%22visual+studio%22+) for these kind of bugs). I understand that even in 2016, complete C++11 support isn't there yet. But please also understand that I do not want to drop features or uglify the code just to make Microsoft's sub-standard compiler happy. The past has shown that there are ways to express the functionality such that the code compiles with the most recent MSVC - unfortunately, this is not the main objective of the project.
- Please refrain from proposing changes that would **break [JSON](http://json.org) conformance**. If you propose a conformant extension of JSON to be supported by the library, please motivate this extension.
## Wanted
The following areas really need contribution:
- Getting the code to compile without errors with the latest **Microsoft Visual Studio** version. I am not using Windows, so I cannot debug code with MSVC myself. There is a job on [AppVeyor](https://ci.appveyor.com/project/nlohmann/json) though.
- Extending the **continuous integration** beyond Linux running some versions of GCC and Clang on [Travis](https://travis-ci.org/nlohmann/json) and Microsoft Visual Studio on [AppVeyor](https://ci.appveyor.com/project/nlohmann/json). We have found a lot of bugs just because several compilers behave in a slightly different manner.
- Improving the efficiency of the **JSON parser**. The current parser is implemented as a naive recursive descent parser with hand coded string handling. More sophisticated approaches like LALR parsers would be really appreciated. That said, parser generators like Bison or ANTLR do not play nice with single-header files -- I really would like to keep the parser inside the `json.hpp` header, and I am not aware of approaches similar to [`re2c`](http://re2c.org) for parsing.
- Extending and updating existing **benchmarks** to include (the most recent version of) this library. Though efficiency is not everything, speed and memory consumption are very important characteristics for C++ developers, so having proper comparisons would be interesting.

View file

@ -1,7 +1,7 @@
The library is licensed under the MIT License
<http://opensource.org/licenses/MIT>:
Copyright (c) 2013-2015 Niels Lohmann
Copyright (c) 2013-2016 Niels Lohmann
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in

View file

@ -40,14 +40,17 @@ to the files you want to use JSON objects. That's it. Do not forget to set the n
## Supported compilers
Though it's 2015 already, the support for C++11 is still a bit sparse. Currently, the following compilers are known to work:
Though it's 2016 already, the support for C++11 is still a bit sparse. Currently, the following compilers are known to work:
- GCC 4.8 - 5.2
- Clang 3.4 - 3.7
- Microsoft Visual C++ 14.0 RC
- GCC 4.9 - 6.0 (and possibly later)
- Clang 3.4 - 3.8 (and possibly later)
- Microsoft Visual C++ 14.0 RC (and possibly later)
I would be happy to learn about other compilers/versions.
For GCC running on MinGW or Android SDK, the error `'to_string' is not a member of 'std'` (or similarly, for `strtod`) may occur. Note this is not an issue with the code, but rather with the compiler itself. Please refer to [this site](http://tehsausage.com/mingw-to-string) and [this discussion](https://github.com/nlohmann/json/issues/136) for information on how to fix this bug.
## Examples
Here are some examples to give you an idea how to use the class.
@ -348,7 +351,7 @@ int vi = jn.get<int>();
The class is licensed under the [MIT License](http://opensource.org/licenses/MIT):
Copyright &copy; 2013-2015 [Niels Lohmann](http://nlohmann.me)
Copyright &copy; 2013-2016 [Niels Lohmann](http://nlohmann.me)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
@ -372,16 +375,26 @@ I deeply appreciate the help of the following people.
- [Eric Cornelius](https://github.com/EricMCornelius) pointed out a bug in the handling with NaN and infinity values. He also improved the performance of the string escaping.
- [易思龙](https://github.com/likebeta) implemented a conversion from anonymous enums.
- [kepkin](https://github.com/kepkin) patiently pushed forward the support for Microsoft Visual studio.
- [gregmarr](https://github.com/gregmarr) simplified the implementation of reverse iterators.
- [gregmarr](https://github.com/gregmarr) simplified the implementation of reverse iterators and helped with numerous hints and improvements.
- [Caio Luppi](https://github.com/caiovlp) fixed a bug in the Unicode handling.
- [dariomt](https://github.com/dariomt) fixed some typos in the examples.
- [Daniel Frey](https://github.com/d-frey) cleaned up some pointers and implemented exception-safe memory allocation.
- [Colin Hirsch](https://github.com/ColinH) took care of a small namespace issue.
- [Huu Nguyen](https://github.com/whoshuu) correct a variable name in the documentation.
- [Silverweed](https://github.com/silverweed) overloaded `parse()` to accept an rvalue reference.
- [dariomt](https://github.com/dariomt) fixed a subtlety in MSVC type support.
- [ZahlGraf](https://github.com/ZahlGraf) added a workaround that allows compilation using Android NDK.
- [whackashoe](https://github.com/whackashoe) replaced a function that was marked as unsafe by Visual Studio.
- [406345](https://github.com/406345) fixed two small warnings.
- [Glen Fernandes](https://github.com/glenfe) noted a potential portability problem in the `has_mapped_type` function.
- [Corbin Hughes](https://github.com/nibroc) fixed some typos in the contribution guidelines.
Thanks a lot for helping out!
## Notes
- The code contains numerous debug assertions which can be switched off by defining the preprocessor macro `NDEBUG`, see the [documentation of `assert`](http://en.cppreference.com/w/cpp/error/assert).
## Execute unit tests
To compile and run the tests, you need to execute
@ -391,7 +404,7 @@ $ make
$ ./json_unit "*"
===============================================================================
All tests passed (3341774 assertions in 27 test cases)
All tests passed (3343239 assertions in 28 test cases)
```
For more information, have a look at the file [.travis.yml](https://github.com/nlohmann/json/blob/master/.travis.yml).

View file

@ -1,5 +1,27 @@
# JSON for Modern C++
## Version 1.0.0
- Release date: 2015-12-28
- MD5: 331c30e4407ecdcf591e9e3ed85100d0
### Summary
This is the first official release. Compared to the [prerelease version 1.0.0-rc1](https://github.com/nlohmann/json/releases/tag/v1.0.0-rc1), only a few minor improvements have been made:
### Changes
- *Changed*: A **UTF-8 byte order mark** is silently ignored.
- *Changed*: `sprintf` is no longer used.
- *Changed*: `iterator_wrapper` also works for const objects; note: the name may change!
- *Changed*: **Error messages** during deserialization have been improved.
- *Added*: The `parse` function now also works with type `std::istream&&`.
- *Added*: Function `value(key, default_value)` returns either a copy of an object's element at the specified key or a given default value if no element with the key exists.
- *Added*: Public functions are tagged with the version they were introduced. This shall allow for better **versioning** in the future.
- *Added*: All public functions and types are **documented** (see http://nlohmann.github.io/json/) including executable examples.
- *Added*: Allocation of all types (in particular arrays, strings, and objects) is now exception-safe.
- *Added*: They descriptions of thrown exceptions have been overworked and are part of the tests suite and documentation.
## Version 1.0.0-rc1
- Release date: 2015-07-26

View file

@ -5,7 +5,7 @@
#---------------------------------------------------------------------------
DOXYFILE_ENCODING = UTF-8
PROJECT_NAME = "JSON for Modern C++"
PROJECT_NUMBER = 1.0.0-rc1
PROJECT_NUMBER = 1.0.0
PROJECT_BRIEF =
PROJECT_LOGO =
OUTPUT_DIRECTORY = .
@ -121,7 +121,7 @@ USE_MDFILE_AS_MAINPAGE =
#---------------------------------------------------------------------------
# Configuration options related to source browsing
#---------------------------------------------------------------------------
SOURCE_BROWSER = NO
SOURCE_BROWSER = YES
INLINE_SOURCES = NO
STRIP_CODE_COMMENTS = YES
REFERENCED_BY_RELATION = NO

View file

@ -26,8 +26,8 @@ int main()
{
object.at("the fast") = "il rapido";
}
catch (std::out_of_range)
catch (std::out_of_range& e)
{
std::cout << "out of range" << '\n';
std::cout << "out of range: " << e.what() << '\n';
}
}

View file

@ -1 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/syN4hQrhPvlUy5AG"><b>online</b></a>
<a target="_blank" href="http://melpon.org/wandbox/permlink/3ir8W1OZ5KtbRz6r"><b>online</b></a>

View file

@ -1,3 +1,3 @@
"il brutto"
{"the bad":"il cattivo","the good":"il buono","the ugly":"il brutto"}
out of range
out of range: key 'the fast' not found

View file

@ -21,8 +21,8 @@ int main()
{
array.at(5) = "sixth";
}
catch (std::out_of_range)
catch (std::out_of_range& e)
{
std::cout << "out of range" << '\n';
std::cout << "out of range: " << e.what() << '\n';
}
}

View file

@ -1 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/wKBBW3ORmTHPlgJV"><b>online</b></a>
<a target="_blank" href="http://melpon.org/wandbox/permlink/9Ae4DO4HJjULnq5j"><b>online</b></a>

View file

@ -1,3 +1,3 @@
"third"
["first","second","third","fourth"]
out of range
out of range: array index 5 is out of range

View file

@ -0,0 +1,29 @@
#include <json.hpp>
using namespace nlohmann;
int main()
{
// create a JSON object with different entry types
json j =
{
{"integer", 1},
{"floating", 42.23},
{"string", "hello world"},
{"boolean", true},
{"object", {{"key1", 1}, {"key2", 2}}},
{"array", {1, 2, 3}}
};
// access existing values
int v_integer = j.value("integer", 0);
double v_floating = j.value("floating", 47.11);
// access nonexisting values and rely on default value
std::string v_string = j.value("nonexisting", "oops");
bool v_boolean = j.value("nonexisting", false);
// output values
std::cout << std::boolalpha << v_integer << " " << v_floating
<< " " << v_string << " " << v_boolean << "\n";
}

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/pehnjdIduvv90qfX"><b>online</b></a>

View file

@ -0,0 +1 @@
1 42.23 oops false

View file

@ -5,7 +5,7 @@ using namespace nlohmann;
int main()
{
// create a JSON object
json object =
const json object =
{
{"one", 1}, {"two", 2}, {"three", 2.9}
};

View file

@ -1 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/9X5Q8p7DlWA6v0Ey"><b>online</b></a>
<a target="_blank" href="http://melpon.org/wandbox/permlink/4WLxv7id8P64Q1KI"><b>online</b></a>

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,3 @@
{
"foo": true
}

File diff suppressed because it is too large Load diff