Compare commits

..

98 Commits

Author SHA1 Message Date
nlohmann 4308c54cef deploy: a5f8e230ac 2026-06-16 20:06:58 +00:00
nlohmann 5c7c81d6fc deploy: d8ebaf61d7 2026-06-12 16:08:46 +00:00
nlohmann 0d8a3f1278 deploy: e054d4df94 2026-05-21 20:20:43 +00:00
nlohmann 74caa1b78d deploy: 58cfecf7f7 2026-05-18 18:38:21 +00:00
nlohmann 6fa04ddf18 deploy: cba5dc0ed8 2026-05-16 08:05:25 +00:00
nlohmann 96be658738 deploy: 85cb7ea9aa 2026-05-14 07:15:32 +00:00
nlohmann 02f17c1018 deploy: a0a4e7cc0b 2026-05-13 06:00:22 +00:00
nlohmann 9446cb8fa1 deploy: e0f8cdfb7f 2026-05-09 09:02:24 +00:00
nlohmann 3d26d62a39 deploy: 62f3b41b30 2026-05-08 06:21:40 +00:00
nlohmann c0aa9485a6 deploy: fd17b0889e 2026-05-08 05:56:16 +00:00
nlohmann cf7c116a75 deploy: a64fc21243 2026-05-08 04:59:00 +00:00
nlohmann fa76a96d90 deploy: 0b5010242c 2026-05-07 18:33:50 +00:00
nlohmann 74f72a3c59 deploy: 342ebab83e 2026-04-24 14:59:39 +00:00
nlohmann 7823c2ade7 deploy: 05646c0db6 2026-04-24 14:47:35 +00:00
nlohmann 0a7e3eb0e7 deploy: 9a737481ae 2026-03-28 20:06:27 +00:00
nlohmann 7cb41d25ca deploy: 75ade57e9c 2026-03-13 06:18:53 +00:00
nlohmann 1b402111d4 deploy: f534f4f75e 2026-03-04 20:53:19 +00:00
nlohmann 1ce3fa5bc3 deploy: 2a1a5a7b1a 2026-03-03 19:23:56 +00:00
nlohmann b14b932a24 deploy: 4b78c06368 2026-03-03 07:02:03 +00:00
nlohmann 9747b9a034 deploy: ae4c81858b 2026-01-27 16:09:20 +00:00
nlohmann d62f424acb deploy: deb4c4f69f 2026-01-22 16:59:56 +00:00
nlohmann 6b276104d1 deploy: 515d994acb 2026-01-01 19:04:24 +00:00
nlohmann bbee86a3d1 deploy: 8c7a7d474f 2025-12-19 06:40:15 +00:00
nlohmann 094c066118 deploy: 49026f7999 2025-11-12 18:53:58 +00:00
nlohmann de6545005b deploy: 11627521ae 2025-11-04 10:27:48 +00:00
nlohmann c25905a6df deploy: 9c38185167 2025-11-04 07:04:52 +00:00
nlohmann 0451d43da5 deploy: cda9c1e3bd 2025-10-30 09:44:34 +00:00
nlohmann d2014a5a79 deploy: cddd4b831e 2025-10-27 19:11:04 +00:00
nlohmann bc67341534 deploy: 29913ca760 2025-10-25 05:57:14 +00:00
nlohmann 3d161f7495 deploy: 80069a970f 2025-10-22 19:37:24 +00:00
nlohmann de508bc2d8 deploy: 0d69a72167 2025-10-22 09:19:23 +00:00
nlohmann 7a570ae601 deploy: 2566828ed5 2025-10-22 09:15:05 +00:00
nlohmann 8b3e448529 deploy: 3ed64e502a 2025-09-28 18:17:18 +00:00
nlohmann fab37adabc deploy: 44bee1b138 2025-09-16 06:08:09 +00:00
nlohmann 0b14a60e6f deploy: 60b39ce2d7 2025-09-15 18:20:33 +00:00
nlohmann 06c7d96cae deploy: 4bc4e37f4f 2025-08-28 14:22:00 +00:00
nlohmann c5b048860d deploy: 22db828de4 2025-08-22 16:23:15 +00:00
nlohmann 980085f8fb deploy: cb13999032 2025-08-20 20:51:24 +00:00
nlohmann 2edffa5d7b deploy: e00484f866 2025-08-03 09:22:07 +00:00
nlohmann 3f8f635b63 deploy: a3211ab3ca 2025-08-03 08:03:14 +00:00
nlohmann baa3082b26 deploy: efcf9efb4f 2025-08-02 19:07:01 +00:00
nlohmann 8ac879d12d deploy: c637a8b453 2025-07-31 21:00:07 +00:00
nlohmann 57e7092dfa deploy: b7f741165b 2025-06-29 20:03:31 +00:00
nlohmann 72e81f5b40 deploy: 5f77df4e22 2025-06-01 15:57:09 +00:00
nlohmann 5723019bbd deploy: 64ebc6d511 2025-06-01 14:12:24 +00:00
nlohmann 0b82731bef deploy: 68c25aec60 2025-05-27 18:56:49 +00:00
nlohmann cd01edbea6 deploy: ac0133ea89 2025-05-26 18:50:39 +00:00
nlohmann 90f3fa4749 deploy: 7421ac31a7 2025-05-22 10:54:49 +00:00
nlohmann c5cf12c0d5 deploy: 4b17f90f65 2025-05-22 06:02:49 +00:00
nlohmann 954dedad9d deploy: 410c96228c 2025-05-15 17:25:15 +00:00
nlohmann 71490250f6 deploy: 9110918cf8 2025-05-04 08:29:27 +00:00
nlohmann 5875ebf0ac deploy: 6b9199382b 2025-04-27 16:54:57 +00:00
nlohmann c789bb9b27 deploy: 85df7ed593 2025-04-23 11:15:39 +00:00
nlohmann e567fb9a17 deploy: 88c92e605c 2025-04-16 14:10:24 +00:00
nlohmann 0ad5544857 deploy: 4cca3b9cb2 2025-04-13 15:59:56 +00:00
nlohmann efa0173f9b deploy: 1705bfe914 2025-04-11 08:42:13 +00:00
nlohmann f3ded41873 deploy: 4424a0fcc1 2025-04-05 16:55:38 +00:00
nlohmann 404c03b9f0 deploy: 79587f896e 2025-04-03 05:41:31 +00:00
nlohmann b0d0937680 deploy: 9f40a7b454 2025-04-01 12:45:15 +00:00
nlohmann 1d305a9c1b deploy: d41ca94fa8 2025-04-01 12:43:11 +00:00
nlohmann 2e3e066195 deploy: b477d2b95e 2025-03-29 08:39:44 +00:00
nlohmann 3f8c22f631 deploy: b592b6f2d8 2025-03-19 07:30:31 +00:00
nlohmann a9409755c7 deploy: e2c95baef1 2025-03-18 15:21:28 +00:00
nlohmann 702a1c2a48 deploy: 5474b22277 2025-03-18 13:01:42 +00:00
nlohmann a0711a40c0 deploy: 34665ae648 2025-03-06 10:55:08 +00:00
nlohmann e41981a751 deploy: 8215dbafbd 2025-02-21 07:33:23 +00:00
nlohmann bd06f48825 deploy: a43350c4e5 2025-02-15 11:26:55 +00:00
nlohmann cb1747470b deploy: 8fb5d6f92d 2025-02-15 11:24:15 +00:00
nlohmann 29e9ba0511 deploy: 2dc82053dc 2025-02-15 11:20:30 +00:00
nlohmann de895b1d1a deploy: e90c860d53 2025-01-26 17:41:56 +00:00
nlohmann b0759e09b1 deploy: b23cdeac2c 2025-01-20 18:31:56 +00:00
nlohmann 32cf7af82a deploy: d23291ba26 2025-01-19 09:46:40 +00:00
nlohmann 6c9075c692 deploy: 0f9e6ae098 2025-01-18 22:21:46 +00:00
nlohmann 6ff5604324 deploy: 8a882f32ed 2025-01-18 22:03:48 +00:00
nlohmann 9cd1c63d00 deploy: 26cfec34be 2025-01-17 05:54:35 +00:00
nlohmann 4ce072a05e deploy: 2d42229f4d 2025-01-10 14:14:16 +00:00
nlohmann a40ca752f0 deploy: 48e7b4c23b 2025-01-07 21:11:33 +00:00
nlohmann c818701d98 deploy: 0cb1241d5e 2025-01-07 17:50:01 +00:00
nlohmann 1a2fb08d1c deploy: 2e50d5b2f3 2025-01-07 17:13:02 +00:00
nlohmann fb8527384c deploy: 60c48755e3 2025-01-03 21:20:32 +00:00
nlohmann a46ad99086 deploy: 6057b31df7 2024-12-29 16:08:49 +00:00
nlohmann 28c1cbfef9 deploy: 663058e7d1 2024-12-21 11:54:13 +00:00
nlohmann 1a095c93fb deploy: af4ad7915c 2024-12-20 10:32:18 +00:00
nlohmann fd94ec80c7 deploy: 58f5f25968 2024-12-18 21:56:22 +00:00
dependabot[bot] 9ed446a688 deploy: f4910b20ce 2024-12-17 09:56:00 +00:00
nlohmann ac48b5c374 deploy: f2a4ac0673 2024-12-17 08:49:25 +00:00
nlohmann e3f1e1d35b deploy: e6cafa573a 2024-12-11 07:32:31 +00:00
nlohmann 41cbe4b3ef deploy: e0a17f8475 2024-12-10 22:24:12 +00:00
nlohmann 364a48d9d6 deploy: 20ae363a71 2024-12-10 19:52:33 +00:00
nlohmann 836d436a40 deploy: f385fe28f2 2024-12-10 10:59:47 +00:00
nlohmann ab14172173 deploy: 981a4c39f4 2024-12-10 08:42:33 +00:00
Niels Lohmann b014c90f94 Deployed c883fb0f1 with MkDocs version: 1.5.3 2024-04-10 18:23:02 +02:00
Niels Lohmann a87fd638da Deployed df962081 with MkDocs version: 1.5.3 2023-11-28 22:39:22 +01:00
Niels Lohmann 344ced4012 Deployed 360ce457f with MkDocs version: 1.5.3 2023-11-26 14:01:20 +01:00
Niels Lohmann 1bda49b536 Deployed 35c0b3e54 with MkDocs version: 1.4.2 2023-10-02 21:02:30 +02:00
Niels Lohmann 5de3c82c9a Deployed 6d4b72d3c with MkDocs version: 1.4.2 2023-09-24 11:31:05 +02:00
Niels Lohmann 6a2266aff4 Deployed 6e36c721f with MkDocs version: 1.4.2 2023-09-23 17:28:29 +02:00
55f10f44a9 Deployed fe4b663 with MkDocs version: 1.4.2 2023-03-08 12:43:29 +00:00
1117 changed files with 34626 additions and 1223345 deletions
-7
View File
@@ -1,7 +0,0 @@
json_unit
json_benchmarks
working
html
me.nlohmann.json.docset
View File
-58
View File
@@ -1,58 +0,0 @@
language: cpp
sudo: false
# from http://stackoverflow.com/a/32127147/266378
matrix:
include:
- os: linux
compiler: gcc
addons:
apt:
sources: ['ubuntu-toolchain-r-test']
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
compiler: gcc
addons:
apt:
sources: ['ubuntu-toolchain-r-test']
packages: ['g++-5', 'valgrind']
env: COMPILER=g++-5
- os: linux
compiler: clang
addons:
apt:
sources: ['ubuntu-toolchain-r-test', 'llvm-toolchain-precise-3.6']
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
+1
View File
File diff suppressed because one or more lines are too long
-25
View File
@@ -1,25 +0,0 @@
cmake_minimum_required(VERSION 2.8)
project(json CXX)
add_executable(json_unit
src/json.hpp test/catch.hpp test/unit.cpp
)
if(MSVC)
set(CMAKE_CXX_FLAGS
"/EHsc"
)
STRING(REPLACE "/O2" "/Od" CMAKE_CXX_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE})
add_definitions(-D_SCL_SECURE_NO_WARNINGS)
else(MSVC)
set(CMAKE_CXX_FLAGS
"-std=c++11"
)
endif(MSVC)
include_directories(
src test
)
-78
View File
@@ -1,78 +0,0 @@
[![Issue Stats](http://issuestats.com/github/nlohmann/json/badge/pr?style=flat)](http://issuestats.com/github/nlohmann/json) [![Issue Stats](http://issuestats.com/github/nlohmann/json/badge/issue?style=flat)](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.
-22
View File
@@ -1,22 +0,0 @@
The library is licensed under the MIT License
<http://opensource.org/licenses/MIT>:
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
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:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
-61
View File
@@ -1,61 +0,0 @@
.PHONY: pretty clean
# used programs
RE2C = re2c
SED = sed
# main target
all: json_unit
# clean up
clean:
rm -f json_unit json_benchmarks
##########################################################################
# unit tests
##########################################################################
# additional flags
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 -Wdeprecated -Wfloat-equal
# build unit tests
json_unit: test/unit.cpp src/json.hpp test/catch.hpp
$(CXX) -std=c++11 $(CXXFLAGS) $(FLAGS) $(CPPFLAGS) -I src -I test $< $(LDFLAGS) -o $@
##########################################################################
# static analyzer
##########################################################################
# call cppcheck on the main header file
cppcheck:
cppcheck --enable=all --inconclusive --std=c++11 src/json.hpp
##########################################################################
# maintainer targets
##########################################################################
# create scanner with re2c
re2c: src/json.hpp.re2c
$(RE2C) -b -s -i --no-generation-date $< | $(SED) '1d' > src/json.hpp
# pretty printer
pretty:
astyle --style=allman --indent=spaces=4 --indent-modifiers \
--indent-switches --indent-preproc-block --indent-preproc-define \
--indent-col1-comments --pad-oper --pad-header --align-pointer=type \
--align-reference=type --add-brackets --convert-tabs --close-templates \
--lineend=linux --preserve-date --suffix=none \
src/json.hpp src/json.hpp.re2c test/unit.cpp benchmarks/benchmarks.cpp doc/examples/*.cpp
##########################################################################
# benchmarks
##########################################################################
# benchmarks
json_benchmarks: benchmarks/benchmarks.cpp benchmarks/benchpress.hpp benchmarks/cxxopts.hpp src/json.hpp
$(CXX) -std=c++11 $(CXXFLAGS) -O3 -flto -I src -I benchmarks $< $(LDFLAGS) -o $@
./json_benchmarks
-413
View File
@@ -1,413 +0,0 @@
![JSON for Modern C++](https://raw.githubusercontent.com/nlohmann/json/master/doc/json.gif)
[![Build Status](https://travis-ci.org/nlohmann/json.svg?branch=master)](https://travis-ci.org/nlohmann/json)
[![Build Status](https://ci.appveyor.com/api/projects/status/1acb366xfyg3qybk?svg=true)](https://ci.appveyor.com/project/nlohmann/json)
[![Coverage Status](https://img.shields.io/coveralls/nlohmann/json.svg)](https://coveralls.io/r/nlohmann/json)
[![Try online](https://img.shields.io/badge/try-online-blue.svg)](http://melpon.org/wandbox/permlink/WSW3gHHE4UcZ9K3G)
[![Documentation Status](https://img.shields.io/badge/docs-doxygen-blue.svg)](http://nlohmann.github.io/json)
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/nlohmann/json/master/LICENSE.MIT)
[![Github Releases](https://img.shields.io/github/release/nlohmann/json.svg)](https://github.com/nlohmann/json/releases)
[![Github Issues](https://img.shields.io/github/issues/nlohmann/json.svg)](http://github.com/nlohmann/json/issues)
## Design goals
There are myriads of [JSON](http://json.org) libraries out there, and each may even have its reason to exist. Our class had these design goals:
- **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 you know, what I mean.
- **Trivial integration**. Our whole code consists of a single header file `json.hpp`. That's it. No library, no subproject, no dependencies, no complex build system. 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.
Other aspects were not so important to us:
- **Memory efficiency**. Each JSON object has an overhead of one pointer (the maximal size of a union) and one enumeration element (1 byte). The default generalization uses the following C++ data types: `std::string` for strings, `int64_t` or `double` for numbers, `std::map` for objects, `std::vector` for arrays, and `bool` for Booleans. However, you can template the generalized class `basic_json` to your needs.
- **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
```cpp
#include "json.hpp"
// for convenience
using json = nlohmann::json;
```
to the files you want to use JSON objects. That's it. Do not forget to set the necessary switches to enable C++11 (e.g., `-std=c++11` for GCC and Clang).
## Supported compilers
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)
- 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.
Assume you want to create the JSON object
```json
{
"pi": 3.141,
"happy": true,
"name": "Niels",
"nothing": null,
"answer": {
"everything": 42
},
"list": [1, 0, 2],
"object": {
"currency": "USD",
"value": 42.99
}
}
```
With the JSON class, you could write:
```cpp
// create an empty structure (null)
json j;
// add a number that is stored as double (note the implicit conversion of j to an object)
j["pi"] = 3.141;
// add a Boolean that is stored as bool
j["happy"] = true;
// add a string that is stored as std::string
j["name"] = "Niels";
// add another null object by passing nullptr
j["nothing"] = nullptr;
// add an object inside the object
j["answer"]["everything"] = 42;
// add an array that is stored as std::vector (using an initializer list)
j["list"] = { 1, 0, 2 };
// add another object (using an initializer list of pairs)
j["object"] = { {"currency", "USD"}, {"value", 42.99} };
// instead, you could also write (which looks very similar to the JSON above)
json j2 = {
{"pi", 3.141},
{"happy", true},
{"name", "Niels"},
{"nothing", nullptr},
{"answer", {
{"everything", 42}
}},
{"list", {1, 0, 2}},
{"object", {
{"currency", "USD"},
{"value", 42.99}
}}
};
```
Note that in all these cases, you never need to "tell" the compiler which JSON value you want to use. If you want to be explicit or express some edge cases, the functions `json::array` and `json::object` will help:
```cpp
// a way to express the empty array []
json empty_array_explicit = json::array();
// ways to express the empty object {}
json empty_object_implicit = json({});
json empty_object_explicit = json::object();
// a way to express an _array_ of key/value pairs [["currency", "USD"], ["value", 42.99]]
json array_not_object = { json::array({"currency", "USD"}), json::array({"value", 42.99}) };
```
### Serialization / Deserialization
You can create an object (deserialization) by appending `_json` to a string literal:
```cpp
// create object from string literal
json j = "{ \"happy\": true, \"pi\": 3.141 }"_json;
// or even nicer (thanks http://isocpp.org/blog/2015/01/json-for-modern-cpp)
auto j2 = R"(
{
"happy": true,
"pi": 3.141
}
)"_json;
// or explicitly
auto j3 = json::parse("{ \"happy\": true, \"pi\": 3.141 }");
```
You can also get a string representation (serialize):
```cpp
// explicit conversion to string
std::string s = j.dump(); // {\"happy\":true,\"pi\":3.141}
// serialization with pretty printing
// pass in the amount of spaces to indent
std::cout << j.dump(4) << std::endl;
// {
// "happy": true,
// "pi": 3.141
// }
```
You can also use streams to serialize and deserialize:
```cpp
// deserialize from standard input
json j;
std::cin >> j;
// serialize to standard output
std::cout << j;
// the setw manipulator was overloaded to set the indentation for pretty printing
std::cout << std::setw(4) << j << std::endl;
```
These operators work for any subclasses of `std::istream` or `std::ostream`.
### STL-like access
We designed the JSON class to behave just like an STL container. In fact, it satisfies the [**ReversibleContainer**](http://en.cppreference.com/w/cpp/concept/ReversibleContainer) requirement.
```cpp
// create an array using push_back
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';
}
// range-based for
for (auto element : j) {
std::cout << element << '\n';
}
// getter/setter
const std::string tmp = j[0];
j[1] = 42;
bool foo = j.at(2);
// other stuff
j.size(); // 3 entries
j.empty(); // false
j.type(); // json::value_t::array
j.clear(); // the array is empty again
// convenience type checkers
j.is_null();
j.is_boolean();
j.is_number();
j.is_object();
j.is_array();
j.is_string();
// comparison
j == "[\"foo\", 1, true]"_json; // true
// create an object
json o;
o["foo"] = 23;
o["bar"] = false;
o["baz"] = 3.141;
// special iterator member functions for objects
for (json::iterator it = o.begin(); it != o.end(); ++it) {
std::cout << it.key() << " : " << it.value() << "\n";
}
// find an entry
if (o.find("foo") != o.end()) {
// there is an entry with key "foo"
}
// or simpler using count()
int foo_present = o.count("foo"); // 1
int fob_present = o.count("fob"); // 0
// delete an entry
o.erase("foo");
```
### Conversion from STL containers
Any sequence container (`std::array`, `std::vector`, `std::deque`, `std::forward_list`, `std::list`) whose values can be used to construct JSON types (e.g., integers, floating point numbers, Booleans, string types, or again STL containers described in this section) can be used to create a JSON array. The same holds for similar associative containers (`std::set`, `std::multiset`, `std::unordered_set`, `std::unordered_multiset`), but in these cases the order of the elements of the array depends how the elements are ordered in the respective STL container.
```cpp
std::vector<int> c_vector {1, 2, 3, 4};
json j_vec(c_vector);
// [1, 2, 3, 4]
std::deque<double> c_deque {1.2, 2.3, 3.4, 5.6};
json j_deque(c_deque);
// [1.2, 2.3, 3.4, 5.6]
std::list<bool> c_list {true, true, false, true};
json j_list(c_list);
// [true, true, false, true]
std::forward_list<int64_t> c_flist {12345678909876, 23456789098765, 34567890987654, 45678909876543};
json j_flist(c_flist);
// [12345678909876, 23456789098765, 34567890987654, 45678909876543]
std::array<unsigned long, 4> c_array {{1, 2, 3, 4}};
json j_array(c_array);
// [1, 2, 3, 4]
std::set<std::string> c_set {"one", "two", "three", "four", "one"};
json j_set(c_set); // only one entry for "one" is used
// ["four", "one", "three", "two"]
std::unordered_set<std::string> c_uset {"one", "two", "three", "four", "one"};
json j_uset(c_uset); // only one entry for "one" is used
// maybe ["two", "three", "four", "one"]
std::multiset<std::string> c_mset {"one", "two", "one", "four"};
json j_mset(c_mset); // only one entry for "one" is used
// maybe ["one", "two", "four"]
std::unordered_multiset<std::string> c_umset {"one", "two", "one", "four"};
json j_umset(c_umset); // both entries for "one" are used
// maybe ["one", "two", "one", "four"]
```
Likewise, any associative key-value containers (`std::map`, `std::multimap`, `std::unordered_map`, `std::unordered_multimap`) whose keys are can construct an `std::string` and whose values can be used to construct JSON types (see examples above) can be used to to create a JSON object. Note that in case of multimaps only one key is used in the JSON object and the value depends on the internal order of the STL container.
```cpp
std::map<std::string, int> c_map { {"one", 1}, {"two", 2}, {"three", 3} };
json j_map(c_map);
// {"one": 1, "two": 2, "three": 3}
std::unordered_map<const char*, double> c_umap { {"one", 1.2}, {"two", 2.3}, {"three", 3.4} };
json j_umap(c_umap);
// {"one": 1.2, "two": 2.3, "three": 3.4}
std::multimap<std::string, bool> c_mmap { {"one", true}, {"two", true}, {"three", false}, {"three", true} };
json j_mmap(c_mmap); // only one entry for key "three" is used
// maybe {"one": true, "two": true, "three": true}
std::unordered_multimap<std::string, bool> c_ummap { {"one", true}, {"two", true}, {"three", false}, {"three", true} };
json j_ummap(c_ummap); // only one entry for key "three" is used
// maybe {"one": true, "two": true, "three": true}
```
### Implicit conversions
The type of the JSON object is determined automatically by the expression to store. Likewise, the stored value is implicitly converted.
```cpp
/// strings
std::string s1 = "Hello, world!";
json js = s1;
std::string s2 = js;
// Booleans
bool b1 = true;
json jb = b1;
bool b2 = jb;
// numbers
int i = 42;
json jn = i;
double f = jn;
// etc.
```
You can also explicitly ask for the value:
```cpp
std::string vs = js.get<std::string>();
bool vb = jb.get<bool>();
int vi = jn.get<int>();
// etc.
```
## License
<img align="right" src="http://opensource.org/trademarks/opensource/OSI-Approved-License-100x137.png">
The class is licensed under the [MIT License](http://opensource.org/licenses/MIT):
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:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
## Thanks
I deeply appreciate the help of the following people.
- [Teemperor](https://github.com/Teemperor) implemented CMake support and lcov integration, realized escape and Unicode handling in the string parser, and fixed the JSON serialization.
- [elliotgoodrich](https://github.com/elliotgoodrich) fixed an issue with double deletion in the iterator classes.
- [kirkshoop](https://github.com/kirkshoop) made the iterators of the class composable to other libraries.
- [wancw](https://github.com/wanwc) fixed a bug that hindered the class to compile with Clang.
- Tomas Åblad found a bug in the iterator implementation.
- [Joshua C. Randall](https://github.com/jrandall) fixed a bug in the floating-point serialization.
- [Aaron Burghardt](https://github.com/aburgh) implemented code to parse streams incrementally. Furthermore, he greatly improved the parser class by allowing the definition of a filter function to discard undesired elements while parsing.
- [Daniel Kopeček](https://github.com/dkopecek) fixed a bug in the compilation with GCC 5.0.
- [Florian Weber](https://github.com/Florianjw) fixed a bug in and improved the performance of the comparison operators.
- [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 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 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
```sh
$ make
$ ./json_unit "*"
===============================================================================
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).
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+14
View File
@@ -0,0 +1,14 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Redirecting...</title>
<link rel="canonical" href="../../operator_gtgt/">
<script>var anchor=window.location.hash.substr(1);location.href="../../operator_gtgt/"+(anchor?"#"+anchor:"")</script>
<meta http-equiv="refresh" content="0; url=../../operator_gtgt/">
</head>
<body>
You're being redirected to a <a href="../../operator_gtgt/">new destination</a>.
</body>
</html>
File diff suppressed because one or more lines are too long
@@ -0,0 +1,14 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Redirecting...</title>
<link rel="canonical" href="../../operator_literal_json/">
<script>var anchor=window.location.hash.substr(1);location.href="../../operator_literal_json/"+(anchor?"#"+anchor:"")</script>
<meta http-equiv="refresh" content="0; url=../../operator_literal_json/">
</head>
<body>
You're being redirected to a <a href="../../operator_literal_json/">new destination</a>.
</body>
</html>
@@ -0,0 +1,14 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Redirecting...</title>
<link rel="canonical" href="../../operator_literal_json_pointer/">
<script>var anchor=window.location.hash.substr(1);location.href="../../operator_literal_json_pointer/"+(anchor?"#"+anchor:"")</script>
<meta http-equiv="refresh" content="0; url=../../operator_literal_json_pointer/">
</head>
<body>
You're being redirected to a <a href="../../operator_literal_json_pointer/">new destination</a>.
</body>
</html>
File diff suppressed because one or more lines are too long
+14
View File
@@ -0,0 +1,14 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Redirecting...</title>
<link rel="canonical" href="../../operator_ltlt/">
<script>var anchor=window.location.hash.substr(1);location.href="../../operator_ltlt/"+(anchor?"#"+anchor:"")</script>
<meta http-equiv="refresh" content="0; url=../../operator_ltlt/">
</head>
<body>
You're being redirected to a <a href="../../operator_ltlt/">new destination</a>.
</body>
</html>
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long

Some files were not shown because too many files have changed in this diff Show More