mirror of
https://github.com/nlohmann/json.git
synced 2026-02-18 01:16:25 +00:00
Compare commits
64 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2b13711009 | ||
|
|
c09d03fba7 | ||
|
|
31093694ce | ||
|
|
aee99b362d | ||
|
|
54a4139157 | ||
|
|
ad5d1dabb2 | ||
|
|
81b7079255 | ||
|
|
19918b948d | ||
|
|
9584a99a43 | ||
|
|
421affd06e | ||
|
|
4d6985d4e2 | ||
|
|
dfb01c659b | ||
|
|
3245a0dc06 | ||
|
|
8b6e0dc02d | ||
|
|
d7d1e55648 | ||
|
|
8a0490a011 | ||
|
|
e00b00fea8 | ||
|
|
600ad330c5 | ||
|
|
21a44da89d | ||
|
|
f79d52b973 | ||
|
|
c1f5f0451d | ||
|
|
bd8db5d40e | ||
|
|
a1c6f16bd3 | ||
|
|
3a1403409f | ||
|
|
bd0f3001c6 | ||
|
|
7e3245786c | ||
|
|
9de14a4861 | ||
|
|
f854410542 | ||
|
|
5c4624c351 | ||
|
|
a04bd4f851 | ||
|
|
663ad13fc3 | ||
|
|
db3c7fdb96 | ||
|
|
e2f0e8b945 | ||
|
|
4d079d135e | ||
|
|
e1645a09e9 | ||
|
|
72e33eec1a | ||
|
|
8968885c61 | ||
|
|
8ecccf8628 | ||
|
|
c0bb5a56eb | ||
|
|
a05f27867b | ||
|
|
832fe49bc1 | ||
|
|
57e018483a | ||
|
|
1741e58eb5 | ||
|
|
4511145d3d | ||
|
|
151d43f63e | ||
|
|
4a452f11f9 | ||
|
|
d27c8a8ea8 | ||
|
|
fb7ff414ad | ||
|
|
2d48a36f5c | ||
|
|
b4bb3afb65 | ||
|
|
4a87ed4441 | ||
|
|
33abe87407 | ||
|
|
dbf58ae9c0 | ||
|
|
7614aa31c0 | ||
|
|
9b07504e23 | ||
|
|
3905406f9c | ||
|
|
aca6a3b373 | ||
|
|
cb5581392a | ||
|
|
4b8a822f33 | ||
|
|
95090a7ddd | ||
|
|
5198e1f2e4 | ||
|
|
bd2783f45c | ||
|
|
8f97e99feb | ||
|
|
8d53c5e06f |
28
.travis.yml
28
.travis.yml
@@ -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
|
||||
|
||||
78
CONTRIBUTING.md
Normal file
78
CONTRIBUTING.md
Normal file
@@ -0,0 +1,78 @@
|
||||
[](http://issuestats.com/github/nlohmann/json) [](http://issuestats.com/github/nlohmann/json)
|
||||
|
||||
# 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`.
|
||||
|
||||
|
||||
## Note
|
||||
|
||||
- If you open a pull request, the code will be automatically tested with [Valgrind](http://valgrind.org)'s Memcheck tool to detect memory leaks. Please be aware that the execution with Valgrind _may_ in rare cases yield different behavior than running the code directly. This can result in failing unit tests which run successfully without Valgrind.
|
||||
|
||||
## 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.
|
||||
- Please do not open pull requests that address **multiple issues**.
|
||||
|
||||
## 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.
|
||||
- Check the code with [**Coverity**](https://scan.coverity.com).
|
||||
- Make the code **locale-independent**. The library currently only works for a locale where `.` is the decimal point character.
|
||||
@@ -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
|
||||
|
||||
21
README.md
21
README.md
@@ -3,7 +3,7 @@
|
||||
[](https://travis-ci.org/nlohmann/json)
|
||||
[](https://ci.appveyor.com/project/nlohmann/json)
|
||||
[](https://coveralls.io/r/nlohmann/json)
|
||||
[](http://melpon.org/wandbox/permlink/GnGKwji06WeVonlI)
|
||||
[](http://melpon.org/wandbox/permlink/WSW3gHHE4UcZ9K3G)
|
||||
[](http://nlohmann.github.io/json)
|
||||
[](https://raw.githubusercontent.com/nlohmann/json/master/LICENSE.MIT)
|
||||
[](https://github.com/nlohmann/json/releases)
|
||||
@@ -25,6 +25,8 @@ Other aspects were not so important to us:
|
||||
|
||||
- **Speed**. We currently implement the parser as naive [recursive descent parser](http://en.wikipedia.org/wiki/Recursive_descent_parser) with hand coded string handling. It is fast enough, but a [LALR-parser](http://en.wikipedia.org/wiki/LALR_parser) with a decent regular expression processor should be even faster (but would consist of more files which makes the integration harder).
|
||||
|
||||
See the [https://github.com/nlohmann/json/blob/master/CONTRIBUTING.md#please-dont](contribution guidelines) for more information.
|
||||
|
||||
## Integration
|
||||
|
||||
The single required source, file `json.hpp` is in the `src` directory or [released here](https://github.com/nlohmann/json/releases). All you need to do is add
|
||||
@@ -40,7 +42,7 @@ 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.9 - 6.0 (and possibly later)
|
||||
- Clang 3.4 - 3.8 (and possibly later)
|
||||
@@ -351,7 +353,7 @@ int vi = jn.get<int>();
|
||||
|
||||
The class is licensed under the [MIT License](http://opensource.org/licenses/MIT):
|
||||
|
||||
Copyright © 2013-2015 [Niels Lohmann](http://nlohmann.me)
|
||||
Copyright © 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:
|
||||
|
||||
@@ -375,20 +377,27 @@ 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.
|
||||
- [dariomt](https://github.com/dariomt) fixed a subtlety in MSVC type support and implemented the `get_ref()` function to get a reference to stored values.
|
||||
- [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.
|
||||
- [twelsby](https://github.com/twelsby) fixed the array subscript operator, an issue that failed the MSVC build, and floating-point parsing/dumping.
|
||||
|
||||
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
|
||||
@@ -398,7 +407,7 @@ $ make
|
||||
$ ./json_unit "*"
|
||||
|
||||
===============================================================================
|
||||
All tests passed (3343239 assertions in 28 test cases)
|
||||
All tests passed (3343329 assertions in 29 test cases)
|
||||
```
|
||||
|
||||
For more information, have a look at the file [.travis.yml](https://github.com/nlohmann/json/blob/master/.travis.yml).
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
#---------------------------------------------------------------------------
|
||||
DOXYFILE_ENCODING = UTF-8
|
||||
PROJECT_NAME = "JSON for Modern C++"
|
||||
PROJECT_NUMBER = 1.0.0-rc1
|
||||
PROJECT_NUMBER = 1.1.0
|
||||
PROJECT_BRIEF =
|
||||
PROJECT_LOGO =
|
||||
OUTPUT_DIRECTORY = .
|
||||
|
||||
@@ -1 +1 @@
|
||||
<a target="_blank" href="http://melpon.org/wandbox/permlink/GnGKwji06WeVonlI"><b>online</b></a>
|
||||
<a target="_blank" href="http://melpon.org/wandbox/permlink/WSW3gHHE4UcZ9K3G"><b>online</b></a>
|
||||
@@ -1,7 +1,7 @@
|
||||
null
|
||||
false
|
||||
0
|
||||
0
|
||||
0.0
|
||||
{}
|
||||
[]
|
||||
""
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
null
|
||||
false
|
||||
0
|
||||
0
|
||||
0.0
|
||||
{}
|
||||
[]
|
||||
""
|
||||
|
||||
26
doc/examples/get_ref.cpp
Normal file
26
doc/examples/get_ref.cpp
Normal file
@@ -0,0 +1,26 @@
|
||||
#include <json.hpp>
|
||||
|
||||
using namespace nlohmann;
|
||||
|
||||
int main()
|
||||
{
|
||||
// create a JSON number
|
||||
json value = 17;
|
||||
|
||||
// explicitly getting references
|
||||
auto r1 = value.get_ref<const json::number_integer_t&>();
|
||||
auto r2 = value.get_ref<json::number_integer_t&>();
|
||||
|
||||
// print the values
|
||||
std::cout << r1 << ' ' << r2 << '\n';
|
||||
|
||||
// incompatible type throws exception
|
||||
try
|
||||
{
|
||||
auto r3 = value.get_ref<json::number_float_t&>();
|
||||
}
|
||||
catch (std::domain_error& ex)
|
||||
{
|
||||
std::cout << ex.what() << '\n';
|
||||
}
|
||||
}
|
||||
1
doc/examples/get_ref.link
Normal file
1
doc/examples/get_ref.link
Normal file
@@ -0,0 +1 @@
|
||||
<a target="_blank" href="http://melpon.org/wandbox/permlink/nBNBANVonG9HCQqQ"><b>online</b></a>
|
||||
2
doc/examples/get_ref.output
Normal file
2
doc/examples/get_ref.output
Normal file
@@ -0,0 +1,2 @@
|
||||
17 17
|
||||
incompatible ReferenceType for get_ref, actual type is number
|
||||
@@ -1,4 +1,4 @@
|
||||
[1,2,3] == [1,2,4] false
|
||||
{"A":"a","B":"b"} == {"A":"a","B":"b"} true
|
||||
17 == 17 true
|
||||
17 == 17.0 true
|
||||
"foo" == "bar" false
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
[1,2,3] == [1,2,4] true
|
||||
{"A":"a","B":"b"} == {"A":"a","B":"b"} false
|
||||
17 == 17 false
|
||||
17 == 17.0 false
|
||||
"foo" == "bar" true
|
||||
|
||||
765
src/json.hpp
765
src/json.hpp
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
294
test/unit.cpp
294
test/unit.cpp
@@ -25,6 +25,11 @@
|
||||
#include "json.hpp"
|
||||
using nlohmann::json;
|
||||
|
||||
// disable float-equal warnings on GCC/clang
|
||||
#if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__)
|
||||
#pragma GCC diagnostic ignored "-Wfloat-equal"
|
||||
#endif
|
||||
|
||||
TEST_CASE("constructors")
|
||||
{
|
||||
SECTION("create an empty value with a given type")
|
||||
@@ -2604,6 +2609,22 @@ TEST_CASE("pointer access")
|
||||
CHECK(value.get_ptr<json::number_float_t*>() == nullptr);
|
||||
}
|
||||
|
||||
SECTION("pointer access to const object_t")
|
||||
{
|
||||
using test_type = json::object_t;
|
||||
const json value = {{"one", 1}, {"two", 2}};
|
||||
|
||||
// this should not compile
|
||||
// test_type* p1 = value.get_ptr<test_type*>();
|
||||
|
||||
// check if pointers are returned correctly
|
||||
const test_type* p2 = value.get_ptr<const test_type*>();
|
||||
CHECK(*p2 == value.get<test_type>());
|
||||
|
||||
const test_type* const p3 = value.get_ptr<const test_type* const>();
|
||||
CHECK(p2 == p3);
|
||||
}
|
||||
|
||||
SECTION("pointer access to array_t")
|
||||
{
|
||||
using test_type = json::array_t;
|
||||
@@ -2740,6 +2761,176 @@ TEST_CASE("pointer access")
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("reference access")
|
||||
{
|
||||
// create a JSON value with different types
|
||||
json json_types =
|
||||
{
|
||||
{"boolean", true},
|
||||
{
|
||||
"number", {
|
||||
{"integer", 42},
|
||||
{"floating-point", 17.23}
|
||||
}
|
||||
},
|
||||
{"string", "Hello, world!"},
|
||||
{"array", {1, 2, 3, 4, 5}},
|
||||
{"null", nullptr}
|
||||
};
|
||||
|
||||
SECTION("reference access to object_t")
|
||||
{
|
||||
using test_type = json::object_t;
|
||||
json value = {{"one", 1}, {"two", 2}};
|
||||
|
||||
// check if references are returned correctly
|
||||
test_type& p1 = value.get_ref<test_type&>();
|
||||
CHECK(&p1 == value.get_ptr<test_type*>());
|
||||
CHECK(p1 == value.get<test_type>());
|
||||
|
||||
const test_type& p2 = value.get_ref<const test_type&>();
|
||||
CHECK(&p2 == value.get_ptr<const test_type*>());
|
||||
CHECK(p2 == value.get<test_type>());
|
||||
|
||||
// check if mismatching references throw correctly
|
||||
CHECK_NOTHROW(value.get_ref<json::object_t&>());
|
||||
CHECK_THROWS(value.get_ref<json::array_t&>());
|
||||
CHECK_THROWS(value.get_ref<json::string_t&>());
|
||||
CHECK_THROWS(value.get_ref<json::boolean_t&>());
|
||||
CHECK_THROWS(value.get_ref<json::number_integer_t&>());
|
||||
CHECK_THROWS(value.get_ref<json::number_float_t&>());
|
||||
}
|
||||
|
||||
SECTION("const reference access to const object_t")
|
||||
{
|
||||
using test_type = json::object_t;
|
||||
const json value = {{"one", 1}, {"two", 2}};
|
||||
|
||||
// this should not compile
|
||||
// test_type& p1 = value.get_ref<test_type&>();
|
||||
|
||||
// check if references are returned correctly
|
||||
const test_type& p2 = value.get_ref<const test_type&>();
|
||||
CHECK(&p2 == value.get_ptr<const test_type*>());
|
||||
CHECK(p2 == value.get<test_type>());
|
||||
}
|
||||
|
||||
SECTION("reference access to array_t")
|
||||
{
|
||||
using test_type = json::array_t;
|
||||
json value = {1, 2, 3, 4};
|
||||
|
||||
// check if references are returned correctly
|
||||
test_type& p1 = value.get_ref<test_type&>();
|
||||
CHECK(&p1 == value.get_ptr<test_type*>());
|
||||
CHECK(p1 == value.get<test_type>());
|
||||
|
||||
const test_type& p2 = value.get_ref<const test_type&>();
|
||||
CHECK(&p2 == value.get_ptr<const test_type*>());
|
||||
CHECK(p2 == value.get<test_type>());
|
||||
|
||||
// check if mismatching references throw correctly
|
||||
CHECK_THROWS(value.get_ref<json::object_t&>());
|
||||
CHECK_NOTHROW(value.get_ref<json::array_t&>());
|
||||
CHECK_THROWS(value.get_ref<json::string_t&>());
|
||||
CHECK_THROWS(value.get_ref<json::boolean_t&>());
|
||||
CHECK_THROWS(value.get_ref<json::number_integer_t&>());
|
||||
CHECK_THROWS(value.get_ref<json::number_float_t&>());
|
||||
}
|
||||
|
||||
SECTION("reference access to string_t")
|
||||
{
|
||||
using test_type = json::string_t;
|
||||
json value = "hello";
|
||||
|
||||
// check if references are returned correctly
|
||||
test_type& p1 = value.get_ref<test_type&>();
|
||||
CHECK(&p1 == value.get_ptr<test_type*>());
|
||||
CHECK(p1 == value.get<test_type>());
|
||||
|
||||
const test_type& p2 = value.get_ref<const test_type&>();
|
||||
CHECK(&p2 == value.get_ptr<const test_type*>());
|
||||
CHECK(p2 == value.get<test_type>());
|
||||
|
||||
// check if mismatching references throw correctly
|
||||
CHECK_THROWS(value.get_ref<json::object_t&>());
|
||||
CHECK_THROWS(value.get_ref<json::array_t&>());
|
||||
CHECK_NOTHROW(value.get_ref<json::string_t&>());
|
||||
CHECK_THROWS(value.get_ref<json::boolean_t&>());
|
||||
CHECK_THROWS(value.get_ref<json::number_integer_t&>());
|
||||
CHECK_THROWS(value.get_ref<json::number_float_t&>());
|
||||
}
|
||||
|
||||
SECTION("reference access to boolean_t")
|
||||
{
|
||||
using test_type = json::boolean_t;
|
||||
json value = false;
|
||||
|
||||
// check if references are returned correctly
|
||||
test_type& p1 = value.get_ref<test_type&>();
|
||||
CHECK(&p1 == value.get_ptr<test_type*>());
|
||||
CHECK(p1 == value.get<test_type>());
|
||||
|
||||
const test_type& p2 = value.get_ref<const test_type&>();
|
||||
CHECK(&p2 == value.get_ptr<const test_type*>());
|
||||
CHECK(p2 == value.get<test_type>());
|
||||
|
||||
// check if mismatching references throw correctly
|
||||
CHECK_THROWS(value.get_ref<json::object_t&>());
|
||||
CHECK_THROWS(value.get_ref<json::array_t&>());
|
||||
CHECK_THROWS(value.get_ref<json::string_t&>());
|
||||
CHECK_NOTHROW(value.get_ref<json::boolean_t&>());
|
||||
CHECK_THROWS(value.get_ref<json::number_integer_t&>());
|
||||
CHECK_THROWS(value.get_ref<json::number_float_t&>());
|
||||
}
|
||||
|
||||
SECTION("reference access to number_integer_t")
|
||||
{
|
||||
using test_type = json::number_integer_t;
|
||||
json value = 23;
|
||||
|
||||
// check if references are returned correctly
|
||||
test_type& p1 = value.get_ref<test_type&>();
|
||||
CHECK(&p1 == value.get_ptr<test_type*>());
|
||||
CHECK(p1 == value.get<test_type>());
|
||||
|
||||
const test_type& p2 = value.get_ref<const test_type&>();
|
||||
CHECK(&p2 == value.get_ptr<const test_type*>());
|
||||
CHECK(p2 == value.get<test_type>());
|
||||
|
||||
// check if mismatching references throw correctly
|
||||
CHECK_THROWS(value.get_ref<json::object_t&>());
|
||||
CHECK_THROWS(value.get_ref<json::array_t&>());
|
||||
CHECK_THROWS(value.get_ref<json::string_t&>());
|
||||
CHECK_THROWS(value.get_ref<json::boolean_t&>());
|
||||
CHECK_NOTHROW(value.get_ref<json::number_integer_t&>());
|
||||
CHECK_THROWS(value.get_ref<json::number_float_t&>());
|
||||
}
|
||||
|
||||
SECTION("reference access to number_float_t")
|
||||
{
|
||||
using test_type = json::number_float_t;
|
||||
json value = 42.23;
|
||||
|
||||
// check if references are returned correctly
|
||||
test_type& p1 = value.get_ref<test_type&>();
|
||||
CHECK(&p1 == value.get_ptr<test_type*>());
|
||||
CHECK(p1 == value.get<test_type>());
|
||||
|
||||
const test_type& p2 = value.get_ref<const test_type&>();
|
||||
CHECK(&p2 == value.get_ptr<const test_type*>());
|
||||
CHECK(p2 == value.get<test_type>());
|
||||
|
||||
// check if mismatching references throw correctly
|
||||
CHECK_THROWS(value.get_ref<json::object_t&>());
|
||||
CHECK_THROWS(value.get_ref<json::array_t&>());
|
||||
CHECK_THROWS(value.get_ref<json::string_t&>());
|
||||
CHECK_THROWS(value.get_ref<json::boolean_t&>());
|
||||
CHECK_THROWS(value.get_ref<json::number_integer_t&>());
|
||||
CHECK_NOTHROW(value.get_ref<json::number_float_t&>());
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("element access")
|
||||
{
|
||||
SECTION("array")
|
||||
@@ -10899,7 +11090,7 @@ TEST_CASE("compliance tests from nativejson-benchmark")
|
||||
//"test/json_roundtrip/roundtrip18.json",
|
||||
//"test/json_roundtrip/roundtrip19.json",
|
||||
//"test/json_roundtrip/roundtrip20.json",
|
||||
//"test/json_roundtrip/roundtrip21.json",
|
||||
"test/json_roundtrip/roundtrip21.json",
|
||||
"test/json_roundtrip/roundtrip22.json",
|
||||
"test/json_roundtrip/roundtrip23.json",
|
||||
//"test/json_roundtrip/roundtrip24.json",
|
||||
@@ -11216,7 +11407,8 @@ TEST_CASE("regression tests")
|
||||
SECTION("issue #89 - nonstandard integer type")
|
||||
{
|
||||
// create JSON class with nonstandard integer number type
|
||||
nlohmann::basic_json<std::map, std::vector, std::string, bool, int32_t, float> j;
|
||||
using custom_json = nlohmann::basic_json<std::map, std::vector, std::string, bool, int32_t, float>;
|
||||
custom_json j;
|
||||
j["int_1"] = 1;
|
||||
// we need to cast to int to compile with Catch - the value is int32_t
|
||||
CHECK(static_cast<int>(j["int_1"]) == 1);
|
||||
@@ -11314,8 +11506,104 @@ TEST_CASE("regression tests")
|
||||
CHECK(s2 == "value");
|
||||
}
|
||||
|
||||
SECTION("character following a surrogate pair is skipped")
|
||||
SECTION("issue #146 - character following a surrogate pair is skipped")
|
||||
{
|
||||
CHECK(json::parse("\"\\ud80c\\udc60abc\"").get<json::string_t>() == u8"\U00013060abc");
|
||||
}
|
||||
|
||||
SECTION("issue #171 - Cannot index by key of type static constexpr const char*")
|
||||
{
|
||||
json j;
|
||||
|
||||
// Non-const access with key as "char []"
|
||||
char array_key[] = "Key1";
|
||||
CHECK_NOTHROW(j[array_key] = 1);
|
||||
CHECK(j[array_key] == json(1));
|
||||
|
||||
// Non-const access with key as "const char[]"
|
||||
const char const_array_key[] = "Key2";
|
||||
CHECK_NOTHROW(j[const_array_key] = 2);
|
||||
CHECK(j[const_array_key] == json(2));
|
||||
|
||||
// Non-const access with key as "char *"
|
||||
char _ptr_key[] = "Key3";
|
||||
char* ptr_key = &_ptr_key[0];
|
||||
CHECK_NOTHROW(j[ptr_key] = 3);
|
||||
CHECK(j[ptr_key] == json(3));
|
||||
|
||||
// Non-const access with key as "const char *"
|
||||
const char* const_ptr_key = "Key4";
|
||||
CHECK_NOTHROW(j[const_ptr_key] = 4);
|
||||
CHECK(j[const_ptr_key] == json(4));
|
||||
|
||||
// Non-const access with key as "static constexpr const char *"
|
||||
static constexpr const char* constexpr_ptr_key = "Key5";
|
||||
CHECK_NOTHROW(j[constexpr_ptr_key] = 5);
|
||||
CHECK(j[constexpr_ptr_key] == json(5));
|
||||
|
||||
const json j_const = j;
|
||||
|
||||
// Const access with key as "char []"
|
||||
CHECK(j_const[array_key] == json(1));
|
||||
|
||||
// Const access with key as "const char[]"
|
||||
CHECK(j_const[const_array_key] == json(2));
|
||||
|
||||
//Const access with key as "char *"
|
||||
CHECK(j_const[ptr_key] == json(3));
|
||||
|
||||
// Const access with key as "const char *"
|
||||
CHECK(j_const[const_ptr_key] == json(4));
|
||||
|
||||
// Const access with key as "static constexpr const char *"
|
||||
CHECK(j_const[constexpr_ptr_key] == json(5));
|
||||
}
|
||||
|
||||
SECTION("issue #186 miloyip/nativejson-benchmark: floating-point parsing")
|
||||
{
|
||||
json j;
|
||||
|
||||
j = json::parse("-0.0");
|
||||
CHECK(j.get<double>() == -0.0);
|
||||
|
||||
j = json::parse("2.22507385850720113605740979670913197593481954635164564e-308");
|
||||
CHECK(j.get<double>() == 2.2250738585072009e-308);
|
||||
|
||||
j = json::parse("0.999999999999999944488848768742172978818416595458984374");
|
||||
CHECK(j.get<double>() == 0.99999999999999989);
|
||||
|
||||
// Test fails under GCC/clang due to strtod() error (may originate in libstdc++
|
||||
// but seems to have been fixed in the most current versions - just not on Travis)
|
||||
#if !defined(__clang__) && !defined(__GNUC__) && !defined(__GNUG__)
|
||||
j = json::parse("1.00000000000000011102230246251565404236316680908203126");
|
||||
CHECK(j.get<double>() == 1.00000000000000022);
|
||||
#endif
|
||||
|
||||
j = json::parse("7205759403792793199999e-5");
|
||||
CHECK(j.get<double>() == 72057594037927928.0);
|
||||
|
||||
j = json::parse("922337203685477529599999e-5");
|
||||
CHECK(j.get<double>() == 9223372036854774784.0);
|
||||
|
||||
j = json::parse("1014120480182583464902367222169599999e-5");
|
||||
CHECK(j.get<double>() == 10141204801825834086073718800384.0);
|
||||
|
||||
j = json::parse("5708990770823839207320493820740630171355185151999e-3");
|
||||
CHECK(j.get<double>() == 5708990770823838890407843763683279797179383808.0);
|
||||
|
||||
// create JSON class with nonstandard float number type
|
||||
|
||||
// float
|
||||
nlohmann::basic_json<std::map, std::vector, std::string, bool, int32_t, float> j_float = 1.23e25f;
|
||||
CHECK(j_float.get<float>() == 1.23e25f);
|
||||
|
||||
// double
|
||||
nlohmann::basic_json<std::map, std::vector, std::string, bool, int64_t, double> j_double = 1.23e45;
|
||||
CHECK(j_double.get<double>() == 1.23e45);
|
||||
|
||||
// long double
|
||||
nlohmann::basic_json<std::map, std::vector, std::string, bool, int64_t, long double> j_long_double =
|
||||
1.23e45L;
|
||||
CHECK(j_long_double.get<long double>() == 1.23e45L);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user