diff --git a/CMakeLists.txt b/CMakeLists.txt deleted file mode 100644 index 9e19f57f..00000000 --- a/CMakeLists.txt +++ /dev/null @@ -1,61 +0,0 @@ -cmake_minimum_required(VERSION 2.8.4) -project(json) - -# Enable C++11 and set flags for coverage testing -SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -g -O0 --coverage -fprofile-arcs -ftest-coverage") - -# Make everything public for testing purposes -add_definitions(-Dprivate=public) - -# If not specified, use Debug as build type (necessary for coverage testing) -if( NOT CMAKE_BUILD_TYPE ) - set( CMAKE_BUILD_TYPE Debug CACHE STRING - "" - FORCE ) -endif() - -# CMake addons for lcov -# Only possible with g++ at the moment. We run otherwise just the test -if(CMAKE_COMPILER_IS_GNUCXX) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O0 --coverage -fprofile-arcs -ftest-coverage") - set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/CMakeModules) - include(CodeCoverage) - - setup_coverage(coverage) -endif() - -# Normal sources -include_directories(src/) -aux_source_directory(src/ json_list) -add_library(json ${json_list}) - -# Testing -enable_testing() - -# Search all test files in the test directory with a .cc suffix -file(GLOB TEST_FILES "test/*.cc") -foreach(TEST_FILE ${TEST_FILES}) - # We use the basename to identify the test. E.g "json_unit" for "json_unit.cc" - get_filename_component(BASENAME ${TEST_FILE} NAME_WE) - # Create a test executable - add_executable(${BASENAME} ${TEST_FILE}) - # Link it with our main json file - target_link_libraries(${BASENAME} json) - - # Add test if people want to use ctest - add_test(${BASENAME} ${BASENAME}) - - # If we are using g++, we also need to setup the commands for coverage - # testing - if(CMAKE_COMPILER_IS_GNUCXX) - # Add a run_XXX target that runs the executable and produces the - # coverage data automatically - add_custom_target(run_${BASENAME} COMMAND ./${BASENAME}) - # Make sure that running requires the executable to be build - add_dependencies (run_${BASENAME} ${BASENAME}) - # To create a valid coverage report, the executable has to be - # executed first - add_dependencies (coverage run_${BASENAME}) - endif() -endforeach() - diff --git a/CMakeModules/CodeCoverage.cmake b/CMakeModules/CodeCoverage.cmake deleted file mode 100644 index 9d9c68dd..00000000 --- a/CMakeModules/CodeCoverage.cmake +++ /dev/null @@ -1,116 +0,0 @@ -# -# -# 2012-01-31, Lars Bilke -# - Enable Code Coverage -# -# 2013-09-17, Joakim Söderberg -# - Added support for Clang. -# - Some additional usage instructions. -# -# USAGE: - -# 0. (Mac only) If you use Xcode 5.1 make sure to patch geninfo as described here: -# http://stackoverflow.com/a/22404544/80480 -# -# 1. Copy this file into your cmake modules path. -# -# 2. Add the following line to your CMakeLists.txt: -# INCLUDE(CodeCoverage) -# -# 3. Set compiler flags to turn off optimization and enable coverage: -# SET(CMAKE_CXX_FLAGS "-g -O0 -fprofile-arcs -ftest-coverage") -# SET(CMAKE_C_FLAGS "-g -O0 -fprofile-arcs -ftest-coverage") -# -# 3. Use the function SETUP_TARGET_FOR_COVERAGE to create a custom make target -# which runs your test executable and produces a lcov code coverage report: -# Example: -# SETUP_TARGET_FOR_COVERAGE( -# my_coverage_target # Name for custom target. -# test_driver # Name of the test driver executable that runs the tests. -# # NOTE! This should always have a ZERO as exit code -# # otherwise the coverage generation will not complete. -# coverage # Name of output directory. -# ) -# -# 4. Build a Debug build: -# cmake -DCMAKE_BUILD_TYPE=Debug .. -# make -# make my_coverage_target -# -# - -# Check prereqs -FIND_PROGRAM( GCOV_PATH gcov ) -FIND_PROGRAM( LCOV_PATH lcov ) -FIND_PROGRAM( GENHTML_PATH genhtml ) -FIND_PROGRAM( GCOVR_PATH gcovr PATHS ${CMAKE_SOURCE_DIR}/tests) - -IF(NOT GCOV_PATH) - MESSAGE(FATAL_ERROR "gcov not found! Aborting...") -ENDIF() # NOT GCOV_PATH - -IF(NOT CMAKE_COMPILER_IS_GNUCXX) - # Clang version 3.0.0 and greater now supports gcov as well. - MESSAGE(WARNING "Compiler is not GNU gcc! Clang Version 3.0.0 and greater supports gcov as well, but older versions don't.") - - IF(NOT "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") - MESSAGE(FATAL_ERROR "Compiler is not GNU gcc! Aborting...") - ENDIF() -ENDIF() # NOT CMAKE_COMPILER_IS_GNUCXX - -SET(CMAKE_CXX_FLAGS_COVERAGE - "-g -O0 --coverage -fprofile-arcs -ftest-coverage" - CACHE STRING "Flags used by the C++ compiler during coverage builds." - FORCE ) -SET(CMAKE_C_FLAGS_COVERAGE - "-g -O0 --coverage -fprofile-arcs -ftest-coverage" - CACHE STRING "Flags used by the C compiler during coverage builds." - FORCE ) -SET(CMAKE_EXE_LINKER_FLAGS_COVERAGE - "" - CACHE STRING "Flags used for linking binaries during coverage builds." - FORCE ) -SET(CMAKE_SHARED_LINKER_FLAGS_COVERAGE - "" - CACHE STRING "Flags used by the shared libraries linker during coverage builds." - FORCE ) -MARK_AS_ADVANCED( - CMAKE_CXX_FLAGS_COVERAGE - CMAKE_C_FLAGS_COVERAGE - CMAKE_EXE_LINKER_FLAGS_COVERAGE - CMAKE_SHARED_LINKER_FLAGS_COVERAGE ) - -IF ( NOT (CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "Coverage")) - MESSAGE( WARNING "Code coverage results with an optimized (non-Debug) build may be misleading" ) -ENDIF() # NOT CMAKE_BUILD_TYPE STREQUAL "Debug" - - -# Param _outputname lcov output is generated as _outputname.info -# HTML report is generated in _outputname/index.html -FUNCTION(SETUP_COVERAGE _outputname) - - IF(NOT LCOV_PATH) - MESSAGE(FATAL_ERROR "lcov not found! Aborting...") - ENDIF() # NOT LCOV_PATH - - IF(NOT GENHTML_PATH) - MESSAGE(FATAL_ERROR "genhtml not found! Aborting...") - ENDIF() # NOT GENHTML_PATH - - # Setup target - ADD_CUSTOM_TARGET(coverage - - - # Capturing lcov counters and generating report - COMMAND ${LCOV_PATH} --rc lcov_branch_coverage=1 --directory . --capture --output-file ${_outputname}.info - COMMAND ${LCOV_PATH} --rc lcov_branch_coverage=1 --remove ${_outputname}.info 'tests/*' '/usr/*' --output-file ${_outputname}.info.cleaned - COMMAND ${GENHTML_PATH} --branch-coverage --rc lcov_branch_coverage=1 -o ${_outputname} ${_outputname}.info.cleaned - COMMAND ${CMAKE_COMMAND} -E remove ${_outputname}.info ${_outputname}.info.cleaned - - WORKING_DIRECTORY ${CMAKE_BINARY_DIR} - COMMENT "Resetting code coverage counters to zero.\nProcessing code coverage counters and generating report." - # Cleanup lcov - ${LCOV_PATH} --directory . --zerocounters - ) - -ENDFUNCTION() # SETUP_TARGET_FOR_COVERAGE diff --git a/Makefile.am b/Makefile.am index eda21026..d079b5b8 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,20 +1,13 @@ .PHONY: header_only -# the order is important: header before other sources -CORE_SOURCES = src/json.h src/json.cc - -noinst_PROGRAMS = json_unit json_parser +noinst_PROGRAMS = json_unit FLAGS = -Wall -Wextra -pedantic -Weffc++ -Wcast-align -Wcast-qual -Wctor-dtor-privacy -Wdisabled-optimization -Wformat=2 -Winit-self -Wmissing-declarations -Wmissing-include-dirs -Wold-style-cast -Woverloaded-virtual -Wredundant-decls -Wshadow -Wsign-conversion -Wsign-promo -Wstrict-overflow=5 -Wswitch -Wundef -Wno-unused -Wnon-virtual-dtor -Wreorder -json_unit_SOURCES = $(CORE_SOURCES) test/catch.hpp test/json_unit.cc +json_unit_SOURCES = $(CORE_SOURCES) test/catch.hpp test/unit.cpp src/json.hpp json_unit_CXXFLAGS = $(FLAGS) -std=c++11 json_unit_CPPFLAGS = -I$(top_srcdir)/src -I$(top_srcdir)/test -Dprivate=public -json_parser_SOURCES = $(CORE_SOURCES) benchmark/parse.cc -json_parser_CXXFLAGS = -O3 -std=c++11 -flto -json_parser_CPPFLAGS = -I$(top_srcdir)/src -I$(top_srcdir)/benchmark - cppcheck: cppcheck --enable=all --inconclusive --std=c++11 src/json.* @@ -29,13 +22,3 @@ pretty: --align-reference=type --add-brackets --convert-tabs --close-templates \ --lineend=linux --preserve-date --suffix=none \ $(SOURCES) - -parser: - make CXXFLAGS="" json_parser - -header_only/json.h: $(CORE_SOURCES) - $(AM_V_GEN) - $(AM_V_at)mkdir -p header_only - $(AM_V_at)cat $(CORE_SOURCES) | $(SED) 's/#include "json.h"//' > header_only/json.h - -BUILT_SOURCES = header_only/json.h diff --git a/README.md b/README.md deleted file mode 100644 index fec2466c..00000000 --- a/README.md +++ /dev/null @@ -1,383 +0,0 @@ -# JSON for Modern C++ - -*What if JSON was part of modern C++?* - -[](https://travis-ci.org/nlohmann/json) -[](https://coveralls.io/r/nlohmann/json) -[](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 the [reference](https://github.com/nlohmann/json/blob/master/doc/Reference.md), and you know, what I mean. - -- **Trivial integration**. Our whole code consists of a class in just two files: A header file `json.h` and a source file `json.cc`. That's it. No library, no subproject, no dependencies. The class is written in vanilla C++11. All in all, everything should require no adjustment of your compiler flags or project settings. - -- **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). We use 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. We know that there are more efficient ways to store the values, but we are happy enough right now. - -- **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). - -- **Rigorous standard compliance**. Any [compliant](http://json.org) JSON file can be read by our class, and any output of the class is standard-compliant. However, we do not check for some details in the format of numbers and strings. For instance, `-0` will be treated as `0` whereas the standard forbids this. Furthermore, we allow for more escape symbols in strings as the JSON specification. While this may not be a problem in reality, we are aware of it, but as long as we have a hand-written parser, we won't invest too much to be fully compliant. - -## Integration - -The two required source files are in the `src` directory. All you need to do is add - -```cpp -#include "json.h" - -// for convenience -using json = nlohmann::json; -``` - -to the files you want to use JSON objects. Furthermore, you need to compile the file `json.cc` and link it to your binaries. Do not forget to set the necessary switches to enable C++11 (e.g., `-std=c++11` for GCC and Clang). - -If you want a single header file, use the `json.h` file from the `header_only` directory. - -## 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 theses 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 -// ways to express the empty array [] -json empty_array_implicit = {{}}; -json empty_array_explicit = json::array(); - -// a way to express the empty object {} -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 -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; -j << std::cin; - -// serialize to standard output -std::cout << j; -``` - -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: - -```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 - -// comparison -j == "[\"foo\", 1, true]"_json; // true - -// create an object -json o; -o["foo"] = 23; -o["bar"] = false; -o["baz"] = 3.141; - -// find an entry -if (o.find("foo") != o.end()) { - // there is an entry with key "foo" -} - -// iterate the object -for (json::iterator it = o.begin(); it != o.end(); ++it) { - std::cout << it.key() << ':' << it.value() << '\n'; -} -``` - -### 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<float> 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*, float> 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 © 2013-2014 [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. - -Thanks a lot for helping out! - -## Execute unit tests with CMake - -To compile and run the tests, you need to execute - -```sh -$ cmake . -$ make -$ ctest -``` - -If you want to generate a coverage report with the lcov/genhtml tools, execute this instead: - -```sh -$ cmake . -$ make coverage -``` - -**Note: You need to use GCC for now as otherwise the target coverage doesn't exist!** - -The report is now in the subfolder coverage/index.html - -## Execute unit tests with automake - -To compile the unit tests, you need to execute - -```sh -$ autoreconf -i -$ ./configure -$ make -``` - -The unit tests can then be executed with - -```sh -$ ./json_unit - -=============================================================================== -All tests passed (887 assertions in 10 test cases) -``` - -For more information, have a look at the file [.travis.yml](https://github.com/nlohmann/json/blob/master/.travis.yml). diff --git a/benchmark/json_parser.py b/benchmark/json_parser.py deleted file mode 100755 index 4e97b2ca..00000000 --- a/benchmark/json_parser.py +++ /dev/null @@ -1,6 +0,0 @@ -#!/usr/bin/env python - -import sys -import json - -j = json.load(sys.stdin) diff --git a/benchmark/parse.cc b/benchmark/parse.cc deleted file mode 100644 index 6cbf7596..00000000 --- a/benchmark/parse.cc +++ /dev/null @@ -1,8 +0,0 @@ -#include "json.h" - -int main() -{ - nlohmann::json j; - j << std::cin; - return 0; -} diff --git a/configure.ac b/configure.ac index a41c85df..255281f3 100644 --- a/configure.ac +++ b/configure.ac @@ -1,11 +1,10 @@ -AC_INIT([JSON], [2.0], [mail@nlohmann.me]) -AC_CONFIG_SRCDIR([src/json.h]) +AC_INIT([JSON], [3.0], [mail@nlohmann.me]) +AC_CONFIG_SRCDIR([src/json.hpp]) AM_INIT_AUTOMAKE([foreign subdir-objects]) AM_SILENT_RULES([yes]) AC_PROG_CXX -AC_PROG_SED AC_CONFIG_FILES(Makefile) AC_OUTPUT diff --git a/doc/Doxyfile b/doc/Doxyfile deleted file mode 100644 index e8b7b6be..00000000 --- a/doc/Doxyfile +++ /dev/null @@ -1,2331 +0,0 @@ -# Doxyfile 1.8.8 - -# This file describes the settings to be used by the documentation system -# doxygen (www.doxygen.org) for a project. -# -# All text after a double hash (##) is considered a comment and is placed in -# front of the TAG it is preceding. -# -# All text after a single hash (#) is considered a comment and will be ignored. -# The format is: -# TAG = value [value, ...] -# For lists, items can also be appended using: -# TAG += value [value, ...] -# Values that contain spaces should be placed between quotes (\" \"). - -#--------------------------------------------------------------------------- -# Project related configuration options -#--------------------------------------------------------------------------- - -# This tag specifies the encoding used for all characters in the config file -# that follow. The default is UTF-8 which is also the encoding used for all text -# before the first occurrence of this tag. Doxygen uses libiconv (or the iconv -# built into libc) for the transcoding. See http://www.gnu.org/software/libiconv -# for the list of possible encodings. -# The default value is: UTF-8. - -DOXYFILE_ENCODING = UTF-8 - -# The PROJECT_NAME tag is a single word (or a sequence of words surrounded by -# double-quotes, unless you are using Doxywizard) that should identify the -# project for which the documentation is generated. This name is used in the -# title of most generated pages and in a few other places. -# The default value is: My Project. - -PROJECT_NAME = "JSON" - -# The PROJECT_NUMBER tag can be used to enter a project or revision number. This -# could be handy for archiving the generated documentation or if some version -# control system is used. - -PROJECT_NUMBER = - -# Using the PROJECT_BRIEF tag one can provide an optional one line description -# for a project that appears at the top of each page and should give viewer a -# quick idea about the purpose of the project. Keep the description short. - -PROJECT_BRIEF = - -# With the PROJECT_LOGO tag one can specify an logo or icon that is included in -# the documentation. The maximum height of the logo should not exceed 55 pixels -# and the maximum width should not exceed 200 pixels. Doxygen will copy the logo -# to the output directory. - -PROJECT_LOGO = - -# The OUTPUT_DIRECTORY tag is used to specify the (relative or absolute) path -# into which the generated documentation will be written. If a relative path is -# entered, it will be relative to the location where doxygen was started. If -# left blank the current directory will be used. - -OUTPUT_DIRECTORY = - -# If the CREATE_SUBDIRS tag is set to YES, then doxygen will create 4096 sub- -# directories (in 2 levels) under the output directory of each output format and -# will distribute the generated files over these directories. Enabling this -# option can be useful when feeding doxygen a huge amount of source files, where -# putting all generated files in the same directory would otherwise causes -# performance problems for the file system. -# The default value is: NO. - -CREATE_SUBDIRS = NO - -# If the ALLOW_UNICODE_NAMES tag is set to YES, doxygen will allow non-ASCII -# characters to appear in the names of generated files. If set to NO, non-ASCII -# characters will be escaped, for example _xE3_x81_x84 will be used for Unicode -# U+3044. -# The default value is: NO. - -ALLOW_UNICODE_NAMES = NO - -# The OUTPUT_LANGUAGE tag is used to specify the language in which all -# documentation generated by doxygen is written. Doxygen will use this -# information to generate all constant output in the proper language. -# Possible values are: Afrikaans, Arabic, Armenian, Brazilian, Catalan, Chinese, -# Chinese-Traditional, Croatian, Czech, Danish, Dutch, English (United States), -# Esperanto, Farsi (Persian), Finnish, French, German, Greek, Hungarian, -# Indonesian, Italian, Japanese, Japanese-en (Japanese with English messages), -# Korean, Korean-en (Korean with English messages), Latvian, Lithuanian, -# Macedonian, Norwegian, Persian (Farsi), Polish, Portuguese, Romanian, Russian, -# Serbian, Serbian-Cyrillic, Slovak, Slovene, Spanish, Swedish, Turkish, -# Ukrainian and Vietnamese. -# The default value is: English. - -OUTPUT_LANGUAGE = English - -# If the BRIEF_MEMBER_DESC tag is set to YES doxygen will include brief member -# descriptions after the members that are listed in the file and class -# documentation (similar to Javadoc). Set to NO to disable this. -# The default value is: YES. - -BRIEF_MEMBER_DESC = YES - -# If the REPEAT_BRIEF tag is set to YES doxygen will prepend the brief -# description of a member or function before the detailed description -# -# Note: If both HIDE_UNDOC_MEMBERS and BRIEF_MEMBER_DESC are set to NO, the -# brief descriptions will be completely suppressed. -# The default value is: YES. - -REPEAT_BRIEF = NO - -# This tag implements a quasi-intelligent brief description abbreviator that is -# used to form the text in various listings. Each string in this list, if found -# as the leading text of the brief description, will be stripped from the text -# and the result, after processing the whole list, is used as the annotated -# text. Otherwise, the brief description is used as-is. If left blank, the -# following values are used ($name is automatically replaced with the name of -# the entity):The $name class, The $name widget, The $name file, is, provides, -# specifies, contains, represents, a, an and the. - -ABBREVIATE_BRIEF = - -# If the ALWAYS_DETAILED_SEC and REPEAT_BRIEF tags are both set to YES then -# doxygen will generate a detailed section even if there is only a brief -# description. -# The default value is: NO. - -ALWAYS_DETAILED_SEC = NO - -# If the INLINE_INHERITED_MEMB tag is set to YES, doxygen will show all -# inherited members of a class in the documentation of that class as if those -# members were ordinary class members. Constructors, destructors and assignment -# operators of the base classes will not be shown. -# The default value is: NO. - -INLINE_INHERITED_MEMB = NO - -# If the FULL_PATH_NAMES tag is set to YES doxygen will prepend the full path -# before files name in the file list and in the header files. If set to NO the -# shortest path that makes the file name unique will be used -# The default value is: YES. - -FULL_PATH_NAMES = NO - -# The STRIP_FROM_PATH tag can be used to strip a user-defined part of the path. -# Stripping is only done if one of the specified strings matches the left-hand -# part of the path. The tag can be used to show relative paths in the file list. -# If left blank the directory from which doxygen is run is used as the path to -# strip. -# -# Note that you can specify absolute paths here, but also relative paths, which -# will be relative from the directory where doxygen is started. -# This tag requires that the tag FULL_PATH_NAMES is set to YES. - -STRIP_FROM_PATH = - -# The STRIP_FROM_INC_PATH tag can be used to strip a user-defined part of the -# path mentioned in the documentation of a class, which tells the reader which -# header file to include in order to use a class. If left blank only the name of -# the header file containing the class definition is used. Otherwise one should -# specify the list of include paths that are normally passed to the compiler -# using the -I flag. - -STRIP_FROM_INC_PATH = - -# If the SHORT_NAMES tag is set to YES, doxygen will generate much shorter (but -# less readable) file names. This can be useful is your file systems doesn't -# support long names like on DOS, Mac, or CD-ROM. -# The default value is: NO. - -SHORT_NAMES = NO - -# If the JAVADOC_AUTOBRIEF tag is set to YES then doxygen will interpret the -# first line (until the first dot) of a Javadoc-style comment as the brief -# description. If set to NO, the Javadoc-style will behave just like regular Qt- -# style comments (thus requiring an explicit @brief command for a brief -# description.) -# The default value is: NO. - -JAVADOC_AUTOBRIEF = NO - -# If the QT_AUTOBRIEF tag is set to YES then doxygen will interpret the first -# line (until the first dot) of a Qt-style comment as the brief description. If -# set to NO, the Qt-style will behave just like regular Qt-style comments (thus -# requiring an explicit \brief command for a brief description.) -# The default value is: NO. - -QT_AUTOBRIEF = NO - -# The MULTILINE_CPP_IS_BRIEF tag can be set to YES to make doxygen treat a -# multi-line C++ special comment block (i.e. a block of //! or /// comments) as -# a brief description. This used to be the default behavior. The new default is -# to treat a multi-line C++ comment block as a detailed description. Set this -# tag to YES if you prefer the old behavior instead. -# -# Note that setting this tag to YES also means that rational rose comments are -# not recognized any more. -# The default value is: NO. - -MULTILINE_CPP_IS_BRIEF = NO - -# If the INHERIT_DOCS tag is set to YES then an undocumented member inherits the -# documentation from any documented member that it re-implements. -# The default value is: YES. - -INHERIT_DOCS = YES - -# If the SEPARATE_MEMBER_PAGES tag is set to YES, then doxygen will produce a -# new page for each member. If set to NO, the documentation of a member will be -# part of the file/class/namespace that contains it. -# The default value is: NO. - -SEPARATE_MEMBER_PAGES = YES - -# The TAB_SIZE tag can be used to set the number of spaces in a tab. Doxygen -# uses this value to replace tabs by spaces in code fragments. -# Minimum value: 1, maximum value: 16, default value: 4. - -TAB_SIZE = 4 - -# This tag can be used to specify a number of aliases that act as commands in -# the documentation. An alias has the form: -# name=value -# For example adding -# "sideeffect=@par Side Effects:\n" -# will allow you to put the command \sideeffect (or @sideeffect) in the -# documentation, which will result in a user-defined paragraph with heading -# "Side Effects:". You can put \n's in the value part of an alias to insert -# newlines. - -ALIASES = - -# This tag can be used to specify a number of word-keyword mappings (TCL only). -# A mapping has the form "name=value". For example adding "class=itcl::class" -# will allow you to use the command class in the itcl::class meaning. - -TCL_SUBST = - -# Set the OPTIMIZE_OUTPUT_FOR_C tag to YES if your project consists of C sources -# only. Doxygen will then generate output that is more tailored for C. For -# instance, some of the names that are used will be different. The list of all -# members will be omitted, etc. -# The default value is: NO. - -OPTIMIZE_OUTPUT_FOR_C = NO - -# Set the OPTIMIZE_OUTPUT_JAVA tag to YES if your project consists of Java or -# Python sources only. Doxygen will then generate output that is more tailored -# for that language. For instance, namespaces will be presented as packages, -# qualified scopes will look different, etc. -# The default value is: NO. - -OPTIMIZE_OUTPUT_JAVA = NO - -# Set the OPTIMIZE_FOR_FORTRAN tag to YES if your project consists of Fortran -# sources. Doxygen will then generate output that is tailored for Fortran. -# The default value is: NO. - -OPTIMIZE_FOR_FORTRAN = NO - -# Set the OPTIMIZE_OUTPUT_VHDL tag to YES if your project consists of VHDL -# sources. Doxygen will then generate output that is tailored for VHDL. -# The default value is: NO. - -OPTIMIZE_OUTPUT_VHDL = NO - -# Doxygen selects the parser to use depending on the extension of the files it -# parses. With this tag you can assign which parser to use for a given -# extension. Doxygen has a built-in mapping, but you can override or extend it -# using this tag. The format is ext=language, where ext is a file extension, and -# language is one of the parsers supported by doxygen: IDL, Java, Javascript, -# C#, C, C++, D, PHP, Objective-C, Python, Fortran (fixed format Fortran: -# FortranFixed, free formatted Fortran: FortranFree, unknown formatted Fortran: -# Fortran. In the later case the parser tries to guess whether the code is fixed -# or free formatted code, this is the default for Fortran type files), VHDL. For -# instance to make doxygen treat .inc files as Fortran files (default is PHP), -# and .f files as C (default is Fortran), use: inc=Fortran f=C. -# -# Note For files without extension you can use no_extension as a placeholder. -# -# Note that for custom extensions you also need to set FILE_PATTERNS otherwise -# the files are not read by doxygen. - -EXTENSION_MAPPING = - -# If the MARKDOWN_SUPPORT tag is enabled then doxygen pre-processes all comments -# according to the Markdown format, which allows for more readable -# documentation. See http://daringfireball.net/projects/markdown/ for details. -# The output of markdown processing is further processed by doxygen, so you can -# mix doxygen, HTML, and XML commands with Markdown formatting. Disable only in -# case of backward compatibilities issues. -# The default value is: YES. - -MARKDOWN_SUPPORT = YES - -# When enabled doxygen tries to link words that correspond to documented -# classes, or namespaces to their corresponding documentation. Such a link can -# be prevented in individual cases by by putting a % sign in front of the word -# or globally by setting AUTOLINK_SUPPORT to NO. -# The default value is: YES. - -AUTOLINK_SUPPORT = NO - -# If you use STL classes (i.e. std::string, std::vector, etc.) but do not want -# to include (a tag file for) the STL sources as input, then you should set this -# tag to YES in order to let doxygen match functions declarations and -# definitions whose arguments contain STL classes (e.g. func(std::string); -# versus func(std::string) {}). This also make the inheritance and collaboration -# diagrams that involve STL classes more complete and accurate. -# The default value is: NO. - -BUILTIN_STL_SUPPORT = YES - -# If you use Microsoft's C++/CLI language, you should set this option to YES to -# enable parsing support. -# The default value is: NO. - -CPP_CLI_SUPPORT = NO - -# Set the SIP_SUPPORT tag to YES if your project consists of sip (see: -# http://www.riverbankcomputing.co.uk/software/sip/intro) sources only. Doxygen -# will parse them like normal C++ but will assume all classes use public instead -# of private inheritance when no explicit protection keyword is present. -# The default value is: NO. - -SIP_SUPPORT = NO - -# For Microsoft's IDL there are propget and propput attributes to indicate -# getter and setter methods for a property. Setting this option to YES will make -# doxygen to replace the get and set methods by a property in the documentation. -# This will only work if the methods are indeed getting or setting a simple -# type. If this is not the case, or you want to show the methods anyway, you -# should set this option to NO. -# The default value is: YES. - -IDL_PROPERTY_SUPPORT = YES - -# If member grouping is used in the documentation and the DISTRIBUTE_GROUP_DOC -# tag is set to YES, then doxygen will reuse the documentation of the first -# member in the group (if any) for the other members of the group. By default -# all members of a group must be documented explicitly. -# The default value is: NO. - -DISTRIBUTE_GROUP_DOC = NO - -# Set the SUBGROUPING tag to YES to allow class member groups of the same type -# (for instance a group of public functions) to be put as a subgroup of that -# type (e.g. under the Public Functions section). Set it to NO to prevent -# subgrouping. Alternatively, this can be done per class using the -# \nosubgrouping command. -# The default value is: YES. - -SUBGROUPING = YES - -# When the INLINE_GROUPED_CLASSES tag is set to YES, classes, structs and unions -# are shown inside the group in which they are included (e.g. using \ingroup) -# instead of on a separate page (for HTML and Man pages) or section (for LaTeX -# and RTF). -# -# Note that this feature does not work in combination with -# SEPARATE_MEMBER_PAGES. -# The default value is: NO. - -INLINE_GROUPED_CLASSES = NO - -# When the INLINE_SIMPLE_STRUCTS tag is set to YES, structs, classes, and unions -# with only public data fields or simple typedef fields will be shown inline in -# the documentation of the scope in which they are defined (i.e. file, -# namespace, or group documentation), provided this scope is documented. If set -# to NO, structs, classes, and unions are shown on a separate page (for HTML and -# Man pages) or section (for LaTeX and RTF). -# The default value is: NO. - -INLINE_SIMPLE_STRUCTS = NO - -# When TYPEDEF_HIDES_STRUCT tag is enabled, a typedef of a struct, union, or -# enum is documented as struct, union, or enum with the name of the typedef. So -# typedef struct TypeS {} TypeT, will appear in the documentation as a struct -# with name TypeT. When disabled the typedef will appear as a member of a file, -# namespace, or class. And the struct will be named TypeS. This can typically be -# useful for C code in case the coding convention dictates that all compound -# types are typedef'ed and only the typedef is referenced, never the tag name. -# The default value is: NO. - -TYPEDEF_HIDES_STRUCT = NO - -# The size of the symbol lookup cache can be set using LOOKUP_CACHE_SIZE. This -# cache is used to resolve symbols given their name and scope. Since this can be -# an expensive process and often the same symbol appears multiple times in the -# code, doxygen keeps a cache of pre-resolved symbols. If the cache is too small -# doxygen will become slower. If the cache is too large, memory is wasted. The -# cache size is given by this formula: 2^(16+LOOKUP_CACHE_SIZE). The valid range -# is 0..9, the default is 0, corresponding to a cache size of 2^16=65536 -# symbols. At the end of a run doxygen will report the cache usage and suggest -# the optimal cache size from a speed point of view. -# Minimum value: 0, maximum value: 9, default value: 0. - -LOOKUP_CACHE_SIZE = 0 - -#--------------------------------------------------------------------------- -# Build related configuration options -#--------------------------------------------------------------------------- - -# If the EXTRACT_ALL tag is set to YES doxygen will assume all entities in -# documentation are documented, even if no documentation was available. Private -# class members and static file members will be hidden unless the -# EXTRACT_PRIVATE respectively EXTRACT_STATIC tags are set to YES. -# Note: This will also disable the warnings about undocumented members that are -# normally produced when WARNINGS is set to YES. -# The default value is: NO. - -EXTRACT_ALL = YES - -# If the EXTRACT_PRIVATE tag is set to YES all private members of a class will -# be included in the documentation. -# The default value is: NO. - -EXTRACT_PRIVATE = YES - -# If the EXTRACT_PACKAGE tag is set to YES all members with package or internal -# scope will be included in the documentation. -# The default value is: NO. - -EXTRACT_PACKAGE = NO - -# If the EXTRACT_STATIC tag is set to YES all static members of a file will be -# included in the documentation. -# The default value is: NO. - -EXTRACT_STATIC = YES - -# If the EXTRACT_LOCAL_CLASSES tag is set to YES classes (and structs) defined -# locally in source files will be included in the documentation. If set to NO -# only classes defined in header files are included. Does not have any effect -# for Java sources. -# The default value is: YES. - -EXTRACT_LOCAL_CLASSES = YES - -# This flag is only useful for Objective-C code. When set to YES local methods, -# which are defined in the implementation section but not in the interface are -# included in the documentation. If set to NO only methods in the interface are -# included. -# The default value is: NO. - -EXTRACT_LOCAL_METHODS = YES - -# If this flag is set to YES, the members of anonymous namespaces will be -# extracted and appear in the documentation as a namespace called -# 'anonymous_namespace{file}', where file will be replaced with the base name of -# the file that contains the anonymous namespace. By default anonymous namespace -# are hidden. -# The default value is: NO. - -EXTRACT_ANON_NSPACES = YES - -# If the HIDE_UNDOC_MEMBERS tag is set to YES, doxygen will hide all -# undocumented members inside documented classes or files. If set to NO these -# members will be included in the various overviews, but no documentation -# section is generated. This option has no effect if EXTRACT_ALL is enabled. -# The default value is: NO. - -HIDE_UNDOC_MEMBERS = NO - -# If the HIDE_UNDOC_CLASSES tag is set to YES, doxygen will hide all -# undocumented classes that are normally visible in the class hierarchy. If set -# to NO these classes will be included in the various overviews. This option has -# no effect if EXTRACT_ALL is enabled. -# The default value is: NO. - -HIDE_UNDOC_CLASSES = NO - -# If the HIDE_FRIEND_COMPOUNDS tag is set to YES, doxygen will hide all friend -# (class|struct|union) declarations. If set to NO these declarations will be -# included in the documentation. -# The default value is: NO. - -HIDE_FRIEND_COMPOUNDS = NO - -# If the HIDE_IN_BODY_DOCS tag is set to YES, doxygen will hide any -# documentation blocks found inside the body of a function. If set to NO these -# blocks will be appended to the function's detailed documentation block. -# The default value is: NO. - -HIDE_IN_BODY_DOCS = NO - -# The INTERNAL_DOCS tag determines if documentation that is typed after a -# \internal command is included. If the tag is set to NO then the documentation -# will be excluded. Set it to YES to include the internal documentation. -# The default value is: NO. - -INTERNAL_DOCS = NO - -# If the CASE_SENSE_NAMES tag is set to NO then doxygen will only generate file -# names in lower-case letters. If set to YES upper-case letters are also -# allowed. This is useful if you have classes or files whose names only differ -# in case and if your file system supports case sensitive file names. Windows -# and Mac users are advised to set this option to NO. -# The default value is: system dependent. - -CASE_SENSE_NAMES = NO - -# If the HIDE_SCOPE_NAMES tag is set to NO then doxygen will show members with -# their full class and namespace scopes in the documentation. If set to YES the -# scope will be hidden. -# The default value is: NO. - -HIDE_SCOPE_NAMES = NO - -# If the SHOW_INCLUDE_FILES tag is set to YES then doxygen will put a list of -# the files that are included by a file in the documentation of that file. -# The default value is: YES. - -SHOW_INCLUDE_FILES = YES - -# If the SHOW_GROUPED_MEMB_INC tag is set to YES then Doxygen will add for each -# grouped member an include statement to the documentation, telling the reader -# which file to include in order to use the member. -# The default value is: NO. - -SHOW_GROUPED_MEMB_INC = NO - -# If the FORCE_LOCAL_INCLUDES tag is set to YES then doxygen will list include -# files with double quotes in the documentation rather than with sharp brackets. -# The default value is: NO. - -FORCE_LOCAL_INCLUDES = NO - -# If the INLINE_INFO tag is set to YES then a tag [inline] is inserted in the -# documentation for inline members. -# The default value is: YES. - -INLINE_INFO = YES - -# If the SORT_MEMBER_DOCS tag is set to YES then doxygen will sort the -# (detailed) documentation of file and class members alphabetically by member -# name. If set to NO the members will appear in declaration order. -# The default value is: YES. - -SORT_MEMBER_DOCS = YES - -# If the SORT_BRIEF_DOCS tag is set to YES then doxygen will sort the brief -# descriptions of file, namespace and class members alphabetically by member -# name. If set to NO the members will appear in declaration order. Note that -# this will also influence the order of the classes in the class list. -# The default value is: NO. - -SORT_BRIEF_DOCS = NO - -# If the SORT_MEMBERS_CTORS_1ST tag is set to YES then doxygen will sort the -# (brief and detailed) documentation of class members so that constructors and -# destructors are listed first. If set to NO the constructors will appear in the -# respective orders defined by SORT_BRIEF_DOCS and SORT_MEMBER_DOCS. -# Note: If SORT_BRIEF_DOCS is set to NO this option is ignored for sorting brief -# member documentation. -# Note: If SORT_MEMBER_DOCS is set to NO this option is ignored for sorting -# detailed member documentation. -# The default value is: NO. - -SORT_MEMBERS_CTORS_1ST = NO - -# If the SORT_GROUP_NAMES tag is set to YES then doxygen will sort the hierarchy -# of group names into alphabetical order. If set to NO the group names will -# appear in their defined order. -# The default value is: NO. - -SORT_GROUP_NAMES = NO - -# If the SORT_BY_SCOPE_NAME tag is set to YES, the class list will be sorted by -# fully-qualified names, including namespaces. If set to NO, the class list will -# be sorted only by class name, not including the namespace part. -# Note: This option is not very useful if HIDE_SCOPE_NAMES is set to YES. -# Note: This option applies only to the class list, not to the alphabetical -# list. -# The default value is: NO. - -SORT_BY_SCOPE_NAME = NO - -# If the STRICT_PROTO_MATCHING option is enabled and doxygen fails to do proper -# type resolution of all parameters of a function it will reject a match between -# the prototype and the implementation of a member function even if there is -# only one candidate or it is obvious which candidate to choose by doing a -# simple string match. By disabling STRICT_PROTO_MATCHING doxygen will still -# accept a match between prototype and implementation in such cases. -# The default value is: NO. - -STRICT_PROTO_MATCHING = NO - -# The GENERATE_TODOLIST tag can be used to enable ( YES) or disable ( NO) the -# todo list. This list is created by putting \todo commands in the -# documentation. -# The default value is: YES. - -GENERATE_TODOLIST = YES - -# The GENERATE_TESTLIST tag can be used to enable ( YES) or disable ( NO) the -# test list. This list is created by putting \test commands in the -# documentation. -# The default value is: YES. - -GENERATE_TESTLIST = YES - -# The GENERATE_BUGLIST tag can be used to enable ( YES) or disable ( NO) the bug -# list. This list is created by putting \bug commands in the documentation. -# The default value is: YES. - -GENERATE_BUGLIST = YES - -# The GENERATE_DEPRECATEDLIST tag can be used to enable ( YES) or disable ( NO) -# the deprecated list. This list is created by putting \deprecated commands in -# the documentation. -# The default value is: YES. - -GENERATE_DEPRECATEDLIST= YES - -# The ENABLED_SECTIONS tag can be used to enable conditional documentation -# sections, marked by \if <section_label> ... \endif and \cond <section_label> -# ... \endcond blocks. - -ENABLED_SECTIONS = - -# The MAX_INITIALIZER_LINES tag determines the maximum number of lines that the -# initial value of a variable or macro / define can have for it to appear in the -# documentation. If the initializer consists of more lines than specified here -# it will be hidden. Use a value of 0 to hide initializers completely. The -# appearance of the value of individual variables and macros / defines can be -# controlled using \showinitializer or \hideinitializer command in the -# documentation regardless of this setting. -# Minimum value: 0, maximum value: 10000, default value: 30. - -MAX_INITIALIZER_LINES = 30 - -# Set the SHOW_USED_FILES tag to NO to disable the list of files generated at -# the bottom of the documentation of classes and structs. If set to YES the list -# will mention the files that were used to generate the documentation. -# The default value is: YES. - -SHOW_USED_FILES = YES - -# Set the SHOW_FILES tag to NO to disable the generation of the Files page. This -# will remove the Files entry from the Quick Index and from the Folder Tree View -# (if specified). -# The default value is: YES. - -SHOW_FILES = YES - -# Set the SHOW_NAMESPACES tag to NO to disable the generation of the Namespaces -# page. This will remove the Namespaces entry from the Quick Index and from the -# Folder Tree View (if specified). -# The default value is: YES. - -SHOW_NAMESPACES = YES - -# The FILE_VERSION_FILTER tag can be used to specify a program or script that -# doxygen should invoke to get the current version for each file (typically from -# the version control system). Doxygen will invoke the program by executing (via -# popen()) the command command input-file, where command is the value of the -# FILE_VERSION_FILTER tag, and input-file is the name of an input file provided -# by doxygen. Whatever the program writes to standard output is used as the file -# version. For an example see the documentation. - -FILE_VERSION_FILTER = - -# The LAYOUT_FILE tag can be used to specify a layout file which will be parsed -# by doxygen. The layout file controls the global structure of the generated -# output files in an output format independent way. To create the layout file -# that represents doxygen's defaults, run doxygen with the -l option. You can -# optionally specify a file name after the option, if omitted DoxygenLayout.xml -# will be used as the name of the layout file. -# -# Note that if you run doxygen from a directory containing a file called -# DoxygenLayout.xml, doxygen will parse it automatically even if the LAYOUT_FILE -# tag is left empty. - -LAYOUT_FILE = - -# The CITE_BIB_FILES tag can be used to specify one or more bib files containing -# the reference definitions. This must be a list of .bib files. The .bib -# extension is automatically appended if omitted. This requires the bibtex tool -# to be installed. See also http://en.wikipedia.org/wiki/BibTeX for more info. -# For LaTeX the style of the bibliography can be controlled using -# LATEX_BIB_STYLE. To use this feature you need bibtex and perl available in the -# search path. See also \cite for info how to create references. - -CITE_BIB_FILES = - -#--------------------------------------------------------------------------- -# Configuration options related to warning and progress messages -#--------------------------------------------------------------------------- - -# The QUIET tag can be used to turn on/off the messages that are generated to -# standard output by doxygen. If QUIET is set to YES this implies that the -# messages are off. -# The default value is: NO. - -QUIET = NO - -# The WARNINGS tag can be used to turn on/off the warning messages that are -# generated to standard error ( stderr) by doxygen. If WARNINGS is set to YES -# this implies that the warnings are on. -# -# Tip: Turn warnings on while writing the documentation. -# The default value is: YES. - -WARNINGS = YES - -# If the WARN_IF_UNDOCUMENTED tag is set to YES, then doxygen will generate -# warnings for undocumented members. If EXTRACT_ALL is set to YES then this flag -# will automatically be disabled. -# The default value is: YES. - -WARN_IF_UNDOCUMENTED = YES - -# If the WARN_IF_DOC_ERROR tag is set to YES, doxygen will generate warnings for -# potential errors in the documentation, such as not documenting some parameters -# in a documented function, or documenting parameters that don't exist or using -# markup commands wrongly. -# The default value is: YES. - -WARN_IF_DOC_ERROR = YES - -# This WARN_NO_PARAMDOC option can be enabled to get warnings for functions that -# are documented, but have no documentation for their parameters or return -# value. If set to NO doxygen will only warn about wrong or incomplete parameter -# documentation, but not about the absence of documentation. -# The default value is: NO. - -WARN_NO_PARAMDOC = NO - -# The WARN_FORMAT tag determines the format of the warning messages that doxygen -# can produce. The string should contain the $file, $line, and $text tags, which -# will be replaced by the file and line number from which the warning originated -# and the warning text. Optionally the format may contain $version, which will -# be replaced by the version of the file (if it could be obtained via -# FILE_VERSION_FILTER) -# The default value is: $file:$line: $text. - -WARN_FORMAT = "$file:$line: $text" - -# The WARN_LOGFILE tag can be used to specify a file to which warning and error -# messages should be written. If left blank the output is written to standard -# error (stderr). - -WARN_LOGFILE = - -#--------------------------------------------------------------------------- -# Configuration options related to the input files -#--------------------------------------------------------------------------- - -# The INPUT tag is used to specify the files and/or directories that contain -# documented source files. You may enter file names like myfile.cpp or -# directories like /usr/src/myproject. Separate the files or directories with -# spaces. -# Note: If this tag is empty the current directory is searched. - -INPUT = ../src - -# This tag can be used to specify the character encoding of the source files -# that doxygen parses. Internally doxygen uses the UTF-8 encoding. Doxygen uses -# libiconv (or the iconv built into libc) for the transcoding. See the libiconv -# documentation (see: http://www.gnu.org/software/libiconv) for the list of -# possible encodings. -# The default value is: UTF-8. - -INPUT_ENCODING = UTF-8 - -# If the value of the INPUT tag contains directories, you can use the -# FILE_PATTERNS tag to specify one or more wildcard patterns (like *.cpp and -# *.h) to filter out the source-files in the directories. If left blank the -# following patterns are tested:*.c, *.cc, *.cxx, *.cpp, *.c++, *.java, *.ii, -# *.ixx, *.ipp, *.i++, *.inl, *.idl, *.ddl, *.odl, *.h, *.hh, *.hxx, *.hpp, -# *.h++, *.cs, *.d, *.php, *.php4, *.php5, *.phtml, *.inc, *.m, *.markdown, -# *.md, *.mm, *.dox, *.py, *.f90, *.f, *.for, *.tcl, *.vhd, *.vhdl, *.ucf, -# *.qsf, *.as and *.js. - -FILE_PATTERNS = - -# The RECURSIVE tag can be used to specify whether or not subdirectories should -# be searched for input files as well. -# The default value is: NO. - -RECURSIVE = NO - -# The EXCLUDE tag can be used to specify files and/or directories that should be -# excluded from the INPUT source files. This way you can easily exclude a -# subdirectory from a directory tree whose root is specified with the INPUT tag. -# -# Note that relative paths are relative to the directory from which doxygen is -# run. - -EXCLUDE = - -# The EXCLUDE_SYMLINKS tag can be used to select whether or not files or -# directories that are symbolic links (a Unix file system feature) are excluded -# from the input. -# The default value is: NO. - -EXCLUDE_SYMLINKS = NO - -# If the value of the INPUT tag contains directories, you can use the -# EXCLUDE_PATTERNS tag to specify one or more wildcard patterns to exclude -# certain files from those directories. -# -# Note that the wildcards are matched against the file with absolute path, so to -# exclude all test directories for example use the pattern */test/* - -EXCLUDE_PATTERNS = - -# The EXCLUDE_SYMBOLS tag can be used to specify one or more symbol names -# (namespaces, classes, functions, etc.) that should be excluded from the -# output. The symbol name can be a fully qualified name, a word, or if the -# wildcard * is used, a substring. Examples: ANamespace, AClass, -# AClass::ANamespace, ANamespace::*Test -# -# Note that the wildcards are matched against the file with absolute path, so to -# exclude all test directories use the pattern */test/* - -EXCLUDE_SYMBOLS = - -# The EXAMPLE_PATH tag can be used to specify one or more files or directories -# that contain example code fragments that are included (see the \include -# command). - -EXAMPLE_PATH = - -# If the value of the EXAMPLE_PATH tag contains directories, you can use the -# EXAMPLE_PATTERNS tag to specify one or more wildcard pattern (like *.cpp and -# *.h) to filter out the source-files in the directories. If left blank all -# files are included. - -EXAMPLE_PATTERNS = - -# If the EXAMPLE_RECURSIVE tag is set to YES then subdirectories will be -# searched for input files to be used with the \include or \dontinclude commands -# irrespective of the value of the RECURSIVE tag. -# The default value is: NO. - -EXAMPLE_RECURSIVE = NO - -# The IMAGE_PATH tag can be used to specify one or more files or directories -# that contain images that are to be included in the documentation (see the -# \image command). - -IMAGE_PATH = - -# The INPUT_FILTER tag can be used to specify a program that doxygen should -# invoke to filter for each input file. Doxygen will invoke the filter program -# by executing (via popen()) the command: -# -# <filter> <input-file> -# -# where <filter> is the value of the INPUT_FILTER tag, and <input-file> is the -# name of an input file. Doxygen will then use the output that the filter -# program writes to standard output. If FILTER_PATTERNS is specified, this tag -# will be ignored. -# -# Note that the filter must not add or remove lines; it is applied before the -# code is scanned, but not when the output code is generated. If lines are added -# or removed, the anchors will not be placed correctly. - -INPUT_FILTER = - -# The FILTER_PATTERNS tag can be used to specify filters on a per file pattern -# basis. Doxygen will compare the file name with each pattern and apply the -# filter if there is a match. The filters are a list of the form: pattern=filter -# (like *.cpp=my_cpp_filter). See INPUT_FILTER for further information on how -# filters are used. If the FILTER_PATTERNS tag is empty or if none of the -# patterns match the file name, INPUT_FILTER is applied. - -FILTER_PATTERNS = - -# If the FILTER_SOURCE_FILES tag is set to YES, the input filter (if set using -# INPUT_FILTER ) will also be used to filter the input files that are used for -# producing the source files to browse (i.e. when SOURCE_BROWSER is set to YES). -# The default value is: NO. - -FILTER_SOURCE_FILES = NO - -# The FILTER_SOURCE_PATTERNS tag can be used to specify source filters per file -# pattern. A pattern will override the setting for FILTER_PATTERN (if any) and -# it is also possible to disable source filtering for a specific pattern using -# *.ext= (so without naming a filter). -# This tag requires that the tag FILTER_SOURCE_FILES is set to YES. - -FILTER_SOURCE_PATTERNS = - -# If the USE_MDFILE_AS_MAINPAGE tag refers to the name of a markdown file that -# is part of the input, its contents will be placed on the main page -# (index.html). This can be useful if you have a project on for instance GitHub -# and want to reuse the introduction page also for the doxygen output. - -USE_MDFILE_AS_MAINPAGE = - -#--------------------------------------------------------------------------- -# Configuration options related to source browsing -#--------------------------------------------------------------------------- - -# If the SOURCE_BROWSER tag is set to YES then a list of source files will be -# generated. Documented entities will be cross-referenced with these sources. -# -# Note: To get rid of all source code in the generated output, make sure that -# also VERBATIM_HEADERS is set to NO. -# The default value is: NO. - -SOURCE_BROWSER = YES - -# Setting the INLINE_SOURCES tag to YES will include the body of functions, -# classes and enums directly into the documentation. -# The default value is: NO. - -INLINE_SOURCES = YES - -# Setting the STRIP_CODE_COMMENTS tag to YES will instruct doxygen to hide any -# special comment blocks from generated source code fragments. Normal C, C++ and -# Fortran comments will always remain visible. -# The default value is: YES. - -STRIP_CODE_COMMENTS = YES - -# If the REFERENCED_BY_RELATION tag is set to YES then for each documented -# function all documented functions referencing it will be listed. -# The default value is: NO. - -REFERENCED_BY_RELATION = NO - -# If the REFERENCES_RELATION tag is set to YES then for each documented function -# all documented entities called/used by that function will be listed. -# The default value is: NO. - -REFERENCES_RELATION = NO - -# If the REFERENCES_LINK_SOURCE tag is set to YES and SOURCE_BROWSER tag is set -# to YES, then the hyperlinks from functions in REFERENCES_RELATION and -# REFERENCED_BY_RELATION lists will link to the source code. Otherwise they will -# link to the documentation. -# The default value is: YES. - -REFERENCES_LINK_SOURCE = YES - -# If SOURCE_TOOLTIPS is enabled (the default) then hovering a hyperlink in the -# source code will show a tooltip with additional information such as prototype, -# brief description and links to the definition and documentation. Since this -# will make the HTML file larger and loading of large files a bit slower, you -# can opt to disable this feature. -# The default value is: YES. -# This tag requires that the tag SOURCE_BROWSER is set to YES. - -SOURCE_TOOLTIPS = YES - -# If the USE_HTAGS tag is set to YES then the references to source code will -# point to the HTML generated by the htags(1) tool instead of doxygen built-in -# source browser. The htags tool is part of GNU's global source tagging system -# (see http://www.gnu.org/software/global/global.html). You will need version -# 4.8.6 or higher. -# -# To use it do the following: -# - Install the latest version of global -# - Enable SOURCE_BROWSER and USE_HTAGS in the config file -# - Make sure the INPUT points to the root of the source tree -# - Run doxygen as normal -# -# Doxygen will invoke htags (and that will in turn invoke gtags), so these -# tools must be available from the command line (i.e. in the search path). -# -# The result: instead of the source browser generated by doxygen, the links to -# source code will now point to the output of htags. -# The default value is: NO. -# This tag requires that the tag SOURCE_BROWSER is set to YES. - -USE_HTAGS = NO - -# If the VERBATIM_HEADERS tag is set the YES then doxygen will generate a -# verbatim copy of the header file for each class for which an include is -# specified. Set to NO to disable this. -# See also: Section \class. -# The default value is: YES. - -VERBATIM_HEADERS = YES - -#--------------------------------------------------------------------------- -# Configuration options related to the alphabetical class index -#--------------------------------------------------------------------------- - -# If the ALPHABETICAL_INDEX tag is set to YES, an alphabetical index of all -# compounds will be generated. Enable this if the project contains a lot of -# classes, structs, unions or interfaces. -# The default value is: YES. - -ALPHABETICAL_INDEX = YES - -# The COLS_IN_ALPHA_INDEX tag can be used to specify the number of columns in -# which the alphabetical index list will be split. -# Minimum value: 1, maximum value: 20, default value: 5. -# This tag requires that the tag ALPHABETICAL_INDEX is set to YES. - -COLS_IN_ALPHA_INDEX = 5 - -# In case all classes in a project start with a common prefix, all classes will -# be put under the same header in the alphabetical index. The IGNORE_PREFIX tag -# can be used to specify a prefix (or a list of prefixes) that should be ignored -# while generating the index headers. -# This tag requires that the tag ALPHABETICAL_INDEX is set to YES. - -IGNORE_PREFIX = - -#--------------------------------------------------------------------------- -# Configuration options related to the HTML output -#--------------------------------------------------------------------------- - -# If the GENERATE_HTML tag is set to YES doxygen will generate HTML output -# The default value is: YES. - -GENERATE_HTML = YES - -# The HTML_OUTPUT tag is used to specify where the HTML docs will be put. If a -# relative path is entered the value of OUTPUT_DIRECTORY will be put in front of -# it. -# The default directory is: html. -# This tag requires that the tag GENERATE_HTML is set to YES. - -HTML_OUTPUT = html - -# The HTML_FILE_EXTENSION tag can be used to specify the file extension for each -# generated HTML page (for example: .htm, .php, .asp). -# The default value is: .html. -# This tag requires that the tag GENERATE_HTML is set to YES. - -HTML_FILE_EXTENSION = .html - -# The HTML_HEADER tag can be used to specify a user-defined HTML header file for -# each generated HTML page. If the tag is left blank doxygen will generate a -# standard header. -# -# To get valid HTML the header file that includes any scripts and style sheets -# that doxygen needs, which is dependent on the configuration options used (e.g. -# the setting GENERATE_TREEVIEW). It is highly recommended to start with a -# default header using -# doxygen -w html new_header.html new_footer.html new_stylesheet.css -# YourConfigFile -# and then modify the file new_header.html. See also section "Doxygen usage" -# for information on how to generate the default header that doxygen normally -# uses. -# Note: The header is subject to change so you typically have to regenerate the -# default header when upgrading to a newer version of doxygen. For a description -# of the possible markers and block names see the documentation. -# This tag requires that the tag GENERATE_HTML is set to YES. - -HTML_HEADER = - -# The HTML_FOOTER tag can be used to specify a user-defined HTML footer for each -# generated HTML page. If the tag is left blank doxygen will generate a standard -# footer. See HTML_HEADER for more information on how to generate a default -# footer and what special commands can be used inside the footer. See also -# section "Doxygen usage" for information on how to generate the default footer -# that doxygen normally uses. -# This tag requires that the tag GENERATE_HTML is set to YES. - -HTML_FOOTER = - -# The HTML_STYLESHEET tag can be used to specify a user-defined cascading style -# sheet that is used by each HTML page. It can be used to fine-tune the look of -# the HTML output. If left blank doxygen will generate a default style sheet. -# See also section "Doxygen usage" for information on how to generate the style -# sheet that doxygen normally uses. -# Note: It is recommended to use HTML_EXTRA_STYLESHEET instead of this tag, as -# it is more robust and this tag (HTML_STYLESHEET) will in the future become -# obsolete. -# This tag requires that the tag GENERATE_HTML is set to YES. - -HTML_STYLESHEET = - -# The HTML_EXTRA_STYLESHEET tag can be used to specify additional user-defined -# cascading style sheets that are included after the standard style sheets -# created by doxygen. Using this option one can overrule certain style aspects. -# This is preferred over using HTML_STYLESHEET since it does not replace the -# standard style sheet and is therefor more robust against future updates. -# Doxygen will copy the style sheet files to the output directory. -# Note: The order of the extra stylesheet files is of importance (e.g. the last -# stylesheet in the list overrules the setting of the previous ones in the -# list). For an example see the documentation. -# This tag requires that the tag GENERATE_HTML is set to YES. - -HTML_EXTRA_STYLESHEET = - -# The HTML_EXTRA_FILES tag can be used to specify one or more extra images or -# other source files which should be copied to the HTML output directory. Note -# that these files will be copied to the base HTML output directory. Use the -# $relpath^ marker in the HTML_HEADER and/or HTML_FOOTER files to load these -# files. In the HTML_STYLESHEET file, use the file name only. Also note that the -# files will be copied as-is; there are no commands or markers available. -# This tag requires that the tag GENERATE_HTML is set to YES. - -HTML_EXTRA_FILES = - -# The HTML_COLORSTYLE_HUE tag controls the color of the HTML output. Doxygen -# will adjust the colors in the stylesheet and background images according to -# this color. Hue is specified as an angle on a colorwheel, see -# http://en.wikipedia.org/wiki/Hue for more information. For instance the value -# 0 represents red, 60 is yellow, 120 is green, 180 is cyan, 240 is blue, 300 -# purple, and 360 is red again. -# Minimum value: 0, maximum value: 359, default value: 220. -# This tag requires that the tag GENERATE_HTML is set to YES. - -HTML_COLORSTYLE_HUE = 220 - -# The HTML_COLORSTYLE_SAT tag controls the purity (or saturation) of the colors -# in the HTML output. For a value of 0 the output will use grayscales only. A -# value of 255 will produce the most vivid colors. -# Minimum value: 0, maximum value: 255, default value: 100. -# This tag requires that the tag GENERATE_HTML is set to YES. - -HTML_COLORSTYLE_SAT = 100 - -# The HTML_COLORSTYLE_GAMMA tag controls the gamma correction applied to the -# luminance component of the colors in the HTML output. Values below 100 -# gradually make the output lighter, whereas values above 100 make the output -# darker. The value divided by 100 is the actual gamma applied, so 80 represents -# a gamma of 0.8, The value 220 represents a gamma of 2.2, and 100 does not -# change the gamma. -# Minimum value: 40, maximum value: 240, default value: 80. -# This tag requires that the tag GENERATE_HTML is set to YES. - -HTML_COLORSTYLE_GAMMA = 80 - -# If the HTML_TIMESTAMP tag is set to YES then the footer of each generated HTML -# page will contain the date and time when the page was generated. Setting this -# to NO can help when comparing the output of multiple runs. -# The default value is: YES. -# This tag requires that the tag GENERATE_HTML is set to YES. - -HTML_TIMESTAMP = YES - -# If the HTML_DYNAMIC_SECTIONS tag is set to YES then the generated HTML -# documentation will contain sections that can be hidden and shown after the -# page has loaded. -# The default value is: NO. -# This tag requires that the tag GENERATE_HTML is set to YES. - -HTML_DYNAMIC_SECTIONS = NO - -# With HTML_INDEX_NUM_ENTRIES one can control the preferred number of entries -# shown in the various tree structured indices initially; the user can expand -# and collapse entries dynamically later on. Doxygen will expand the tree to -# such a level that at most the specified number of entries are visible (unless -# a fully collapsed tree already exceeds this amount). So setting the number of -# entries 1 will produce a full collapsed tree by default. 0 is a special value -# representing an infinite number of entries and will result in a full expanded -# tree by default. -# Minimum value: 0, maximum value: 9999, default value: 100. -# This tag requires that the tag GENERATE_HTML is set to YES. - -HTML_INDEX_NUM_ENTRIES = 100 - -# If the GENERATE_DOCSET tag is set to YES, additional index files will be -# generated that can be used as input for Apple's Xcode 3 integrated development -# environment (see: http://developer.apple.com/tools/xcode/), introduced with -# OSX 10.5 (Leopard). To create a documentation set, doxygen will generate a -# Makefile in the HTML output directory. Running make will produce the docset in -# that directory and running make install will install the docset in -# ~/Library/Developer/Shared/Documentation/DocSets so that Xcode will find it at -# startup. See http://developer.apple.com/tools/creatingdocsetswithdoxygen.html -# for more information. -# The default value is: NO. -# This tag requires that the tag GENERATE_HTML is set to YES. - -GENERATE_DOCSET = NO - -# This tag determines the name of the docset feed. A documentation feed provides -# an umbrella under which multiple documentation sets from a single provider -# (such as a company or product suite) can be grouped. -# The default value is: Doxygen generated docs. -# This tag requires that the tag GENERATE_DOCSET is set to YES. - -DOCSET_FEEDNAME = "Doxygen generated docs" - -# This tag specifies a string that should uniquely identify the documentation -# set bundle. This should be a reverse domain-name style string, e.g. -# com.mycompany.MyDocSet. Doxygen will append .docset to the name. -# The default value is: org.doxygen.Project. -# This tag requires that the tag GENERATE_DOCSET is set to YES. - -DOCSET_BUNDLE_ID = org.doxygen.Project - -# The DOCSET_PUBLISHER_ID tag specifies a string that should uniquely identify -# the documentation publisher. This should be a reverse domain-name style -# string, e.g. com.mycompany.MyDocSet.documentation. -# The default value is: org.doxygen.Publisher. -# This tag requires that the tag GENERATE_DOCSET is set to YES. - -DOCSET_PUBLISHER_ID = org.doxygen.Publisher - -# The DOCSET_PUBLISHER_NAME tag identifies the documentation publisher. -# The default value is: Publisher. -# This tag requires that the tag GENERATE_DOCSET is set to YES. - -DOCSET_PUBLISHER_NAME = Publisher - -# If the GENERATE_HTMLHELP tag is set to YES then doxygen generates three -# additional HTML index files: index.hhp, index.hhc, and index.hhk. The -# index.hhp is a project file that can be read by Microsoft's HTML Help Workshop -# (see: http://www.microsoft.com/en-us/download/details.aspx?id=21138) on -# Windows. -# -# The HTML Help Workshop contains a compiler that can convert all HTML output -# generated by doxygen into a single compiled HTML file (.chm). Compiled HTML -# files are now used as the Windows 98 help format, and will replace the old -# Windows help format (.hlp) on all Windows platforms in the future. Compressed -# HTML files also contain an index, a table of contents, and you can search for -# words in the documentation. The HTML workshop also contains a viewer for -# compressed HTML files. -# The default value is: NO. -# This tag requires that the tag GENERATE_HTML is set to YES. - -GENERATE_HTMLHELP = NO - -# The CHM_FILE tag can be used to specify the file name of the resulting .chm -# file. You can add a path in front of the file if the result should not be -# written to the html output directory. -# This tag requires that the tag GENERATE_HTMLHELP is set to YES. - -CHM_FILE = - -# The HHC_LOCATION tag can be used to specify the location (absolute path -# including file name) of the HTML help compiler ( hhc.exe). If non-empty -# doxygen will try to run the HTML help compiler on the generated index.hhp. -# The file has to be specified with full path. -# This tag requires that the tag GENERATE_HTMLHELP is set to YES. - -HHC_LOCATION = - -# The GENERATE_CHI flag controls if a separate .chi index file is generated ( -# YES) or that it should be included in the master .chm file ( NO). -# The default value is: NO. -# This tag requires that the tag GENERATE_HTMLHELP is set to YES. - -GENERATE_CHI = NO - -# The CHM_INDEX_ENCODING is used to encode HtmlHelp index ( hhk), content ( hhc) -# and project file content. -# This tag requires that the tag GENERATE_HTMLHELP is set to YES. - -CHM_INDEX_ENCODING = - -# The BINARY_TOC flag controls whether a binary table of contents is generated ( -# YES) or a normal table of contents ( NO) in the .chm file. Furthermore it -# enables the Previous and Next buttons. -# The default value is: NO. -# This tag requires that the tag GENERATE_HTMLHELP is set to YES. - -BINARY_TOC = NO - -# The TOC_EXPAND flag can be set to YES to add extra items for group members to -# the table of contents of the HTML help documentation and to the tree view. -# The default value is: NO. -# This tag requires that the tag GENERATE_HTMLHELP is set to YES. - -TOC_EXPAND = NO - -# If the GENERATE_QHP tag is set to YES and both QHP_NAMESPACE and -# QHP_VIRTUAL_FOLDER are set, an additional index file will be generated that -# can be used as input for Qt's qhelpgenerator to generate a Qt Compressed Help -# (.qch) of the generated HTML documentation. -# The default value is: NO. -# This tag requires that the tag GENERATE_HTML is set to YES. - -GENERATE_QHP = NO - -# If the QHG_LOCATION tag is specified, the QCH_FILE tag can be used to specify -# the file name of the resulting .qch file. The path specified is relative to -# the HTML output folder. -# This tag requires that the tag GENERATE_QHP is set to YES. - -QCH_FILE = - -# The QHP_NAMESPACE tag specifies the namespace to use when generating Qt Help -# Project output. For more information please see Qt Help Project / Namespace -# (see: http://qt-project.org/doc/qt-4.8/qthelpproject.html#namespace). -# The default value is: org.doxygen.Project. -# This tag requires that the tag GENERATE_QHP is set to YES. - -QHP_NAMESPACE = org.doxygen.Project - -# The QHP_VIRTUAL_FOLDER tag specifies the namespace to use when generating Qt -# Help Project output. For more information please see Qt Help Project / Virtual -# Folders (see: http://qt-project.org/doc/qt-4.8/qthelpproject.html#virtual- -# folders). -# The default value is: doc. -# This tag requires that the tag GENERATE_QHP is set to YES. - -QHP_VIRTUAL_FOLDER = doc - -# If the QHP_CUST_FILTER_NAME tag is set, it specifies the name of a custom -# filter to add. For more information please see Qt Help Project / Custom -# Filters (see: http://qt-project.org/doc/qt-4.8/qthelpproject.html#custom- -# filters). -# This tag requires that the tag GENERATE_QHP is set to YES. - -QHP_CUST_FILTER_NAME = - -# The QHP_CUST_FILTER_ATTRS tag specifies the list of the attributes of the -# custom filter to add. For more information please see Qt Help Project / Custom -# Filters (see: http://qt-project.org/doc/qt-4.8/qthelpproject.html#custom- -# filters). -# This tag requires that the tag GENERATE_QHP is set to YES. - -QHP_CUST_FILTER_ATTRS = - -# The QHP_SECT_FILTER_ATTRS tag specifies the list of the attributes this -# project's filter section matches. Qt Help Project / Filter Attributes (see: -# http://qt-project.org/doc/qt-4.8/qthelpproject.html#filter-attributes). -# This tag requires that the tag GENERATE_QHP is set to YES. - -QHP_SECT_FILTER_ATTRS = - -# The QHG_LOCATION tag can be used to specify the location of Qt's -# qhelpgenerator. If non-empty doxygen will try to run qhelpgenerator on the -# generated .qhp file. -# This tag requires that the tag GENERATE_QHP is set to YES. - -QHG_LOCATION = - -# If the GENERATE_ECLIPSEHELP tag is set to YES, additional index files will be -# generated, together with the HTML files, they form an Eclipse help plugin. To -# install this plugin and make it available under the help contents menu in -# Eclipse, the contents of the directory containing the HTML and XML files needs -# to be copied into the plugins directory of eclipse. The name of the directory -# within the plugins directory should be the same as the ECLIPSE_DOC_ID value. -# After copying Eclipse needs to be restarted before the help appears. -# The default value is: NO. -# This tag requires that the tag GENERATE_HTML is set to YES. - -GENERATE_ECLIPSEHELP = NO - -# A unique identifier for the Eclipse help plugin. When installing the plugin -# the directory name containing the HTML and XML files should also have this -# name. Each documentation set should have its own identifier. -# The default value is: org.doxygen.Project. -# This tag requires that the tag GENERATE_ECLIPSEHELP is set to YES. - -ECLIPSE_DOC_ID = org.doxygen.Project - -# If you want full control over the layout of the generated HTML pages it might -# be necessary to disable the index and replace it with your own. The -# DISABLE_INDEX tag can be used to turn on/off the condensed index (tabs) at top -# of each HTML page. A value of NO enables the index and the value YES disables -# it. Since the tabs in the index contain the same information as the navigation -# tree, you can set this option to YES if you also set GENERATE_TREEVIEW to YES. -# The default value is: NO. -# This tag requires that the tag GENERATE_HTML is set to YES. - -DISABLE_INDEX = NO - -# The GENERATE_TREEVIEW tag is used to specify whether a tree-like index -# structure should be generated to display hierarchical information. If the tag -# value is set to YES, a side panel will be generated containing a tree-like -# index structure (just like the one that is generated for HTML Help). For this -# to work a browser that supports JavaScript, DHTML, CSS and frames is required -# (i.e. any modern browser). Windows users are probably better off using the -# HTML help feature. Via custom stylesheets (see HTML_EXTRA_STYLESHEET) one can -# further fine-tune the look of the index. As an example, the default style -# sheet generated by doxygen has an example that shows how to put an image at -# the root of the tree instead of the PROJECT_NAME. Since the tree basically has -# the same information as the tab index, you could consider setting -# DISABLE_INDEX to YES when enabling this option. -# The default value is: NO. -# This tag requires that the tag GENERATE_HTML is set to YES. - -GENERATE_TREEVIEW = YES - -# The ENUM_VALUES_PER_LINE tag can be used to set the number of enum values that -# doxygen will group on one line in the generated HTML documentation. -# -# Note that a value of 0 will completely suppress the enum values from appearing -# in the overview section. -# Minimum value: 0, maximum value: 20, default value: 4. -# This tag requires that the tag GENERATE_HTML is set to YES. - -ENUM_VALUES_PER_LINE = 4 - -# If the treeview is enabled (see GENERATE_TREEVIEW) then this tag can be used -# to set the initial width (in pixels) of the frame in which the tree is shown. -# Minimum value: 0, maximum value: 1500, default value: 250. -# This tag requires that the tag GENERATE_HTML is set to YES. - -TREEVIEW_WIDTH = 250 - -# When the EXT_LINKS_IN_WINDOW option is set to YES doxygen will open links to -# external symbols imported via tag files in a separate window. -# The default value is: NO. -# This tag requires that the tag GENERATE_HTML is set to YES. - -EXT_LINKS_IN_WINDOW = NO - -# Use this tag to change the font size of LaTeX formulas included as images in -# the HTML documentation. When you change the font size after a successful -# doxygen run you need to manually remove any form_*.png images from the HTML -# output directory to force them to be regenerated. -# Minimum value: 8, maximum value: 50, default value: 10. -# This tag requires that the tag GENERATE_HTML is set to YES. - -FORMULA_FONTSIZE = 10 - -# Use the FORMULA_TRANPARENT tag to determine whether or not the images -# generated for formulas are transparent PNGs. Transparent PNGs are not -# supported properly for IE 6.0, but are supported on all modern browsers. -# -# Note that when changing this option you need to delete any form_*.png files in -# the HTML output directory before the changes have effect. -# The default value is: YES. -# This tag requires that the tag GENERATE_HTML is set to YES. - -FORMULA_TRANSPARENT = YES - -# Enable the USE_MATHJAX option to render LaTeX formulas using MathJax (see -# http://www.mathjax.org) which uses client side Javascript for the rendering -# instead of using prerendered bitmaps. Use this if you do not have LaTeX -# installed or if you want to formulas look prettier in the HTML output. When -# enabled you may also need to install MathJax separately and configure the path -# to it using the MATHJAX_RELPATH option. -# The default value is: NO. -# This tag requires that the tag GENERATE_HTML is set to YES. - -USE_MATHJAX = NO - -# When MathJax is enabled you can set the default output format to be used for -# the MathJax output. See the MathJax site (see: -# http://docs.mathjax.org/en/latest/output.html) for more details. -# Possible values are: HTML-CSS (which is slower, but has the best -# compatibility), NativeMML (i.e. MathML) and SVG. -# The default value is: HTML-CSS. -# This tag requires that the tag USE_MATHJAX is set to YES. - -MATHJAX_FORMAT = HTML-CSS - -# When MathJax is enabled you need to specify the location relative to the HTML -# output directory using the MATHJAX_RELPATH option. The destination directory -# should contain the MathJax.js script. For instance, if the mathjax directory -# is located at the same level as the HTML output directory, then -# MATHJAX_RELPATH should be ../mathjax. The default value points to the MathJax -# Content Delivery Network so you can quickly see the result without installing -# MathJax. However, it is strongly recommended to install a local copy of -# MathJax from http://www.mathjax.org before deployment. -# The default value is: http://cdn.mathjax.org/mathjax/latest. -# This tag requires that the tag USE_MATHJAX is set to YES. - -MATHJAX_RELPATH = http://cdn.mathjax.org/mathjax/latest - -# The MATHJAX_EXTENSIONS tag can be used to specify one or more MathJax -# extension names that should be enabled during MathJax rendering. For example -# MATHJAX_EXTENSIONS = TeX/AMSmath TeX/AMSsymbols -# This tag requires that the tag USE_MATHJAX is set to YES. - -MATHJAX_EXTENSIONS = - -# The MATHJAX_CODEFILE tag can be used to specify a file with javascript pieces -# of code that will be used on startup of the MathJax code. See the MathJax site -# (see: http://docs.mathjax.org/en/latest/output.html) for more details. For an -# example see the documentation. -# This tag requires that the tag USE_MATHJAX is set to YES. - -MATHJAX_CODEFILE = - -# When the SEARCHENGINE tag is enabled doxygen will generate a search box for -# the HTML output. The underlying search engine uses javascript and DHTML and -# should work on any modern browser. Note that when using HTML help -# (GENERATE_HTMLHELP), Qt help (GENERATE_QHP), or docsets (GENERATE_DOCSET) -# there is already a search function so this one should typically be disabled. -# For large projects the javascript based search engine can be slow, then -# enabling SERVER_BASED_SEARCH may provide a better solution. It is possible to -# search using the keyboard; to jump to the search box use <access key> + S -# (what the <access key> is depends on the OS and browser, but it is typically -# <CTRL>, <ALT>/<option>, or both). Inside the search box use the <cursor down -# key> to jump into the search results window, the results can be navigated -# using the <cursor keys>. Press <Enter> to select an item or <escape> to cancel -# the search. The filter options can be selected when the cursor is inside the -# search box by pressing <Shift>+<cursor down>. Also here use the <cursor keys> -# to select a filter and <Enter> or <escape> to activate or cancel the filter -# option. -# The default value is: YES. -# This tag requires that the tag GENERATE_HTML is set to YES. - -SEARCHENGINE = YES - -# When the SERVER_BASED_SEARCH tag is enabled the search engine will be -# implemented using a web server instead of a web client using Javascript. There -# are two flavors of web server based searching depending on the EXTERNAL_SEARCH -# setting. When disabled, doxygen will generate a PHP script for searching and -# an index file used by the script. When EXTERNAL_SEARCH is enabled the indexing -# and searching needs to be provided by external tools. See the section -# "External Indexing and Searching" for details. -# The default value is: NO. -# This tag requires that the tag SEARCHENGINE is set to YES. - -SERVER_BASED_SEARCH = NO - -# When EXTERNAL_SEARCH tag is enabled doxygen will no longer generate the PHP -# script for searching. Instead the search results are written to an XML file -# which needs to be processed by an external indexer. Doxygen will invoke an -# external search engine pointed to by the SEARCHENGINE_URL option to obtain the -# search results. -# -# Doxygen ships with an example indexer ( doxyindexer) and search engine -# (doxysearch.cgi) which are based on the open source search engine library -# Xapian (see: http://xapian.org/). -# -# See the section "External Indexing and Searching" for details. -# The default value is: NO. -# This tag requires that the tag SEARCHENGINE is set to YES. - -EXTERNAL_SEARCH = NO - -# The SEARCHENGINE_URL should point to a search engine hosted by a web server -# which will return the search results when EXTERNAL_SEARCH is enabled. -# -# Doxygen ships with an example indexer ( doxyindexer) and search engine -# (doxysearch.cgi) which are based on the open source search engine library -# Xapian (see: http://xapian.org/). See the section "External Indexing and -# Searching" for details. -# This tag requires that the tag SEARCHENGINE is set to YES. - -SEARCHENGINE_URL = - -# When SERVER_BASED_SEARCH and EXTERNAL_SEARCH are both enabled the unindexed -# search data is written to a file for indexing by an external tool. With the -# SEARCHDATA_FILE tag the name of this file can be specified. -# The default file is: searchdata.xml. -# This tag requires that the tag SEARCHENGINE is set to YES. - -SEARCHDATA_FILE = searchdata.xml - -# When SERVER_BASED_SEARCH and EXTERNAL_SEARCH are both enabled the -# EXTERNAL_SEARCH_ID tag can be used as an identifier for the project. This is -# useful in combination with EXTRA_SEARCH_MAPPINGS to search through multiple -# projects and redirect the results back to the right project. -# This tag requires that the tag SEARCHENGINE is set to YES. - -EXTERNAL_SEARCH_ID = - -# The EXTRA_SEARCH_MAPPINGS tag can be used to enable searching through doxygen -# projects other than the one defined by this configuration file, but that are -# all added to the same external search index. Each project needs to have a -# unique id set via EXTERNAL_SEARCH_ID. The search mapping then maps the id of -# to a relative location where the documentation can be found. The format is: -# EXTRA_SEARCH_MAPPINGS = tagname1=loc1 tagname2=loc2 ... -# This tag requires that the tag SEARCHENGINE is set to YES. - -EXTRA_SEARCH_MAPPINGS = - -#--------------------------------------------------------------------------- -# Configuration options related to the LaTeX output -#--------------------------------------------------------------------------- - -# If the GENERATE_LATEX tag is set to YES doxygen will generate LaTeX output. -# The default value is: YES. - -GENERATE_LATEX = NO - -# The LATEX_OUTPUT tag is used to specify where the LaTeX docs will be put. If a -# relative path is entered the value of OUTPUT_DIRECTORY will be put in front of -# it. -# The default directory is: latex. -# This tag requires that the tag GENERATE_LATEX is set to YES. - -LATEX_OUTPUT = latex - -# The LATEX_CMD_NAME tag can be used to specify the LaTeX command name to be -# invoked. -# -# Note that when enabling USE_PDFLATEX this option is only used for generating -# bitmaps for formulas in the HTML output, but not in the Makefile that is -# written to the output directory. -# The default file is: latex. -# This tag requires that the tag GENERATE_LATEX is set to YES. - -LATEX_CMD_NAME = latex - -# The MAKEINDEX_CMD_NAME tag can be used to specify the command name to generate -# index for LaTeX. -# The default file is: makeindex. -# This tag requires that the tag GENERATE_LATEX is set to YES. - -MAKEINDEX_CMD_NAME = makeindex - -# If the COMPACT_LATEX tag is set to YES doxygen generates more compact LaTeX -# documents. This may be useful for small projects and may help to save some -# trees in general. -# The default value is: NO. -# This tag requires that the tag GENERATE_LATEX is set to YES. - -COMPACT_LATEX = NO - -# The PAPER_TYPE tag can be used to set the paper type that is used by the -# printer. -# Possible values are: a4 (210 x 297 mm), letter (8.5 x 11 inches), legal (8.5 x -# 14 inches) and executive (7.25 x 10.5 inches). -# The default value is: a4. -# This tag requires that the tag GENERATE_LATEX is set to YES. - -PAPER_TYPE = a4 - -# The EXTRA_PACKAGES tag can be used to specify one or more LaTeX package names -# that should be included in the LaTeX output. To get the times font for -# instance you can specify -# EXTRA_PACKAGES=times -# If left blank no extra packages will be included. -# This tag requires that the tag GENERATE_LATEX is set to YES. - -EXTRA_PACKAGES = - -# The LATEX_HEADER tag can be used to specify a personal LaTeX header for the -# generated LaTeX document. The header should contain everything until the first -# chapter. If it is left blank doxygen will generate a standard header. See -# section "Doxygen usage" for information on how to let doxygen write the -# default header to a separate file. -# -# Note: Only use a user-defined header if you know what you are doing! The -# following commands have a special meaning inside the header: $title, -# $datetime, $date, $doxygenversion, $projectname, $projectnumber, -# $projectbrief, $projectlogo. Doxygen will replace $title with the empy string, -# for the replacement values of the other commands the user is refered to -# HTML_HEADER. -# This tag requires that the tag GENERATE_LATEX is set to YES. - -LATEX_HEADER = - -# The LATEX_FOOTER tag can be used to specify a personal LaTeX footer for the -# generated LaTeX document. The footer should contain everything after the last -# chapter. If it is left blank doxygen will generate a standard footer. See -# LATEX_HEADER for more information on how to generate a default footer and what -# special commands can be used inside the footer. -# -# Note: Only use a user-defined footer if you know what you are doing! -# This tag requires that the tag GENERATE_LATEX is set to YES. - -LATEX_FOOTER = - -# The LATEX_EXTRA_FILES tag can be used to specify one or more extra images or -# other source files which should be copied to the LATEX_OUTPUT output -# directory. Note that the files will be copied as-is; there are no commands or -# markers available. -# This tag requires that the tag GENERATE_LATEX is set to YES. - -LATEX_EXTRA_FILES = - -# If the PDF_HYPERLINKS tag is set to YES, the LaTeX that is generated is -# prepared for conversion to PDF (using ps2pdf or pdflatex). The PDF file will -# contain links (just like the HTML output) instead of page references. This -# makes the output suitable for online browsing using a PDF viewer. -# The default value is: YES. -# This tag requires that the tag GENERATE_LATEX is set to YES. - -PDF_HYPERLINKS = YES - -# If the USE_PDFLATEX tag is set to YES, doxygen will use pdflatex to generate -# the PDF file directly from the LaTeX files. Set this option to YES to get a -# higher quality PDF documentation. -# The default value is: YES. -# This tag requires that the tag GENERATE_LATEX is set to YES. - -USE_PDFLATEX = YES - -# If the LATEX_BATCHMODE tag is set to YES, doxygen will add the \batchmode -# command to the generated LaTeX files. This will instruct LaTeX to keep running -# if errors occur, instead of asking the user for help. This option is also used -# when generating formulas in HTML. -# The default value is: NO. -# This tag requires that the tag GENERATE_LATEX is set to YES. - -LATEX_BATCHMODE = NO - -# If the LATEX_HIDE_INDICES tag is set to YES then doxygen will not include the -# index chapters (such as File Index, Compound Index, etc.) in the output. -# The default value is: NO. -# This tag requires that the tag GENERATE_LATEX is set to YES. - -LATEX_HIDE_INDICES = NO - -# If the LATEX_SOURCE_CODE tag is set to YES then doxygen will include source -# code with syntax highlighting in the LaTeX output. -# -# Note that which sources are shown also depends on other settings such as -# SOURCE_BROWSER. -# The default value is: NO. -# This tag requires that the tag GENERATE_LATEX is set to YES. - -LATEX_SOURCE_CODE = NO - -# The LATEX_BIB_STYLE tag can be used to specify the style to use for the -# bibliography, e.g. plainnat, or ieeetr. See -# http://en.wikipedia.org/wiki/BibTeX and \cite for more info. -# The default value is: plain. -# This tag requires that the tag GENERATE_LATEX is set to YES. - -LATEX_BIB_STYLE = plain - -#--------------------------------------------------------------------------- -# Configuration options related to the RTF output -#--------------------------------------------------------------------------- - -# If the GENERATE_RTF tag is set to YES doxygen will generate RTF output. The -# RTF output is optimized for Word 97 and may not look too pretty with other RTF -# readers/editors. -# The default value is: NO. - -GENERATE_RTF = NO - -# The RTF_OUTPUT tag is used to specify where the RTF docs will be put. If a -# relative path is entered the value of OUTPUT_DIRECTORY will be put in front of -# it. -# The default directory is: rtf. -# This tag requires that the tag GENERATE_RTF is set to YES. - -RTF_OUTPUT = rtf - -# If the COMPACT_RTF tag is set to YES doxygen generates more compact RTF -# documents. This may be useful for small projects and may help to save some -# trees in general. -# The default value is: NO. -# This tag requires that the tag GENERATE_RTF is set to YES. - -COMPACT_RTF = NO - -# If the RTF_HYPERLINKS tag is set to YES, the RTF that is generated will -# contain hyperlink fields. The RTF file will contain links (just like the HTML -# output) instead of page references. This makes the output suitable for online -# browsing using Word or some other Word compatible readers that support those -# fields. -# -# Note: WordPad (write) and others do not support links. -# The default value is: NO. -# This tag requires that the tag GENERATE_RTF is set to YES. - -RTF_HYPERLINKS = NO - -# Load stylesheet definitions from file. Syntax is similar to doxygen's config -# file, i.e. a series of assignments. You only have to provide replacements, -# missing definitions are set to their default value. -# -# See also section "Doxygen usage" for information on how to generate the -# default style sheet that doxygen normally uses. -# This tag requires that the tag GENERATE_RTF is set to YES. - -RTF_STYLESHEET_FILE = - -# Set optional variables used in the generation of an RTF document. Syntax is -# similar to doxygen's config file. A template extensions file can be generated -# using doxygen -e rtf extensionFile. -# This tag requires that the tag GENERATE_RTF is set to YES. - -RTF_EXTENSIONS_FILE = - -#--------------------------------------------------------------------------- -# Configuration options related to the man page output -#--------------------------------------------------------------------------- - -# If the GENERATE_MAN tag is set to YES doxygen will generate man pages for -# classes and files. -# The default value is: NO. - -GENERATE_MAN = NO - -# The MAN_OUTPUT tag is used to specify where the man pages will be put. If a -# relative path is entered the value of OUTPUT_DIRECTORY will be put in front of -# it. A directory man3 will be created inside the directory specified by -# MAN_OUTPUT. -# The default directory is: man. -# This tag requires that the tag GENERATE_MAN is set to YES. - -MAN_OUTPUT = man - -# The MAN_EXTENSION tag determines the extension that is added to the generated -# man pages. In case the manual section does not start with a number, the number -# 3 is prepended. The dot (.) at the beginning of the MAN_EXTENSION tag is -# optional. -# The default value is: .3. -# This tag requires that the tag GENERATE_MAN is set to YES. - -MAN_EXTENSION = .3 - -# The MAN_SUBDIR tag determines the name of the directory created within -# MAN_OUTPUT in which the man pages are placed. If defaults to man followed by -# MAN_EXTENSION with the initial . removed. -# This tag requires that the tag GENERATE_MAN is set to YES. - -MAN_SUBDIR = - -# If the MAN_LINKS tag is set to YES and doxygen generates man output, then it -# will generate one additional man file for each entity documented in the real -# man page(s). These additional files only source the real man page, but without -# them the man command would be unable to find the correct page. -# The default value is: NO. -# This tag requires that the tag GENERATE_MAN is set to YES. - -MAN_LINKS = NO - -#--------------------------------------------------------------------------- -# Configuration options related to the XML output -#--------------------------------------------------------------------------- - -# If the GENERATE_XML tag is set to YES doxygen will generate an XML file that -# captures the structure of the code including all documentation. -# The default value is: NO. - -GENERATE_XML = NO - -# The XML_OUTPUT tag is used to specify where the XML pages will be put. If a -# relative path is entered the value of OUTPUT_DIRECTORY will be put in front of -# it. -# The default directory is: xml. -# This tag requires that the tag GENERATE_XML is set to YES. - -XML_OUTPUT = xml - -# If the XML_PROGRAMLISTING tag is set to YES doxygen will dump the program -# listings (including syntax highlighting and cross-referencing information) to -# the XML output. Note that enabling this will significantly increase the size -# of the XML output. -# The default value is: YES. -# This tag requires that the tag GENERATE_XML is set to YES. - -XML_PROGRAMLISTING = YES - -#--------------------------------------------------------------------------- -# Configuration options related to the DOCBOOK output -#--------------------------------------------------------------------------- - -# If the GENERATE_DOCBOOK tag is set to YES doxygen will generate Docbook files -# that can be used to generate PDF. -# The default value is: NO. - -GENERATE_DOCBOOK = NO - -# The DOCBOOK_OUTPUT tag is used to specify where the Docbook pages will be put. -# If a relative path is entered the value of OUTPUT_DIRECTORY will be put in -# front of it. -# The default directory is: docbook. -# This tag requires that the tag GENERATE_DOCBOOK is set to YES. - -DOCBOOK_OUTPUT = docbook - -# If the DOCBOOK_PROGRAMLISTING tag is set to YES doxygen will include the -# program listings (including syntax highlighting and cross-referencing -# information) to the DOCBOOK output. Note that enabling this will significantly -# increase the size of the DOCBOOK output. -# The default value is: NO. -# This tag requires that the tag GENERATE_DOCBOOK is set to YES. - -DOCBOOK_PROGRAMLISTING = NO - -#--------------------------------------------------------------------------- -# Configuration options for the AutoGen Definitions output -#--------------------------------------------------------------------------- - -# If the GENERATE_AUTOGEN_DEF tag is set to YES doxygen will generate an AutoGen -# Definitions (see http://autogen.sf.net) file that captures the structure of -# the code including all documentation. Note that this feature is still -# experimental and incomplete at the moment. -# The default value is: NO. - -GENERATE_AUTOGEN_DEF = NO - -#--------------------------------------------------------------------------- -# Configuration options related to the Perl module output -#--------------------------------------------------------------------------- - -# If the GENERATE_PERLMOD tag is set to YES doxygen will generate a Perl module -# file that captures the structure of the code including all documentation. -# -# Note that this feature is still experimental and incomplete at the moment. -# The default value is: NO. - -GENERATE_PERLMOD = NO - -# If the PERLMOD_LATEX tag is set to YES doxygen will generate the necessary -# Makefile rules, Perl scripts and LaTeX code to be able to generate PDF and DVI -# output from the Perl module output. -# The default value is: NO. -# This tag requires that the tag GENERATE_PERLMOD is set to YES. - -PERLMOD_LATEX = NO - -# If the PERLMOD_PRETTY tag is set to YES the Perl module output will be nicely -# formatted so it can be parsed by a human reader. This is useful if you want to -# understand what is going on. On the other hand, if this tag is set to NO the -# size of the Perl module output will be much smaller and Perl will parse it -# just the same. -# The default value is: YES. -# This tag requires that the tag GENERATE_PERLMOD is set to YES. - -PERLMOD_PRETTY = YES - -# The names of the make variables in the generated doxyrules.make file are -# prefixed with the string contained in PERLMOD_MAKEVAR_PREFIX. This is useful -# so different doxyrules.make files included by the same Makefile don't -# overwrite each other's variables. -# This tag requires that the tag GENERATE_PERLMOD is set to YES. - -PERLMOD_MAKEVAR_PREFIX = - -#--------------------------------------------------------------------------- -# Configuration options related to the preprocessor -#--------------------------------------------------------------------------- - -# If the ENABLE_PREPROCESSING tag is set to YES doxygen will evaluate all -# C-preprocessor directives found in the sources and include files. -# The default value is: YES. - -ENABLE_PREPROCESSING = YES - -# If the MACRO_EXPANSION tag is set to YES doxygen will expand all macro names -# in the source code. If set to NO only conditional compilation will be -# performed. Macro expansion can be done in a controlled way by setting -# EXPAND_ONLY_PREDEF to YES. -# The default value is: NO. -# This tag requires that the tag ENABLE_PREPROCESSING is set to YES. - -MACRO_EXPANSION = NO - -# If the EXPAND_ONLY_PREDEF and MACRO_EXPANSION tags are both set to YES then -# the macro expansion is limited to the macros specified with the PREDEFINED and -# EXPAND_AS_DEFINED tags. -# The default value is: NO. -# This tag requires that the tag ENABLE_PREPROCESSING is set to YES. - -EXPAND_ONLY_PREDEF = NO - -# If the SEARCH_INCLUDES tag is set to YES the includes files in the -# INCLUDE_PATH will be searched if a #include is found. -# The default value is: YES. -# This tag requires that the tag ENABLE_PREPROCESSING is set to YES. - -SEARCH_INCLUDES = YES - -# The INCLUDE_PATH tag can be used to specify one or more directories that -# contain include files that are not input files but should be processed by the -# preprocessor. -# This tag requires that the tag SEARCH_INCLUDES is set to YES. - -INCLUDE_PATH = - -# You can use the INCLUDE_FILE_PATTERNS tag to specify one or more wildcard -# patterns (like *.h and *.hpp) to filter out the header-files in the -# directories. If left blank, the patterns specified with FILE_PATTERNS will be -# used. -# This tag requires that the tag ENABLE_PREPROCESSING is set to YES. - -INCLUDE_FILE_PATTERNS = - -# The PREDEFINED tag can be used to specify one or more macro names that are -# defined before the preprocessor is started (similar to the -D option of e.g. -# gcc). The argument of the tag is a list of macros of the form: name or -# name=definition (no spaces). If the definition and the "=" are omitted, "=1" -# is assumed. To prevent a macro definition from being undefined via #undef or -# recursively expanded use the := operator instead of the = operator. -# This tag requires that the tag ENABLE_PREPROCESSING is set to YES. - -PREDEFINED = - -# If the MACRO_EXPANSION and EXPAND_ONLY_PREDEF tags are set to YES then this -# tag can be used to specify a list of macro names that should be expanded. The -# macro definition that is found in the sources will be used. Use the PREDEFINED -# tag if you want to use a different macro definition that overrules the -# definition found in the source code. -# This tag requires that the tag ENABLE_PREPROCESSING is set to YES. - -EXPAND_AS_DEFINED = - -# If the SKIP_FUNCTION_MACROS tag is set to YES then doxygen's preprocessor will -# remove all references to function-like macros that are alone on a line, have -# an all uppercase name, and do not end with a semicolon. Such function macros -# are typically used for boiler-plate code, and will confuse the parser if not -# removed. -# The default value is: YES. -# This tag requires that the tag ENABLE_PREPROCESSING is set to YES. - -SKIP_FUNCTION_MACROS = YES - -#--------------------------------------------------------------------------- -# Configuration options related to external references -#--------------------------------------------------------------------------- - -# The TAGFILES tag can be used to specify one or more tag files. For each tag -# file the location of the external documentation should be added. The format of -# a tag file without this location is as follows: -# TAGFILES = file1 file2 ... -# Adding location for the tag files is done as follows: -# TAGFILES = file1=loc1 "file2 = loc2" ... -# where loc1 and loc2 can be relative or absolute paths or URLs. See the -# section "Linking to external documentation" for more information about the use -# of tag files. -# Note: Each tag file must have a unique name (where the name does NOT include -# the path). If a tag file is not located in the directory in which doxygen is -# run, you must also specify the path to the tagfile here. - -TAGFILES = - -# When a file name is specified after GENERATE_TAGFILE, doxygen will create a -# tag file that is based on the input files it reads. See section "Linking to -# external documentation" for more information about the usage of tag files. - -GENERATE_TAGFILE = - -# If the ALLEXTERNALS tag is set to YES all external class will be listed in the -# class index. If set to NO only the inherited external classes will be listed. -# The default value is: NO. - -ALLEXTERNALS = NO - -# If the EXTERNAL_GROUPS tag is set to YES all external groups will be listed in -# the modules index. If set to NO, only the current project's groups will be -# listed. -# The default value is: YES. - -EXTERNAL_GROUPS = YES - -# If the EXTERNAL_PAGES tag is set to YES all external pages will be listed in -# the related pages index. If set to NO, only the current project's pages will -# be listed. -# The default value is: YES. - -EXTERNAL_PAGES = YES - -# The PERL_PATH should be the absolute path and name of the perl script -# interpreter (i.e. the result of 'which perl'). -# The default file (with absolute path) is: /usr/bin/perl. - -PERL_PATH = /usr/bin/perl - -#--------------------------------------------------------------------------- -# Configuration options related to the dot tool -#--------------------------------------------------------------------------- - -# If the CLASS_DIAGRAMS tag is set to YES doxygen will generate a class diagram -# (in HTML and LaTeX) for classes with base or super classes. Setting the tag to -# NO turns the diagrams off. Note that this option also works with HAVE_DOT -# disabled, but it is recommended to install and use dot, since it yields more -# powerful graphs. -# The default value is: YES. - -CLASS_DIAGRAMS = YES - -# You can define message sequence charts within doxygen comments using the \msc -# command. Doxygen will then run the mscgen tool (see: -# http://www.mcternan.me.uk/mscgen/)) to produce the chart and insert it in the -# documentation. The MSCGEN_PATH tag allows you to specify the directory where -# the mscgen tool resides. If left empty the tool is assumed to be found in the -# default search path. - -MSCGEN_PATH = - -# You can include diagrams made with dia in doxygen documentation. Doxygen will -# then run dia to produce the diagram and insert it in the documentation. The -# DIA_PATH tag allows you to specify the directory where the dia binary resides. -# If left empty dia is assumed to be found in the default search path. - -DIA_PATH = - -# If set to YES, the inheritance and collaboration graphs will hide inheritance -# and usage relations if the target is undocumented or is not a class. -# The default value is: YES. - -HIDE_UNDOC_RELATIONS = YES - -# If you set the HAVE_DOT tag to YES then doxygen will assume the dot tool is -# available from the path. This tool is part of Graphviz (see: -# http://www.graphviz.org/), a graph visualization toolkit from AT&T and Lucent -# Bell Labs. The other options in this section have no effect if this option is -# set to NO -# The default value is: NO. - -HAVE_DOT = YES - -# The DOT_NUM_THREADS specifies the number of dot invocations doxygen is allowed -# to run in parallel. When set to 0 doxygen will base this on the number of -# processors available in the system. You can set it explicitly to a value -# larger than 0 to get control over the balance between CPU load and processing -# speed. -# Minimum value: 0, maximum value: 32, default value: 0. -# This tag requires that the tag HAVE_DOT is set to YES. - -DOT_NUM_THREADS = 0 - -# When you want a differently looking font in the dot files that doxygen -# generates you can specify the font name using DOT_FONTNAME. You need to make -# sure dot is able to find the font, which can be done by putting it in a -# standard location or by setting the DOTFONTPATH environment variable or by -# setting DOT_FONTPATH to the directory containing the font. -# The default value is: Helvetica. -# This tag requires that the tag HAVE_DOT is set to YES. - -DOT_FONTNAME = Helvetica - -# The DOT_FONTSIZE tag can be used to set the size (in points) of the font of -# dot graphs. -# Minimum value: 4, maximum value: 24, default value: 10. -# This tag requires that the tag HAVE_DOT is set to YES. - -DOT_FONTSIZE = 10 - -# By default doxygen will tell dot to use the default font as specified with -# DOT_FONTNAME. If you specify a different font using DOT_FONTNAME you can set -# the path where dot can find it using this tag. -# This tag requires that the tag HAVE_DOT is set to YES. - -DOT_FONTPATH = - -# If the CLASS_GRAPH tag is set to YES then doxygen will generate a graph for -# each documented class showing the direct and indirect inheritance relations. -# Setting this tag to YES will force the CLASS_DIAGRAMS tag to NO. -# The default value is: YES. -# This tag requires that the tag HAVE_DOT is set to YES. - -CLASS_GRAPH = YES - -# If the COLLABORATION_GRAPH tag is set to YES then doxygen will generate a -# graph for each documented class showing the direct and indirect implementation -# dependencies (inheritance, containment, and class references variables) of the -# class with other documented classes. -# The default value is: YES. -# This tag requires that the tag HAVE_DOT is set to YES. - -COLLABORATION_GRAPH = YES - -# If the GROUP_GRAPHS tag is set to YES then doxygen will generate a graph for -# groups, showing the direct groups dependencies. -# The default value is: YES. -# This tag requires that the tag HAVE_DOT is set to YES. - -GROUP_GRAPHS = YES - -# If the UML_LOOK tag is set to YES doxygen will generate inheritance and -# collaboration diagrams in a style similar to the OMG's Unified Modeling -# Language. -# The default value is: NO. -# This tag requires that the tag HAVE_DOT is set to YES. - -UML_LOOK = YES - -# If the UML_LOOK tag is enabled, the fields and methods are shown inside the -# class node. If there are many fields or methods and many nodes the graph may -# become too big to be useful. The UML_LIMIT_NUM_FIELDS threshold limits the -# number of items for each type to make the size more manageable. Set this to 0 -# for no limit. Note that the threshold may be exceeded by 50% before the limit -# is enforced. So when you set the threshold to 10, up to 15 fields may appear, -# but if the number exceeds 15, the total amount of fields shown is limited to -# 10. -# Minimum value: 0, maximum value: 100, default value: 10. -# This tag requires that the tag HAVE_DOT is set to YES. - -UML_LIMIT_NUM_FIELDS = 10 - -# If the TEMPLATE_RELATIONS tag is set to YES then the inheritance and -# collaboration graphs will show the relations between templates and their -# instances. -# The default value is: NO. -# This tag requires that the tag HAVE_DOT is set to YES. - -TEMPLATE_RELATIONS = NO - -# If the INCLUDE_GRAPH, ENABLE_PREPROCESSING and SEARCH_INCLUDES tags are set to -# YES then doxygen will generate a graph for each documented file showing the -# direct and indirect include dependencies of the file with other documented -# files. -# The default value is: YES. -# This tag requires that the tag HAVE_DOT is set to YES. - -INCLUDE_GRAPH = YES - -# If the INCLUDED_BY_GRAPH, ENABLE_PREPROCESSING and SEARCH_INCLUDES tags are -# set to YES then doxygen will generate a graph for each documented file showing -# the direct and indirect include dependencies of the file with other documented -# files. -# The default value is: YES. -# This tag requires that the tag HAVE_DOT is set to YES. - -INCLUDED_BY_GRAPH = YES - -# If the CALL_GRAPH tag is set to YES then doxygen will generate a call -# dependency graph for every global function or class method. -# -# Note that enabling this option will significantly increase the time of a run. -# So in most cases it will be better to enable call graphs for selected -# functions only using the \callgraph command. -# The default value is: NO. -# This tag requires that the tag HAVE_DOT is set to YES. - -CALL_GRAPH = YES - -# If the CALLER_GRAPH tag is set to YES then doxygen will generate a caller -# dependency graph for every global function or class method. -# -# Note that enabling this option will significantly increase the time of a run. -# So in most cases it will be better to enable caller graphs for selected -# functions only using the \callergraph command. -# The default value is: NO. -# This tag requires that the tag HAVE_DOT is set to YES. - -CALLER_GRAPH = YES - -# If the GRAPHICAL_HIERARCHY tag is set to YES then doxygen will graphical -# hierarchy of all classes instead of a textual one. -# The default value is: YES. -# This tag requires that the tag HAVE_DOT is set to YES. - -GRAPHICAL_HIERARCHY = YES - -# If the DIRECTORY_GRAPH tag is set to YES then doxygen will show the -# dependencies a directory has on other directories in a graphical way. The -# dependency relations are determined by the #include relations between the -# files in the directories. -# The default value is: YES. -# This tag requires that the tag HAVE_DOT is set to YES. - -DIRECTORY_GRAPH = YES - -# The DOT_IMAGE_FORMAT tag can be used to set the image format of the images -# generated by dot. -# Note: If you choose svg you need to set HTML_FILE_EXTENSION to xhtml in order -# to make the SVG files visible in IE 9+ (other browsers do not have this -# requirement). -# Possible values are: png, jpg, gif and svg. -# The default value is: png. -# This tag requires that the tag HAVE_DOT is set to YES. - -DOT_IMAGE_FORMAT = svg - -# If DOT_IMAGE_FORMAT is set to svg, then this option can be set to YES to -# enable generation of interactive SVG images that allow zooming and panning. -# -# Note that this requires a modern browser other than Internet Explorer. Tested -# and working are Firefox, Chrome, Safari, and Opera. -# Note: For IE 9+ you need to set HTML_FILE_EXTENSION to xhtml in order to make -# the SVG files visible. Older versions of IE do not have SVG support. -# The default value is: NO. -# This tag requires that the tag HAVE_DOT is set to YES. - -INTERACTIVE_SVG = YES - -# The DOT_PATH tag can be used to specify the path where the dot tool can be -# found. If left blank, it is assumed the dot tool can be found in the path. -# This tag requires that the tag HAVE_DOT is set to YES. - -DOT_PATH = - -# The DOTFILE_DIRS tag can be used to specify one or more directories that -# contain dot files that are included in the documentation (see the \dotfile -# command). -# This tag requires that the tag HAVE_DOT is set to YES. - -DOTFILE_DIRS = - -# The MSCFILE_DIRS tag can be used to specify one or more directories that -# contain msc files that are included in the documentation (see the \mscfile -# command). - -MSCFILE_DIRS = - -# The DIAFILE_DIRS tag can be used to specify one or more directories that -# contain dia files that are included in the documentation (see the \diafile -# command). - -DIAFILE_DIRS = - -# When using plantuml, the PLANTUML_JAR_PATH tag should be used to specify the -# path where java can find the plantuml.jar file. If left blank, it is assumed -# PlantUML is not used or called during a preprocessing step. Doxygen will -# generate a warning when it encounters a \startuml command in this case and -# will not generate output for the diagram. -# This tag requires that the tag HAVE_DOT is set to YES. - -PLANTUML_JAR_PATH = - -# The DOT_GRAPH_MAX_NODES tag can be used to set the maximum number of nodes -# that will be shown in the graph. If the number of nodes in a graph becomes -# larger than this value, doxygen will truncate the graph, which is visualized -# by representing a node as a red box. Note that doxygen if the number of direct -# children of the root node in a graph is already larger than -# DOT_GRAPH_MAX_NODES then the graph will not be shown at all. Also note that -# the size of a graph can be further restricted by MAX_DOT_GRAPH_DEPTH. -# Minimum value: 0, maximum value: 10000, default value: 50. -# This tag requires that the tag HAVE_DOT is set to YES. - -DOT_GRAPH_MAX_NODES = 50 - -# The MAX_DOT_GRAPH_DEPTH tag can be used to set the maximum depth of the graphs -# generated by dot. A depth value of 3 means that only nodes reachable from the -# root by following a path via at most 3 edges will be shown. Nodes that lay -# further from the root node will be omitted. Note that setting this option to 1 -# or 2 may greatly reduce the computation time needed for large code bases. Also -# note that the size of a graph can be further restricted by -# DOT_GRAPH_MAX_NODES. Using a depth of 0 means no depth restriction. -# Minimum value: 0, maximum value: 1000, default value: 0. -# This tag requires that the tag HAVE_DOT is set to YES. - -MAX_DOT_GRAPH_DEPTH = 0 - -# Set the DOT_TRANSPARENT tag to YES to generate images with a transparent -# background. This is disabled by default, because dot on Windows does not seem -# to support this out of the box. -# -# Warning: Depending on the platform used, enabling this option may lead to -# badly anti-aliased labels on the edges of a graph (i.e. they become hard to -# read). -# The default value is: NO. -# This tag requires that the tag HAVE_DOT is set to YES. - -DOT_TRANSPARENT = YES - -# Set the DOT_MULTI_TARGETS tag to YES allow dot to generate multiple output -# files in one run (i.e. multiple -o and -T options on the command line). This -# makes dot run faster, but since only newer versions of dot (>1.8.10) support -# this, this feature is disabled by default. -# The default value is: NO. -# This tag requires that the tag HAVE_DOT is set to YES. - -DOT_MULTI_TARGETS = YES - -# If the GENERATE_LEGEND tag is set to YES doxygen will generate a legend page -# explaining the meaning of the various boxes and arrows in the dot generated -# graphs. -# The default value is: YES. -# This tag requires that the tag HAVE_DOT is set to YES. - -GENERATE_LEGEND = YES - -# If the DOT_CLEANUP tag is set to YES doxygen will remove the intermediate dot -# files that are used to generate the various graphs. -# The default value is: YES. -# This tag requires that the tag HAVE_DOT is set to YES. - -DOT_CLEANUP = YES diff --git a/doc/Reference.md b/doc/Reference.md deleted file mode 100644 index 5b11d98a..00000000 --- a/doc/Reference.md +++ /dev/null @@ -1,53 +0,0 @@ -# Reference - -## Nomenclature - -We use the term "JSON" when we mean the [JavaScript Object Notation](http://json.org); that is, the file format. When we talk about the class implementing our library, we use "`json`" (typewriter font). Instances of this class are called "`json` values" to differentiate them from "JSON objects"; that is, unordered mappings, hashes, and whatnot. - -## Types and default values - -This table describes how JSON values are mapped to C++ types. - -| JSON type | value_type | C++ type | type alias | default value | -| ----------------------- | -------------------------- | ----------------------------- | ---------------------- | -------------- -| null | `value_type::null` | `nullptr_t` | - | `nullptr` | -| string | `value_type::string` | `std::string` | `json::string_t` | `""` | -| number (integer) | `value_type::number` | `int` | `json::number_t` | `0` | -| number (floating point) | `value_type::number_float` | `double` | `json::number_float_t` | `0.0` | -| array | `value_type::array ` | `std::array<json>` | `json::array_t` | `{}` | -| object | `value_type::object` | `std::map<std::string, json>` | `json::object_t` | `{}` | - -The second column list entries of an enumeration `value_type` which can be queried by calling `type()` on a `json` value. The column "C++ types" list the internal type that is used to represent the respective JSON value. The "type alias" column furthermore lists type aliases that are used in the `json` class to allow for more flexibility. The last column list the default value; that is, the value that is set if none is passed to the constructor or that is set if `clear()` is called. - -## Type conversions - -There are only a few type conversions possible: - -- An integer number can be translated to a floating point number. -- A floating point number can be translated to an integer number. Note the number is truncated and not rounded, ceiled or floored. -- Any value (i.e., boolean, string, number, null) but JSON objects can be translated into an array. The result is a singleton array that consists of the value before. -- Any other conversion will throw a `std::logic_error` exception. - -When compatible, `json` values **implicitly convert** to `std::string`, `int`, `double`, `json::array_t`, and `json::object_t`. Furthermore, **explicit type conversion** is possible using the `get<>()` function with the aforementioned types. - -## Initialization - -`json` values can be created from many literals and variable types: - -| JSON type | literal/variable types | examples | -| --------- | ---------------------- | -------- | -| none | null pointer literal, `nullptr_t` type, no value | `nullptr` | -| boolean | boolean literals, `bool` type, `json::boolean_t` type | `true`, `false` | -| string | string literal, `char*` type, `std::string` type, `std::string&&` rvalue reference, `json::string_t` type | `"Hello"` | -| number (integer) | integer literal, `short int` type, `int` type, `json_number_t` type | `42` | -| number (floating point) | floating point literal, `float` type, `double` type, `json::number_float_t` type | `3.141529` -| array | initializer list whose elements are `json` values (or can be translated into `json` values using the rules above), `std::vector<json>` type, `json::array_t` type, `json::array_t&&` rvalue reference | `{1, 2, 3, true, "foo"}` | -| object | initializer list whose elements are pairs of a string literal and a `json` value (or can be translated into `json` values using the rules above), `std::map<std::string, json>` type, `json::object_t` type, `json::object_t&&` rvalue reference | `{ {"key1", 42}, {"key2", false} }` | - -## Number types - -[](http://json.org) - -The JSON specification explicitly does not define an internal number representation, but only the syntax of how numbers can be written down. Consequently, we would need to use the largest possible floating point number format (e.g., `long double`) to internally store JSON numbers. - -However, this would be a waste of space, so we let the JSON parser decide which format to use: If the number can be precisely stored in an `int`, we use an `int` to store it. However, if it is a floating point number, we use `double` to store it. diff --git a/header_only/json.h b/header_only/json.h deleted file mode 100644 index 517baea1..00000000 --- a/header_only/json.h +++ /dev/null @@ -1,3174 +0,0 @@ -/*! -@file -@copyright The code is licensed under the MIT License - <http://opensource.org/licenses/MIT>, - Copyright (c) 2013-2015 Niels Lohmann. - -@author Niels Lohmann <http://nlohmann.me> - -@see https://github.com/nlohmann/json -*/ - -#pragma once - -#include <initializer_list> // std::initializer_list -#include <iostream> // std::istream, std::ostream -#include <map> // std::map -#include <string> // std::string -#include <vector> // std::vector -#include <iterator> // std::iterator -#include <limits> // std::numeric_limits -#include <functional> // std::hash - -namespace nlohmann -{ - -/*! -@brief JSON for Modern C++ - -The size of a JSON object is 16 bytes: 8 bytes for the value union whose -largest item is a pointer type and another 8 byte for an element of the -type union. The latter only needs 1 byte - the remaining 7 bytes are wasted -due to alignment. - -@see http://stackoverflow.com/questions/7758580/writing-your-own-stl-container/7759622#7759622 - -@bug Numbers are currently handled too generously. There are several formats - that are forbidden by the standard, but are accepted by the parser. - -@todo Implement json::insert(), json::emplace(), json::emplace_back, json::erase -*/ -class json -{ - public: - // forward declaration to friend this class - class iterator; - class const_iterator; - - public: - // container types - /// the type of elements in a JSON class - using value_type = json; - /// the type of element references - using reference = json&; - /// the type of const element references - using const_reference = const json&; - /// the type of pointers to elements - using pointer = json*; - /// the type of const pointers to elements - using const_pointer = const json*; - /// a type to represent differences between iterators - using difference_type = std::ptrdiff_t; - /// a type to represent container sizes - using size_type = std::size_t; - /// an iterator for a JSON container - using iterator = json::iterator; - /// a const iterator for a JSON container - using const_iterator = json::const_iterator; - /// a reverse iterator for a JSON container - using reverse_iterator = std::reverse_iterator<iterator>; - /// a const reverse iterator for a JSON container - using const_reverse_iterator = std::reverse_iterator<const_iterator>; - - /// a type for an object - using object_t = std::map<std::string, json>; - /// a type for an array - using array_t = std::vector<json>; - /// a type for a string - using string_t = std::string; - /// a type for a Boolean - using boolean_t = bool; - /// a type for an integer number - using number_t = int64_t; - /// a type for a floating point number - using number_float_t = double; - /// a type for list initialization - using list_init_t = std::initializer_list<json>; - - /// a JSON value - union value - { - /// array as pointer to array_t - array_t* array; - /// object as pointer to object_t - object_t* object; - /// string as pointer to string_t - string_t* string; - /// Boolean - boolean_t boolean; - /// number (integer) - number_t number; - /// number (float) - number_float_t number_float; - - /// default constructor - value() = default; - /// constructor for arrays - value(array_t*); - /// constructor for objects - value(object_t*); - /// constructor for strings - value(string_t*); - /// constructor for Booleans - value(boolean_t); - /// constructor for numbers (integer) - value(number_t); - /// constructor for numbers (float) - value(number_float_t); - }; - - /// possible types of a JSON object - enum class value_t : uint8_t - { - /// ordered collection of values - array = 0, - /// unordered set of name/value pairs - object, - /// null value - null, - /// string value - string, - /// Boolean value - boolean, - /// number value (integer) - number, - /// number value (float) - number_float - }; - - public: - /// create an object according to given type - json(const value_t); - /// create a null object - json() noexcept; - /// create a null object - json(std::nullptr_t) noexcept; - /// create a string object from a C++ string - json(const std::string&); - /// create a string object from a C++ string (move) - json(std::string&&); - /// create a string object from a C string - json(const char*); - /// create a Boolean object - json(const bool) noexcept; - /// create an array - json(const array_t&); - /// create an array (move) - json(array_t&&); - /// create an object - json(const object_t&); - /// create an object (move) - json(object_t&&); - /// create from an initializer list (to an array or object) - json(list_init_t, bool = true, value_t = value_t::array); - - /*! - @brief create a number object (integer) - @param n an integer number to wrap in a JSON object - */ - template<typename T, typename - std::enable_if< - std::numeric_limits<T>::is_integer, T>::type - = 0> - json(const T n) noexcept - : final_type_(0), type_(value_t::number), - value_(static_cast<number_t>(n)) - {} - - /*! - @brief create a number object (float) - @param n a floating point number to wrap in a JSON object - */ - template<typename T, typename = typename - std::enable_if< - std::is_floating_point<T>::value>::type - > - json(const T n) noexcept - : final_type_(0), type_(value_t::number_float), - value_(static_cast<number_float_t>(n)) - {} - - /*! - @brief create an array object - @param v any type of container whose elements can be use to construct - JSON objects (e.g., std::vector, std::set, std::array) - @note For some reason, we need to explicitly forbid JSON iterator types. - */ - template <class V, typename - std::enable_if< - not std::is_same<V, json::iterator>::value and - not std::is_same<V, json::const_iterator>::value and - not std::is_same<V, json::reverse_iterator>::value and - not std::is_same<V, json::const_reverse_iterator>::value and - std::is_constructible<json, typename V::value_type>::value, int>::type - = 0> - json(const V& v) : json(array_t(v.begin(), v.end())) - {} - - /*! - @brief create a JSON object - @param v any type of associative container whose elements can be use to - construct JSON objects (e.g., std::map<std::string, *>) - */ - template <class V, typename - std::enable_if< - std::is_constructible<std::string, typename V::key_type>::value and - std::is_constructible<json, typename V::mapped_type>::value, int>::type - = 0> - json(const V& v) : json(object_t(v.begin(), v.end())) - {} - - /// copy constructor - json(const json&); - /// move constructor - json(json&&) noexcept; - - /// copy assignment - json& operator=(json) noexcept; - - /// destructor - ~json() noexcept; - - /// explicit keyword to force array creation - static json array(list_init_t = list_init_t()); - /// explicit keyword to force object creation - static json object(list_init_t = list_init_t()); - - /// create from string representation - static json parse(const std::string&); - /// create from string representation - static json parse(const char*); - - private: - /// return the type as string - std::string type_name() const noexcept; - - /// dump the object (with pretty printer) - std::string dump(const bool, const unsigned int, unsigned int = 0) const noexcept; - /// replaced a character in a string with another string - void replaceChar(std::string& str, char c, const std::string& replacement) const; - /// escapes special characters to safely dump the string - std::string escapeString(const std::string&) const; - - public: - /// explicit value conversion - template<typename T> - T get() const; - - /// implicit conversion to string representation - operator std::string() const; - /// implicit conversion to integer (only for numbers) - operator int() const; - /// implicit conversion to integer (only for numbers) - operator int64_t() const; - /// implicit conversion to double (only for numbers) - operator double() const; - /// implicit conversion to Boolean (only for Booleans) - operator bool() const; - /// implicit conversion to JSON vector (not for objects) - operator array_t() const; - /// implicit conversion to JSON map (only for objects) - operator object_t() const; - - /// serialize to stream - friend std::ostream& operator<<(std::ostream& o, const json& j) - { - o << j.dump(); - return o; - } - /// serialize to stream - friend std::ostream& operator>>(const json& j, std::ostream& o) - { - o << j.dump(); - return o; - } - - /// deserialize from stream - friend std::istream& operator>>(std::istream& i, json& j) - { - j = parser(i).parse(); - return i; - } - /// deserialize from stream - friend std::istream& operator<<(json& j, std::istream& i) - { - j = parser(i).parse(); - return i; - } - - /// explicit serialization - std::string dump(int = -1) const noexcept; - - /// add constructible objects to an array - template<class T, typename std::enable_if<std::is_constructible<json, T>::value>::type = 0> - json & operator+=(const T& o) - { - push_back(json(o)); - return *this; - } - - /// add an object/array to an array - json& operator+=(const json&); - - /// add a pair to an object - json& operator+=(const object_t::value_type&); - /// add a list of elements to array or list of pairs to object - json& operator+=(list_init_t); - - /// add constructible objects to an array - template<class T, typename std::enable_if<std::is_constructible<json, T>::value>::type = 0> - void push_back(const T& o) - { - push_back(json(o)); - } - - /// add an object/array to an array - void push_back(const json&); - /// add an object/array to an array (move) - void push_back(json&&); - - /// add a pair to an object - void push_back(const object_t::value_type&); - /// add a list of elements to array or list of pairs to object - void push_back(list_init_t); - - /// operator to set an element in an array - reference operator[](const int); - /// operator to get an element in an array - const_reference operator[](const int) const; - /// operator to get an element in an array - reference at(const int); - /// operator to get an element in an array - const_reference at(const int) const; - - /// operator to set an element in an object - reference operator[](const std::string&); - /// operator to set an element in an object - reference operator[](const char*); - /// operator to get an element in an object - const_reference operator[](const std::string&) const; - /// operator to get an element in an object - const_reference operator[](const char*) const; - /// operator to set an element in an object - reference at(const std::string&); - /// operator to set an element in an object - reference at(const char*); - /// operator to get an element in an object - const_reference at(const std::string&) const; - /// operator to get an element in an object - const_reference at(const char*) const; - - /// return the number of stored values - size_type size() const noexcept; - /// return the maximal number of values that can be stored - size_type max_size() const noexcept; - /// checks whether object is empty - bool empty() const noexcept; - /// removes all elements from compounds and resets values to default - void clear() noexcept; - - /// swaps content with other object - void swap(json&) noexcept; - - /// return the type of the object - value_t type() const noexcept; - - /// find an element in an object (returns end() iterator otherwise) - iterator find(const std::string&); - /// find an element in an object (returns end() iterator otherwise) - const_iterator find(const std::string&) const; - /// find an element in an object (returns end() iterator otherwise) - iterator find(const char*); - /// find an element in an object (returns end() iterator otherwise) - const_iterator find(const char*) const; - - /// lexicographically compares the values - bool operator==(const json&) const noexcept; - /// lexicographically compares the values - bool operator!=(const json&) const noexcept; - - /// returns an iterator to the beginning (array/object) - iterator begin() noexcept; - /// returns an iterator to the end (array/object) - iterator end() noexcept; - /// returns an iterator to the beginning (array/object) - const_iterator begin() const noexcept; - /// returns an iterator to the end (array/object) - const_iterator end() const noexcept; - /// returns an iterator to the beginning (array/object) - const_iterator cbegin() const noexcept; - /// returns an iterator to the end (array/object) - const_iterator cend() const noexcept; - /// returns a reverse iterator to the beginning - reverse_iterator rbegin() noexcept; - /// returns a reverse iterator to the end - reverse_iterator rend() noexcept; - /// returns a reverse iterator to the beginning - const_reverse_iterator crbegin() const noexcept; - /// returns a reverse iterator to the end - const_reverse_iterator crend() const noexcept; - - private: - /// whether the type is final - unsigned final_type_ : 1; - /// the type of this object - value_t type_ = value_t::null; - /// the payload - value value_ {}; - - public: - /// an iterator - class iterator : public std::iterator<std::bidirectional_iterator_tag, json> - { - friend class json; - friend class json::const_iterator; - - public: - iterator() = default; - iterator(json*, bool); - iterator(const iterator&); - ~iterator(); - - iterator& operator=(iterator); - bool operator==(const iterator&) const; - bool operator!=(const iterator&) const; - iterator& operator++(); - iterator& operator--(); - json& operator*() const; - json* operator->() const; - - /// getter for the key (in case of objects) - std::string key() const; - /// getter for the value - json& value() const; - - private: - /// a JSON value - json* object_ = nullptr; - /// an iterator for JSON arrays - array_t::iterator* vi_ = nullptr; - /// an iterator for JSON objects - object_t::iterator* oi_ = nullptr; - /// whether iterator points to a valid object - bool invalid = true; - }; - - /// a const iterator - class const_iterator : public std::iterator<std::bidirectional_iterator_tag, const json> - { - friend class json; - - public: - const_iterator() = default; - const_iterator(const json*, bool); - const_iterator(const const_iterator&); - const_iterator(const json::iterator&); - ~const_iterator(); - - const_iterator& operator=(const_iterator); - bool operator==(const const_iterator&) const; - bool operator!=(const const_iterator&) const; - const_iterator& operator++(); - const_iterator& operator--(); - const json& operator*() const; - const json* operator->() const; - - /// getter for the key (in case of objects) - std::string key() const; - /// getter for the value - const json& value() const; - - private: - /// a JSON value - const json* object_ = nullptr; - /// an iterator for JSON arrays - array_t::const_iterator* vi_ = nullptr; - /// an iterator for JSON objects - object_t::const_iterator* oi_ = nullptr; - /// whether iterator reached past the end - bool invalid = true; - }; - - private: - /// a helper class to parse a JSON object - class parser - { - public: - /// a parser reading from a C string - parser(const char*); - /// a parser reading from a C++ string - parser(const std::string&); - /// a parser reading from an input stream - parser(std::istream&); - /// destructor of the parser - ~parser() = default; - - // no copy constructor - parser(const parser&) = delete; - // no copy assignment - parser& operator=(parser) = delete; - - /// parse and return a JSON object - json parse(); - - private: - /// read the next character, stripping whitespace - bool next(); - /// raise an exception with an error message - [[noreturn]] inline void error(const std::string&) const; - /// parse a quoted string - inline std::string parseString(); - /// transforms a unicode codepoint to it's UTF-8 presentation - std::string codePointToUTF8(unsigned int codePoint) const; - /// parses 4 hex characters that represent a unicode code point - inline unsigned int parse4HexCodePoint(); - /// parses \uXXXX[\uXXXX] unicode escape characters - inline std::string parseUnicodeEscape(); - /// parse a Boolean "true" - inline void parseTrue(); - /// parse a Boolean "false" - inline void parseFalse(); - /// parse a null object - inline void parseNull(); - /// a helper function to expect a certain character - inline void expect(const char); - - private: - /// a buffer of the input - std::string buffer_ {}; - /// the current character - char current_ {}; - /// the position inside the input buffer - std::size_t pos_ = 0; - }; -}; - -} - -/// user-defined literal operator to create JSON objects from strings -nlohmann::json operator "" _json(const char*, std::size_t); - -// specialization of std::swap, and std::hash -namespace std -{ -template <> -/// swaps the values of two JSON objects -inline void swap(nlohmann::json& j1, - nlohmann::json& j2) noexcept( - is_nothrow_move_constructible<nlohmann::json>::value and - is_nothrow_move_assignable<nlohmann::json>::value - ) -{ - j1.swap(j2); -} - -template <> -/// hash value for JSON objects -struct hash<nlohmann::json> -{ - size_t operator()(const nlohmann::json& j) const - { - // a naive hashing via the string representation - return hash<std::string>()(j.dump()); - } -}; - -} -/*! -@file -@copyright The code is licensed under the MIT License - <http://opensource.org/licenses/MIT>, - Copyright (c) 2013-2015 Niels Lohmann. - -@author Niels Lohmann <http://nlohmann.me> - -@see https://github.com/nlohmann/json -*/ - - - -#include <cctype> // std::isdigit, std::isspace -#include <cstddef> // std::size_t -#include <stdexcept> // std::runtime_error -#include <utility> // std::swap, std::move - -namespace nlohmann -{ - -/////////////////////////////////// -// CONSTRUCTORS OF UNION "value" // -/////////////////////////////////// - -json::value::value(array_t* _array): array(_array) {} -json::value::value(object_t* object_): object(object_) {} -json::value::value(string_t* _string): string(_string) {} -json::value::value(boolean_t _boolean) : boolean(_boolean) {} -json::value::value(number_t _number) : number(_number) {} -json::value::value(number_float_t _number_float) : number_float(_number_float) {} - - -///////////////////////////////// -// CONSTRUCTORS AND DESTRUCTOR // -///////////////////////////////// - -/*! -Construct an empty JSON given the type. - -@param t the type from the @ref json::type enumeration. - -@post Memory for array, object, and string are allocated. -*/ -json::json(const value_t t) - : type_(t) -{ - switch (type_) - { - case (value_t::array): - { - value_.array = new array_t(); - break; - } - case (value_t::object): - { - value_.object = new object_t(); - break; - } - case (value_t::string): - { - value_.string = new string_t(); - break; - } - case (value_t::boolean): - { - value_.boolean = boolean_t(); - break; - } - case (value_t::number): - { - value_.number = number_t(); - break; - } - case (value_t::number_float): - { - value_.number_float = number_float_t(); - break; - } - default: - { - break; - } - } -} - -json::json() noexcept : final_type_(0), type_(value_t::null) -{} - -/*! -Construct a null JSON object. -*/ -json::json(std::nullptr_t) noexcept : json() -{} - -/*! -Construct a string JSON object. - -@param s a string to initialize the JSON object with -*/ -json::json(const std::string& s) - : final_type_(0), type_(value_t::string), value_(new string_t(s)) -{} - -json::json(std::string&& s) - : final_type_(0), type_(value_t::string), value_(new string_t(std::move(s))) -{} - -json::json(const char* s) - : final_type_(0), type_(value_t::string), value_(new string_t(s)) -{} - -json::json(const bool b) noexcept - : final_type_(0), type_(value_t::boolean), value_(b) -{} - -json::json(const array_t& a) - : final_type_(0), type_(value_t::array), value_(new array_t(a)) -{} - -json::json(array_t&& a) - : final_type_(0), type_(value_t::array), value_(new array_t(std::move(a))) -{} - -json::json(const object_t& o) - : final_type_(0), type_(value_t::object), value_(new object_t(o)) -{} - -json::json(object_t&& o) - : final_type_(0), type_(value_t::object), value_(new object_t(std::move(o))) -{} - -/*! -This function is a bit tricky as it uses an initializer list of JSON objects -for both arrays and objects. This is not supported by C++, so we use the -following trick. Both initializer lists for objects and arrays will transform -to a list of JSON objects. The only difference is that in case of an object, -the list will contain JSON array objects with two elements - one for the key -and one for the value. As a result, it is sufficient to check if each element -of the initializer list is an array (1) with two elements (2) whose first -element is of type string (3). If this is the case, we treat the whole -initializer list as list of pairs to construct an object. If not, we pass it -as is to create an array. - -@bug With the described approach, we would fail to recognize an array whose - first element is again an arrays as array. - -@param a an initializer list to create from -@param type_deduction whether the type (array/object) shall eb deducted -@param manual_type if type deduction is switched of, pass a manual type -*/ -json::json(list_init_t a, bool type_deduction, value_t manual_type) : final_type_(0) -{ - // the initializer list could describe an object - bool is_object = true; - - // check if each element is an array with two elements whose first element - // is a string - for (const auto& element : a) - { - if ((element.final_type_ == 1 and element.type_ == value_t::array) - or (element.type_ != value_t::array or element.size() != 2 or element[0].type_ != value_t::string)) - { - // we found an element that makes it impossible to use the - // initializer list as object - is_object = false; - break; - } - } - - // adjust type if type deduction is not wanted - if (not type_deduction) - { - // mark this object's type as final - final_type_ = 1; - - // if array is wanted, do not create an object though possible - if (manual_type == value_t::array) - { - is_object = false; - } - - // if object is wanted but impossible, throw an exception - if (manual_type == value_t::object and not is_object) - { - throw std::logic_error("cannot create JSON object"); - } - } - - if (is_object) - { - // the initializer list is a list of pairs -> create object - type_ = value_t::object; - value_ = new object_t(); - for (auto& element : a) - { - value_.object->emplace(std::make_pair(std::move(element[0]), std::move(element[1]))); - } - } - else - { - // the initializer list describes an array -> create array - type_ = value_t::array; - value_ = new array_t(std::move(a)); - } -} - -/*! -@param a initializer list to create an array from -@return array -*/ -json json::array(list_init_t a) -{ - return json(a, false, value_t::array); -} - -/*! -@param a initializer list to create an object from -@return object -*/ -json json::object(list_init_t a) -{ - // if more than one element is in the initializer list, wrap it - if (a.size() > 1) - { - return json({a}, false, value_t::object); - } - else - { - return json(a, false, value_t::object); - } -} - -/*! -A copy constructor for the JSON class. - -@param o the JSON object to copy -*/ -json::json(const json& o) - : type_(o.type_) -{ - switch (type_) - { - case (value_t::array): - { - value_.array = new array_t(*o.value_.array); - break; - } - case (value_t::object): - { - value_.object = new object_t(*o.value_.object); - break; - } - case (value_t::string): - { - value_.string = new string_t(*o.value_.string); - break; - } - case (value_t::boolean): - { - value_.boolean = o.value_.boolean; - break; - } - case (value_t::number): - { - value_.number = o.value_.number; - break; - } - case (value_t::number_float): - { - value_.number_float = o.value_.number_float; - break; - } - default: - { - break; - } - } -} - -/*! -A move constructor for the JSON class. - -@param o the JSON object to move - -@post The JSON object \p o is invalidated. -*/ -json::json(json&& o) noexcept - : type_(std::move(o.type_)), value_(std::move(o.value_)) -{ - // invalidate payload - o.type_ = value_t::null; - o.value_ = {}; -} - -/*! -A copy assignment operator for the JSON class, following the copy-and-swap -idiom. - -@param o A JSON object to assign to this object. -*/ -json& json::operator=(json o) noexcept -{ - std::swap(type_, o.type_); - std::swap(value_, o.value_); - return *this; -} - -json::~json() noexcept -{ - switch (type_) - { - case (value_t::array): - { - delete value_.array; - break; - } - case (value_t::object): - { - delete value_.object; - break; - } - case (value_t::string): - { - delete value_.string; - break; - } - default: - { - // nothing to do for non-pointer types - break; - } - } -} - -/*! -@param s a string representation of a JSON object -@return a JSON object -*/ -json json::parse(const std::string& s) -{ - return parser(s).parse(); -} - -/*! -@param s a string representation of a JSON object -@return a JSON object -*/ -json json::parse(const char* s) -{ - return parser(s).parse(); -} - - -std::string json::type_name() const noexcept -{ - switch (type_) - { - case (value_t::array): - { - return "array"; - } - case (value_t::object): - { - return "object"; - } - case (value_t::null): - { - return "null"; - } - case (value_t::string): - { - return "string"; - } - case (value_t::boolean): - { - return "boolean"; - } - default: - { - return "number"; - } - } -} - - -/////////////////////////////// -// OPERATORS AND CONVERSIONS // -/////////////////////////////// - -/*! -@exception std::logic_error if the function is called for JSON objects whose - type is not string -*/ -template<> -std::string json::get() const -{ - switch (type_) - { - case (value_t::string): - return *value_.string; - default: - throw std::logic_error("cannot cast " + type_name() + " to JSON string"); - } -} - -/*! -@exception std::logic_error if the function is called for JSON objects whose - type is not number (int or float) -*/ -template<> -int json::get() const -{ - switch (type_) - { - case (value_t::number): - return value_.number; - case (value_t::number_float): - return static_cast<int>(value_.number_float); - default: - throw std::logic_error("cannot cast " + type_name() + " to JSON number"); - } -} - -/*! -@exception std::logic_error if the function is called for JSON objects whose - type is not number (int or float) -*/ -template<> -int64_t json::get() const -{ - switch (type_) - { - case (value_t::number): - return value_.number; - case (value_t::number_float): - return static_cast<number_t>(value_.number_float); - default: - throw std::logic_error("cannot cast " + type_name() + " to JSON number"); - } -} - -/*! -@exception std::logic_error if the function is called for JSON objects whose - type is not number (int or float) -*/ -template<> -double json::get() const -{ - switch (type_) - { - case (value_t::number): - return static_cast<number_float_t>(value_.number); - case (value_t::number_float): - return value_.number_float; - default: - throw std::logic_error("cannot cast " + type_name() + " to JSON number"); - } -} - -/*! -@exception std::logic_error if the function is called for JSON objects whose - type is not boolean -*/ -template<> -bool json::get() const -{ - switch (type_) - { - case (value_t::boolean): - return value_.boolean; - default: - throw std::logic_error("cannot cast " + type_name() + " to JSON Boolean"); - } -} - -/*! -@exception std::logic_error if the function is called for JSON objects whose - type is an object -*/ -template<> -json::array_t json::get() const -{ - if (type_ == value_t::array) - { - return *value_.array; - } - if (type_ == value_t::object) - { - throw std::logic_error("cannot cast " + type_name() + " to JSON array"); - } - - array_t result; - result.push_back(*this); - return result; -} - -/*! -@exception std::logic_error if the function is called for JSON objects whose - type is not object -*/ -template<> -json::object_t json::get() const -{ - if (type_ == value_t::object) - { - return *value_.object; - } - else - { - throw std::logic_error("cannot cast " + type_name() + " to JSON object"); - } -} - -json::operator std::string() const -{ - return get<std::string>(); -} - -json::operator int() const -{ - return get<int>(); -} - -json::operator int64_t() const -{ - return get<int64_t>(); -} - -json::operator double() const -{ - return get<double>(); -} - -json::operator bool() const -{ - return get<bool>(); -} - -json::operator array_t() const -{ - return get<array_t>(); -} - -json::operator object_t() const -{ - return get<object_t>(); -} - -/*! -Internal implementation of the serialization function. - -\param prettyPrint whether the output shall be pretty-printed -\param indentStep the indent level -\param currentIndent the current indent level (only used internally) -*/ -std::string json::dump(const bool prettyPrint, const unsigned int indentStep, - unsigned int currentIndent) const noexcept -{ - // helper function to return whitespace as indentation - const auto indent = [prettyPrint, ¤tIndent]() - { - return prettyPrint ? std::string(currentIndent, ' ') : std::string(); - }; - - switch (type_) - { - case (value_t::string): - { - return std::string("\"") + escapeString(*value_.string) + "\""; - } - - case (value_t::boolean): - { - return value_.boolean ? "true" : "false"; - } - - case (value_t::number): - { - return std::to_string(value_.number); - } - - case (value_t::number_float): - { - return std::to_string(value_.number_float); - } - - case (value_t::array): - { - if (value_.array->empty()) - { - return "[]"; - } - - std::string result = "["; - - // increase indentation - if (prettyPrint) - { - currentIndent += indentStep; - result += "\n"; - } - - for (array_t::const_iterator i = value_.array->begin(); i != value_.array->end(); ++i) - { - if (i != value_.array->begin()) - { - result += prettyPrint ? ",\n" : ","; - } - result += indent() + i->dump(prettyPrint, indentStep, currentIndent); - } - - // decrease indentation - if (prettyPrint) - { - currentIndent -= indentStep; - result += "\n"; - } - - return result + indent() + "]"; - } - - case (value_t::object): - { - if (value_.object->empty()) - { - return "{}"; - } - - std::string result = "{"; - - // increase indentation - if (prettyPrint) - { - currentIndent += indentStep; - result += "\n"; - } - - for (object_t::const_iterator i = value_.object->begin(); i != value_.object->end(); ++i) - { - if (i != value_.object->begin()) - { - result += prettyPrint ? ",\n" : ","; - } - result += indent() + "\"" + i->first + "\":" + (prettyPrint ? " " : "") + i->second.dump( - prettyPrint, indentStep, - currentIndent); - } - - // decrease indentation - if (prettyPrint) - { - currentIndent -= indentStep; - result += "\n"; - } - - return result + indent() + "}"; - } - - // actually only value_t::null - but making the compiler happy - default: - { - return "null"; - } - } -} - -/*! -Internal function to replace all occurrences of a character in a given string -with another string. - -\param str the string that contains tokens to replace -\param c the character that needs to be replaced -\param replacement the string that is the replacement for the character -*/ -void json::replaceChar(std::string& str, char c, const std::string& replacement) -const -{ - size_t start_pos = 0; - while ((start_pos = str.find(c, start_pos)) != std::string::npos) - { - str.replace(start_pos, 1, replacement); - start_pos += replacement.length(); - } -} - -/*! -Escapes all special characters in the given string according to ECMA-404. -Necessary as some characters such as quotes, backslashes and so on -can't be used as is when dumping a string value. - -\param str the string that should be escaped. - -\return a copy of the given string with all special characters escaped. -*/ -std::string json::escapeString(const std::string& str) const -{ - std::string result(str); - // we first need to escape the backslashes as all other methods will insert - // legitimate backslashes into the result. - replaceChar(result, '\\', "\\\\"); - // replace all other characters - replaceChar(result, '"', "\\\""); - replaceChar(result, '\n', "\\n"); - replaceChar(result, '\r', "\\r"); - replaceChar(result, '\f', "\\f"); - replaceChar(result, '\b', "\\b"); - replaceChar(result, '\t', "\\t"); - return result; -} - -/*! -Serialization function for JSON objects. The function tries to mimick Python's -\p json.dumps() function, and currently supports its \p indent parameter. - -\param indent if indent is nonnegative, then array elements and object members - will be pretty-printed with that indent level. An indent level - of 0 will only insert newlines. -1 (the default) selects the - most compact representation - -\see https://docs.python.org/2/library/json.html#json.dump -*/ -std::string json::dump(int indent) const noexcept -{ - if (indent >= 0) - { - return dump(true, static_cast<unsigned int>(indent)); - } - else - { - return dump(false, 0); - } -} - - -/////////////////////////////////////////// -// ADDING ELEMENTS TO OBJECTS AND ARRAYS // -/////////////////////////////////////////// - -json& json::operator+=(const json& o) -{ - push_back(o); - return *this; -} - -/*! -@todo comment me -*/ -json& json::operator+=(const object_t::value_type& p) -{ - return operator[](p.first) = p.second; -} - -/*! -@todo comment me -*/ -json& json::operator+=(list_init_t a) -{ - push_back(a); - return *this; -} - -/*! -This function implements the actual "adding to array" function and is called -by all other push_back or operator+= functions. If the function is called for -an array, the passed element is added to the array. - -@param o The element to add to the array. - -@pre The JSON object is an array or null. -@post The JSON object is an array whose last element is the passed element o. -@exception std::runtime_error The function was called for a JSON type that - does not support addition to an array (e.g., int or string). - -@note Null objects are silently transformed into an array before the addition. -*/ -void json::push_back(const json& o) -{ - // push_back only works for null objects or arrays - if (not(type_ == value_t::null or type_ == value_t::array)) - { - throw std::runtime_error("cannot add element to " + type_name()); - } - - // transform null object into an array - if (type_ == value_t::null) - { - type_ = value_t::array; - value_.array = new array_t; - } - - // add element to array - value_.array->push_back(o); -} - -/*! -This function implements the actual "adding to array" function and is called -by all other push_back or operator+= functions. If the function is called for -an array, the passed element is added to the array using move semantics. - -@param o The element to add to the array. - -@pre The JSON object is an array or null. -@post The JSON object is an array whose last element is the passed element o. -@post The element o is destroyed. -@exception std::runtime_error The function was called for a JSON type that - does not support addition to an array (e.g., int or string). - -@note Null objects are silently transformed into an array before the addition. -@note This function applies move semantics for the given element. -*/ -void json::push_back(json&& o) -{ - // push_back only works for null objects or arrays - if (not(type_ == value_t::null or type_ == value_t::array)) - { - throw std::runtime_error("cannot add element to " + type_name()); - } - - // transform null object into an array - if (type_ == value_t::null) - { - type_ = value_t::array; - value_.array = new array_t; - } - - // add element to array (move semantics) - value_.array->emplace_back(std::move(o)); - // invalidate object - o.type_ = value_t::null; -} - -/*! -@todo comment me -*/ -void json::push_back(const object_t::value_type& p) -{ - operator[](p.first) = p.second; -} - -/*! -@todo comment me -*/ -void json::push_back(list_init_t a) -{ - bool is_array = false; - - // check if each element is an array with two elements whose first element - // is a string - for (const auto& element : a) - { - if (element.type_ != value_t::array or - element.size() != 2 or - element[0].type_ != value_t::string) - { - // the initializer list describes an array - is_array = true; - break; - } - } - - if (is_array) - { - for (const json& element : a) - { - push_back(element); - } - } - else - { - for (const json& element : a) - { - const object_t::value_type tmp {element[0].get<std::string>(), element[1]}; - push_back(tmp); - } - } -} - -/*! -This operator realizes read/write access to array elements given an integer -index. Bounds will not be checked. - -@note The "index" variable should be of type size_t as it is compared against - size() and used in the at() function. However, the compiler will have - problems in case integer literals are used. In this case, an implicit - conversion to both size_t and JSON is possible. Therefore, we use int as - type and convert it to size_t where necessary. - -@param index the index of the element to return from the array -@return reference to element for the given index - -@pre Object is an array. -@exception std::domain_error if object is not an array -*/ -json::reference json::operator[](const int index) -{ - // this [] operator only works for arrays - if (type_ != value_t::array) - { - throw std::domain_error("cannot add entry with index " + - std::to_string(index) + " to " + type_name()); - } - - // return reference to element from array at given index - return (*value_.array)[static_cast<std::size_t>(index)]; -} - -/*! -This operator realizes read-only access to array elements given an integer -index. Bounds will not be checked. - -@note The "index" variable should be of type size_t as it is compared against - size() and used in the at() function. However, the compiler will have - problems in case integer literals are used. In this case, an implicit - conversion to both size_t and JSON is possible. Therefore, we use int as - type and convert it to size_t where necessary. - -@param index the index of the element to return from the array -@return read-only reference to element for the given index - -@pre Object is an array. -@exception std::domain_error if object is not an array -*/ -json::const_reference json::operator[](const int index) const -{ - // this [] operator only works for arrays - if (type_ != value_t::array) - { - throw std::domain_error("cannot get entry with index " + - std::to_string(index) + " from " + type_name()); - } - - // return element from array at given index - return (*value_.array)[static_cast<std::size_t>(index)]; -} - -/*! -This function realizes read/write access to array elements given an integer -index. Bounds will be checked. - -@note The "index" variable should be of type size_t as it is compared against - size() and used in the at() function. However, the compiler will have - problems in case integer literals are used. In this case, an implicit - conversion to both size_t and JSON is possible. Therefore, we use int as - type and convert it to size_t where necessary. - -@param index the index of the element to return from the array -@return reference to element for the given index - -@pre Object is an array. -@exception std::domain_error if object is not an array -@exception std::out_of_range if index is out of range (via std::vector::at) -*/ -json::reference json::at(const int index) -{ - // this function only works for arrays - if (type_ != value_t::array) - { - throw std::domain_error("cannot add entry with index " + - std::to_string(index) + " to " + type_name()); - } - - // return reference to element from array at given index - return value_.array->at(static_cast<std::size_t>(index)); -} - -/*! -This operator realizes read-only access to array elements given an integer -index. Bounds will be checked. - -@note The "index" variable should be of type size_t as it is compared against - size() and used in the at() function. However, the compiler will have - problems in case integer literals are used. In this case, an implicit - conversion to both size_t and JSON is possible. Therefore, we use int as - type and convert it to size_t where necessary. - -@param index the index of the element to return from the array -@return read-only reference to element for the given index - -@pre Object is an array. -@exception std::domain_error if object is not an array -@exception std::out_of_range if index is out of range (via std::vector::at) -*/ -json::const_reference json::at(const int index) const -{ - // this function only works for arrays - if (type_ != value_t::array) - { - throw std::domain_error("cannot get entry with index " + - std::to_string(index) + " from " + type_name()); - } - - // return element from array at given index - return value_.array->at(static_cast<std::size_t>(index)); -} - -/*! -@copydoc json::operator[](const char* key) -*/ -json::reference json::operator[](const std::string& key) -{ - return operator[](key.c_str()); -} - -/*! -This operator realizes read/write access to object elements given a string -key. - -@param key the key index of the element to return from the object -@return reference to a JSON object for the given key (null if key does not - exist) - -@pre Object is an object or a null object. -@post null objects are silently converted to objects. - -@exception std::domain_error if object is not an object (or null) -*/ -json::reference json::operator[](const char* key) -{ - // implicitly convert null to object - if (type_ == value_t::null) - { - type_ = value_t::object; - value_.object = new object_t; - } - - // this [] operator only works for objects - if (type_ != value_t::object) - { - throw std::domain_error("cannot add entry with key " + - std::string(key) + " to " + type_name()); - } - - // if the key does not exist, create it - if (value_.object->find(key) == value_.object->end()) - { - (*value_.object)[key] = json(); - } - - // return reference to element from array at given index - return (*value_.object)[key]; -} - -/*! -@copydoc json::operator[](const char* key) -*/ -json::const_reference json::operator[](const std::string& key) const -{ - return operator[](key.c_str()); -} - -/*! -This operator realizes read-only access to object elements given a string -key. - -@param key the key index of the element to return from the object -@return read-only reference to element for the given key - -@pre Object is an object. -@exception std::domain_error if object is not an object -@exception std::out_of_range if key is not found in object -*/ -json::const_reference json::operator[](const char* key) const -{ - // this [] operator only works for objects - if (type_ != value_t::object) - { - throw std::domain_error("cannot get entry with key " + - std::string(key) + " from " + type_name()); - } - - // search for the key - const auto it = value_.object->find(key); - - // make sure the key exists in the object - if (it == value_.object->end()) - { - throw std::out_of_range("key " + std::string(key) + " not found"); - } - - // return element from array at given key - return it->second; -} - -/*! -@copydoc json::at(const char* key) -*/ -json::reference json::at(const std::string& key) -{ - return at(key.c_str()); -} - -/*! -This function realizes read/write access to object elements given a string -key. - -@param key the key index of the element to return from the object -@return reference to a JSON object for the given key (exception if key does not - exist) - -@pre Object is an object. - -@exception std::domain_error if object is not an object -@exception std::out_of_range if key was not found (via std::map::at) -*/ -json::reference json::at(const char* key) -{ - // this function operator only works for objects - if (type_ != value_t::object) - { - throw std::domain_error("cannot add entry with key " + - std::string(key) + " to " + type_name()); - } - - // return reference to element from array at given index - return value_.object->at(key); -} - -/*! -@copydoc json::at(const char *key) const -*/ -json::const_reference json::at(const std::string& key) const -{ - return at(key.c_str()); -} - -/*! -This operator realizes read-only access to object elements given a string -key. - -@param key the key index of the element to return from the object -@return read-only reference to element for the given key - -@pre Object is an object. -@exception std::domain_error if object is not an object -@exception std::out_of_range if key is not found (via std::map::at) -*/ -json::const_reference json::at(const char* key) const -{ - // this [] operator only works for objects - if (type_ != value_t::object) - { - throw std::domain_error("cannot get entry with key " + - std::string(key) + " from " + type_name()); - } - - // return element from array at given key - return value_.object->at(key); -} - -/*! -Returns the size of the JSON object. - -@return the size of the JSON object; the size is the number of elements in - compounds (array and object), 1 for value types (true, false, number, - string), and 0 for null. - -@invariant The size is reported as 0 if and only if empty() would return true. -*/ -json::size_type json::size() const noexcept -{ - switch (type_) - { - case (value_t::array): - { - return value_.array->size(); - } - case (value_t::object): - { - return value_.object->size(); - } - case (value_t::null): - { - return 0; - } - default: - { - return 1; - } - } -} - -/*! -Returns the maximal size of the JSON object. - -@return the maximal size of the JSON object; the maximal size is the maximal - number of elements in compounds (array and object), 1 for value types - (true, false, number, string), and 0 for null. -*/ -json::size_type json::max_size() const noexcept -{ - switch (type_) - { - case (value_t::array): - { - return value_.array->max_size(); - } - case (value_t::object): - { - return value_.object->max_size(); - } - case (value_t::null): - { - return 0; - } - default: - { - return 1; - } - } -} - -/*! -Returns whether a JSON object is empty. - -@return true for null objects and empty compounds (array and object); false - for value types (true, false, number, string) and filled compounds - (array and object). - -@invariant Empty would report true if and only if size() would return 0. -*/ -bool json::empty() const noexcept -{ - switch (type_) - { - case (value_t::array): - { - return value_.array->empty(); - } - case (value_t::object): - { - return value_.object->empty(); - } - case (value_t::null): - { - return true; - } - default: - { - return false; - } - } -} - -/*! -Removes all elements from compounds and resets values to default. - -@invariant Clear will set any value type to its default value which is empty - for compounds, false for booleans, 0 for integer numbers, and 0.0 - for floating numbers. -*/ -void json::clear() noexcept -{ - switch (type_) - { - case (value_t::array): - { - value_.array->clear(); - break; - } - case (value_t::object): - { - value_.object->clear(); - break; - } - case (value_t::string): - { - value_.string->clear(); - break; - } - case (value_t::boolean): - { - value_.boolean = {}; - break; - } - case (value_t::number): - { - value_.number = {}; - break; - } - case (value_t::number_float): - { - value_.number_float = {}; - break; - } - default: - { - break; - } - } -} - -void json::swap(json& o) noexcept -{ - std::swap(type_, o.type_); - std::swap(value_, o.value_); -} - -json::value_t json::type() const noexcept -{ - return type_; -} - -json::iterator json::find(const std::string& key) -{ - return find(key.c_str()); -} - -json::const_iterator json::find(const std::string& key) const -{ - return find(key.c_str()); -} - -json::iterator json::find(const char* key) -{ - auto result = end(); - - if (type_ == value_t::object) - { - delete result.oi_; - result.oi_ = new object_t::iterator(value_.object->find(key)); - result.invalid = (*(result.oi_) == value_.object->end()); - } - - return result; -} - -json::const_iterator json::find(const char* key) const -{ - auto result = cend(); - - if (type_ == value_t::object) - { - delete result.oi_; - result.oi_ = new object_t::const_iterator(value_.object->find(key)); - result.invalid = (*(result.oi_) == value_.object->cend()); - } - - return result; -} - -bool json::operator==(const json& o) const noexcept -{ - switch (type_) - { - case (value_t::array): - { - if (o.type_ == value_t::array) - { - return *value_.array == *o.value_.array; - } - break; - } - case (value_t::object): - { - if (o.type_ == value_t::object) - { - return *value_.object == *o.value_.object; - } - break; - } - case (value_t::null): - { - if (o.type_ == value_t::null) - { - return true; - } - break; - } - case (value_t::string): - { - if (o.type_ == value_t::string) - { - return *value_.string == *o.value_.string; - } - break; - } - case (value_t::boolean): - { - if (o.type_ == value_t::boolean) - { - return value_.boolean == o.value_.boolean; - } - break; - } - case (value_t::number): - { - if (o.type_ == value_t::number) - { - return value_.number == o.value_.number; - } - if (o.type_ == value_t::number_float) - { - return value_.number == static_cast<number_t>(o.value_.number_float); - } - break; - } - case (value_t::number_float): - { - if (o.type_ == value_t::number) - { - return value_.number_float == static_cast<number_float_t>(o.value_.number); - } - if (o.type_ == value_t::number_float) - { - return value_.number_float == o.value_.number_float; - } - break; - } - } - - return false; -} - -bool json::operator!=(const json& o) const noexcept -{ - return not operator==(o); -} - - -json::iterator json::begin() noexcept -{ - return json::iterator(this, true); -} - -json::iterator json::end() noexcept -{ - return json::iterator(this, false); -} - -json::const_iterator json::begin() const noexcept -{ - return json::const_iterator(this, true); -} - -json::const_iterator json::end() const noexcept -{ - return json::const_iterator(this, false); -} - -json::const_iterator json::cbegin() const noexcept -{ - return json::const_iterator(this, true); -} - -json::const_iterator json::cend() const noexcept -{ - return json::const_iterator(this, false); -} - -json::reverse_iterator json::rbegin() noexcept -{ - return reverse_iterator(end()); -} - -json::reverse_iterator json::rend() noexcept -{ - return reverse_iterator(begin()); -} - -json::const_reverse_iterator json::crbegin() const noexcept -{ - return const_reverse_iterator(cend()); -} - -json::const_reverse_iterator json::crend() const noexcept -{ - return const_reverse_iterator(cbegin()); -} - - -json::iterator::iterator(json* j, bool begin) - : object_(j), invalid(not begin or j == nullptr) -{ - if (object_ != nullptr) - { - if (object_->type_ == json::value_t::array) - { - if (begin) - { - vi_ = new array_t::iterator(object_->value_.array->begin()); - invalid = (*vi_ == object_->value_.array->end()); - } - else - { - vi_ = new array_t::iterator(object_->value_.array->end()); - } - } - else if (object_->type_ == json::value_t::object) - { - if (begin) - { - oi_ = new object_t::iterator(object_->value_.object->begin()); - invalid = (*oi_ == object_->value_.object->end()); - } - else - { - oi_ = new object_t::iterator(object_->value_.object->end()); - } - } - } -} - -json::iterator::iterator(const json::iterator& o) - : object_(o.object_), invalid(o.invalid) -{ - if (o.vi_ != nullptr) - { - vi_ = new array_t::iterator(*(o.vi_)); - } - - if (o.oi_ != nullptr) - { - oi_ = new object_t::iterator(*(o.oi_)); - } -} - -json::iterator::~iterator() -{ - delete vi_; - delete oi_; -} - -json::iterator& json::iterator::operator=(json::iterator o) -{ - std::swap(object_, o.object_); - std::swap(vi_, o.vi_); - std::swap(oi_, o.oi_); - std::swap(invalid, o.invalid); - return *this; -} - -bool json::iterator::operator==(const json::iterator& o) const -{ - if (object_ != nullptr and o.object_ != nullptr) - { - if (object_->type_ == json::value_t::array and o.object_->type_ == json::value_t::array) - { - return (*vi_ == *(o.vi_)); - } - if (object_->type_ == json::value_t::object and o.object_->type_ == json::value_t::object) - { - return (*oi_ == *(o.oi_)); - } - - if (invalid == o.invalid and object_ == o.object_) - { - return true; - } - } - return false; -} - -bool json::iterator::operator!=(const json::iterator& o) const -{ - return not operator==(o); -} - -json::iterator& json::iterator::operator++() -{ - if (object_ != nullptr) - { - switch (object_->type_) - { - case (json::value_t::array): - { - std::advance(*vi_, 1); - invalid = (*vi_ == object_->value_.array->end()); - break; - } - case (json::value_t::object): - { - std::advance(*oi_, 1); - invalid = (*oi_ == object_->value_.object->end()); - break; - } - default: - { - invalid = true; - break; - } - } - } - - return *this; -} - -json::iterator& json::iterator::operator--() -{ - if (object_ != nullptr) - { - switch (object_->type_) - { - case (json::value_t::array): - { - invalid = (*vi_ == object_->value_.array->begin()); - std::advance(*vi_, -1); - break; - } - case (json::value_t::object): - { - invalid = (*oi_ == object_->value_.object->begin()); - std::advance(*oi_, -1); - break; - } - default: - { - invalid = true; - break; - } - } - } - - return *this; -} - -json& json::iterator::operator*() const -{ - if (object_ == nullptr or invalid) - { - throw std::out_of_range("cannot get value"); - } - - switch (object_->type_) - { - case (json::value_t::array): - { - return **vi_; - } - case (json::value_t::object): - { - return (*oi_)->second; - } - default: - { - return *object_; - } - } -} - -json* json::iterator::operator->() const -{ - if (object_ == nullptr or invalid) - { - throw std::out_of_range("cannot get value"); - } - - switch (object_->type_) - { - case (json::value_t::array): - { - return &(**vi_); - } - case (json::value_t::object): - { - return &((*oi_)->second); - } - default: - { - return object_; - } - } -} - -std::string json::iterator::key() const -{ - if (object_ == nullptr or invalid or object_->type_ != json::value_t::object) - { - throw std::out_of_range("cannot get value"); - } - - return (*oi_)->first; -} - -json& json::iterator::value() const -{ - if (object_ == nullptr or invalid) - { - throw std::out_of_range("cannot get value"); - } - - switch (object_->type_) - { - case (json::value_t::array): - { - return **vi_; - } - case (json::value_t::object): - { - return (*oi_)->second; - } - default: - { - return *object_; - } - } -} - - -json::const_iterator::const_iterator(const json* j, bool begin) - : object_(j), invalid(not begin or j == nullptr) -{ - if (object_ != nullptr) - { - if (object_->type_ == json::value_t::array) - { - if (begin) - { - vi_ = new array_t::const_iterator(object_->value_.array->cbegin()); - invalid = (*vi_ == object_->value_.array->cend()); - } - else - { - vi_ = new array_t::const_iterator(object_->value_.array->cend()); - } - } - else if (object_->type_ == json::value_t::object) - { - if (begin) - { - oi_ = new object_t::const_iterator(object_->value_.object->cbegin()); - invalid = (*oi_ == object_->value_.object->cend()); - } - else - { - oi_ = new object_t::const_iterator(object_->value_.object->cend()); - } - } - } -} - -json::const_iterator::const_iterator(const json::const_iterator& o) - : object_(o.object_), invalid(o.invalid) -{ - if (o.vi_ != nullptr) - { - vi_ = new array_t::const_iterator(*(o.vi_)); - } - if (o.oi_ != nullptr) - { - oi_ = new object_t::const_iterator(*(o.oi_)); - } -} - -json::const_iterator::const_iterator(const json::iterator& o) - : object_(o.object_), invalid(o.invalid) -{ - if (o.vi_ != nullptr) - { - vi_ = new array_t::const_iterator(*(o.vi_)); - } - if (o.oi_ != nullptr) - { - oi_ = new object_t::const_iterator(*(o.oi_)); - } -} - -json::const_iterator::~const_iterator() -{ - delete vi_; - delete oi_; -} - -json::const_iterator& json::const_iterator::operator=(json::const_iterator o) -{ - std::swap(object_, o.object_); - std::swap(vi_, o.vi_); - std::swap(oi_, o.oi_); - std::swap(invalid, o.invalid); - return *this; -} - -bool json::const_iterator::operator==(const json::const_iterator& o) const -{ - if (object_ != nullptr and o.object_ != nullptr) - { - if (object_->type_ == json::value_t::array and o.object_->type_ == json::value_t::array) - { - return (*vi_ == *(o.vi_)); - } - if (object_->type_ == json::value_t::object and o.object_->type_ == json::value_t::object) - { - return (*oi_ == *(o.oi_)); - } - if (invalid == o.invalid and object_ == o.object_) - { - return true; - } - } - - return false; -} - -bool json::const_iterator::operator!=(const json::const_iterator& o) const -{ - return not operator==(o); -} - -json::const_iterator& json::const_iterator::operator++() -{ - if (object_ != nullptr) - { - switch (object_->type_) - { - case (json::value_t::array): - { - std::advance(*vi_, 1); - invalid = (*vi_ == object_->value_.array->end()); - break; - } - case (json::value_t::object): - { - std::advance(*oi_, 1); - invalid = (*oi_ == object_->value_.object->end()); - break; - } - default: - { - invalid = true; - break; - } - } - } - - return *this; -} - -json::const_iterator& json::const_iterator::operator--() -{ - if (object_ != nullptr) - { - switch (object_->type_) - { - case (json::value_t::array): - { - invalid = (*vi_ == object_->value_.array->begin()); - std::advance(*vi_, -1); - break; - } - case (json::value_t::object): - { - invalid = (*oi_ == object_->value_.object->begin()); - std::advance(*oi_, -1); - break; - } - default: - { - invalid = true; - break; - } - } - } - - return *this; -} - -const json& json::const_iterator::operator*() const -{ - if (object_ == nullptr or invalid) - { - throw std::out_of_range("cannot get value"); - } - - switch (object_->type_) - { - case (json::value_t::array): - { - return **vi_; - } - case (json::value_t::object): - { - return (*oi_)->second; - } - default: - { - return *object_; - } - } -} - -const json* json::const_iterator::operator->() const -{ - if (object_ == nullptr or invalid) - { - throw std::out_of_range("cannot get value"); - } - - switch (object_->type_) - { - case (json::value_t::array): - { - return &(**vi_); - } - case (json::value_t::object): - { - return &((*oi_)->second); - } - default: - { - return object_; - } - } -} - -std::string json::const_iterator::key() const -{ - if (object_ == nullptr or invalid or object_->type_ != json::value_t::object) - { - throw std::out_of_range("cannot get value"); - } - - return (*oi_)->first; -} - -const json& json::const_iterator::value() const -{ - if (object_ == nullptr or invalid) - { - throw std::out_of_range("cannot get value"); - } - - switch (object_->type_) - { - case (json::value_t::array): - { - return **vi_; - } - case (json::value_t::object): - { - return (*oi_)->second; - } - default: - { - return *object_; - } - } -} - - -/*! -Initialize the JSON parser given a string \p s. - -@note After initialization, the function @ref parse has to be called manually. - -@param s string to parse - -@post \p s is copied to the buffer @ref buffer_ and the first character is - read. Whitespace is skipped. -*/ -json::parser::parser(const char* s) - : buffer_(s) -{ - // read first character - next(); -} - -/*! -@copydoc json::parser::parser(const char* s) -*/ -json::parser::parser(const std::string& s) - : buffer_(s) -{ - // read first character - next(); -} - -/*! -Initialize the JSON parser given an input stream \p _is. - -@note After initialization, the function @ref parse has to be called manually. - -\param _is input stream to parse - -@post \p _is is copied to the buffer @ref buffer_ and the firsr character is - read. Whitespace is skipped. - -*/ -json::parser::parser(std::istream& _is) -{ - while (_is) - { - std::string input_line; - std::getline(_is, input_line); - buffer_ += input_line; - } - - // read first character - next(); -} - -json json::parser::parse() -{ - switch (current_) - { - case ('{'): - { - // explicitly set result to object to cope with {} - json result(value_t::object); - - next(); - - // process nonempty object - if (current_ != '}') - { - do - { - // key - auto key = parseString(); - - // colon - expect(':'); - - // value - result[std::move(key)] = parse(); - key.clear(); - } - while (current_ == ',' and next()); - } - - // closing brace - expect('}'); - - return result; - } - - case ('['): - { - // explicitly set result to array to cope with [] - json result(value_t::array); - - next(); - - // process nonempty array - if (current_ != ']') - { - do - { - result.push_back(parse()); - } - while (current_ == ',' and next()); - } - - // closing bracket - expect(']'); - - return result; - } - - case ('\"'): - { - return json(parseString()); - } - - case ('t'): - { - parseTrue(); - return json(true); - } - - case ('f'): - { - parseFalse(); - return json(false); - } - - case ('n'): - { - parseNull(); - return json(); - } - - case ('-'): - case ('0'): - case ('1'): - case ('2'): - case ('3'): - case ('4'): - case ('5'): - case ('6'): - case ('7'): - case ('8'): - case ('9'): - { - // remember position of number's first character - const auto _firstpos_ = pos_ - 1; - - while (next() and (std::isdigit(current_) or current_ == '.' - or current_ == 'e' or current_ == 'E' - or current_ == '+' or current_ == '-')); - - try - { - const auto float_val = std::stold(buffer_.substr(_firstpos_, pos_ - _firstpos_)); - const auto int_val = static_cast<number_t>(float_val); - - // check if conversion loses precision - if (float_val == int_val) - { - // we would not lose precision -> int - return json(int_val); - } - else - { - // we would lose precision -> float - return json(static_cast<number_float_t>(float_val)); - } - } - catch (...) - { - error("error translating " + - buffer_.substr(_firstpos_, pos_ - _firstpos_) + " to number"); - } - } - - default: - { - error("unexpected character"); - } - } -} - -/*! -This function reads the next character from the buffer while ignoring all -trailing whitespace. If another character could be read, the function returns -true. If the end of the buffer is reached, false is returned. - -@return whether another non-whitespace character could be read - -@post current_ holds the next character -*/ -bool json::parser::next() -{ - if (pos_ == buffer_.size()) - { - return false; - } - - current_ = buffer_[pos_++]; - - // skip trailing whitespace - while (std::isspace(current_)) - { - if (pos_ == buffer_.size()) - { - return false; - } - - current_ = buffer_[pos_++]; - } - - return true; -} - -/*! -This function encapsulates the error reporting functions of the parser class. -It throws a \p std::invalid_argument exception with a description where the -error occurred (given as the number of characters read), what went wrong (using -the error message \p msg), and the last read token. - -@param msg an error message -@return <em>This function does not return.</em> - -@exception std::invalid_argument whenever the function is called -*/ -void json::parser::error(const std::string& msg) const -{ - throw std::invalid_argument("parse error at position " + - std::to_string(pos_) + ": " + msg + - ", last read: '" + current_ + "'"); -} - -/*! -Parses a string after opening quotes (\p ") where read. - -@return the parsed string - -@pre An opening quote \p " was read in the main parse function @ref parse. - pos_ is the position after the opening quote. - -@post The character after the closing quote \p " is the current character @ref - current_. Whitespace is skipped. - -@todo Unicode escapes such as \uxxxx are missing - see - https://github.com/nlohmann/json/issues/12 -*/ -std::string json::parser::parseString() -{ - // true if and only if the amount of backslashes before the current - // character is even - bool evenAmountOfBackslashes = true; - - // the result of the parse process - std::string result; - - // iterate with pos_ over the whole input until we found the end and return - // or we exit via error() - for (; pos_ < buffer_.size(); pos_++) - { - char currentChar = buffer_[pos_]; - - if (not evenAmountOfBackslashes) - { - // uneven amount of backslashes means the user wants to escape - // something so we know there is a case such as '\X' or '\\\X' but - // we don't know yet what X is. - // at this point in the code, the currentChar has the value of X. - - // slash, backslash and quote are copied as is - if (currentChar == '/' or currentChar == '\\' or currentChar == '"') - { - result += currentChar; - } - else - { - // all other characters are replaced by their respective special - // character - switch (currentChar) - { - case 't': - { - result += '\t'; - break; - } - case 'b': - { - result += '\b'; - break; - } - case 'f': - { - result += '\f'; - break; - } - case 'n': - { - result += '\n'; - break; - } - case 'r': - { - result += '\r'; - break; - } - case 'u': - { - // \uXXXX[\uXXXX] is used for escaping unicode, which - // has it's own subroutine. - result += parseUnicodeEscape(); - // the parsing process has brought us one step behind - // the unicode escape sequence: - // \uXXXX - // ^ - // we need to go one character back or the parser would - // skip the character we are currently pointing at as - // the for-loop will decrement pos_ after this iteration - pos_--; - break; - } - default: - { - error("expected one of \\, /, b, f, n, r, t, u behind backslash."); - } - } - } - } - else - { - if (currentChar == '"') - { - // currentChar is a quote, so we found the end of the string - - // set pos_ behind the trailing quote - pos_++; - // find next char to parse - next(); - - // bring the result of the parsing process back to the caller - return result; - } - else if (currentChar != '\\') - { - // all non-backslash characters are added to the end of the - // result string. The only backslashes we want in the result - // are the ones that are escaped (which happens above). - result += currentChar; - } - } - - // remember if we have an even amount of backslashes before the current - // character - if (currentChar == '\\') - { - // jump between even/uneven for each backslash we encounter - evenAmountOfBackslashes = not evenAmountOfBackslashes; - } - else - { - // zero backslashes are also an even number, so as soon as we - // encounter a non-backslash the chain of backslashes breaks and - // we start again from zero - evenAmountOfBackslashes = true; - } - } - - // we iterated over the whole string without finding a unescaped quote - // so the given string is malformed - error("expected '\"'"); -} - - - -/*! -Turns a code point into it's UTF-8 representation. -You should only pass numbers < 0x10ffff into this function -(everything else is a invalid code point). - -@return the UTF-8 representation of the given code point -*/ -std::string json::parser::codePointToUTF8(unsigned int codePoint) const -{ - // this method contains a lot of bit manipulations to - // build the bytes for UTF-8. - - // the '(... >> S) & 0xHH'-patterns are used to retrieve - // certain bits from the code points. - - // all static casts in this method have boundary checks - - // we initialize all strings with their final length - // (e.g. 1 to 4 bytes) to save the reallocations. - - if (codePoint <= 0x7f) - { - // it's just a ASCII compatible codePoint, - // so we just interpret the point as a character - // and return ASCII - - return std::string(1, static_cast<char>(codePoint)); - } - // if true, we need two bytes to encode this as UTF-8 - else if (codePoint <= 0x7ff) - { - // the 0xC0 enables the two most significant two bits - // to make this a two-byte UTF-8 character. - std::string result(2, static_cast<char>(0xC0 | ((codePoint >> 6) & 0x1F))); - result[1] = static_cast<char>(0x80 | (codePoint & 0x3F)); - return result; - } - // if true, now we need three bytes to encode this as UTF-8 - else if (codePoint <= 0xffff) - { - // the 0xE0 enables the three most significant two bits - // to make this a three-byte UTF-8 character. - std::string result(3, static_cast<char>(0xE0 | ((codePoint >> 12) & 0x0F))); - result[1] = static_cast<char>(0x80 | ((codePoint >> 6) & 0x3F)); - result[2] = static_cast<char>(0x80 | (codePoint & 0x3F)); - return result; - } - // if true, we need maximal four bytes to encode this as UTF-8 - else if (codePoint <= 0x10ffff) - { - // the 0xE0 enables the four most significant two bits - // to make this a three-byte UTF-8 character. - std::string result(4, static_cast<char>(0xF0 | ((codePoint >> 18) & 0x07))); - result[1] = static_cast<char>(0x80 | ((codePoint >> 12) & 0x3F)); - result[2] = static_cast<char>(0x80 | ((codePoint >> 6) & 0x3F)); - result[3] = static_cast<char>(0x80 | (codePoint & 0x3F)); - return result; - } - else - { - // Can't be tested without direct access to this private method. - std::string errorMessage = "Invalid codePoint: "; - errorMessage += codePoint; - error(errorMessage); - } -} - -/*! -Parses 4 hexadecimal characters as a number. - -@return the value of the number the hexadecimal characters represent. - -@pre pos_ is pointing to the first of the 4 hexadecimal characters. - -@post pos_ is pointing to the character after the 4 hexadecimal characters. -*/ -unsigned int json::parser::parse4HexCodePoint() -{ - const auto startPos = pos_; - - // check if the remaining buffer is long enough to even hold 4 characters - if (pos_ + 3 >= buffer_.size()) - { - error("Got end of input while parsing unicode escape sequence \\uXXXX"); - } - - // make a string that can hold the pair - std::string hexCode(4, ' '); - - for (; pos_ < startPos + 4; pos_++) - { - // no boundary check here as we already checked above - char currentChar = buffer_[pos_]; - - // check if we have a hexadecimal character - if ((currentChar >= '0' and currentChar <= '9') - or (currentChar >= 'a' and currentChar <= 'f') - or (currentChar >= 'A' and currentChar <= 'F')) - { - // all is well, we have valid hexadecimal chars - // so we copy that char into our string - hexCode[pos_ - startPos] = currentChar; - } - else - { - error("Found non-hexadecimal character in unicode escape sequence!"); - } - } - // the cast is safe as 4 hex characters can't present more than 16 bits - // the input to stoul was checked to contain only hexadecimal characters - // (see above) - return static_cast<unsigned int>(std::stoul(hexCode, nullptr, 16)); -} - -/*! -Parses the unicode escape codes as defined in the ECMA-404. -The escape sequence has two forms: -1. \uXXXX -2. \uXXXX\uYYYY -where X and Y are a hexadecimal character (a-zA-Z0-9). - -Form 1 just contains the unicode code point in the hexadecimal number XXXX. -Form 2 is encoding a UTF-16 surrogate pair. The high surrogate is XXXX, the low -surrogate is YYYY. - -@return the UTF-8 character this unicode escape sequence escaped. - -@pre pos_ is pointing at at the 'u' behind the first backslash. - -@post pos_ is pointing at the character behind the last X (or Y in form 2). -*/ -std::string json::parser::parseUnicodeEscape() -{ - // jump to the first hex value - pos_++; - // parse the hex first hex values - unsigned int firstCodePoint = parse4HexCodePoint(); - - if (firstCodePoint >= 0xD800 and firstCodePoint <= 0xDBFF) - { - // we found invalid code points, which means we either have a malformed - // input or we found a high surrogate. - // we can only find out by seeing if the next character also wants to - // encode a unicode character (so, we have the \uXXXX\uXXXX case here). - - // jump behind the next \u - pos_ += 2; - // try to parse the next hex values. - // the method does boundary checking for us, so no need to do that here - unsigned secondCodePoint = parse4HexCodePoint(); - // ok, we have a low surrogate, check if it is a valid one - if (secondCodePoint >= 0xDC00 and secondCodePoint <= 0xDFFF) - { - // calculate the code point from the pair according to the spec - unsigned int finalCodePoint = - // high surrogate occupies the most significant 22 bits - (firstCodePoint << 10) - // low surrogate occupies the least significant 15 bits - + secondCodePoint - // there is still the 0xD800, 0xDC00 and 0x10000 noise in - // the result - // so we have to substract with: - // (0xD800 << 10) + DC00 - 0x10000 = 0x35FDC00 - - 0x35FDC00; - - // we transform the calculated point into UTF-8 - return codePointToUTF8(finalCodePoint); - } - else - { - error("missing low surrogate"); - } - - } - // We have Form 1, so we just interpret the XXXX as a code point - return codePointToUTF8(firstCodePoint); -} - - -/*! -This function is called in case a \p "t" is read in the main parse function -@ref parse. In the standard, the \p "true" token is the only candidate, so the -next three characters are expected to be \p "rue". In case of a mismatch, an -error is raised via @ref error. - -@pre A \p "t" was read in the main parse function @ref parse. -@post The character after the \p "true" is the current character. Whitespace is - skipped. -*/ -void json::parser::parseTrue() -{ - if (buffer_.substr(pos_, 3) != "rue") - { - error("expected true"); - } - - pos_ += 3; - - // read next character - next(); -} - -/*! -This function is called in case an \p "f" is read in the main parse function -@ref parse. In the standard, the \p "false" token is the only candidate, so the -next four characters are expected to be \p "alse". In case of a mismatch, an -error is raised via @ref error. - -@pre An \p "f" was read in the main parse function. -@post The character after the \p "false" is the current character. Whitespace - is skipped. -*/ -void json::parser::parseFalse() -{ - if (buffer_.substr(pos_, 4) != "alse") - { - error("expected false"); - } - - pos_ += 4; - - // read next character - next(); -} - -/*! -This function is called in case an \p "n" is read in the main parse function -@ref parse. In the standard, the \p "null" token is the only candidate, so the -next three characters are expected to be \p "ull". In case of a mismatch, an -error is raised via @ref error. - -@pre An \p "n" was read in the main parse function. -@post The character after the \p "null" is the current character. Whitespace is - skipped. -*/ -void json::parser::parseNull() -{ - if (buffer_.substr(pos_, 3) != "ull") - { - error("expected null"); - } - - pos_ += 3; - - // read next character - next(); -} - -/*! -This function wraps functionality to check whether the current character @ref -current_ matches a given character \p c. In case of a match, the next character -of the buffer @ref buffer_ is read. In case of a mismatch, an error is raised -via @ref error. - -@param c character that is expected - -@post The next chatacter is read. Whitespace is skipped. -*/ -void json::parser::expect(const char c) -{ - if (current_ != c) - { - std::string msg = "expected '"; - msg.append(1, c); - msg += "'"; - error(msg); - } - else - { - next(); - } -} - -} - -/*! -This operator implements a user-defined string literal for JSON objects. It can -be used by adding \p "_json" to a string literal and returns a JSON object if -no parse error occurred. - -@param s a string representation of a JSON object -@return a JSON object -*/ -nlohmann::json operator "" _json(const char* s, std::size_t) -{ - return nlohmann::json::parse(s); -} diff --git a/src/json.cc b/src/json.cc deleted file mode 100644 index 1457e2e0..00000000 --- a/src/json.cc +++ /dev/null @@ -1,2598 +0,0 @@ -/*! -@file -@copyright The code is licensed under the MIT License - <http://opensource.org/licenses/MIT>, - Copyright (c) 2013-2015 Niels Lohmann. - -@author Niels Lohmann <http://nlohmann.me> - -@see https://github.com/nlohmann/json -*/ - -#include "json.h" - -#include <cctype> // std::isdigit, std::isspace -#include <cstddef> // std::size_t -#include <stdexcept> // std::runtime_error -#include <utility> // std::swap, std::move - -namespace nlohmann -{ - -/////////////////////////////////// -// CONSTRUCTORS OF UNION "value" // -/////////////////////////////////// - -json::value::value(array_t* _array): array(_array) {} -json::value::value(object_t* object_): object(object_) {} -json::value::value(string_t* _string): string(_string) {} -json::value::value(boolean_t _boolean) : boolean(_boolean) {} -json::value::value(number_t _number) : number(_number) {} -json::value::value(number_float_t _number_float) : number_float(_number_float) {} - - -///////////////////////////////// -// CONSTRUCTORS AND DESTRUCTOR // -///////////////////////////////// - -/*! -Construct an empty JSON given the type. - -@param t the type from the @ref json::type enumeration. - -@post Memory for array, object, and string are allocated. -*/ -json::json(const value_t t) - : type_(t) -{ - switch (type_) - { - case (value_t::array): - { - value_.array = new array_t(); - break; - } - case (value_t::object): - { - value_.object = new object_t(); - break; - } - case (value_t::string): - { - value_.string = new string_t(); - break; - } - case (value_t::boolean): - { - value_.boolean = boolean_t(); - break; - } - case (value_t::number): - { - value_.number = number_t(); - break; - } - case (value_t::number_float): - { - value_.number_float = number_float_t(); - break; - } - default: - { - break; - } - } -} - -json::json() noexcept : final_type_(0), type_(value_t::null) -{} - -/*! -Construct a null JSON object. -*/ -json::json(std::nullptr_t) noexcept : json() -{} - -/*! -Construct a string JSON object. - -@param s a string to initialize the JSON object with -*/ -json::json(const std::string& s) - : final_type_(0), type_(value_t::string), value_(new string_t(s)) -{} - -json::json(std::string&& s) - : final_type_(0), type_(value_t::string), value_(new string_t(std::move(s))) -{} - -json::json(const char* s) - : final_type_(0), type_(value_t::string), value_(new string_t(s)) -{} - -json::json(const bool b) noexcept - : final_type_(0), type_(value_t::boolean), value_(b) -{} - -json::json(const array_t& a) - : final_type_(0), type_(value_t::array), value_(new array_t(a)) -{} - -json::json(array_t&& a) - : final_type_(0), type_(value_t::array), value_(new array_t(std::move(a))) -{} - -json::json(const object_t& o) - : final_type_(0), type_(value_t::object), value_(new object_t(o)) -{} - -json::json(object_t&& o) - : final_type_(0), type_(value_t::object), value_(new object_t(std::move(o))) -{} - -/*! -This function is a bit tricky as it uses an initializer list of JSON objects -for both arrays and objects. This is not supported by C++, so we use the -following trick. Both initializer lists for objects and arrays will transform -to a list of JSON objects. The only difference is that in case of an object, -the list will contain JSON array objects with two elements - one for the key -and one for the value. As a result, it is sufficient to check if each element -of the initializer list is an array (1) with two elements (2) whose first -element is of type string (3). If this is the case, we treat the whole -initializer list as list of pairs to construct an object. If not, we pass it -as is to create an array. - -@bug With the described approach, we would fail to recognize an array whose - first element is again an arrays as array. - -@param a an initializer list to create from -@param type_deduction whether the type (array/object) shall eb deducted -@param manual_type if type deduction is switched of, pass a manual type -*/ -json::json(list_init_t a, bool type_deduction, value_t manual_type) : final_type_(0) -{ - // the initializer list could describe an object - bool is_object = true; - - // check if each element is an array with two elements whose first element - // is a string - for (const auto& element : a) - { - if ((element.final_type_ == 1 and element.type_ == value_t::array) - or (element.type_ != value_t::array or element.size() != 2 or element[0].type_ != value_t::string)) - { - // we found an element that makes it impossible to use the - // initializer list as object - is_object = false; - break; - } - } - - // adjust type if type deduction is not wanted - if (not type_deduction) - { - // mark this object's type as final - final_type_ = 1; - - // if array is wanted, do not create an object though possible - if (manual_type == value_t::array) - { - is_object = false; - } - - // if object is wanted but impossible, throw an exception - if (manual_type == value_t::object and not is_object) - { - throw std::logic_error("cannot create JSON object"); - } - } - - if (is_object) - { - // the initializer list is a list of pairs -> create object - type_ = value_t::object; - value_ = new object_t(); - for (auto& element : a) - { - value_.object->emplace(std::make_pair(std::move(element[0]), std::move(element[1]))); - } - } - else - { - // the initializer list describes an array -> create array - type_ = value_t::array; - value_ = new array_t(std::move(a)); - } -} - -/*! -@param a initializer list to create an array from -@return array -*/ -json json::array(list_init_t a) -{ - return json(a, false, value_t::array); -} - -/*! -@param a initializer list to create an object from -@return object -*/ -json json::object(list_init_t a) -{ - // if more than one element is in the initializer list, wrap it - if (a.size() > 1) - { - return json({a}, false, value_t::object); - } - else - { - return json(a, false, value_t::object); - } -} - -/*! -A copy constructor for the JSON class. - -@param o the JSON object to copy -*/ -json::json(const json& o) - : type_(o.type_) -{ - switch (type_) - { - case (value_t::array): - { - value_.array = new array_t(*o.value_.array); - break; - } - case (value_t::object): - { - value_.object = new object_t(*o.value_.object); - break; - } - case (value_t::string): - { - value_.string = new string_t(*o.value_.string); - break; - } - case (value_t::boolean): - { - value_.boolean = o.value_.boolean; - break; - } - case (value_t::number): - { - value_.number = o.value_.number; - break; - } - case (value_t::number_float): - { - value_.number_float = o.value_.number_float; - break; - } - default: - { - break; - } - } -} - -/*! -A move constructor for the JSON class. - -@param o the JSON object to move - -@post The JSON object \p o is invalidated. -*/ -json::json(json&& o) noexcept - : type_(std::move(o.type_)), value_(std::move(o.value_)) -{ - // invalidate payload - o.type_ = value_t::null; - o.value_ = {}; -} - -/*! -A copy assignment operator for the JSON class, following the copy-and-swap -idiom. - -@param o A JSON object to assign to this object. -*/ -json& json::operator=(json o) noexcept -{ - std::swap(type_, o.type_); - std::swap(value_, o.value_); - return *this; -} - -json::~json() noexcept -{ - switch (type_) - { - case (value_t::array): - { - delete value_.array; - break; - } - case (value_t::object): - { - delete value_.object; - break; - } - case (value_t::string): - { - delete value_.string; - break; - } - default: - { - // nothing to do for non-pointer types - break; - } - } -} - -/*! -@param s a string representation of a JSON object -@return a JSON object -*/ -json json::parse(const std::string& s) -{ - return parser(s).parse(); -} - -/*! -@param s a string representation of a JSON object -@return a JSON object -*/ -json json::parse(const char* s) -{ - return parser(s).parse(); -} - - -std::string json::type_name() const noexcept -{ - switch (type_) - { - case (value_t::array): - { - return "array"; - } - case (value_t::object): - { - return "object"; - } - case (value_t::null): - { - return "null"; - } - case (value_t::string): - { - return "string"; - } - case (value_t::boolean): - { - return "boolean"; - } - default: - { - return "number"; - } - } -} - - -/////////////////////////////// -// OPERATORS AND CONVERSIONS // -/////////////////////////////// - -/*! -@exception std::logic_error if the function is called for JSON objects whose - type is not string -*/ -template<> -std::string json::get() const -{ - switch (type_) - { - case (value_t::string): - return *value_.string; - default: - throw std::logic_error("cannot cast " + type_name() + " to JSON string"); - } -} - -/*! -@exception std::logic_error if the function is called for JSON objects whose - type is not number (int or float) -*/ -template<> -int json::get() const -{ - switch (type_) - { - case (value_t::number): - return value_.number; - case (value_t::number_float): - return static_cast<int>(value_.number_float); - default: - throw std::logic_error("cannot cast " + type_name() + " to JSON number"); - } -} - -/*! -@exception std::logic_error if the function is called for JSON objects whose - type is not number (int or float) -*/ -template<> -int64_t json::get() const -{ - switch (type_) - { - case (value_t::number): - return value_.number; - case (value_t::number_float): - return static_cast<number_t>(value_.number_float); - default: - throw std::logic_error("cannot cast " + type_name() + " to JSON number"); - } -} - -/*! -@exception std::logic_error if the function is called for JSON objects whose - type is not number (int or float) -*/ -template<> -double json::get() const -{ - switch (type_) - { - case (value_t::number): - return static_cast<number_float_t>(value_.number); - case (value_t::number_float): - return value_.number_float; - default: - throw std::logic_error("cannot cast " + type_name() + " to JSON number"); - } -} - -/*! -@exception std::logic_error if the function is called for JSON objects whose - type is not boolean -*/ -template<> -bool json::get() const -{ - switch (type_) - { - case (value_t::boolean): - return value_.boolean; - default: - throw std::logic_error("cannot cast " + type_name() + " to JSON Boolean"); - } -} - -/*! -@exception std::logic_error if the function is called for JSON objects whose - type is an object -*/ -template<> -json::array_t json::get() const -{ - if (type_ == value_t::array) - { - return *value_.array; - } - if (type_ == value_t::object) - { - throw std::logic_error("cannot cast " + type_name() + " to JSON array"); - } - - array_t result; - result.push_back(*this); - return result; -} - -/*! -@exception std::logic_error if the function is called for JSON objects whose - type is not object -*/ -template<> -json::object_t json::get() const -{ - if (type_ == value_t::object) - { - return *value_.object; - } - else - { - throw std::logic_error("cannot cast " + type_name() + " to JSON object"); - } -} - -json::operator std::string() const -{ - return get<std::string>(); -} - -json::operator int() const -{ - return get<int>(); -} - -json::operator int64_t() const -{ - return get<int64_t>(); -} - -json::operator double() const -{ - return get<double>(); -} - -json::operator bool() const -{ - return get<bool>(); -} - -json::operator array_t() const -{ - return get<array_t>(); -} - -json::operator object_t() const -{ - return get<object_t>(); -} - -/*! -Internal implementation of the serialization function. - -\param prettyPrint whether the output shall be pretty-printed -\param indentStep the indent level -\param currentIndent the current indent level (only used internally) -*/ -std::string json::dump(const bool prettyPrint, const unsigned int indentStep, - unsigned int currentIndent) const noexcept -{ - // helper function to return whitespace as indentation - const auto indent = [prettyPrint, ¤tIndent]() - { - return prettyPrint ? std::string(currentIndent, ' ') : std::string(); - }; - - switch (type_) - { - case (value_t::string): - { - return std::string("\"") + escapeString(*value_.string) + "\""; - } - - case (value_t::boolean): - { - return value_.boolean ? "true" : "false"; - } - - case (value_t::number): - { - return std::to_string(value_.number); - } - - case (value_t::number_float): - { - return std::to_string(value_.number_float); - } - - case (value_t::array): - { - if (value_.array->empty()) - { - return "[]"; - } - - std::string result = "["; - - // increase indentation - if (prettyPrint) - { - currentIndent += indentStep; - result += "\n"; - } - - for (array_t::const_iterator i = value_.array->begin(); i != value_.array->end(); ++i) - { - if (i != value_.array->begin()) - { - result += prettyPrint ? ",\n" : ","; - } - result += indent() + i->dump(prettyPrint, indentStep, currentIndent); - } - - // decrease indentation - if (prettyPrint) - { - currentIndent -= indentStep; - result += "\n"; - } - - return result + indent() + "]"; - } - - case (value_t::object): - { - if (value_.object->empty()) - { - return "{}"; - } - - std::string result = "{"; - - // increase indentation - if (prettyPrint) - { - currentIndent += indentStep; - result += "\n"; - } - - for (object_t::const_iterator i = value_.object->begin(); i != value_.object->end(); ++i) - { - if (i != value_.object->begin()) - { - result += prettyPrint ? ",\n" : ","; - } - result += indent() + "\"" + i->first + "\":" + (prettyPrint ? " " : "") + i->second.dump( - prettyPrint, indentStep, - currentIndent); - } - - // decrease indentation - if (prettyPrint) - { - currentIndent -= indentStep; - result += "\n"; - } - - return result + indent() + "}"; - } - - // actually only value_t::null - but making the compiler happy - default: - { - return "null"; - } - } -} - -/*! -Internal function to replace all occurrences of a character in a given string -with another string. - -\param str the string that contains tokens to replace -\param c the character that needs to be replaced -\param replacement the string that is the replacement for the character -*/ -void json::replaceChar(std::string& str, char c, const std::string& replacement) -const -{ - size_t start_pos = 0; - while ((start_pos = str.find(c, start_pos)) != std::string::npos) - { - str.replace(start_pos, 1, replacement); - start_pos += replacement.length(); - } -} - -/*! -Escapes all special characters in the given string according to ECMA-404. -Necessary as some characters such as quotes, backslashes and so on -can't be used as is when dumping a string value. - -\param str the string that should be escaped. - -\return a copy of the given string with all special characters escaped. -*/ -std::string json::escapeString(const std::string& str) const -{ - std::string result(str); - // we first need to escape the backslashes as all other methods will insert - // legitimate backslashes into the result. - replaceChar(result, '\\', "\\\\"); - // replace all other characters - replaceChar(result, '"', "\\\""); - replaceChar(result, '\n', "\\n"); - replaceChar(result, '\r', "\\r"); - replaceChar(result, '\f', "\\f"); - replaceChar(result, '\b', "\\b"); - replaceChar(result, '\t', "\\t"); - return result; -} - -/*! -Serialization function for JSON objects. The function tries to mimick Python's -\p json.dumps() function, and currently supports its \p indent parameter. - -\param indent if indent is nonnegative, then array elements and object members - will be pretty-printed with that indent level. An indent level - of 0 will only insert newlines. -1 (the default) selects the - most compact representation - -\see https://docs.python.org/2/library/json.html#json.dump -*/ -std::string json::dump(int indent) const noexcept -{ - if (indent >= 0) - { - return dump(true, static_cast<unsigned int>(indent)); - } - else - { - return dump(false, 0); - } -} - - -/////////////////////////////////////////// -// ADDING ELEMENTS TO OBJECTS AND ARRAYS // -/////////////////////////////////////////// - -json& json::operator+=(const json& o) -{ - push_back(o); - return *this; -} - -/*! -@todo comment me -*/ -json& json::operator+=(const object_t::value_type& p) -{ - return operator[](p.first) = p.second; -} - -/*! -@todo comment me -*/ -json& json::operator+=(list_init_t a) -{ - push_back(a); - return *this; -} - -/*! -This function implements the actual "adding to array" function and is called -by all other push_back or operator+= functions. If the function is called for -an array, the passed element is added to the array. - -@param o The element to add to the array. - -@pre The JSON object is an array or null. -@post The JSON object is an array whose last element is the passed element o. -@exception std::runtime_error The function was called for a JSON type that - does not support addition to an array (e.g., int or string). - -@note Null objects are silently transformed into an array before the addition. -*/ -void json::push_back(const json& o) -{ - // push_back only works for null objects or arrays - if (not(type_ == value_t::null or type_ == value_t::array)) - { - throw std::runtime_error("cannot add element to " + type_name()); - } - - // transform null object into an array - if (type_ == value_t::null) - { - type_ = value_t::array; - value_.array = new array_t; - } - - // add element to array - value_.array->push_back(o); -} - -/*! -This function implements the actual "adding to array" function and is called -by all other push_back or operator+= functions. If the function is called for -an array, the passed element is added to the array using move semantics. - -@param o The element to add to the array. - -@pre The JSON object is an array or null. -@post The JSON object is an array whose last element is the passed element o. -@post The element o is destroyed. -@exception std::runtime_error The function was called for a JSON type that - does not support addition to an array (e.g., int or string). - -@note Null objects are silently transformed into an array before the addition. -@note This function applies move semantics for the given element. -*/ -void json::push_back(json&& o) -{ - // push_back only works for null objects or arrays - if (not(type_ == value_t::null or type_ == value_t::array)) - { - throw std::runtime_error("cannot add element to " + type_name()); - } - - // transform null object into an array - if (type_ == value_t::null) - { - type_ = value_t::array; - value_.array = new array_t; - } - - // add element to array (move semantics) - value_.array->emplace_back(std::move(o)); - // invalidate object - o.type_ = value_t::null; -} - -/*! -@todo comment me -*/ -void json::push_back(const object_t::value_type& p) -{ - operator[](p.first) = p.second; -} - -/*! -@todo comment me -*/ -void json::push_back(list_init_t a) -{ - bool is_array = false; - - // check if each element is an array with two elements whose first element - // is a string - for (const auto& element : a) - { - if (element.type_ != value_t::array or - element.size() != 2 or - element[0].type_ != value_t::string) - { - // the initializer list describes an array - is_array = true; - break; - } - } - - if (is_array) - { - for (const json& element : a) - { - push_back(element); - } - } - else - { - for (const json& element : a) - { - const object_t::value_type tmp {element[0].get<std::string>(), element[1]}; - push_back(tmp); - } - } -} - -/*! -This operator realizes read/write access to array elements given an integer -index. Bounds will not be checked. - -@note The "index" variable should be of type size_t as it is compared against - size() and used in the at() function. However, the compiler will have - problems in case integer literals are used. In this case, an implicit - conversion to both size_t and JSON is possible. Therefore, we use int as - type and convert it to size_t where necessary. - -@param index the index of the element to return from the array -@return reference to element for the given index - -@pre Object is an array. -@exception std::domain_error if object is not an array -*/ -json::reference json::operator[](const int index) -{ - // this [] operator only works for arrays - if (type_ != value_t::array) - { - throw std::domain_error("cannot add entry with index " + - std::to_string(index) + " to " + type_name()); - } - - // return reference to element from array at given index - return (*value_.array)[static_cast<std::size_t>(index)]; -} - -/*! -This operator realizes read-only access to array elements given an integer -index. Bounds will not be checked. - -@note The "index" variable should be of type size_t as it is compared against - size() and used in the at() function. However, the compiler will have - problems in case integer literals are used. In this case, an implicit - conversion to both size_t and JSON is possible. Therefore, we use int as - type and convert it to size_t where necessary. - -@param index the index of the element to return from the array -@return read-only reference to element for the given index - -@pre Object is an array. -@exception std::domain_error if object is not an array -*/ -json::const_reference json::operator[](const int index) const -{ - // this [] operator only works for arrays - if (type_ != value_t::array) - { - throw std::domain_error("cannot get entry with index " + - std::to_string(index) + " from " + type_name()); - } - - // return element from array at given index - return (*value_.array)[static_cast<std::size_t>(index)]; -} - -/*! -This function realizes read/write access to array elements given an integer -index. Bounds will be checked. - -@note The "index" variable should be of type size_t as it is compared against - size() and used in the at() function. However, the compiler will have - problems in case integer literals are used. In this case, an implicit - conversion to both size_t and JSON is possible. Therefore, we use int as - type and convert it to size_t where necessary. - -@param index the index of the element to return from the array -@return reference to element for the given index - -@pre Object is an array. -@exception std::domain_error if object is not an array -@exception std::out_of_range if index is out of range (via std::vector::at) -*/ -json::reference json::at(const int index) -{ - // this function only works for arrays - if (type_ != value_t::array) - { - throw std::domain_error("cannot add entry with index " + - std::to_string(index) + " to " + type_name()); - } - - // return reference to element from array at given index - return value_.array->at(static_cast<std::size_t>(index)); -} - -/*! -This operator realizes read-only access to array elements given an integer -index. Bounds will be checked. - -@note The "index" variable should be of type size_t as it is compared against - size() and used in the at() function. However, the compiler will have - problems in case integer literals are used. In this case, an implicit - conversion to both size_t and JSON is possible. Therefore, we use int as - type and convert it to size_t where necessary. - -@param index the index of the element to return from the array -@return read-only reference to element for the given index - -@pre Object is an array. -@exception std::domain_error if object is not an array -@exception std::out_of_range if index is out of range (via std::vector::at) -*/ -json::const_reference json::at(const int index) const -{ - // this function only works for arrays - if (type_ != value_t::array) - { - throw std::domain_error("cannot get entry with index " + - std::to_string(index) + " from " + type_name()); - } - - // return element from array at given index - return value_.array->at(static_cast<std::size_t>(index)); -} - -/*! -@copydoc json::operator[](const char* key) -*/ -json::reference json::operator[](const std::string& key) -{ - return operator[](key.c_str()); -} - -/*! -This operator realizes read/write access to object elements given a string -key. - -@param key the key index of the element to return from the object -@return reference to a JSON object for the given key (null if key does not - exist) - -@pre Object is an object or a null object. -@post null objects are silently converted to objects. - -@exception std::domain_error if object is not an object (or null) -*/ -json::reference json::operator[](const char* key) -{ - // implicitly convert null to object - if (type_ == value_t::null) - { - type_ = value_t::object; - value_.object = new object_t; - } - - // this [] operator only works for objects - if (type_ != value_t::object) - { - throw std::domain_error("cannot add entry with key " + - std::string(key) + " to " + type_name()); - } - - // if the key does not exist, create it - if (value_.object->find(key) == value_.object->end()) - { - (*value_.object)[key] = json(); - } - - // return reference to element from array at given index - return (*value_.object)[key]; -} - -/*! -@copydoc json::operator[](const char* key) -*/ -json::const_reference json::operator[](const std::string& key) const -{ - return operator[](key.c_str()); -} - -/*! -This operator realizes read-only access to object elements given a string -key. - -@param key the key index of the element to return from the object -@return read-only reference to element for the given key - -@pre Object is an object. -@exception std::domain_error if object is not an object -@exception std::out_of_range if key is not found in object -*/ -json::const_reference json::operator[](const char* key) const -{ - // this [] operator only works for objects - if (type_ != value_t::object) - { - throw std::domain_error("cannot get entry with key " + - std::string(key) + " from " + type_name()); - } - - // search for the key - const auto it = value_.object->find(key); - - // make sure the key exists in the object - if (it == value_.object->end()) - { - throw std::out_of_range("key " + std::string(key) + " not found"); - } - - // return element from array at given key - return it->second; -} - -/*! -@copydoc json::at(const char* key) -*/ -json::reference json::at(const std::string& key) -{ - return at(key.c_str()); -} - -/*! -This function realizes read/write access to object elements given a string -key. - -@param key the key index of the element to return from the object -@return reference to a JSON object for the given key (exception if key does not - exist) - -@pre Object is an object. - -@exception std::domain_error if object is not an object -@exception std::out_of_range if key was not found (via std::map::at) -*/ -json::reference json::at(const char* key) -{ - // this function operator only works for objects - if (type_ != value_t::object) - { - throw std::domain_error("cannot add entry with key " + - std::string(key) + " to " + type_name()); - } - - // return reference to element from array at given index - return value_.object->at(key); -} - -/*! -@copydoc json::at(const char *key) const -*/ -json::const_reference json::at(const std::string& key) const -{ - return at(key.c_str()); -} - -/*! -This operator realizes read-only access to object elements given a string -key. - -@param key the key index of the element to return from the object -@return read-only reference to element for the given key - -@pre Object is an object. -@exception std::domain_error if object is not an object -@exception std::out_of_range if key is not found (via std::map::at) -*/ -json::const_reference json::at(const char* key) const -{ - // this [] operator only works for objects - if (type_ != value_t::object) - { - throw std::domain_error("cannot get entry with key " + - std::string(key) + " from " + type_name()); - } - - // return element from array at given key - return value_.object->at(key); -} - -/*! -Returns the size of the JSON object. - -@return the size of the JSON object; the size is the number of elements in - compounds (array and object), 1 for value types (true, false, number, - string), and 0 for null. - -@invariant The size is reported as 0 if and only if empty() would return true. -*/ -json::size_type json::size() const noexcept -{ - switch (type_) - { - case (value_t::array): - { - return value_.array->size(); - } - case (value_t::object): - { - return value_.object->size(); - } - case (value_t::null): - { - return 0; - } - default: - { - return 1; - } - } -} - -/*! -Returns the maximal size of the JSON object. - -@return the maximal size of the JSON object; the maximal size is the maximal - number of elements in compounds (array and object), 1 for value types - (true, false, number, string), and 0 for null. -*/ -json::size_type json::max_size() const noexcept -{ - switch (type_) - { - case (value_t::array): - { - return value_.array->max_size(); - } - case (value_t::object): - { - return value_.object->max_size(); - } - case (value_t::null): - { - return 0; - } - default: - { - return 1; - } - } -} - -/*! -Returns whether a JSON object is empty. - -@return true for null objects and empty compounds (array and object); false - for value types (true, false, number, string) and filled compounds - (array and object). - -@invariant Empty would report true if and only if size() would return 0. -*/ -bool json::empty() const noexcept -{ - switch (type_) - { - case (value_t::array): - { - return value_.array->empty(); - } - case (value_t::object): - { - return value_.object->empty(); - } - case (value_t::null): - { - return true; - } - default: - { - return false; - } - } -} - -/*! -Removes all elements from compounds and resets values to default. - -@invariant Clear will set any value type to its default value which is empty - for compounds, false for booleans, 0 for integer numbers, and 0.0 - for floating numbers. -*/ -void json::clear() noexcept -{ - switch (type_) - { - case (value_t::array): - { - value_.array->clear(); - break; - } - case (value_t::object): - { - value_.object->clear(); - break; - } - case (value_t::string): - { - value_.string->clear(); - break; - } - case (value_t::boolean): - { - value_.boolean = {}; - break; - } - case (value_t::number): - { - value_.number = {}; - break; - } - case (value_t::number_float): - { - value_.number_float = {}; - break; - } - default: - { - break; - } - } -} - -void json::swap(json& o) noexcept -{ - std::swap(type_, o.type_); - std::swap(value_, o.value_); -} - -json::value_t json::type() const noexcept -{ - return type_; -} - -json::iterator json::find(const std::string& key) -{ - return find(key.c_str()); -} - -json::const_iterator json::find(const std::string& key) const -{ - return find(key.c_str()); -} - -json::iterator json::find(const char* key) -{ - auto result = end(); - - if (type_ == value_t::object) - { - delete result.oi_; - result.oi_ = new object_t::iterator(value_.object->find(key)); - result.invalid = (*(result.oi_) == value_.object->end()); - } - - return result; -} - -json::const_iterator json::find(const char* key) const -{ - auto result = cend(); - - if (type_ == value_t::object) - { - delete result.oi_; - result.oi_ = new object_t::const_iterator(value_.object->find(key)); - result.invalid = (*(result.oi_) == value_.object->cend()); - } - - return result; -} - -bool json::operator==(const json& o) const noexcept -{ - switch (type_) - { - case (value_t::array): - { - if (o.type_ == value_t::array) - { - return *value_.array == *o.value_.array; - } - break; - } - case (value_t::object): - { - if (o.type_ == value_t::object) - { - return *value_.object == *o.value_.object; - } - break; - } - case (value_t::null): - { - if (o.type_ == value_t::null) - { - return true; - } - break; - } - case (value_t::string): - { - if (o.type_ == value_t::string) - { - return *value_.string == *o.value_.string; - } - break; - } - case (value_t::boolean): - { - if (o.type_ == value_t::boolean) - { - return value_.boolean == o.value_.boolean; - } - break; - } - case (value_t::number): - { - if (o.type_ == value_t::number) - { - return value_.number == o.value_.number; - } - if (o.type_ == value_t::number_float) - { - return value_.number == static_cast<number_t>(o.value_.number_float); - } - break; - } - case (value_t::number_float): - { - if (o.type_ == value_t::number) - { - return value_.number_float == static_cast<number_float_t>(o.value_.number); - } - if (o.type_ == value_t::number_float) - { - return value_.number_float == o.value_.number_float; - } - break; - } - } - - return false; -} - -bool json::operator!=(const json& o) const noexcept -{ - return not operator==(o); -} - - -json::iterator json::begin() noexcept -{ - return json::iterator(this, true); -} - -json::iterator json::end() noexcept -{ - return json::iterator(this, false); -} - -json::const_iterator json::begin() const noexcept -{ - return json::const_iterator(this, true); -} - -json::const_iterator json::end() const noexcept -{ - return json::const_iterator(this, false); -} - -json::const_iterator json::cbegin() const noexcept -{ - return json::const_iterator(this, true); -} - -json::const_iterator json::cend() const noexcept -{ - return json::const_iterator(this, false); -} - -json::reverse_iterator json::rbegin() noexcept -{ - return reverse_iterator(end()); -} - -json::reverse_iterator json::rend() noexcept -{ - return reverse_iterator(begin()); -} - -json::const_reverse_iterator json::crbegin() const noexcept -{ - return const_reverse_iterator(cend()); -} - -json::const_reverse_iterator json::crend() const noexcept -{ - return const_reverse_iterator(cbegin()); -} - - -json::iterator::iterator(json* j, bool begin) - : object_(j), invalid(not begin or j == nullptr) -{ - if (object_ != nullptr) - { - if (object_->type_ == json::value_t::array) - { - if (begin) - { - vi_ = new array_t::iterator(object_->value_.array->begin()); - invalid = (*vi_ == object_->value_.array->end()); - } - else - { - vi_ = new array_t::iterator(object_->value_.array->end()); - } - } - else if (object_->type_ == json::value_t::object) - { - if (begin) - { - oi_ = new object_t::iterator(object_->value_.object->begin()); - invalid = (*oi_ == object_->value_.object->end()); - } - else - { - oi_ = new object_t::iterator(object_->value_.object->end()); - } - } - } -} - -json::iterator::iterator(const json::iterator& o) - : object_(o.object_), invalid(o.invalid) -{ - if (o.vi_ != nullptr) - { - vi_ = new array_t::iterator(*(o.vi_)); - } - - if (o.oi_ != nullptr) - { - oi_ = new object_t::iterator(*(o.oi_)); - } -} - -json::iterator::~iterator() -{ - delete vi_; - delete oi_; -} - -json::iterator& json::iterator::operator=(json::iterator o) -{ - std::swap(object_, o.object_); - std::swap(vi_, o.vi_); - std::swap(oi_, o.oi_); - std::swap(invalid, o.invalid); - return *this; -} - -bool json::iterator::operator==(const json::iterator& o) const -{ - if (object_ != nullptr and o.object_ != nullptr) - { - if (object_->type_ == json::value_t::array and o.object_->type_ == json::value_t::array) - { - return (*vi_ == *(o.vi_)); - } - if (object_->type_ == json::value_t::object and o.object_->type_ == json::value_t::object) - { - return (*oi_ == *(o.oi_)); - } - - if (invalid == o.invalid and object_ == o.object_) - { - return true; - } - } - return false; -} - -bool json::iterator::operator!=(const json::iterator& o) const -{ - return not operator==(o); -} - -json::iterator& json::iterator::operator++() -{ - if (object_ != nullptr) - { - switch (object_->type_) - { - case (json::value_t::array): - { - std::advance(*vi_, 1); - invalid = (*vi_ == object_->value_.array->end()); - break; - } - case (json::value_t::object): - { - std::advance(*oi_, 1); - invalid = (*oi_ == object_->value_.object->end()); - break; - } - default: - { - invalid = true; - break; - } - } - } - - return *this; -} - -json::iterator& json::iterator::operator--() -{ - if (object_ != nullptr) - { - switch (object_->type_) - { - case (json::value_t::array): - { - invalid = (*vi_ == object_->value_.array->begin()); - std::advance(*vi_, -1); - break; - } - case (json::value_t::object): - { - invalid = (*oi_ == object_->value_.object->begin()); - std::advance(*oi_, -1); - break; - } - default: - { - invalid = true; - break; - } - } - } - - return *this; -} - -json& json::iterator::operator*() const -{ - if (object_ == nullptr or invalid) - { - throw std::out_of_range("cannot get value"); - } - - switch (object_->type_) - { - case (json::value_t::array): - { - return **vi_; - } - case (json::value_t::object): - { - return (*oi_)->second; - } - default: - { - return *object_; - } - } -} - -json* json::iterator::operator->() const -{ - if (object_ == nullptr or invalid) - { - throw std::out_of_range("cannot get value"); - } - - switch (object_->type_) - { - case (json::value_t::array): - { - return &(**vi_); - } - case (json::value_t::object): - { - return &((*oi_)->second); - } - default: - { - return object_; - } - } -} - -std::string json::iterator::key() const -{ - if (object_ == nullptr or invalid or object_->type_ != json::value_t::object) - { - throw std::out_of_range("cannot get value"); - } - - return (*oi_)->first; -} - -json& json::iterator::value() const -{ - if (object_ == nullptr or invalid) - { - throw std::out_of_range("cannot get value"); - } - - switch (object_->type_) - { - case (json::value_t::array): - { - return **vi_; - } - case (json::value_t::object): - { - return (*oi_)->second; - } - default: - { - return *object_; - } - } -} - - -json::const_iterator::const_iterator(const json* j, bool begin) - : object_(j), invalid(not begin or j == nullptr) -{ - if (object_ != nullptr) - { - if (object_->type_ == json::value_t::array) - { - if (begin) - { - vi_ = new array_t::const_iterator(object_->value_.array->cbegin()); - invalid = (*vi_ == object_->value_.array->cend()); - } - else - { - vi_ = new array_t::const_iterator(object_->value_.array->cend()); - } - } - else if (object_->type_ == json::value_t::object) - { - if (begin) - { - oi_ = new object_t::const_iterator(object_->value_.object->cbegin()); - invalid = (*oi_ == object_->value_.object->cend()); - } - else - { - oi_ = new object_t::const_iterator(object_->value_.object->cend()); - } - } - } -} - -json::const_iterator::const_iterator(const json::const_iterator& o) - : object_(o.object_), invalid(o.invalid) -{ - if (o.vi_ != nullptr) - { - vi_ = new array_t::const_iterator(*(o.vi_)); - } - if (o.oi_ != nullptr) - { - oi_ = new object_t::const_iterator(*(o.oi_)); - } -} - -json::const_iterator::const_iterator(const json::iterator& o) - : object_(o.object_), invalid(o.invalid) -{ - if (o.vi_ != nullptr) - { - vi_ = new array_t::const_iterator(*(o.vi_)); - } - if (o.oi_ != nullptr) - { - oi_ = new object_t::const_iterator(*(o.oi_)); - } -} - -json::const_iterator::~const_iterator() -{ - delete vi_; - delete oi_; -} - -json::const_iterator& json::const_iterator::operator=(json::const_iterator o) -{ - std::swap(object_, o.object_); - std::swap(vi_, o.vi_); - std::swap(oi_, o.oi_); - std::swap(invalid, o.invalid); - return *this; -} - -bool json::const_iterator::operator==(const json::const_iterator& o) const -{ - if (object_ != nullptr and o.object_ != nullptr) - { - if (object_->type_ == json::value_t::array and o.object_->type_ == json::value_t::array) - { - return (*vi_ == *(o.vi_)); - } - if (object_->type_ == json::value_t::object and o.object_->type_ == json::value_t::object) - { - return (*oi_ == *(o.oi_)); - } - if (invalid == o.invalid and object_ == o.object_) - { - return true; - } - } - - return false; -} - -bool json::const_iterator::operator!=(const json::const_iterator& o) const -{ - return not operator==(o); -} - -json::const_iterator& json::const_iterator::operator++() -{ - if (object_ != nullptr) - { - switch (object_->type_) - { - case (json::value_t::array): - { - std::advance(*vi_, 1); - invalid = (*vi_ == object_->value_.array->end()); - break; - } - case (json::value_t::object): - { - std::advance(*oi_, 1); - invalid = (*oi_ == object_->value_.object->end()); - break; - } - default: - { - invalid = true; - break; - } - } - } - - return *this; -} - -json::const_iterator& json::const_iterator::operator--() -{ - if (object_ != nullptr) - { - switch (object_->type_) - { - case (json::value_t::array): - { - invalid = (*vi_ == object_->value_.array->begin()); - std::advance(*vi_, -1); - break; - } - case (json::value_t::object): - { - invalid = (*oi_ == object_->value_.object->begin()); - std::advance(*oi_, -1); - break; - } - default: - { - invalid = true; - break; - } - } - } - - return *this; -} - -const json& json::const_iterator::operator*() const -{ - if (object_ == nullptr or invalid) - { - throw std::out_of_range("cannot get value"); - } - - switch (object_->type_) - { - case (json::value_t::array): - { - return **vi_; - } - case (json::value_t::object): - { - return (*oi_)->second; - } - default: - { - return *object_; - } - } -} - -const json* json::const_iterator::operator->() const -{ - if (object_ == nullptr or invalid) - { - throw std::out_of_range("cannot get value"); - } - - switch (object_->type_) - { - case (json::value_t::array): - { - return &(**vi_); - } - case (json::value_t::object): - { - return &((*oi_)->second); - } - default: - { - return object_; - } - } -} - -std::string json::const_iterator::key() const -{ - if (object_ == nullptr or invalid or object_->type_ != json::value_t::object) - { - throw std::out_of_range("cannot get value"); - } - - return (*oi_)->first; -} - -const json& json::const_iterator::value() const -{ - if (object_ == nullptr or invalid) - { - throw std::out_of_range("cannot get value"); - } - - switch (object_->type_) - { - case (json::value_t::array): - { - return **vi_; - } - case (json::value_t::object): - { - return (*oi_)->second; - } - default: - { - return *object_; - } - } -} - - -/*! -Initialize the JSON parser given a string \p s. - -@note After initialization, the function @ref parse has to be called manually. - -@param s string to parse - -@post \p s is copied to the buffer @ref buffer_ and the first character is - read. Whitespace is skipped. -*/ -json::parser::parser(const char* s) - : buffer_(s) -{ - // read first character - next(); -} - -/*! -@copydoc json::parser::parser(const char* s) -*/ -json::parser::parser(const std::string& s) - : buffer_(s) -{ - // read first character - next(); -} - -/*! -Initialize the JSON parser given an input stream \p _is. - -@note After initialization, the function @ref parse has to be called manually. - -\param _is input stream to parse - -@post \p _is is copied to the buffer @ref buffer_ and the firsr character is - read. Whitespace is skipped. - -*/ -json::parser::parser(std::istream& _is) -{ - while (_is) - { - std::string input_line; - std::getline(_is, input_line); - buffer_ += input_line; - } - - // read first character - next(); -} - -json json::parser::parse() -{ - switch (current_) - { - case ('{'): - { - // explicitly set result to object to cope with {} - json result(value_t::object); - - next(); - - // process nonempty object - if (current_ != '}') - { - do - { - // key - auto key = parseString(); - - // colon - expect(':'); - - // value - result[std::move(key)] = parse(); - key.clear(); - } - while (current_ == ',' and next()); - } - - // closing brace - expect('}'); - - return result; - } - - case ('['): - { - // explicitly set result to array to cope with [] - json result(value_t::array); - - next(); - - // process nonempty array - if (current_ != ']') - { - do - { - result.push_back(parse()); - } - while (current_ == ',' and next()); - } - - // closing bracket - expect(']'); - - return result; - } - - case ('\"'): - { - return json(parseString()); - } - - case ('t'): - { - parseTrue(); - return json(true); - } - - case ('f'): - { - parseFalse(); - return json(false); - } - - case ('n'): - { - parseNull(); - return json(); - } - - case ('-'): - case ('0'): - case ('1'): - case ('2'): - case ('3'): - case ('4'): - case ('5'): - case ('6'): - case ('7'): - case ('8'): - case ('9'): - { - // remember position of number's first character - const auto _firstpos_ = pos_ - 1; - - while (next() and (std::isdigit(current_) or current_ == '.' - or current_ == 'e' or current_ == 'E' - or current_ == '+' or current_ == '-')); - - try - { - const auto float_val = std::stold(buffer_.substr(_firstpos_, pos_ - _firstpos_)); - const auto int_val = static_cast<number_t>(float_val); - - // check if conversion loses precision - if (float_val == int_val) - { - // we would not lose precision -> int - return json(int_val); - } - else - { - // we would lose precision -> float - return json(static_cast<number_float_t>(float_val)); - } - } - catch (...) - { - error("error translating " + - buffer_.substr(_firstpos_, pos_ - _firstpos_) + " to number"); - } - } - - default: - { - error("unexpected character"); - } - } -} - -/*! -This function reads the next character from the buffer while ignoring all -trailing whitespace. If another character could be read, the function returns -true. If the end of the buffer is reached, false is returned. - -@return whether another non-whitespace character could be read - -@post current_ holds the next character -*/ -bool json::parser::next() -{ - if (pos_ == buffer_.size()) - { - return false; - } - - current_ = buffer_[pos_++]; - - // skip trailing whitespace - while (std::isspace(current_)) - { - if (pos_ == buffer_.size()) - { - return false; - } - - current_ = buffer_[pos_++]; - } - - return true; -} - -/*! -This function encapsulates the error reporting functions of the parser class. -It throws a \p std::invalid_argument exception with a description where the -error occurred (given as the number of characters read), what went wrong (using -the error message \p msg), and the last read token. - -@param msg an error message -@return <em>This function does not return.</em> - -@exception std::invalid_argument whenever the function is called -*/ -void json::parser::error(const std::string& msg) const -{ - throw std::invalid_argument("parse error at position " + - std::to_string(pos_) + ": " + msg + - ", last read: '" + current_ + "'"); -} - -/*! -Parses a string after opening quotes (\p ") where read. - -@return the parsed string - -@pre An opening quote \p " was read in the main parse function @ref parse. - pos_ is the position after the opening quote. - -@post The character after the closing quote \p " is the current character @ref - current_. Whitespace is skipped. - -@todo Unicode escapes such as \uxxxx are missing - see - https://github.com/nlohmann/json/issues/12 -*/ -std::string json::parser::parseString() -{ - // true if and only if the amount of backslashes before the current - // character is even - bool evenAmountOfBackslashes = true; - - // the result of the parse process - std::string result; - - // iterate with pos_ over the whole input until we found the end and return - // or we exit via error() - for (; pos_ < buffer_.size(); pos_++) - { - char currentChar = buffer_[pos_]; - - if (not evenAmountOfBackslashes) - { - // uneven amount of backslashes means the user wants to escape - // something so we know there is a case such as '\X' or '\\\X' but - // we don't know yet what X is. - // at this point in the code, the currentChar has the value of X. - - // slash, backslash and quote are copied as is - if (currentChar == '/' or currentChar == '\\' or currentChar == '"') - { - result += currentChar; - } - else - { - // all other characters are replaced by their respective special - // character - switch (currentChar) - { - case 't': - { - result += '\t'; - break; - } - case 'b': - { - result += '\b'; - break; - } - case 'f': - { - result += '\f'; - break; - } - case 'n': - { - result += '\n'; - break; - } - case 'r': - { - result += '\r'; - break; - } - case 'u': - { - // \uXXXX[\uXXXX] is used for escaping unicode, which - // has it's own subroutine. - result += parseUnicodeEscape(); - // the parsing process has brought us one step behind - // the unicode escape sequence: - // \uXXXX - // ^ - // we need to go one character back or the parser would - // skip the character we are currently pointing at as - // the for-loop will decrement pos_ after this iteration - pos_--; - break; - } - default: - { - error("expected one of \\, /, b, f, n, r, t, u behind backslash."); - } - } - } - } - else - { - if (currentChar == '"') - { - // currentChar is a quote, so we found the end of the string - - // set pos_ behind the trailing quote - pos_++; - // find next char to parse - next(); - - // bring the result of the parsing process back to the caller - return result; - } - else if (currentChar != '\\') - { - // all non-backslash characters are added to the end of the - // result string. The only backslashes we want in the result - // are the ones that are escaped (which happens above). - result += currentChar; - } - } - - // remember if we have an even amount of backslashes before the current - // character - if (currentChar == '\\') - { - // jump between even/uneven for each backslash we encounter - evenAmountOfBackslashes = not evenAmountOfBackslashes; - } - else - { - // zero backslashes are also an even number, so as soon as we - // encounter a non-backslash the chain of backslashes breaks and - // we start again from zero - evenAmountOfBackslashes = true; - } - } - - // we iterated over the whole string without finding a unescaped quote - // so the given string is malformed - error("expected '\"'"); -} - - - -/*! -Turns a code point into it's UTF-8 representation. -You should only pass numbers < 0x10ffff into this function -(everything else is a invalid code point). - -@return the UTF-8 representation of the given code point -*/ -std::string json::parser::codePointToUTF8(unsigned int codePoint) const -{ - // this method contains a lot of bit manipulations to - // build the bytes for UTF-8. - - // the '(... >> S) & 0xHH'-patterns are used to retrieve - // certain bits from the code points. - - // all static casts in this method have boundary checks - - // we initialize all strings with their final length - // (e.g. 1 to 4 bytes) to save the reallocations. - - if (codePoint <= 0x7f) - { - // it's just a ASCII compatible codePoint, - // so we just interpret the point as a character - // and return ASCII - - return std::string(1, static_cast<char>(codePoint)); - } - // if true, we need two bytes to encode this as UTF-8 - else if (codePoint <= 0x7ff) - { - // the 0xC0 enables the two most significant two bits - // to make this a two-byte UTF-8 character. - std::string result(2, static_cast<char>(0xC0 | ((codePoint >> 6) & 0x1F))); - result[1] = static_cast<char>(0x80 | (codePoint & 0x3F)); - return result; - } - // if true, now we need three bytes to encode this as UTF-8 - else if (codePoint <= 0xffff) - { - // the 0xE0 enables the three most significant two bits - // to make this a three-byte UTF-8 character. - std::string result(3, static_cast<char>(0xE0 | ((codePoint >> 12) & 0x0F))); - result[1] = static_cast<char>(0x80 | ((codePoint >> 6) & 0x3F)); - result[2] = static_cast<char>(0x80 | (codePoint & 0x3F)); - return result; - } - // if true, we need maximal four bytes to encode this as UTF-8 - else if (codePoint <= 0x10ffff) - { - // the 0xE0 enables the four most significant two bits - // to make this a three-byte UTF-8 character. - std::string result(4, static_cast<char>(0xF0 | ((codePoint >> 18) & 0x07))); - result[1] = static_cast<char>(0x80 | ((codePoint >> 12) & 0x3F)); - result[2] = static_cast<char>(0x80 | ((codePoint >> 6) & 0x3F)); - result[3] = static_cast<char>(0x80 | (codePoint & 0x3F)); - return result; - } - else - { - // Can't be tested without direct access to this private method. - std::string errorMessage = "Invalid codePoint: "; - errorMessage += codePoint; - error(errorMessage); - } -} - -/*! -Parses 4 hexadecimal characters as a number. - -@return the value of the number the hexadecimal characters represent. - -@pre pos_ is pointing to the first of the 4 hexadecimal characters. - -@post pos_ is pointing to the character after the 4 hexadecimal characters. -*/ -unsigned int json::parser::parse4HexCodePoint() -{ - const auto startPos = pos_; - - // check if the remaining buffer is long enough to even hold 4 characters - if (pos_ + 3 >= buffer_.size()) - { - error("Got end of input while parsing unicode escape sequence \\uXXXX"); - } - - // make a string that can hold the pair - std::string hexCode(4, ' '); - - for (; pos_ < startPos + 4; pos_++) - { - // no boundary check here as we already checked above - char currentChar = buffer_[pos_]; - - // check if we have a hexadecimal character - if ((currentChar >= '0' and currentChar <= '9') - or (currentChar >= 'a' and currentChar <= 'f') - or (currentChar >= 'A' and currentChar <= 'F')) - { - // all is well, we have valid hexadecimal chars - // so we copy that char into our string - hexCode[pos_ - startPos] = currentChar; - } - else - { - error("Found non-hexadecimal character in unicode escape sequence!"); - } - } - // the cast is safe as 4 hex characters can't present more than 16 bits - // the input to stoul was checked to contain only hexadecimal characters - // (see above) - return static_cast<unsigned int>(std::stoul(hexCode, nullptr, 16)); -} - -/*! -Parses the unicode escape codes as defined in the ECMA-404. -The escape sequence has two forms: -1. \uXXXX -2. \uXXXX\uYYYY -where X and Y are a hexadecimal character (a-zA-Z0-9). - -Form 1 just contains the unicode code point in the hexadecimal number XXXX. -Form 2 is encoding a UTF-16 surrogate pair. The high surrogate is XXXX, the low -surrogate is YYYY. - -@return the UTF-8 character this unicode escape sequence escaped. - -@pre pos_ is pointing at at the 'u' behind the first backslash. - -@post pos_ is pointing at the character behind the last X (or Y in form 2). -*/ -std::string json::parser::parseUnicodeEscape() -{ - // jump to the first hex value - pos_++; - // parse the hex first hex values - unsigned int firstCodePoint = parse4HexCodePoint(); - - if (firstCodePoint >= 0xD800 and firstCodePoint <= 0xDBFF) - { - // we found invalid code points, which means we either have a malformed - // input or we found a high surrogate. - // we can only find out by seeing if the next character also wants to - // encode a unicode character (so, we have the \uXXXX\uXXXX case here). - - // jump behind the next \u - pos_ += 2; - // try to parse the next hex values. - // the method does boundary checking for us, so no need to do that here - unsigned secondCodePoint = parse4HexCodePoint(); - // ok, we have a low surrogate, check if it is a valid one - if (secondCodePoint >= 0xDC00 and secondCodePoint <= 0xDFFF) - { - // calculate the code point from the pair according to the spec - unsigned int finalCodePoint = - // high surrogate occupies the most significant 22 bits - (firstCodePoint << 10) - // low surrogate occupies the least significant 15 bits - + secondCodePoint - // there is still the 0xD800, 0xDC00 and 0x10000 noise in - // the result - // so we have to substract with: - // (0xD800 << 10) + DC00 - 0x10000 = 0x35FDC00 - - 0x35FDC00; - - // we transform the calculated point into UTF-8 - return codePointToUTF8(finalCodePoint); - } - else - { - error("missing low surrogate"); - } - - } - // We have Form 1, so we just interpret the XXXX as a code point - return codePointToUTF8(firstCodePoint); -} - - -/*! -This function is called in case a \p "t" is read in the main parse function -@ref parse. In the standard, the \p "true" token is the only candidate, so the -next three characters are expected to be \p "rue". In case of a mismatch, an -error is raised via @ref error. - -@pre A \p "t" was read in the main parse function @ref parse. -@post The character after the \p "true" is the current character. Whitespace is - skipped. -*/ -void json::parser::parseTrue() -{ - if (buffer_.substr(pos_, 3) != "rue") - { - error("expected true"); - } - - pos_ += 3; - - // read next character - next(); -} - -/*! -This function is called in case an \p "f" is read in the main parse function -@ref parse. In the standard, the \p "false" token is the only candidate, so the -next four characters are expected to be \p "alse". In case of a mismatch, an -error is raised via @ref error. - -@pre An \p "f" was read in the main parse function. -@post The character after the \p "false" is the current character. Whitespace - is skipped. -*/ -void json::parser::parseFalse() -{ - if (buffer_.substr(pos_, 4) != "alse") - { - error("expected false"); - } - - pos_ += 4; - - // read next character - next(); -} - -/*! -This function is called in case an \p "n" is read in the main parse function -@ref parse. In the standard, the \p "null" token is the only candidate, so the -next three characters are expected to be \p "ull". In case of a mismatch, an -error is raised via @ref error. - -@pre An \p "n" was read in the main parse function. -@post The character after the \p "null" is the current character. Whitespace is - skipped. -*/ -void json::parser::parseNull() -{ - if (buffer_.substr(pos_, 3) != "ull") - { - error("expected null"); - } - - pos_ += 3; - - // read next character - next(); -} - -/*! -This function wraps functionality to check whether the current character @ref -current_ matches a given character \p c. In case of a match, the next character -of the buffer @ref buffer_ is read. In case of a mismatch, an error is raised -via @ref error. - -@param c character that is expected - -@post The next chatacter is read. Whitespace is skipped. -*/ -void json::parser::expect(const char c) -{ - if (current_ != c) - { - std::string msg = "expected '"; - msg.append(1, c); - msg += "'"; - error(msg); - } - else - { - next(); - } -} - -} - -/*! -This operator implements a user-defined string literal for JSON objects. It can -be used by adding \p "_json" to a string literal and returns a JSON object if -no parse error occurred. - -@param s a string representation of a JSON object -@return a JSON object -*/ -nlohmann::json operator "" _json(const char* s, std::size_t) -{ - return nlohmann::json::parse(s); -} diff --git a/src/json.h b/src/json.h deleted file mode 100644 index 8fb09627..00000000 --- a/src/json.h +++ /dev/null @@ -1,576 +0,0 @@ -/*! -@file -@copyright The code is licensed under the MIT License - <http://opensource.org/licenses/MIT>, - Copyright (c) 2013-2015 Niels Lohmann. - -@author Niels Lohmann <http://nlohmann.me> - -@see https://github.com/nlohmann/json -*/ - -#pragma once - -#include <initializer_list> // std::initializer_list -#include <iostream> // std::istream, std::ostream -#include <map> // std::map -#include <string> // std::string -#include <vector> // std::vector -#include <iterator> // std::iterator -#include <limits> // std::numeric_limits -#include <functional> // std::hash - -namespace nlohmann -{ - -/*! -@brief JSON for Modern C++ - -The size of a JSON object is 16 bytes: 8 bytes for the value union whose -largest item is a pointer type and another 8 byte for an element of the -type union. The latter only needs 1 byte - the remaining 7 bytes are wasted -due to alignment. - -@see http://stackoverflow.com/questions/7758580/writing-your-own-stl-container/7759622#7759622 - -@bug Numbers are currently handled too generously. There are several formats - that are forbidden by the standard, but are accepted by the parser. - -@todo Implement json::insert(), json::emplace(), json::emplace_back, json::erase -*/ -class json -{ - public: - // forward declaration to friend this class - class iterator; - class const_iterator; - - public: - // container types - /// the type of elements in a JSON class - using value_type = json; - /// the type of element references - using reference = json&; - /// the type of const element references - using const_reference = const json&; - /// the type of pointers to elements - using pointer = json*; - /// the type of const pointers to elements - using const_pointer = const json*; - /// a type to represent differences between iterators - using difference_type = std::ptrdiff_t; - /// a type to represent container sizes - using size_type = std::size_t; - /// an iterator for a JSON container - using iterator = json::iterator; - /// a const iterator for a JSON container - using const_iterator = json::const_iterator; - /// a reverse iterator for a JSON container - using reverse_iterator = std::reverse_iterator<iterator>; - /// a const reverse iterator for a JSON container - using const_reverse_iterator = std::reverse_iterator<const_iterator>; - - /// a type for an object - using object_t = std::map<std::string, json>; - /// a type for an array - using array_t = std::vector<json>; - /// a type for a string - using string_t = std::string; - /// a type for a Boolean - using boolean_t = bool; - /// a type for an integer number - using number_t = int64_t; - /// a type for a floating point number - using number_float_t = double; - /// a type for list initialization - using list_init_t = std::initializer_list<json>; - - /// a JSON value - union value - { - /// array as pointer to array_t - array_t* array; - /// object as pointer to object_t - object_t* object; - /// string as pointer to string_t - string_t* string; - /// Boolean - boolean_t boolean; - /// number (integer) - number_t number; - /// number (float) - number_float_t number_float; - - /// default constructor - value() = default; - /// constructor for arrays - value(array_t*); - /// constructor for objects - value(object_t*); - /// constructor for strings - value(string_t*); - /// constructor for Booleans - value(boolean_t); - /// constructor for numbers (integer) - value(number_t); - /// constructor for numbers (float) - value(number_float_t); - }; - - /// possible types of a JSON object - enum class value_t : uint8_t - { - /// ordered collection of values - array = 0, - /// unordered set of name/value pairs - object, - /// null value - null, - /// string value - string, - /// Boolean value - boolean, - /// number value (integer) - number, - /// number value (float) - number_float - }; - - public: - /// create an object according to given type - json(const value_t); - /// create a null object - json() noexcept; - /// create a null object - json(std::nullptr_t) noexcept; - /// create a string object from a C++ string - json(const std::string&); - /// create a string object from a C++ string (move) - json(std::string&&); - /// create a string object from a C string - json(const char*); - /// create a Boolean object - json(const bool) noexcept; - /// create an array - json(const array_t&); - /// create an array (move) - json(array_t&&); - /// create an object - json(const object_t&); - /// create an object (move) - json(object_t&&); - /// create from an initializer list (to an array or object) - json(list_init_t, bool = true, value_t = value_t::array); - - /*! - @brief create a number object (integer) - @param n an integer number to wrap in a JSON object - */ - template<typename T, typename - std::enable_if< - std::numeric_limits<T>::is_integer, T>::type - = 0> - json(const T n) noexcept - : final_type_(0), type_(value_t::number), - value_(static_cast<number_t>(n)) - {} - - /*! - @brief create a number object (float) - @param n a floating point number to wrap in a JSON object - */ - template<typename T, typename = typename - std::enable_if< - std::is_floating_point<T>::value>::type - > - json(const T n) noexcept - : final_type_(0), type_(value_t::number_float), - value_(static_cast<number_float_t>(n)) - {} - - /*! - @brief create an array object - @param v any type of container whose elements can be use to construct - JSON objects (e.g., std::vector, std::set, std::array) - @note For some reason, we need to explicitly forbid JSON iterator types. - */ - template <class V, typename - std::enable_if< - not std::is_same<V, json::iterator>::value and - not std::is_same<V, json::const_iterator>::value and - not std::is_same<V, json::reverse_iterator>::value and - not std::is_same<V, json::const_reverse_iterator>::value and - std::is_constructible<json, typename V::value_type>::value, int>::type - = 0> - json(const V& v) : json(array_t(v.begin(), v.end())) - {} - - /*! - @brief create a JSON object - @param v any type of associative container whose elements can be use to - construct JSON objects (e.g., std::map<std::string, *>) - */ - template <class V, typename - std::enable_if< - std::is_constructible<std::string, typename V::key_type>::value and - std::is_constructible<json, typename V::mapped_type>::value, int>::type - = 0> - json(const V& v) : json(object_t(v.begin(), v.end())) - {} - - /// copy constructor - json(const json&); - /// move constructor - json(json&&) noexcept; - - /// copy assignment - json& operator=(json) noexcept; - - /// destructor - ~json() noexcept; - - /// explicit keyword to force array creation - static json array(list_init_t = list_init_t()); - /// explicit keyword to force object creation - static json object(list_init_t = list_init_t()); - - /// create from string representation - static json parse(const std::string&); - /// create from string representation - static json parse(const char*); - - private: - /// return the type as string - std::string type_name() const noexcept; - - /// dump the object (with pretty printer) - std::string dump(const bool, const unsigned int, unsigned int = 0) const noexcept; - /// replaced a character in a string with another string - void replaceChar(std::string& str, char c, const std::string& replacement) const; - /// escapes special characters to safely dump the string - std::string escapeString(const std::string&) const; - - public: - /// explicit value conversion - template<typename T> - T get() const; - - /// implicit conversion to string representation - operator std::string() const; - /// implicit conversion to integer (only for numbers) - operator int() const; - /// implicit conversion to integer (only for numbers) - operator int64_t() const; - /// implicit conversion to double (only for numbers) - operator double() const; - /// implicit conversion to Boolean (only for Booleans) - operator bool() const; - /// implicit conversion to JSON vector (not for objects) - operator array_t() const; - /// implicit conversion to JSON map (only for objects) - operator object_t() const; - - /// serialize to stream - friend std::ostream& operator<<(std::ostream& o, const json& j) - { - o << j.dump(); - return o; - } - /// serialize to stream - friend std::ostream& operator>>(const json& j, std::ostream& o) - { - o << j.dump(); - return o; - } - - /// deserialize from stream - friend std::istream& operator>>(std::istream& i, json& j) - { - j = parser(i).parse(); - return i; - } - /// deserialize from stream - friend std::istream& operator<<(json& j, std::istream& i) - { - j = parser(i).parse(); - return i; - } - - /// explicit serialization - std::string dump(int = -1) const noexcept; - - /// add constructible objects to an array - template<class T, typename std::enable_if<std::is_constructible<json, T>::value>::type = 0> - json & operator+=(const T& o) - { - push_back(json(o)); - return *this; - } - - /// add an object/array to an array - json& operator+=(const json&); - - /// add a pair to an object - json& operator+=(const object_t::value_type&); - /// add a list of elements to array or list of pairs to object - json& operator+=(list_init_t); - - /// add constructible objects to an array - template<class T, typename std::enable_if<std::is_constructible<json, T>::value>::type = 0> - void push_back(const T& o) - { - push_back(json(o)); - } - - /// add an object/array to an array - void push_back(const json&); - /// add an object/array to an array (move) - void push_back(json&&); - - /// add a pair to an object - void push_back(const object_t::value_type&); - /// add a list of elements to array or list of pairs to object - void push_back(list_init_t); - - /// operator to set an element in an array - reference operator[](const int); - /// operator to get an element in an array - const_reference operator[](const int) const; - /// operator to get an element in an array - reference at(const int); - /// operator to get an element in an array - const_reference at(const int) const; - - /// operator to set an element in an object - reference operator[](const std::string&); - /// operator to set an element in an object - reference operator[](const char*); - /// operator to get an element in an object - const_reference operator[](const std::string&) const; - /// operator to get an element in an object - const_reference operator[](const char*) const; - /// operator to set an element in an object - reference at(const std::string&); - /// operator to set an element in an object - reference at(const char*); - /// operator to get an element in an object - const_reference at(const std::string&) const; - /// operator to get an element in an object - const_reference at(const char*) const; - - /// return the number of stored values - size_type size() const noexcept; - /// return the maximal number of values that can be stored - size_type max_size() const noexcept; - /// checks whether object is empty - bool empty() const noexcept; - /// removes all elements from compounds and resets values to default - void clear() noexcept; - - /// swaps content with other object - void swap(json&) noexcept; - - /// return the type of the object - value_t type() const noexcept; - - /// find an element in an object (returns end() iterator otherwise) - iterator find(const std::string&); - /// find an element in an object (returns end() iterator otherwise) - const_iterator find(const std::string&) const; - /// find an element in an object (returns end() iterator otherwise) - iterator find(const char*); - /// find an element in an object (returns end() iterator otherwise) - const_iterator find(const char*) const; - - /// lexicographically compares the values - bool operator==(const json&) const noexcept; - /// lexicographically compares the values - bool operator!=(const json&) const noexcept; - - /// returns an iterator to the beginning (array/object) - iterator begin() noexcept; - /// returns an iterator to the end (array/object) - iterator end() noexcept; - /// returns an iterator to the beginning (array/object) - const_iterator begin() const noexcept; - /// returns an iterator to the end (array/object) - const_iterator end() const noexcept; - /// returns an iterator to the beginning (array/object) - const_iterator cbegin() const noexcept; - /// returns an iterator to the end (array/object) - const_iterator cend() const noexcept; - /// returns a reverse iterator to the beginning - reverse_iterator rbegin() noexcept; - /// returns a reverse iterator to the end - reverse_iterator rend() noexcept; - /// returns a reverse iterator to the beginning - const_reverse_iterator crbegin() const noexcept; - /// returns a reverse iterator to the end - const_reverse_iterator crend() const noexcept; - - private: - /// whether the type is final - unsigned final_type_ : 1; - /// the type of this object - value_t type_ = value_t::null; - /// the payload - value value_ {}; - - public: - /// an iterator - class iterator : public std::iterator<std::bidirectional_iterator_tag, json> - { - friend class json; - friend class json::const_iterator; - - public: - iterator() = default; - iterator(json*, bool); - iterator(const iterator&); - ~iterator(); - - iterator& operator=(iterator); - bool operator==(const iterator&) const; - bool operator!=(const iterator&) const; - iterator& operator++(); - iterator& operator--(); - json& operator*() const; - json* operator->() const; - - /// getter for the key (in case of objects) - std::string key() const; - /// getter for the value - json& value() const; - - private: - /// a JSON value - json* object_ = nullptr; - /// an iterator for JSON arrays - array_t::iterator* vi_ = nullptr; - /// an iterator for JSON objects - object_t::iterator* oi_ = nullptr; - /// whether iterator points to a valid object - bool invalid = true; - }; - - /// a const iterator - class const_iterator : public std::iterator<std::bidirectional_iterator_tag, const json> - { - friend class json; - - public: - const_iterator() = default; - const_iterator(const json*, bool); - const_iterator(const const_iterator&); - const_iterator(const json::iterator&); - ~const_iterator(); - - const_iterator& operator=(const_iterator); - bool operator==(const const_iterator&) const; - bool operator!=(const const_iterator&) const; - const_iterator& operator++(); - const_iterator& operator--(); - const json& operator*() const; - const json* operator->() const; - - /// getter for the key (in case of objects) - std::string key() const; - /// getter for the value - const json& value() const; - - private: - /// a JSON value - const json* object_ = nullptr; - /// an iterator for JSON arrays - array_t::const_iterator* vi_ = nullptr; - /// an iterator for JSON objects - object_t::const_iterator* oi_ = nullptr; - /// whether iterator reached past the end - bool invalid = true; - }; - - private: - /// a helper class to parse a JSON object - class parser - { - public: - /// a parser reading from a C string - parser(const char*); - /// a parser reading from a C++ string - parser(const std::string&); - /// a parser reading from an input stream - parser(std::istream&); - /// destructor of the parser - ~parser() = default; - - // no copy constructor - parser(const parser&) = delete; - // no copy assignment - parser& operator=(parser) = delete; - - /// parse and return a JSON object - json parse(); - - private: - /// read the next character, stripping whitespace - bool next(); - /// raise an exception with an error message - [[noreturn]] inline void error(const std::string&) const; - /// parse a quoted string - inline std::string parseString(); - /// transforms a unicode codepoint to it's UTF-8 presentation - std::string codePointToUTF8(unsigned int codePoint) const; - /// parses 4 hex characters that represent a unicode code point - inline unsigned int parse4HexCodePoint(); - /// parses \uXXXX[\uXXXX] unicode escape characters - inline std::string parseUnicodeEscape(); - /// parse a Boolean "true" - inline void parseTrue(); - /// parse a Boolean "false" - inline void parseFalse(); - /// parse a null object - inline void parseNull(); - /// a helper function to expect a certain character - inline void expect(const char); - - private: - /// a buffer of the input - std::string buffer_ {}; - /// the current character - char current_ {}; - /// the position inside the input buffer - std::size_t pos_ = 0; - }; -}; - -} - -/// user-defined literal operator to create JSON objects from strings -nlohmann::json operator "" _json(const char*, std::size_t); - -// specialization of std::swap, and std::hash -namespace std -{ -template <> -/// swaps the values of two JSON objects -inline void swap(nlohmann::json& j1, - nlohmann::json& j2) noexcept( - is_nothrow_move_constructible<nlohmann::json>::value and - is_nothrow_move_assignable<nlohmann::json>::value - ) -{ - j1.swap(j2); -} - -template <> -/// hash value for JSON objects -struct hash<nlohmann::json> -{ - size_t operator()(const nlohmann::json& j) const - { - // a naive hashing via the string representation - return hash<std::string>()(j.dump()); - } -}; - -} diff --git a/src/json.hpp b/src/json.hpp new file mode 100644 index 00000000..2992ffa7 --- /dev/null +++ b/src/json.hpp @@ -0,0 +1,2285 @@ +#ifndef _NLOHMANN_JSON +#define _NLOHMANN_JSON + +#include <algorithm> +#include <cassert> +#include <functional> +#include <initializer_list> +#include <iostream> +#include <iterator> +#include <limits> +#include <map> +#include <memory> +#include <string> +#include <type_traits> +#include <utility> +#include <vector> + +/*! +- ObjectType trick from http://stackoverflow.com/a/9860911 +*/ +/* +template<typename C, typename=void> +struct container_resizable : std::false_type {}; +template<typename C> +struct container_resizable<C, decltype(C().resize(0))> : std::true_type {}; +*/ + +/*! +@see https://github.com/nlohmann +*/ +namespace nlohmann +{ + +/*! +@brief JSON + +@tparam ObjectType type for JSON objects + (@c std::map by default) +@tparam ArrayType type for JSON arrays + (@c std::vector by default) +@tparam StringType type for JSON strings and object keys + (@c std::string by default) +@tparam BooleanType type for JSON booleans + (@c bool by default) +@tparam NumberIntegerType type for JSON integer numbers + (@c int64_t by default) +@tparam NumberFloatType type for JSON floating point numbers + (@c double by default) +*/ +template < + template<typename U, typename V, typename... Args> class ObjectType = std::map, + //template<typename... Args> class ArrayType = std::vector, + template<typename U, typename... Args> class ArrayType = std::vector, + class StringType = std::string, + class BooleanType = bool, + class NumberIntegerType = int64_t, + class NumberFloatType = double + > +class basic_json +{ + public: + ///////////////////// + // container types // + ///////////////////// + + class iterator; + class const_iterator; + + /// the type of elements in a basic_json container + using value_type = basic_json; + /// the type of an element reference + using reference = basic_json&; + /// the type of an element const reference + using const_reference = const basic_json&; + /// the type of an element pointer + using pointer = basic_json*; + /// the type of an element const pointer + using const_pointer = const basic_json*; + /// a type to represent differences between iterators + using difference_type = std::ptrdiff_t; + /// a type to represent container sizes + using size_type = std::size_t; + /// an iterator for a basic_json container + using iterator = basic_json::iterator; + /// a const iterator for a basic_json container + using const_iterator = basic_json::const_iterator; + + + /////////////////////////// + // JSON value data types // + /////////////////////////// + + /// a type for an object + using object_t = ObjectType<StringType, basic_json>; + /// a type for an array + using array_t = ArrayType<basic_json>; + /// a type for a string + using string_t = StringType; + /// a type for a boolean + using boolean_t = BooleanType; + /// a type for a number (integer) + using number_integer_t = NumberIntegerType; + /// a type for a number (floating point) + using number_float_t = NumberFloatType; + /// a type for list initialization + using list_init_t = std::initializer_list<basic_json>; + + + //////////////////////// + // JSON value storage // + //////////////////////// + + /// a JSON value + union json_value + { + /// object (stored with pointer to save storage) + object_t* object; + /// array (stored with pointer to save storage) + array_t* array; + /// string (stored with pointer to save storage) + string_t* string; + /// bolean + boolean_t boolean; + /// number (integer) + number_integer_t number_integer; + /// number (floating point) + number_float_t number_float; + + /// default constructor (for null values) + json_value() = default; + /// constructor for objects + json_value(object_t* v) : object(v) {} + /// constructor for arrays + json_value(array_t* v) : array(v) {} + /// constructor for strings + json_value(string_t* v) : string(v) {} + /// constructor for booleans + json_value(boolean_t v) : boolean(v) {} + /// constructor for numbers (integer) + json_value(number_integer_t v) : number_integer(v) {} + /// constructor for numbers (floating point) + json_value(number_float_t v) : number_float(v) {} + }; + + + ///////////////////////////////// + // JSON value type enumeration // + ///////////////////////////////// + + /// JSON value type enumeration + enum class value_t : uint8_t + { + /// null value + null, + /// object (unordered set of name/value pairs) + object, + /// array (ordered collection of values) + array, + /// string value + string, + /// boolean value + boolean, + /// number value (integer) + number_integer, + /// number value (floating point) + number_float + }; + + + ////////////////// + // constructors // + ////////////////// + + /// create an empty value with a given type + inline basic_json(const value_t value) + : m_type(value) + { + switch (m_type) + { + case (value_t::null): + { + break; + } + + case (value_t::object): + { + m_value.object = new object_t(); + break; + } + + case (value_t::array): + { + m_value.array = new array_t(); + break; + } + + case (value_t::string): + { + m_value.string = new string_t(); + break; + } + + case (value_t::boolean): + { + m_value.boolean = boolean_t(); + break; + } + + case (value_t::number_integer): + { + m_value.number_integer = number_integer_t(); + break; + } + + case (value_t::number_float): + { + m_value.number_float = number_float_t(); + break; + } + } + } + + /// create a null object (implicitly) + inline basic_json() noexcept + : m_type(value_t::null) + {} + + /// create a null object (explicitly) + inline basic_json(std::nullptr_t) noexcept + : m_type(value_t::null) + {} + + /// create an object (explicit) + inline basic_json(const object_t& value) + : m_type(value_t::object), m_value(new object_t(value)) + {} + + /// create an object (implicit) + template <class V, typename + std::enable_if< + std::is_constructible<string_t, typename V::key_type>::value and + std::is_constructible<basic_json, typename V::mapped_type>::value, int>::type + = 0> + inline basic_json(const V& value) + : m_type(value_t::object), m_value(new object_t(value.begin(), value.end())) + {} + + /// create an array (explicit) + inline basic_json(const array_t& value) + : m_type(value_t::array), m_value(new array_t(value)) + {} + + /// create an array (implicit) + template <class V, typename + std::enable_if< + not std::is_same<V, basic_json::iterator>::value and + not std::is_same<V, basic_json::const_iterator>::value and + std::is_constructible<basic_json, typename V::value_type>::value, int>::type + = 0> + inline basic_json(const V& value) + : m_type(value_t::array), m_value(new array_t(value.begin(), value.end())) + {} + + /// create a string (explicit) + inline basic_json(const string_t& value) + : m_type(value_t::string), m_value(new string_t(value)) + {} + + /// create a string (explicit) + inline basic_json(const typename string_t::value_type* value) + : m_type(value_t::string), m_value(new string_t(value)) + {} + + /// create a string (implicit) + template <class V, typename + std::enable_if< + std::is_constructible<string_t, V>::value, int>::type + = 0> + inline basic_json(const V& value) + : basic_json(string_t(value)) + {} + + /// create a boolean (explicit) + inline basic_json(boolean_t value) + : m_type(value_t::boolean), m_value(value) + {} + + /// create an integer number (explicit) + inline basic_json(const number_integer_t& value) + : m_type(value_t::number_integer), m_value(value) + {} + + /// create an integer number (implicit) + template<typename T, typename + std::enable_if< + std::is_constructible<number_integer_t, T>::value and + std::numeric_limits<T>::is_integer, T>::type + = 0> + inline basic_json(const T value) noexcept + : m_type(value_t::number_integer), m_value(number_integer_t(value)) + {} + + /// create a floating point number (explicit) + inline basic_json(const number_float_t& value) + : m_type(value_t::number_float), m_value(value) + {} + + /// create a floating point number (implicit) + template<typename T, typename = typename + std::enable_if< + std::is_constructible<number_float_t, T>::value and + std::is_floating_point<T>::value>::type + > + inline basic_json(const T value) noexcept + : m_type(value_t::number_float), m_value(number_float_t(value)) + {} + + inline basic_json(list_init_t l, bool type_deduction = true, value_t manual_type = value_t::array) + { + // the initializer list could describe an object + bool is_object = true; + + // check if each element is an array with two elements whose first element + // is a string + for (const auto& element : l) + { + if ((element.m_final and element.m_type == value_t::array) + or (element.m_type != value_t::array or element.size() != 2 + or element[0].m_type != value_t::string)) + { + // we found an element that makes it impossible to use the + // initializer list as object + is_object = false; + break; + } + } + + // adjust type if type deduction is not wanted + if (not type_deduction) + { + // mark this object's type as final + m_final = true; + + // if array is wanted, do not create an object though possible + if (manual_type == value_t::array) + { + is_object = false; + } + + // if object is wanted but impossible, throw an exception + if (manual_type == value_t::object and not is_object) + { + throw std::logic_error("cannot create JSON object"); + } + } + + if (is_object) + { + // the initializer list is a list of pairs -> create object + m_type = value_t::object; + m_value = new object_t(); + for (auto& element : l) + { + m_value.object->emplace(std::move(*(element[0].m_value.string)), std::move(element[1])); + } + } + else + { + // the initializer list describes an array -> create array + m_type = value_t::array; + m_value = new array_t(std::move(l)); + } + } + + inline static basic_json array(list_init_t l = list_init_t()) + { + return basic_json(l, false, value_t::array); + } + + inline static basic_json object(list_init_t l = list_init_t()) + { + // if more than one element is in the initializer list, wrap it + if (l.size() > 1) + { + return basic_json({l}, false, value_t::object); + } + else + { + return basic_json(l, false, value_t::object); + } + } + + /////////////////////////////////////// + // other constructors and destructor // + /////////////////////////////////////// + + /// copy constructor + inline basic_json(const basic_json& other) + : m_type(other.m_type) + { + switch (m_type) + { + case (value_t::null): + { + break; + } + case (value_t::object): + { + m_value.object = new object_t(*other.m_value.object); + break; + } + case (value_t::array): + { + m_value.array = new array_t(*other.m_value.array); + break; + } + case (value_t::string): + { + m_value.string = new string_t(*other.m_value.string); + break; + } + case (value_t::boolean): + { + m_value.boolean = other.m_value.boolean; + break; + } + case (value_t::number_integer): + { + m_value.number_integer = other.m_value.number_integer; + break; + } + case (value_t::number_float): + { + m_value.number_float = other.m_value.number_float; + break; + } + } + } + + /// move constructor + inline basic_json(basic_json&& other) noexcept + : m_type(std::move(other.m_type)), + m_value(std::move(other.m_value)) + { + // invaludate payload + other.m_type = value_t::null; + other.m_value = {}; + } + + /// copy assignment + inline reference operator=(basic_json other) noexcept + { + std::swap(m_type, other.m_type); + std::swap(m_value, other.m_value); + return *this; + } + + /// destructor + inline ~basic_json() noexcept + { + switch (m_type) + { + case (value_t::object): + { + delete m_value.object; + m_value.object = nullptr; + break; + } + case (value_t::array): + { + delete m_value.array; + m_value.array = nullptr; + break; + } + case (value_t::string): + { + delete m_value.string; + m_value.string = nullptr; + break; + } + case (value_t::null): + case (value_t::boolean): + case (value_t::number_integer): + case (value_t::number_float): + { + break; + } + } + } + + + public: + /////////////////////// + // object inspection // + /////////////////////// + + /*! + Serialization function for JSON objects. The function tries to mimick Python's + @p json.dumps() function, and currently supports its @p indent parameter. + + @param indent if indent is nonnegative, then array elements and object members + will be pretty-printed with that indent level. An indent level + of 0 will only insert newlines. -1 (the default) selects the + most compact representation + + @see https://docs.python.org/2/library/json.html#json.dump + */ + inline string_t dump(int indent = -1) const noexcept + { + if (indent >= 0) + { + return dump(true, static_cast<unsigned int>(indent)); + } + else + { + return dump(false, 0); + } + } + + /// return the type of the object explicitly + inline value_t type() const noexcept + { + return m_type; + } + + /// return the type of the object implicitly + operator value_t() const noexcept + { + return m_type; + } + + + ////////////////////// + // value conversion // + ////////////////////// + + /// get an object + template <class T, typename + std::enable_if< + std::is_constructible<string_t, typename T::key_type>::value and + std::is_constructible<basic_json, typename T::mapped_type>::value, int>::type + = 0> + inline T get() const + { + switch (m_type) + { + case (value_t::object): + return T(m_value.object->begin(), m_value.object->end()); + default: + throw std::logic_error("cannot cast " + type_name() + " to " + typeid(T).name()); + } + } + + /// get an array + template <class T, typename + std::enable_if< + not std::is_same<T, string_t>::value and + std::is_constructible<basic_json, typename T::value_type>::value, int>::type + = 0> + inline T get() const + { + switch (m_type) + { + case (value_t::array): + return T(m_value.array->begin(), m_value.array->end()); + default: + throw std::logic_error("cannot cast " + type_name() + " to " + typeid(T).name()); + } + } + + /// get a string + template <typename T, typename + std::enable_if< + std::is_constructible<T, string_t>::value, int>::type + = 0> + inline T get() const + { + switch (m_type) + { + case (value_t::string): + return *m_value.string; + default: + throw std::logic_error("cannot cast " + type_name() + " to " + typeid(T).name()); + } + } + + /// get a boolean + template <typename T, typename + std::enable_if< + std::is_same<boolean_t, T>::value, int>::type + = 0> + inline T get() const + { + switch (m_type) + { + case (value_t::boolean): + return m_value.boolean; + default: + throw std::logic_error("cannot cast " + type_name() + " to " + typeid(T).name()); + } + } + + /// explicitly get a number + template<typename T, typename + std::enable_if< + not std::is_same<boolean_t, T>::value and + std::is_arithmetic<T>::value, int>::type + = 0> + inline T get() const + { + switch (m_type) + { + case (value_t::number_integer): + return static_cast<T>(m_value.number_integer); + case (value_t::number_float): + return static_cast<T>(m_value.number_float); + default: + throw std::logic_error("cannot cast " + type_name() + " to " + typeid(T).name()); + } + } + + /// explicitly get a value + template<typename T> + inline operator T() const + { + return get<T>(); + } + + + //////////////////// + // element access // + //////////////////// + + /// access specified element with bounds checking + inline reference at(size_type pos) + { + // at only works for arrays + if (m_type != value_t::array) + { + throw std::runtime_error("cannot use at with " + type_name()); + } + + return m_value.array->at(pos); + } + + /// access specified element with bounds checking + inline const_reference at(size_type pos) const + { + // at only works for arrays + if (m_type != value_t::array) + { + throw std::runtime_error("cannot use at with " + type_name()); + } + + return m_value.array->at(pos); + } + + /// access specified element + inline reference operator[](size_type pos) + { + // at only works for arrays + if (m_type != value_t::array) + { + throw std::runtime_error("cannot use [] with " + type_name()); + } + + return m_value.array->operator[](pos); + } + + /// access specified element + inline const_reference operator[](size_type pos) const + { + // at only works for arrays + if (m_type != value_t::array) + { + throw std::runtime_error("cannot use [] with " + type_name()); + } + + return m_value.array->operator[](pos); + } + + /// access specified element with bounds checking + inline reference at(const typename object_t::key_type& key) + { + // at only works for objects + if (m_type != value_t::object) + { + throw std::runtime_error("cannot use at with " + type_name()); + } + + return m_value.object->at(key); + } + + /// access specified element with bounds checking + inline const_reference at(const typename object_t::key_type& key) const + { + // at only works for objects + if (m_type != value_t::object) + { + throw std::runtime_error("cannot use at with " + type_name()); + } + + return m_value.object->at(key); + } + + /// access specified element + inline reference operator[](const typename object_t::key_type& key) + { + // at only works for objects + if (m_type != value_t::object) + { + throw std::runtime_error("cannot use [] with " + type_name()); + } + + return m_value.object->operator[](key); + } + + /// access specified element (needed for clang) + template<typename T, size_t n> + inline reference operator[](const T (&key)[n]) + { + // at only works for objects + if (m_type != value_t::object) + { + throw std::runtime_error("cannot use [] with " + type_name()); + } + + return m_value.object->operator[](key); + } + + /// access specified element + inline reference operator[](typename object_t::key_type&& key) + { + // at only works for objects + if (m_type != value_t::object) + { + throw std::runtime_error("cannot use [] with " + type_name()); + } + + return m_value.object->operator[](std::move(key)); + } + + + /////////////// + // iterators // + /////////////// + + /// returns an iterator to the beginning of the container + inline iterator begin() noexcept + { + iterator result(this); + result.set_begin(); + return result; + } + + /// returns a const iterator to the beginning of the container + inline const_iterator begin() const noexcept + { + const_iterator result(this); + result.set_begin(); + return result; + } + + /// returns a const iterator to the beginning of the container + inline const_iterator cbegin() const noexcept + { + const_iterator result(this); + result.set_begin(); + return result; + } + + /// returns an iterator to the end of the container + inline iterator end() noexcept + { + iterator result(this); + result.set_end(); + return result; + } + + /// returns a const iterator to the end of the container + inline const_iterator end() const noexcept + { + const_iterator result(this); + result.set_end(); + return result; + } + + /// returns a const iterator to the end of the container + inline const_iterator cend() const noexcept + { + const_iterator result(this); + result.set_end(); + return result; + } + + + ////////////// + // capacity // + ////////////// + + /// checks whether the container is empty + inline bool empty() const noexcept + { + switch (m_type) + { + case (value_t::null): + { + return true; + } + case (value_t::number_integer): + case (value_t::number_float): + case (value_t::boolean): + case (value_t::string): + { + return false; + } + case (value_t::array): + { + return m_value.array->empty(); + } + case (value_t::object): + { + return m_value.object->empty(); + } + } + } + + /// returns the number of elements + inline size_type size() const noexcept + { + switch (m_type) + { + case (value_t::null): + { + return 0; + } + case (value_t::number_integer): + case (value_t::number_float): + case (value_t::boolean): + case (value_t::string): + { + return 1; + } + case (value_t::array): + { + return m_value.array->size(); + } + case (value_t::object): + { + return m_value.object->size(); + } + } + } + + /// returns the maximum possible number of elements + inline size_type max_size() const noexcept + { + switch (m_type) + { + case (value_t::null): + { + return 0; + } + case (value_t::number_integer): + case (value_t::number_float): + case (value_t::boolean): + case (value_t::string): + { + return 1; + } + case (value_t::array): + { + return m_value.array->max_size(); + } + case (value_t::object): + { + return m_value.object->max_size(); + } + } + } + + + /////////////// + // modifiers // + /////////////// + + /// clears the contents + inline void clear() noexcept + { + switch (m_type) + { + case (value_t::null): + { + break; + } + case (value_t::number_integer): + { + m_value.number_integer = {}; + break; + } + case (value_t::number_float): + { + m_value.number_float = {}; + break; + } + case (value_t::boolean): + { + m_value.boolean = {}; + break; + } + case (value_t::string): + { + m_value.string->clear(); + break; + } + case (value_t::array): + { + m_value.array->clear(); + break; + } + case (value_t::object): + { + m_value.object->clear(); + break; + } + } + } + + /// add an object to an array + inline void push_back(basic_json&& value) + { + // push_back only works for null objects or arrays + if (not(m_type == value_t::null or m_type == value_t::array)) + { + throw std::runtime_error("cannot add element to " + type_name()); + } + + // transform null object into an array + if (m_type == value_t::null) + { + m_type = value_t::array; + m_value.array = new array_t; + } + + // add element to array (move semantics) + m_value.array->push_back(std::move(value)); + // invalidate object + value.m_type = value_t::null; + } + + /// add an object to an array + inline void push_back(const basic_json& value) + { + // push_back only works for null objects or arrays + if (not(m_type == value_t::null or m_type == value_t::array)) + { + throw std::runtime_error("cannot add element to " + type_name()); + } + + // transform null object into an array + if (m_type == value_t::null) + { + m_type = value_t::array; + m_value.array = new array_t; + } + + // add element to array + m_value.array->push_back(value); + } + + /* + /// add an object to an array + inline reference operator+=(const basic_json& value) + { + push_back(value); + return *this; + } + */ + + /// add constructible objects to an array + template<class T, typename std::enable_if<std::is_constructible<basic_json, T>::value>::type = 0> + inline void push_back(const T& value) + { + assert(false); // not sure if function will ever be called + push_back(basic_json(value)); + } + + /* + /// add constructible objects to an array + template<class T, typename std::enable_if<std::is_constructible<basic_json, T>::value>::type = 0> + inline reference operator+=(const T& value) + { + push_back(basic_json(value)); + return *this; + } + */ + + /// add an object to an object + inline void push_back(const typename object_t::value_type& value) + { + // push_back only works for null objects or objects + if (not(m_type == value_t::null or m_type == value_t::object)) + { + throw std::runtime_error("cannot add element to " + type_name()); + } + + // transform null object into an object + if (m_type == value_t::null) + { + m_type = value_t::object; + m_value.object = new object_t; + } + + // add element to array + m_value.object->insert(value); + } + + /* + /// add an object to an object + inline reference operator+=(const typename object_t::value_type& value) + { + push_back(value); + return operator[](value.first); + } + */ + + /// constructs element in-place at the end of an array + template <typename T, typename + std::enable_if< + std::is_constructible<basic_json, T>::value, int>::type + = 0> + inline void emplace_back(T && arg) + { + // push_back only works for null objects or arrays + if (not(m_type == value_t::null or m_type == value_t::array)) + { + throw std::runtime_error("cannot add element to " + type_name()); + } + + // transform null object into an array + if (m_type == value_t::null) + { + m_type = value_t::array; + m_value.array = new array_t; + } + + // add element to array + m_value.array->emplace_back(std::forward<T>(arg)); + } + + /// swaps the contents + inline void swap(reference other) noexcept + { + std::swap(m_type, other.m_type); + std::swap(m_value, other.m_value); + } + + /// swaps the contents + inline void swap(array_t& other) + { + // swap only works for arrays + if (m_type != value_t::array) + { + throw std::runtime_error("cannot use swap with " + type_name()); + } + + // swap arrays + std::swap(*(m_value.array), other); + } + + /// swaps the contents + inline void swap(object_t& other) + { + // swap only works for objects + if (m_type != value_t::object) + { + throw std::runtime_error("cannot use swap with " + type_name()); + } + + // swap arrays + std::swap(*(m_value.object), other); + } + + /// swaps the contents + inline void swap(string_t& other) + { + // swap only works for strings + if (m_type != value_t::string) + { + throw std::runtime_error("cannot use swap with " + type_name()); + } + + // swap arrays + std::swap(*(m_value.string), other); + } + + + ////////////////////////////////////////// + // lexicographical comparison operators // + ////////////////////////////////////////// + + /// comparison: equal + friend bool operator==(const_reference lhs, const_reference rhs) + { + switch (lhs.type()) + { + case (value_t::array): + { + if (rhs.type() == value_t::array) + { + return *lhs.m_value.array == *rhs.m_value.array; + } + break; + } + case (value_t::object): + { + if (rhs.type() == value_t::object) + { + return *lhs.m_value.object == *rhs.m_value.object; + } + break; + } + case (value_t::null): + { + if (rhs.type() == value_t::null) + { + return true; + } + break; + } + case (value_t::string): + { + if (rhs.type() == value_t::string) + { + return *lhs.m_value.string == *rhs.m_value.string; + } + break; + } + case (value_t::boolean): + { + if (rhs.type() == value_t::boolean) + { + return lhs.m_value.boolean == rhs.m_value.boolean; + } + break; + } + case (value_t::number_integer): + { + if (rhs.type() == value_t::number_integer) + { + return lhs.m_value.number_integer == rhs.m_value.number_integer; + } + if (rhs.type() == value_t::number_float) + { + return lhs.m_value.number_integer == static_cast<number_integer_t>(rhs.m_value.number_float); + } + break; + } + case (value_t::number_float): + { + if (rhs.type() == value_t::number_integer) + { + return lhs.m_value.number_float == static_cast<number_float_t>(rhs.m_value.number_integer); + } + if (rhs.type() == value_t::number_float) + { + return lhs.m_value.number_float == rhs.m_value.number_float; + } + break; + } + } + + return false; + } + + /// comparison: not equal + friend bool operator!=(const_reference lhs, const_reference rhs) + { + return not (lhs == rhs); + } + + /// comparison: less than + friend bool operator<(const_reference lhs, const_reference rhs) + { + switch (lhs.type()) + { + case (value_t::array): + { + if (rhs.type() == value_t::array) + { + return *lhs.m_value.array < *rhs.m_value.array; + } + break; + } + case (value_t::object): + { + if (rhs.type() == value_t::object) + { + return *lhs.m_value.object < *rhs.m_value.object; + } + break; + } + case (value_t::null): + { + if (rhs.type() == value_t::null) + { + return false; + } + break; + } + case (value_t::string): + { + if (rhs.type() == value_t::string) + { + return *lhs.m_value.string < *rhs.m_value.string; + } + break; + } + case (value_t::boolean): + { + if (rhs.type() == value_t::boolean) + { + return lhs.m_value.boolean < rhs.m_value.boolean; + } + break; + } + case (value_t::number_integer): + { + if (rhs.type() == value_t::number_integer) + { + return lhs.m_value.number_integer < rhs.m_value.number_integer; + } + if (rhs.type() == value_t::number_float) + { + return lhs.m_value.number_integer < static_cast<number_integer_t>(rhs.m_value.number_float); + } + break; + } + case (value_t::number_float): + { + if (rhs.type() == value_t::number_integer) + { + return lhs.m_value.number_float < static_cast<number_float_t>(rhs.m_value.number_integer); + } + if (rhs.type() == value_t::number_float) + { + return lhs.m_value.number_float < rhs.m_value.number_float; + } + break; + } + } + + return false; + } + + /// comparison: less than or equal + friend bool operator<=(const_reference lhs, const_reference rhs) + { + return not (rhs < lhs); + } + + /// comparison: greater than + friend bool operator>(const_reference lhs, const_reference rhs) + { + return not (lhs <= rhs); + } + + /// comparison: greater than or equal + friend bool operator>=(const_reference lhs, const_reference rhs) + { + return not (lhs < rhs); + } + + + /////////////////// + // serialization // + /////////////////// + + /// serialize to stream + friend std::ostream& operator<<(std::ostream& o, const basic_json& j) + { + o << j.dump(); + return o; + } + + /// serialize to stream + friend std::ostream& operator>>(const basic_json& j, std::ostream& o) + { + o << j.dump(); + return o; + } + + + private: + /////////////////////////// + // convenience functions // + /////////////////////////// + + /// return the type as string + inline string_t type_name() const noexcept + { + switch (m_type) + { + case (value_t::null): + { + return "null"; + } + case (value_t::object): + { + return "object"; + } + case (value_t::array): + { + return "array"; + } + case (value_t::string): + { + return "string"; + } + case (value_t::boolean): + { + return "boolean"; + } + case (value_t::number_integer): + case (value_t::number_float): + { + return "number"; + } + } + } + + /*! + Internal implementation of the serialization function. + + @param prettyPrint whether the output shall be pretty-printed + @param indentStep the indent level + @param currentIndent the current indent level (only used internally) + */ + inline string_t dump(const bool prettyPrint, const unsigned int indentStep, + unsigned int currentIndent = 0) const noexcept + { + // helper function to return whitespace as indentation + const auto indent = [prettyPrint, ¤tIndent]() + { + return prettyPrint ? string_t(currentIndent, ' ') : string_t(); + }; + + switch (m_type) + { + case (value_t::null): + { + return "null"; + } + + case (value_t::object): + { + if (m_value.object->empty()) + { + return "{}"; + } + + string_t result = "{"; + + // increase indentation + if (prettyPrint) + { + currentIndent += indentStep; + result += "\n"; + } + + for (typename object_t::const_iterator i = m_value.object->begin(); i != m_value.object->end(); ++i) + { + if (i != m_value.object->begin()) + { + result += prettyPrint ? ",\n" : ","; + } + result += indent() + "\"" + i->first + "\":" + (prettyPrint ? " " : "") + + i->second.dump(prettyPrint, indentStep, currentIndent); + } + + // decrease indentation + if (prettyPrint) + { + currentIndent -= indentStep; + result += "\n"; + } + + return result + indent() + "}"; + } + + case (value_t::array): + { + if (m_value.array->empty()) + { + return "[]"; + } + + string_t result = "["; + + // increase indentation + if (prettyPrint) + { + currentIndent += indentStep; + result += "\n"; + } + + for (typename array_t::const_iterator i = m_value.array->begin(); i != m_value.array->end(); ++i) + { + if (i != m_value.array->begin()) + { + result += prettyPrint ? ",\n" : ","; + } + result += indent() + i->dump(prettyPrint, indentStep, currentIndent); + } + + // decrease indentation + if (prettyPrint) + { + currentIndent -= indentStep; + result += "\n"; + } + + return result + indent() + "]"; + } + + case (value_t::string): + { + return string_t("\"") + *m_value.string + "\""; + } + + case (value_t::boolean): + { + return m_value.boolean ? "true" : "false"; + } + + case (value_t::number_integer): + { + return std::to_string(m_value.number_integer); + } + + case (value_t::number_float): + { + return std::to_string(m_value.number_float); + } + } + } + + + private: + ////////////////////// + // member variables // + ////////////////////// + + /// the type of the current element + value_t m_type = value_t::null; + + /// whether the type of JSON object may change later + bool m_final = false; + + /// the value of the current element + json_value m_value = {}; + + public: + /////////////// + // iterators // + /////////////// + + /// a bidirectional iterator for the basic_json class + class iterator : public std::iterator<std::bidirectional_iterator_tag, basic_json> + { + public: + /// the type of the values when the iterator is dereferenced + using value_type = basic_json::value_type; + /// a type to represent differences between iterators + using difference_type = basic_json::difference_type; + /// defines a pointer to the type iterated over (value_type) + using pointer = basic_json::pointer; + /// defines a reference to the type iterated over (value_type) + using reference = basic_json::reference; + /// the category of the iterator + using iterator_category = std::bidirectional_iterator_tag; + + /// values of a generic iterator type of non-container JSON values + enum class generic_iterator_value + { + /// the iterator was not initialized + uninitialized, + /// the iterator points to the only value + begin, + /// the iterator points past the only value + end + }; + + /// an iterator value + union internal_iterator + { + /// iterator for JSON objects + typename object_t::iterator object_iterator; + /// iterator for JSON arrays + typename array_t::iterator array_iterator; + /// generic iteraotr for all other value types + generic_iterator_value generic_iterator; + + /// default constructor + internal_iterator() : generic_iterator(generic_iterator_value::uninitialized) {} + /// constructor for object iterators + internal_iterator(typename object_t::iterator v) : object_iterator(v) {} + /// constructor for array iterators + internal_iterator(typename array_t::iterator v) : array_iterator(v) {} + /// constructor for generic iterators + internal_iterator(generic_iterator_value v) : generic_iterator(v) {} + }; + + /// constructor for a given JSON instance + inline iterator(pointer object) : m_object(object) + { + if (m_object == nullptr) + { + throw std::logic_error("cannot create iterator"); + } + + switch (m_object->m_type) + { + case (basic_json::value_t::object): + { + m_it.object_iterator = typename object_t::iterator(); + break; + } + case (basic_json::value_t::array): + { + m_it.array_iterator = typename array_t::iterator(); + break; + } + default: + { + m_it.generic_iterator = generic_iterator_value::uninitialized; + break; + } + } + } + + /// copy assignment + inline iterator& operator=(const iterator& other) noexcept + { + m_object = other.m_object; + m_it = other.m_it; + return *this; + } + + /// set the iterator to the first value + inline void set_begin() noexcept + { + switch (m_object->m_type) + { + case (basic_json::value_t::object): + { + m_it.object_iterator = m_object->m_value.object->begin(); + break; + } + + case (basic_json::value_t::array): + { + m_it.array_iterator = m_object->m_value.array->begin(); + break; + } + + case (basic_json::value_t::null): + { + m_it.generic_iterator = generic_iterator_value::end; + break; + } + + default: + { + m_it.generic_iterator = generic_iterator_value::begin; + break; + } + } + } + + /// set the iterator past the last value + inline void set_end() noexcept + { + switch (m_object->m_type) + { + case (basic_json::value_t::object): + { + m_it.object_iterator = m_object->m_value.object->end(); + break; + } + + case (basic_json::value_t::array): + { + m_it.array_iterator = m_object->m_value.array->end(); + break; + } + + default: + { + m_it.generic_iterator = generic_iterator_value::end; + break; + } + } + } + + /// return a reference to the value pointed to by the iterator + inline reference operator*() const + { + if (m_object == nullptr) + { + throw std::out_of_range("cannot get value"); + } + + switch (m_object->m_type) + { + case (basic_json::value_t::object): + { + return m_it.object_iterator->second; + } + + case (basic_json::value_t::array): + { + return *m_it.array_iterator; + } + + case (basic_json::value_t::null): + { + throw std::out_of_range("cannot get value"); + } + + default: + { + if (m_it.generic_iterator == generic_iterator_value::begin) + { + return *m_object; + } + else + { + throw std::out_of_range("cannot get value"); + } + } + } + } + + /// dereference the iterator + inline pointer operator->() const + { + if (m_object == nullptr) + { + throw std::out_of_range("cannot get value"); + } + + switch (m_object->m_type) + { + case (basic_json::value_t::object): + { + return &(m_it.object_iterator->second); + } + + case (basic_json::value_t::array): + { + return &*m_it.array_iterator; + } + + default: + { + if (m_it.generic_iterator == generic_iterator_value::begin) + { + return m_object; + } + else + { + throw std::out_of_range("cannot get value"); + } + } + } + } + + /// post-increment (it++) + inline iterator operator++(int) + { + iterator result = *this; + + if (m_object == nullptr) + { + throw std::out_of_range("cannot increment iterator"); + } + + switch (m_object->m_type) + { + case (basic_json::value_t::object): + { + m_it.object_iterator++; + break; + } + + case (basic_json::value_t::array): + { + m_it.array_iterator++; + break; + } + + default: + { + m_it.generic_iterator = generic_iterator_value::end; + break; + } + } + + return result; + } + + /// pre-increment (++it) + inline iterator& operator++() + { + if (m_object == nullptr) + { + throw std::out_of_range("cannot increment iterator"); + } + + switch (m_object->m_type) + { + case (basic_json::value_t::object): + { + ++m_it.object_iterator; + break; + } + + case (basic_json::value_t::array): + { + ++m_it.array_iterator; + break; + } + + default: + { + m_it.generic_iterator = generic_iterator_value::end; + break; + } + } + + return *this; + } + + /// post-decrement (it--) + inline iterator operator--(int) + { + iterator result = *this; + + if (m_object == nullptr) + { + throw std::out_of_range("cannot decrement iterator"); + } + + switch (m_object->m_type) + { + case (basic_json::value_t::object): + { + m_it.object_iterator--; + break; + } + + case (basic_json::value_t::array): + { + m_it.array_iterator--; + break; + } + + default: + { + m_it.generic_iterator = generic_iterator_value::uninitialized; + break; + } + } + + return result; + } + + /// pre-decrement (--it) + inline iterator& operator--() + { + if (m_object == nullptr) + { + throw std::out_of_range("cannot decrement iterator"); + } + + switch (m_object->m_type) + { + case (basic_json::value_t::object): + { + --m_it.object_iterator; + break; + } + + case (basic_json::value_t::array): + { + --m_it.array_iterator; + break; + } + + default: + { + m_it.generic_iterator = generic_iterator_value::uninitialized; + break; + } + } + + return *this; + } + + /// comparison: equal + inline bool operator==(const iterator& other) const + { + if (m_object == nullptr + or other.m_object == nullptr + or m_object != other.m_object + or m_object->m_type != other.m_object->m_type) + { + return false; + } + + switch (m_object->m_type) + { + case (basic_json::value_t::object): + { + return (m_it.object_iterator == other.m_it.object_iterator); + } + + case (basic_json::value_t::array): + { + return (m_it.array_iterator == other.m_it.array_iterator); + } + + default: + { + return (m_it.generic_iterator == other.m_it.generic_iterator); + } + } + } + + /// comparison: not equal + inline bool operator!=(const iterator& other) const + { + return not operator==(other); + } + + private: + /// associated JSON instance + pointer m_object = nullptr; + /// the actual iterator of the associated instance + internal_iterator m_it; + }; + + /// a const bidirectional iterator for the basic_json class + class const_iterator : public std::iterator<std::bidirectional_iterator_tag, const basic_json> + { + public: + /// the type of the values when the iterator is dereferenced + using value_type = basic_json::value_type; + /// a type to represent differences between iterators + using difference_type = basic_json::difference_type; + /// defines a pointer to the type iterated over (value_type) + using pointer = basic_json::const_pointer; + /// defines a reference to the type iterated over (value_type) + using reference = basic_json::const_reference; + /// the category of the iterator + using iterator_category = std::bidirectional_iterator_tag; + + /// values of a generic iterator type of non-container JSON values + enum class generic_iterator_value + { + /// the iterator was not initialized + uninitialized, + /// the iterator points to the only value + begin, + /// the iterator points past the only value + end + }; + + /// an iterator value + union internal_const_iterator + { + /// iterator for JSON objects + typename object_t::const_iterator object_iterator; + /// iterator for JSON arrays + typename array_t::const_iterator array_iterator; + /// generic iteraotr for all other value types + generic_iterator_value generic_iterator; + + /// default constructor + internal_const_iterator() : generic_iterator(generic_iterator_value::uninitialized) {} + /// constructor for object iterators + internal_const_iterator(typename object_t::iterator v) : object_iterator(v) {} + /// constructor for array iterators + internal_const_iterator(typename array_t::iterator v) : array_iterator(v) {} + /// constructor for generic iterators + internal_const_iterator(generic_iterator_value v) : generic_iterator(v) {} + }; + + /// constructor for a given JSON instance + inline const_iterator(pointer object) : m_object(object) + { + if (m_object == nullptr) + { + throw std::logic_error("cannot create iterator"); + } + + switch (m_object->m_type) + { + case (basic_json::value_t::object): + { + m_it.object_iterator = typename object_t::const_iterator(); + break; + } + case (basic_json::value_t::array): + { + m_it.array_iterator = typename array_t::const_iterator(); + break; + } + default: + { + m_it.generic_iterator = generic_iterator_value::uninitialized; + break; + } + } + } + + inline const_iterator(const iterator& other) : m_object(other.m_object), m_it(other.m_it) + {} + + /// copy assignment + inline const_iterator operator=(const const_iterator& other) noexcept + { + m_object = other.m_object; + m_it = other.m_it; + return *this; + } + + /// set the iterator to the first value + inline void set_begin() noexcept + { + switch (m_object->m_type) + { + case (basic_json::value_t::object): + { + m_it.object_iterator = m_object->m_value.object->cbegin(); + break; + } + + case (basic_json::value_t::array): + { + m_it.array_iterator = m_object->m_value.array->cbegin(); + break; + } + + case (basic_json::value_t::null): + { + m_it.generic_iterator = generic_iterator_value::end; + break; + } + + default: + { + m_it.generic_iterator = generic_iterator_value::begin; + break; + } + } + } + + /// set the iterator past the last value + inline void set_end() noexcept + { + switch (m_object->m_type) + { + case (basic_json::value_t::object): + { + m_it.object_iterator = m_object->m_value.object->cend(); + break; + } + + case (basic_json::value_t::array): + { + m_it.array_iterator = m_object->m_value.array->cend(); + break; + } + + default: + { + m_it.generic_iterator = generic_iterator_value::end; + break; + } + } + } + + /// return a reference to the value pointed to by the iterator + inline reference operator*() const + { + if (m_object == nullptr) + { + throw std::out_of_range("cannot get value"); + } + + switch (m_object->m_type) + { + case (basic_json::value_t::object): + { + return m_it.object_iterator->second; + } + + case (basic_json::value_t::array): + { + return *m_it.array_iterator; + } + + case (basic_json::value_t::null): + { + throw std::out_of_range("cannot get value"); + } + + default: + { + if (m_it.generic_iterator == generic_iterator_value::begin) + { + return *m_object; + } + else + { + throw std::out_of_range("cannot get value"); + } + } + } + } + + /// dereference the iterator + inline pointer operator->() const + { + if (m_object == nullptr) + { + throw std::out_of_range("cannot get value"); + } + + switch (m_object->m_type) + { + case (basic_json::value_t::object): + { + return &(m_it.object_iterator->second); + } + + case (basic_json::value_t::array): + { + return &*m_it.array_iterator; + } + + default: + { + if (m_it.generic_iterator == generic_iterator_value::begin) + { + return m_object; + } + else + { + throw std::out_of_range("cannot get value"); + } + } + } + } + + /// post-increment (it++) + inline const_iterator operator++(int) + { + const_iterator result = *this; + + if (m_object == nullptr) + { + throw std::out_of_range("cannot increment iterator"); + } + + switch (m_object->m_type) + { + case (basic_json::value_t::object): + { + m_it.object_iterator++; + break; + } + + case (basic_json::value_t::array): + { + m_it.array_iterator++; + break; + } + + default: + { + m_it.generic_iterator = generic_iterator_value::end; + break; + } + } + + return result; + } + + /// pre-increment (++it) + inline const_iterator& operator++() + { + if (m_object == nullptr) + { + throw std::out_of_range("cannot increment iterator"); + } + + switch (m_object->m_type) + { + case (basic_json::value_t::object): + { + ++m_it.object_iterator; + break; + } + + case (basic_json::value_t::array): + { + ++m_it.array_iterator; + break; + } + + default: + { + m_it.generic_iterator = generic_iterator_value::end; + break; + } + } + + return *this; + } + + /// post-decrement (it--) + inline const_iterator operator--(int) + { + iterator result = *this; + + if (m_object == nullptr) + { + throw std::out_of_range("cannot decrement iterator"); + } + + switch (m_object->m_type) + { + case (basic_json::value_t::object): + { + m_it.object_iterator--; + break; + } + + case (basic_json::value_t::array): + { + m_it.array_iterator--; + break; + } + + default: + { + m_it.generic_iterator = generic_iterator_value::uninitialized; + break; + } + } + + return result; + } + + /// pre-decrement (--it) + inline const_iterator& operator--() + { + if (m_object == nullptr) + { + throw std::out_of_range("cannot decrement iterator"); + } + + switch (m_object->m_type) + { + case (basic_json::value_t::object): + { + --m_it.object_iterator; + break; + } + + case (basic_json::value_t::array): + { + --m_it.array_iterator; + break; + } + + default: + { + m_it.generic_iterator = generic_iterator_value::uninitialized; + break; + } + } + + return *this; + } + + /// comparison: equal + inline bool operator==(const const_iterator& other) const + { + if (m_object == nullptr + or other.m_object == nullptr + or m_object != other.m_object + or m_object->m_type != other.m_object->m_type) + { + return false; + } + + switch (m_object->m_type) + { + case (basic_json::value_t::object): + { + return (m_it.object_iterator == other.m_it.object_iterator); + } + + case (basic_json::value_t::array): + { + return (m_it.array_iterator == other.m_it.array_iterator); + } + + default: + { + return (m_it.generic_iterator == other.m_it.generic_iterator); + } + } + } + + /// comparison: not equal + inline bool operator!=(const const_iterator& other) const + { + return not operator==(other); + } + + private: + /// associated JSON instance + pointer m_object = nullptr; + /// the actual iterator of the associated instance + internal_const_iterator m_it; + }; +}; + + +///////////// +// presets // +///////////// + +/// default JSON class +using json = basic_json<>; + +} + + +///////////////////////// +// nonmember functions // +///////////////////////// + +// specialization of std::swap, and std::hash +namespace std +{ +/// swaps the values of two JSON objects +template <> +inline void swap(nlohmann::json& j1, + nlohmann::json& j2) noexcept( + is_nothrow_move_constructible<nlohmann::json>::value and + is_nothrow_move_assignable<nlohmann::json>::value + ) +{ + j1.swap(j2); +} + +/// hash value for JSON objects +template <> +struct hash<nlohmann::json> +{ + size_t operator()(const nlohmann::json& j) const + { + // a naive hashing via the string representation + return hash<std::string>()(j.dump()); + } +}; +} + +#endif diff --git a/test/json_unit.cc b/test/json_unit.cc deleted file mode 100644 index 8a9bfce0..00000000 --- a/test/json_unit.cc +++ /dev/null @@ -1,2258 +0,0 @@ -#define CATCH_CONFIG_MAIN -#include "catch.hpp" - -#include "json.h" - -#include <set> -#include <unordered_set> -#include <deque> -#include <list> -#include <forward_list> -#include <array> -#include <map> -#include <unordered_map> - -using json = nlohmann::json; - -TEST_CASE("array") -{ - SECTION("Basics") - { - // construction with given type - json j(json::value_t::array); - CHECK(j.type() == json::value_t::array); - - // const object - const json j_const (j); - - // string representation of default value - CHECK(j.dump() == "[]"); - - // iterators - CHECK(j.begin() == j.end()); - CHECK(j.cbegin() == j.cend()); - - // container members - CHECK(j.size() == 0); - CHECK(j.max_size() > 0); - CHECK(j.empty() == true); - CHECK_NOTHROW(size_t h = std::hash<json>()(j)); - - // implicit conversions - CHECK_NOTHROW(json::array_t v = j); - CHECK_THROWS_AS(json::object_t v = j, std::logic_error); - CHECK_THROWS_AS(std::string v = j, std::logic_error); - CHECK_THROWS_AS(bool v = j, std::logic_error); - CHECK_THROWS_AS(int v = j, std::logic_error); - CHECK_THROWS_AS(double v = j, std::logic_error); - - // explicit conversions - CHECK_NOTHROW(auto v = j.get<json::array_t>()); - CHECK_THROWS_AS(auto v = j.get<json::object_t>(), std::logic_error); - CHECK_THROWS_AS(auto v = j.get<std::string>(), std::logic_error); - CHECK_THROWS_AS(auto v = j.get<bool>(), std::logic_error); - CHECK_THROWS_AS(auto v = j.get<int>(), std::logic_error); - CHECK_THROWS_AS(auto v = j.get<int64_t>(), std::logic_error); - CHECK_THROWS_AS(auto v = j.get<double>(), std::logic_error); - - // transparent usage - auto id = [](json::array_t v) - { - return v; - }; - CHECK(id(j) == j.get<json::array_t>()); - - // copy constructor - json k(j); - CHECK(k == j); - - // copy assignment - k = j; - CHECK(k == j); - - // move constructor - json l = std::move(k); - CHECK(l == j); - } - - SECTION("Create from value") - { - json::array_t v1 = {"string", 1, 1.0, false, nullptr}; - json j1 = v1; - CHECK(j1.get<json::array_t>() == v1); - - json j2 = {"string", 1, 1.0, false, nullptr}; - json::array_t v2 = j2; - CHECK(j2.get<json::array_t>() == v1); - CHECK(j2.get<json::array_t>() == v2); - - // special tests to make sure construction from initializer list works - - // case 1: there is an element that is not an array - json j3 = { {"foo", "bar"}, 3 }; - CHECK(j3.type() == json::value_t::array); - - // case 2: there is an element with more than two elements - json j4 = { {"foo", "bar"}, {"one", "two", "three"} }; - CHECK(j4.type() == json::value_t::array); - - // case 3: there is an element whose first element is not a string - json j5 = { {"foo", "bar"}, {true, "baz"} }; - CHECK(j5.type() == json::value_t::array); - - // check if nested arrays work and are recognized as arrays - json j6 = { {{"foo", "bar"}} }; - CHECK(j6.type() == json::value_t::array); - CHECK(j6.size() == 1); - CHECK(j6[0].type() == json::value_t::object); - - // move constructor - json j7(std::move(v1)); - CHECK(j7 == j1); - } - - SECTION("Array operators") - { - json j = {0, 1, 2, 3, 4, 5, 6}; - - // read - const int v1 = j[3]; - CHECK(v1 == 3); - - // write - j[4] = 9; - int v2 = j[4]; - CHECK(v2 == 9); - - // size - CHECK (j.size() == 7); - - // push_back for different value types - j.push_back(7); - j.push_back("const char*"); - j.push_back(42.23); - std::string s = "std::string"; - j.push_back(s); - j.push_back(false); - j.push_back(nullptr); - j.push_back(j); - - CHECK (j.size() == 14); - - // operator+= for different value types - j += 7; - j += "const char*"; - j += 42.23; - j += s; - j += false; - j += nullptr; - j += j; - - CHECK (j.size() == 21); - - // implicit transformation into an array - json empty1, empty2; - empty1 += "foo"; - empty2.push_back("foo"); - CHECK(empty1.type() == json::value_t::array); - CHECK(empty2.type() == json::value_t::array); - CHECK(empty1 == empty2); - - // exceptions - json nonarray = 1; - CHECK_THROWS_AS(nonarray.at(0), std::domain_error); - CHECK_THROWS_AS(const int i = nonarray[0], std::domain_error); - CHECK_NOTHROW(j[21]); - CHECK_THROWS_AS(const int i = j.at(21), std::out_of_range); - CHECK_THROWS_AS(nonarray[0] = 10, std::domain_error); - // the next test is remove due to undefined behavior - //CHECK_NOTHROW(j[21] = 5); - CHECK_THROWS_AS(j.at(21) = 5, std::out_of_range); - CHECK_THROWS_AS(nonarray += 2, std::runtime_error); - - const json nonarray_const = nonarray; - const json j_const = j; - CHECK_THROWS_AS(nonarray_const.at(0), std::domain_error); - CHECK_THROWS_AS(const int i = nonarray_const[0], std::domain_error); - CHECK_NOTHROW(j_const[21]); - CHECK_THROWS_AS(const int i = j.at(21), std::out_of_range); - - { - json nonarray2 = json(1); - json nonarray3 = json(2); - json empty3 = json(); - CHECK_THROWS_AS(nonarray2.push_back(nonarray3), std::runtime_error); - CHECK_THROWS_AS(nonarray2.push_back(3), std::runtime_error); - CHECK_NOTHROW(empty3.push_back(nonarray3)); - CHECK(empty3.type() == json::value_t::array); - } - - const json k = j; - CHECK_NOTHROW(k[21]); - CHECK_THROWS_AS(const int i = k.at(21), std::out_of_range); - - // add initializer list - j.push_back({"a", "b", "c"}); - CHECK (j.size() == 24); - - // clear() - json j7 = {0, 1, 2, 3, 4, 5, 6};; - CHECK(j7.size() == 7); - j7.clear(); - CHECK(j7.size() == 0); - } - - SECTION("Iterators") - { - std::vector<int> vec = {0, 1, 2, 3, 4, 5, 6}; - json j1 = {0, 1, 2, 3, 4, 5, 6}; - const json j2 = {0, 1, 2, 3, 4, 5, 6}; - - { - // const_iterator - for (json::const_iterator cit = j1.begin(); cit != j1.end(); ++cit) - { - int v = *cit; - CHECK(v == vec[static_cast<size_t>(v)]); - - if (cit == j1.begin()) - { - CHECK(v == 0); - } - } - } - { - for (json::const_reverse_iterator it = j1.rbegin(); it != j1.crend(); ++it) - { - } - for (json::reverse_iterator it = j1.rbegin(); it != j1.rend(); ++it) - { - } - } - { - // const_iterator with cbegin/cend - for (json::const_iterator cit = j1.cbegin(); cit != j1.cend(); ++cit) - { - int v = *cit; - CHECK(v == vec[static_cast<size_t>(v)]); - - if (cit == j1.cbegin()) - { - CHECK(v == 0); - } - } - } - - { - // range based for - for (auto el : j1) - { - int v = el; - CHECK(v == vec[static_cast<size_t>(v)]); - } - } - - { - // iterator - for (json::iterator cit = j1.begin(); cit != j1.end(); ++cit) - { - int v_old = *cit; - *cit = cit->get<int>() * 2; - int v = *cit; - CHECK(v == vec[static_cast<size_t>(v_old)] * 2); - - if (cit == j1.begin()) - { - CHECK(v == 0); - } - } - } - - { - // const_iterator (on const object) - for (json::const_iterator cit = j2.begin(); cit != j2.end(); ++cit) - { - int v = *cit; - CHECK(v == vec[static_cast<size_t>(v)]); - - if (cit == j2.begin()) - { - CHECK(v == 0); - } - } - } - - { - // const_iterator with cbegin/cend (on const object) - for (json::const_iterator cit = j2.cbegin(); cit != j2.cend(); ++cit) - { - int v = *cit; - CHECK(v == vec[static_cast<size_t>(v)]); - - if (cit == j2.cbegin()) - { - CHECK(v == 0); - } - } - } - - { - // range based for (on const object) - for (auto el : j2) - { - int v = el; - CHECK(v == vec[static_cast<size_t>(v)]); - } - } - } - - SECTION("Initializer lists") - { - // edge case: This should be an array with two elements which are in - // turn arrays with two strings. However, this is treated like the - // initializer list of an object. - json j_should_be_an_array = { {"foo", "bar"}, {"baz", "bat"} }; - CHECK(j_should_be_an_array.type() == json::value_t::object); - - json j_is_an_array = { json::array({"foo", "bar"}), {"baz", "bat"} }; - CHECK(j_is_an_array.type() == json::value_t::array); - - // array outside initializer list - json j_pair_array = json::array({"s1", "s2"}); - CHECK(j_pair_array.type() == json::value_t::array); - - // array inside initializer list - json j_pair_array2 = {json::array({"us1", "us2"})}; - CHECK(j_pair_array2.type() == json::value_t::array); - - // array() is [] - json j_empty_array_explicit = json::array(); - CHECK(j_empty_array_explicit.type() == json::value_t::array); - - // object() is [] - json j_empty_object_explicit = json::object(); - CHECK(j_empty_object_explicit.type() == json::value_t::object); - - // {object({"s1", "s2"})} is [{"s1": "s2"}] - json j_explicit_object = {json::object({"s1", "s2"})}; - CHECK(j_explicit_object.type() == json::value_t::array); - - // object({"s1", "s2"}) is [{"s1": "s2"}] - json j_explicit_object2 = json::object({"s1", "s2"}); - CHECK(j_explicit_object2.type() == json::value_t::object); - - // object({{"s1", "s2"}}) is [{"s1": "s2"}] - json j_explicit_object3 = json::object({{"s1", "s2"}}); - CHECK(j_explicit_object3.type() == json::value_t::object); - - // check errors when explicit object creation is demanded - CHECK_THROWS_AS(json::object({"s1"}), std::logic_error); - CHECK_THROWS_AS(json::object({"s1", 1, 3}), std::logic_error); - CHECK_THROWS_AS(json::object({1, "s1"}), std::logic_error); - CHECK_THROWS_AS(json::object({{1, "s1"}}), std::logic_error); - CHECK_THROWS_AS(json::object({{"foo", "bar"}, {1, "s1"}}), std::logic_error); - - // {} is null - json j_null = {}; - CHECK(j_null.type() == json::value_t::null); - - // {{}} is [] - json j_empty_array_implicit = {{}}; - CHECK(j_empty_array_implicit.type() == json::value_t::array); - - // {1} is [1] - json j_singleton_array = {1}; - CHECK(j_singleton_array.type() == json::value_t::array); - - // test case from issue #8 - json j_issue8 = {json::array({"a", "b"}), json::array({"c", "d"})}; - CHECK(j_issue8.type() == json::value_t::array); - CHECK(j_issue8.dump() == "[[\"a\",\"b\"],[\"c\",\"d\"]]"); - } - - SECTION("Iterators and empty arrays") - { - json empty_array(json::value_t::array); - for (json::iterator it = empty_array.begin(); it != empty_array.end(); ++it) {} - for (json::const_iterator it = empty_array.begin(); it != empty_array.end(); ++it) {} - for (json::const_iterator it = empty_array.cbegin(); it != empty_array.cend(); ++it) {} - for (auto el : empty_array) {} - for (const auto el : empty_array) {} - - // create nonempty array, set iterators, clear array, and copy - // existing iterators to cover copy constructor's code - json array = {1, 2, 3}; - json::iterator i1 = array.begin(); - json::const_iterator i2 = array.cbegin(); - array.clear(); - json::iterator i3(i1); - json::const_iterator i4(i1); - json::const_iterator i5(i2); - } - - SECTION("Container operations") - { - json a1 = {1, 2, 3, 4}; - json a2 = {"one", "two", "three"}; - - a1.swap(a2); - - CHECK(a1 == json({"one", "two", "three"})); - CHECK(a2 == json({1, 2, 3, 4})); - } - - SECTION("Construct from sequence objects") - { - std::vector<int> c_vector {1, 2, 3, 4}; - json j_vec(c_vector); - CHECK(j_vec.type() == json::value_t::array); - CHECK(j_vec.size() == 4); - - std::vector<std::vector<int>> cr_vector {{1, 2}, {3}, {4, 5, 6}}; - json j_vecr(cr_vector); - CHECK(j_vecr.type() == json::value_t::array); - CHECK(j_vecr.size() == 3); - - std::set<std::string> c_set {"one", "two", "three", "four", "one"}; - json j_set(c_set); - CHECK(j_set.type() == json::value_t::array); - CHECK(j_set.size() == 4); - - std::unordered_set<std::string> c_uset {"one", "two", "three", "four", "one"}; - json j_uset(c_uset); - CHECK(j_uset.type() == json::value_t::array); - CHECK(j_uset.size() == 4); - - std::multiset<std::string> c_mset {"one", "two", "one", "four"}; - json j_mset(c_mset); - CHECK(j_mset.type() == json::value_t::array); - CHECK(j_mset.size() == 4); - - std::unordered_multiset<std::string> c_umset {"one", "two", "one", "four"}; - json j_umset(c_umset); - CHECK(j_umset.type() == json::value_t::array); - CHECK(j_umset.size() == 4); - - std::deque<float> c_deque {1.2, 2.3, 3.4, 5.6}; - json j_deque(c_deque); - CHECK(j_deque.type() == json::value_t::array); - CHECK(j_deque.size() == 4); - - std::list<bool> c_list {true, true, false, true}; - json j_list(c_list); - CHECK(j_list.type() == json::value_t::array); - CHECK(j_list.size() == 4); - - std::forward_list<int64_t> c_flist {12345678909876, 23456789098765, 34567890987654, 45678909876543}; - json j_flist(c_flist); - CHECK(j_flist.type() == json::value_t::array); - CHECK(j_flist.size() == 4); - - std::array<unsigned long, 4> c_array {{1, 2, 3, 4}}; - json j_array(c_array); - CHECK(j_array.type() == json::value_t::array); - CHECK(j_array.size() == 4); - } -} - -TEST_CASE("object") -{ - SECTION("Basics") - { - // construction with given type - json j(json::value_t::object); - CHECK(j.type() == json::value_t::object); - - // const object - const json j_const = j; - - // string representation of default value - CHECK(j.dump() == "{}"); - - // iterators - CHECK(j.begin() == j.end()); - CHECK(j.cbegin() == j.cend()); - - // container members - CHECK(j.size() == 0); - CHECK(j.max_size() > 0); - CHECK(j.empty() == true); - CHECK_NOTHROW(size_t h = std::hash<json>()(j)); - - // implicit conversions - CHECK_THROWS_AS(json::array_t v = j, std::logic_error); - CHECK_NOTHROW(json::object_t v = j); - CHECK_THROWS_AS(std::string v = j, std::logic_error); - CHECK_THROWS_AS(bool v = j, std::logic_error); - CHECK_THROWS_AS(int v = j, std::logic_error); - CHECK_THROWS_AS(double v = j, std::logic_error); - - // explicit conversions - CHECK_THROWS_AS(auto v = j.get<json::array_t>(), std::logic_error); - CHECK_NOTHROW(auto v = j.get<json::object_t>()); - CHECK_THROWS_AS(auto v = j.get<std::string>(), std::logic_error); - CHECK_THROWS_AS(auto v = j.get<bool>(), std::logic_error); - CHECK_THROWS_AS(auto v = j.get<int>(), std::logic_error); - CHECK_THROWS_AS(auto v = j.get<int64_t>(), std::logic_error); - CHECK_THROWS_AS(auto v = j.get<double>(), std::logic_error); - - // transparent usage - auto id = [](json::object_t v) - { - return v; - }; - CHECK(id(j) == j.get<json::object_t>()); - - // copy constructor - json k(j); - CHECK(k == j); - - // copy assignment - k = j; - CHECK(k == j); - - // move constructor - json l = std::move(k); - CHECK(l == j); - } - - SECTION("Create from value") - { - json::object_t v1 = { {"v1", "string"}, {"v2", 1}, {"v3", 1.0}, {"v4", false} }; - json j1 = v1; - CHECK(j1.get<json::object_t>() == v1); - - json j2 = { {"v1", "string"}, {"v2", 1}, {"v3", 1.0}, {"v4", false} }; - json::object_t v2 = j2; - CHECK(j2.get<json::object_t>() == v1); - CHECK(j2.get<json::object_t>() == v2); - - // check if multiple keys are ignored - json j3 = { {"key", "value"}, {"key", 1} }; - CHECK(j3.size() == 1); - - // move constructor - json j7(std::move(v1)); - CHECK(j7 == j1); - } - - SECTION("Object operators") - { - json j = {{"k0", "v0"}, {"k1", nullptr}, {"k2", 42}, {"k3", 3.141}, {"k4", true}}; - const json k = j; - - // read - { - const std::string v0 = j["k0"]; - CHECK(v0 == "v0"); - auto v1 = j["k1"]; - CHECK(v1 == nullptr); - int v2 = j["k2"]; - CHECK(v2 == 42); - double v3 = j["k3"]; - CHECK(v3 == 3.141); - bool v4 = j["k4"]; - CHECK(v4 == true); - } - { - const std::string v0 = j[std::string("k0")]; - CHECK(v0 == "v0"); - auto v1 = j[std::string("k1")]; - CHECK(v1 == nullptr); - int v2 = j[std::string("k2")]; - CHECK(v2 == 42); - double v3 = j[std::string("k3")]; - CHECK(v3 == 3.141); - bool v4 = j[std::string("k4")]; - CHECK(v4 == true); - } - { - const std::string v0 = k["k0"]; - CHECK(v0 == "v0"); - auto v1 = k["k1"]; - CHECK(v1 == nullptr); - int v2 = k["k2"]; - CHECK(v2 == 42); - double v3 = k["k3"]; - CHECK(v3 == 3.141); - bool v4 = k["k4"]; - CHECK(v4 == true); - } - { - const std::string v0 = k[std::string("k0")]; - CHECK(v0 == "v0"); - auto v1 = k[std::string("k1")]; - CHECK(v1 == nullptr); - int v2 = k[std::string("k2")]; - CHECK(v2 == 42); - double v3 = k[std::string("k3")]; - CHECK(v3 == 3.141); - bool v4 = k[std::string("k4")]; - CHECK(v4 == true); - } - - // write (replace) - j["k0"] = "new v0"; - CHECK(j["k0"] == "new v0"); - - // write (add) - j["k5"] = false; - - // size - CHECK(j.size() == 6); - - // find - CHECK(j.find("k0") != j.end()); - CHECK(j.find("v0") == j.end()); - CHECK(j.find(std::string("v0")) == j.end()); - json::const_iterator i1 = j.find("k0"); - json::iterator i2 = j.find("k0"); - CHECK(k.find("k0") != k.end()); - CHECK(k.find("v0") == k.end()); - CHECK(k.find(std::string("v0")) == k.end()); - json::const_iterator i22 = k.find("k0"); - - // at - CHECK_THROWS_AS(j.at("foo"), std::out_of_range); - CHECK_THROWS_AS(k.at("foo"), std::out_of_range); - CHECK_THROWS_AS(j.at(std::string("foo")), std::out_of_range); - CHECK_THROWS_AS(k.at(std::string("foo")), std::out_of_range); - CHECK_NOTHROW(j.at(std::string("k0"))); - CHECK_NOTHROW(k.at(std::string("k0"))); - { - json noobject = 1; - const json noobject_const = noobject; - CHECK_THROWS_AS(noobject.at("foo"), std::domain_error); - CHECK_THROWS_AS(noobject.at(std::string("foo")), std::domain_error); - CHECK_THROWS_AS(noobject_const.at("foo"), std::domain_error); - CHECK_THROWS_AS(noobject_const.at(std::string("foo")), std::domain_error); - CHECK_THROWS_AS(noobject["foo"], std::domain_error); - CHECK_THROWS_AS(noobject[std::string("foo")], std::domain_error); - CHECK_THROWS_AS(noobject_const[std::string("foo")], std::domain_error); - } - - // add pair - j.push_back(json::object_t::value_type {"int_key", 42}); - CHECK(j["int_key"].get<int>() == 42); - j += json::object_t::value_type {"int_key2", 23}; - CHECK(j["int_key2"].get<int>() == 23); - { - // make sure null objects are transformed - json je; - CHECK_NOTHROW(je.push_back(json::object_t::value_type {"int_key", 42})); - CHECK(je["int_key"].get<int>() == 42); - } - { - // make sure null objects are transformed - json je; - CHECK_NOTHROW((je += json::object_t::value_type {"int_key", 42})); - CHECK(je["int_key"].get<int>() == 42); - } - - // add initializer list (of pairs) - { - json je; - je.push_back({ {"one", 1}, {"two", false}, {"three", {1, 2, 3}} }); - CHECK(je["one"].get<int>() == 1); - CHECK(je["two"].get<bool>() == false); - CHECK(je["three"].size() == 3); - } - { - json je; - je += { {"one", 1}, {"two", false}, {"three", {1, 2, 3}} }; - CHECK(je["one"].get<int>() == 1); - CHECK(je["two"].get<bool>() == false); - CHECK(je["three"].size() == 3); - } - - // key/value for non-end iterator - CHECK(i1.key() == "k0"); - CHECK(i1.value() == j["k0"]); - CHECK(i2.key() == "k0"); - CHECK(i2.value() == j["k0"]); - - // key/value for uninitialzed iterator - json::const_iterator i3; - json::iterator i4; - CHECK_THROWS_AS(i3.key(), std::out_of_range); - CHECK_THROWS_AS(i3.value(), std::out_of_range); - CHECK_THROWS_AS(i4.key(), std::out_of_range); - CHECK_THROWS_AS(i4.value(), std::out_of_range); - - // key/value for end-iterator - json::const_iterator i5 = j.find("v0"); - json::iterator i6 = j.find("v0"); - CHECK_THROWS_AS(i5.key(), std::out_of_range); - CHECK_THROWS_AS(i5.value(), std::out_of_range); - CHECK_THROWS_AS(i6.key(), std::out_of_range); - CHECK_THROWS_AS(i6.value(), std::out_of_range); - - // implicit transformation into an object - json empty; - empty["foo"] = "bar"; - CHECK(empty.type() == json::value_t::object); - CHECK(empty["foo"] == "bar"); - - // exceptions - json nonarray = 1; - CHECK_THROWS_AS(const int i = nonarray["v1"], std::domain_error); - CHECK_THROWS_AS(nonarray["v1"] = 10, std::domain_error); - { - const json c = {{"foo", "bar"}}; - CHECK_THROWS_AS(c[std::string("baz")], std::out_of_range); - } - - // clear() - json j7 = {{"k0", 0}, {"k1", 1}, {"k2", 2}, {"k3", 3}}; - CHECK(j7.size() == 4); - j7.clear(); - CHECK(j7.size() == 0); - } - - SECTION("Iterators") - { - json j1 = {{"k0", 0}, {"k1", 1}, {"k2", 2}, {"k3", 3}}; - const json j2 = {{"k0", 0}, {"k1", 1}, {"k2", 2}, {"k3", 3}}; - - // iterator - for (json::iterator it = j1.begin(); it != j1.end(); ++it) - { - switch (static_cast<int>(it.value())) - { - case (0): - CHECK(it.key() == "k0"); - break; - case (1): - CHECK(it.key() == "k1"); - break; - case (2): - CHECK(it.key() == "k2"); - break; - case (3): - CHECK(it.key() == "k3"); - break; - default: - CHECK(false); - } - - CHECK((*it).type() == json::value_t::number); - CHECK(it->type() == json::value_t::number); - } - - for (json::reverse_iterator it = j1.rbegin(); it != j1.rend(); ++it) - { - } - - // range-based for - for (auto& element : j1) - { - element = 2 * element.get<int>(); - } - - // const_iterator - for (json::const_iterator it = j1.begin(); it != j1.end(); ++it) - { - switch (static_cast<int>(it.value())) - { - case (0): - CHECK(it.key() == "k0"); - break; - case (2): - CHECK(it.key() == "k1"); - break; - case (4): - CHECK(it.key() == "k2"); - break; - case (6): - CHECK(it.key() == "k3"); - break; - default: - CHECK(false); - } - - CHECK((*it).type() == json::value_t::number); - CHECK(it->type() == json::value_t::number); - } - - for (json::const_reverse_iterator it = j1.crbegin(); it != j1.crend(); ++it) - { - } - - // const_iterator using cbegin/cend - for (json::const_iterator it = j1.cbegin(); it != j1.cend(); ++it) - { - switch (static_cast<int>(it.value())) - { - case (0): - CHECK(it.key() == "k0"); - break; - case (2): - CHECK(it.key() == "k1"); - break; - case (4): - CHECK(it.key() == "k2"); - break; - case (6): - CHECK(it.key() == "k3"); - break; - default: - CHECK(false); - } - - CHECK((*it).type() == json::value_t::number); - CHECK(it->type() == json::value_t::number); - } - - // const_iterator (on const object) - for (json::const_iterator it = j2.begin(); it != j2.end(); ++it) - { - switch (static_cast<int>(it.value())) - { - case (0): - CHECK(it.key() == "k0"); - break; - case (1): - CHECK(it.key() == "k1"); - break; - case (2): - CHECK(it.key() == "k2"); - break; - case (3): - CHECK(it.key() == "k3"); - break; - default: - CHECK(false); - } - - CHECK((*it).type() == json::value_t::number); - CHECK(it->type() == json::value_t::number); - } - - // const_iterator using cbegin/cend (on const object) - for (json::const_iterator it = j2.cbegin(); it != j2.cend(); ++it) - { - switch (static_cast<int>(it.value())) - { - case (0): - CHECK(it.key() == "k0"); - break; - case (1): - CHECK(it.key() == "k1"); - break; - case (2): - CHECK(it.key() == "k2"); - break; - case (3): - CHECK(it.key() == "k3"); - break; - default: - CHECK(false); - } - - CHECK((*it).type() == json::value_t::number); - CHECK(it->type() == json::value_t::number); - } - - // range-based for (on const object) - for (auto element : j1) - { - CHECK(element.get<int>() >= 0); - } - } - - SECTION("Iterators and empty objects") - { - json empty_object(json::value_t::object); - for (json::iterator it = empty_object.begin(); it != empty_object.end(); ++it) {} - for (json::const_iterator it = empty_object.begin(); it != empty_object.end(); ++it) {} - for (json::const_iterator it = empty_object.cbegin(); it != empty_object.cend(); ++it) {} - for (auto el : empty_object) {} - for (const auto el : empty_object) {} - - // create nonempty object, set iterators, clear object, and copy - // existing iterators to cover copy constructor's code - json object = {{"foo", 1}}; - json::iterator i1 = object.begin(); - json::const_iterator i2 = object.cbegin(); - object.clear(); - json::iterator i3(i1); - json::const_iterator i4(i1); - json::const_iterator i5(i2); - } - - SECTION("Container operations") - { - json o1 = { {"one", "eins"}, {"two", "zwei"} }; - json o2 = { {"one", 1}, {"two", 2} }; - - o1.swap(o2); - - CHECK(o1 == json({ {"one", 1}, {"two", 2} })); - CHECK(o2 == json({ {"one", "eins"}, {"two", "zwei"} })); - - std::swap(o1, o2); - - CHECK(o1 == json({ {"one", "eins"}, {"two", "zwei"} })); - CHECK(o2 == json({ {"one", 1}, {"two", 2} })); - } - - SECTION("Construct from sequence objects") - { - std::map<std::string, int> c_map { {"one", 1}, {"two", 2}, {"three", 3} }; - json j_map(c_map); - CHECK(j_map.type() == json::value_t::object); - CHECK(j_map.size() == 3); - - std::unordered_map<const char*, float> c_umap { {"one", 1.2}, {"two", 2.3}, {"three", 3.4} }; - json j_umap(c_umap); - CHECK(j_umap.type() == json::value_t::object); - CHECK(j_umap.size() == 3); - - std::multimap<std::string, bool> c_mmap { {"one", true}, {"two", true}, {"three", false}, {"three", true} }; - json j_mmap(c_mmap); - CHECK(j_mmap.type() == json::value_t::object); - CHECK(j_mmap.size() == 3); - - std::unordered_multimap<std::string, bool> c_ummap { {"one", true}, {"two", true}, {"three", false}, {"three", true} }; - json j_ummap(c_ummap); - CHECK(j_ummap.type() == json::value_t::object); - CHECK(j_ummap.size() == 3); - } -} - -TEST_CASE("null") -{ - SECTION("Basics") - { - // construction with given type - json j; - CHECK(j.type() == json::value_t::null); - - // string representation of default value - CHECK(j.dump() == "null"); - - // iterators - CHECK(j.begin() != j.end()); - CHECK(j.cbegin() != j.cend()); - - // container members - CHECK(j.size() == 0); - CHECK(j.max_size() == 0); - CHECK(j.empty() == true); - CHECK_NOTHROW(size_t h = std::hash<json>()(j)); - - // implicit conversions - CHECK_NOTHROW(json::array_t v = j); - CHECK_THROWS_AS(json::object_t v = j, std::logic_error); - CHECK_THROWS_AS(std::string v = j, std::logic_error); - CHECK_THROWS_AS(bool v = j, std::logic_error); - CHECK_THROWS_AS(int v = j, std::logic_error); - CHECK_THROWS_AS(double v = j, std::logic_error); - - // explicit conversions - CHECK_NOTHROW(auto v = j.get<json::array_t>()); - CHECK_THROWS_AS(auto v = j.get<json::object_t>(), std::logic_error); - CHECK_THROWS_AS(auto v = j.get<std::string>(), std::logic_error); - CHECK_THROWS_AS(auto v = j.get<bool>(), std::logic_error); - CHECK_THROWS_AS(auto v = j.get<int>(), std::logic_error); - CHECK_THROWS_AS(auto v = j.get<int64_t>(), std::logic_error); - CHECK_THROWS_AS(auto v = j.get<double>(), std::logic_error); - - // copy constructor - json k(j); - CHECK(k == j); - - // copy assignment - k = j; - CHECK(k == j); - - // move constructor - json l = std::move(k); - CHECK(l == j); - } - - SECTION("Create from value") - { - json j1 = nullptr; - CHECK(j1.type() == json::value_t::null); - } - - SECTION("Operators") - { - // clear() - json j1 = nullptr; - j1.clear(); - CHECK(j1 == json(nullptr)); - } - - SECTION("Container operations") - { - json n1; - json n2; - - n1.swap(n2); - - CHECK(n1 == json()); - CHECK(n2 == json()); - - std::swap(n1, n2); - - CHECK(n1 == json()); - CHECK(n2 == json()); - - json::iterator it = n1.begin(); - --it; - - json::const_iterator cit = n1.cbegin(); - --cit; - } -} - -TEST_CASE("string") -{ - SECTION("Basics") - { - // construction with given type - json j(json::value_t::string); - CHECK(j.type() == json::value_t::string); - - // const object - const json j_const = j; - - // iterators - CHECK(j.begin() != j.end()); - CHECK(j.cbegin() != j.cend()); - - // string representation of default value - CHECK(j.dump() == "\"\""); - - // container members - CHECK(j.size() == 1); - CHECK(j.max_size() == 1); - CHECK(j.empty() == false); - CHECK_NOTHROW(size_t h = std::hash<json>()(j)); - - // implicit conversions - CHECK_NOTHROW(json::array_t v = j); - CHECK_THROWS_AS(json::object_t v = j, std::logic_error); - CHECK_NOTHROW(std::string v = j); - CHECK_THROWS_AS(bool v = j, std::logic_error); - CHECK_THROWS_AS(int v = j, std::logic_error); - CHECK_THROWS_AS(double v = j, std::logic_error); - - // explicit conversions - CHECK_NOTHROW(auto v = j.get<json::array_t>()); - CHECK_THROWS_AS(auto v = j.get<json::object_t>(), std::logic_error); - CHECK_NOTHROW(auto v = j.get<std::string>()); - CHECK_NOTHROW(auto v = static_cast<std::string>(j)); - CHECK_THROWS_AS(auto v = j.get<bool>(), std::logic_error); - CHECK_THROWS_AS(auto v = j.get<int>(), std::logic_error); - CHECK_THROWS_AS(auto v = j.get<int64_t>(), std::logic_error); - CHECK_THROWS_AS(auto v = j.get<double>(), std::logic_error); - - // transparent usage - auto id = [](std::string v) - { - return v; - }; - CHECK(id(j) == j.get<std::string>()); - - // copy constructor - json k(j); - CHECK(k == j); - - // copy assignment - k = j; - CHECK(k == j); - - // move constructor - json l = std::move(k); - CHECK(l == j); - } - - SECTION("Create from value") - { - json j1 = std::string("Hello, world"); - std::string v1 = j1; - CHECK(j1.get<std::string>() == v1); - - json j2 = "Hello, world"; - CHECK(j2.get<std::string>() == "Hello, world"); - - std::string v3 = "Hello, world"; - json j3 = std::move(v3); - CHECK(j3.get<std::string>() == "Hello, world"); - } - - SECTION("Operators") - { - // clear() - json j1 = std::string("Hello, world"); - CHECK(j1.get<std::string>() == "Hello, world"); - j1.clear(); - CHECK(j1.get<std::string>() == ""); - } - - SECTION("Dumping") - { - CHECK(json("\"").dump(0) == "\"\\\"\""); - CHECK(json("\\").dump(0) == "\"\\\\\""); - CHECK(json("\n").dump(0) == "\"\\n\""); - CHECK(json("\t").dump(0) == "\"\\t\""); - CHECK(json("\b").dump(0) == "\"\\b\""); - CHECK(json("\f").dump(0) == "\"\\f\""); - CHECK(json("\r").dump(0) == "\"\\r\""); - } - - SECTION("Container operations") - { - json s1 = "foo"; - json s2 = "bar"; - - s1.swap(s2); - - CHECK(s1 == json("bar")); - CHECK(s2 == json("foo")); - - std::swap(s1, s2); - - CHECK(s1 == json("foo")); - CHECK(s2 == json("bar")); - } -} - -TEST_CASE("boolean") -{ - SECTION("Basics") - { - // construction with given type - json j(json::value_t::boolean); - CHECK(j.type() == json::value_t::boolean); - - // const object - const json j_const = j; - - // iterators - CHECK(j.begin() != j.end()); - CHECK(j.cbegin() != j.cend()); - - // string representation of default value - CHECK(j.dump() == "false"); - - // container members - CHECK(j.size() == 1); - CHECK(j.max_size() == 1); - CHECK(j.empty() == false); - CHECK_NOTHROW(size_t h = std::hash<json>()(j)); - - // implicit conversions - CHECK_NOTHROW(json::array_t v = j); - CHECK_THROWS_AS(json::object_t v = j, std::logic_error); - CHECK_THROWS_AS(std::string v = j, std::logic_error); - CHECK_NOTHROW(bool v = j); - CHECK_THROWS_AS(int v = j, std::logic_error); - CHECK_THROWS_AS(double v = j, std::logic_error); - - // explicit conversions - CHECK_NOTHROW(auto v = j.get<json::array_t>()); - CHECK_THROWS_AS(auto v = j.get<json::object_t>(), std::logic_error); - CHECK_THROWS_AS(auto v = j.get<std::string>(), std::logic_error); - CHECK_NOTHROW(auto v = j.get<bool>()); - CHECK_THROWS_AS(auto v = j.get<int>(), std::logic_error); - CHECK_THROWS_AS(auto v = j.get<int64_t>(), std::logic_error); - CHECK_THROWS_AS(auto v = j.get<double>(), std::logic_error); - - // transparent usage - auto id = [](bool v) - { - return v; - }; - CHECK(id(j) == j.get<bool>()); - - // copy constructor - json k(j); - CHECK(k == j); - - // copy assignment - k = j; - CHECK(k == j); - - // move constructor - json l = std::move(k); - CHECK(l == j); - } - - SECTION("Create from value") - { - json j1 = true; - bool v1 = j1; - CHECK(j1.get<bool>() == v1); - - json j2 = false; - bool v2 = j2; - CHECK(j2.get<bool>() == v2); - } - - SECTION("Operators") - { - // clear() - json j1 = true; - CHECK(j1.get<bool>() == true); - j1.clear(); - CHECK(j1.get<bool>() == false); - } - - SECTION("Container operations") - { - json b1 = true; - json b2 = false; - - b1.swap(b2); - - CHECK(b1 == json(false)); - CHECK(b2 == json(true)); - - std::swap(b1, b2); - - CHECK(b1 == json(true)); - CHECK(b2 == json(false)); - } -} - -TEST_CASE("number (int)") -{ - SECTION("Basics") - { - // construction with given type - json j(json::value_t::number); - CHECK(j.type() == json::value_t::number); - - // const object - const json j_const = j; - - // iterators - CHECK(j.begin() != j.end()); - CHECK(j.cbegin() != j.cend()); - - // string representation of default value - CHECK(j.dump() == "0"); - - // container members - CHECK(j.size() == 1); - CHECK(j.max_size() == 1); - CHECK(j.empty() == false); - CHECK_NOTHROW(size_t h = std::hash<json>()(j)); - - // implicit conversions - CHECK_NOTHROW(json::array_t v = j); - CHECK_THROWS_AS(json::object_t v = j, std::logic_error); - CHECK_THROWS_AS(std::string v = j, std::logic_error); - CHECK_THROWS_AS(bool v = j, std::logic_error); - CHECK_NOTHROW(int v = j); - CHECK_NOTHROW(double v = j); - - // explicit conversions - CHECK_NOTHROW(auto v = j.get<json::array_t>()); - CHECK_THROWS_AS(auto v = j.get<json::object_t>(), std::logic_error); - CHECK_THROWS_AS(auto v = j.get<std::string>(), std::logic_error); - CHECK_THROWS_AS(auto v = j.get<bool>(), std::logic_error); - CHECK_NOTHROW(auto v = j.get<int>()); - CHECK_NOTHROW(auto v = j.get<int64_t>()); - CHECK_NOTHROW(auto v = j.get<double>()); - - // transparent usage - auto id = [](int v) - { - return v; - }; - CHECK(id(j) == j.get<int>()); - - // copy constructor - json k(j); - CHECK(k == j); - - // copy assignment - k = j; - CHECK(k == j); - - // move constructor - json l = std::move(k); - CHECK(l == j); - } - - SECTION("Create from value") - { - json j1 = 23; - int v1 = j1; - CHECK(j1.get<int>() == v1); - - json j2 = 42; - int v2 = j2; - CHECK(j2.get<int>() == v2); - - json j3 = 2147483647; - int v3 = j3; - CHECK(j3.get<int>() == v3); - - json j4 = 9223372036854775807; - int64_t v4 = j4; - CHECK(j4.get<int64_t>() == v4); - } - - SECTION("Operators") - { - // clear() - json j1 = 42; - CHECK(j1.get<int>() == 42); - j1.clear(); - CHECK(j1.get<int>() == 0); - - // find() - CHECK(j1.find("foo") == j1.end()); - CHECK(j1.find(std::string("foo")) == j1.end()); - const json j2 = j1; - CHECK(j2.find("foo") == j2.end()); - CHECK(j2.find(std::string("foo")) == j2.end()); - } - - SECTION("Container operations") - { - json n1 = 23; - json n2 = 42; - - n1.swap(n2); - - CHECK(n1 == json(42)); - CHECK(n2 == json(23)); - - std::swap(n1, n2); - - CHECK(n1 == json(23)); - CHECK(n2 == json(42)); - } -} - -TEST_CASE("number (float)") -{ - SECTION("Basics") - { - // construction with given type - json j(json::value_t::number_float); - CHECK(j.type() == json::value_t::number_float); - - // const object - const json j_const = j; - - // iterators - CHECK(j.begin() != j.end()); - CHECK(j.cbegin() != j.cend()); - - // string representation of default value - CHECK(j.dump() == "0.000000"); - - // container members - CHECK(j.size() == 1); - CHECK(j.max_size() == 1); - CHECK(j.empty() == false); - CHECK_NOTHROW(size_t h = std::hash<json>()(j)); - - // implicit conversions - CHECK_NOTHROW(json::array_t v = j); - CHECK_THROWS_AS(json::object_t v = j, std::logic_error); - CHECK_THROWS_AS(std::string v = j, std::logic_error); - CHECK_THROWS_AS(bool v = j, std::logic_error); - CHECK_NOTHROW(int v = j); - CHECK_NOTHROW(double v = j); - - // explicit conversions - CHECK_NOTHROW(auto v = j.get<json::array_t>()); - CHECK_THROWS_AS(auto v = j.get<json::object_t>(), std::logic_error); - CHECK_THROWS_AS(auto v = j.get<std::string>(), std::logic_error); - CHECK_THROWS_AS(auto v = j.get<bool>(), std::logic_error); - CHECK_NOTHROW(auto v = j.get<int>()); - CHECK_NOTHROW(auto v = j.get<int64_t>()); - CHECK_NOTHROW(auto v = j.get<double>()); - - // transparent usage - auto id = [](double v) - { - return v; - }; - CHECK(id(j) == j.get<double>()); - - // copy constructor - json k(j); - CHECK(k == j); - - // copy assignment - k = j; - CHECK(k == j); - - // move constructor - json l = std::move(k); - CHECK(l == j); - } - - SECTION("Create from value") - { - json j1 = 3.1415926; - double v1 = j1; - CHECK(j1.get<double>() == v1); - - json j2 = 2.7182818; - double v2 = j2; - CHECK(j2.get<double>() == v2); - - int64_t v3 = j2; - CHECK(j2.get<int64_t>() == v3); - } - - SECTION("Operators") - { - // clear() - json j1 = 3.1415926; - CHECK(j1.get<double>() == 3.1415926); - j1.clear(); - CHECK(j1.get<double>() == 0.0); - } - - SECTION("Container operations") - { - json n1 = 23.42; - json n2 = 42.23; - - n1.swap(n2); - - CHECK(n1 == json(42.23)); - CHECK(n2 == json(23.42)); - - std::swap(n1, n2); - - CHECK(n1 == json(23.42)); - CHECK(n2 == json(42.23)); - } -} - -TEST_CASE("Iterators") -{ - json j1 = {0, 1, 2, 3, 4}; - json j2 = {{"foo", "bar"}, {"baz", "bam"}}; - json j3 = true; - json j4 = nullptr; - json j5 = 42; - json j6 = 23.42; - json j7 = "hello"; - - const json j1_const = {0, 1, 2, 3, 4}; - const json j2_const = {{"foo", "bar"}, {"baz", "bam"}}; - const json j3_const = true; - const json j4_const = nullptr; - const json j5_const = 42; - const json j6_const = 23.42; - const json j7_const = "hello"; - - // operator * - CHECK(* j1.begin() == json(0)); - CHECK(* j1_const.begin() == json(0)); - CHECK(* j2.begin() != json()); - CHECK(* j2_const.begin() != json()); - CHECK(* j3.begin() == json(true)); - CHECK(* j3_const.begin() == json(true)); - CHECK(* j4.begin() == json()); - CHECK(* j4_const.begin() == json()); - CHECK(* j5.begin() == json(42)); - CHECK(* j5_const.begin() == json(42)); - CHECK(* j6.begin() == json(23.42)); - CHECK(* j6_const.begin() == json(23.42)); - CHECK(* j7.begin() == json("hello")); - CHECK(* j7_const.begin() == json("hello")); - - CHECK_THROWS_AS(* j1.end(), std::out_of_range); - CHECK_THROWS_AS(* j1.cend(), std::out_of_range); - CHECK_THROWS_AS(* j2.end(), std::out_of_range); - CHECK_THROWS_AS(* j2.cend(), std::out_of_range); - CHECK_THROWS_AS(* j3.end(), std::out_of_range); - CHECK_THROWS_AS(* j3.cend(), std::out_of_range); - CHECK_THROWS_AS(* j4.end(), std::out_of_range); - CHECK_THROWS_AS(* j4.cend(), std::out_of_range); - CHECK_THROWS_AS(* j5.end(), std::out_of_range); - CHECK_THROWS_AS(* j5.cend(), std::out_of_range); - CHECK_THROWS_AS(* j6.end(), std::out_of_range); - CHECK_THROWS_AS(* j6.cend(), std::out_of_range); - CHECK_THROWS_AS(* j7.end(), std::out_of_range); - CHECK_THROWS_AS(* j7.cend(), std::out_of_range); - - CHECK_THROWS_AS(* j1_const.end(), std::out_of_range); - CHECK_THROWS_AS(* j1_const.cend(), std::out_of_range); - CHECK_THROWS_AS(* j2_const.end(), std::out_of_range); - CHECK_THROWS_AS(* j2_const.cend(), std::out_of_range); - CHECK_THROWS_AS(* j3_const.end(), std::out_of_range); - CHECK_THROWS_AS(* j3_const.cend(), std::out_of_range); - CHECK_THROWS_AS(* j4_const.end(), std::out_of_range); - CHECK_THROWS_AS(* j4_const.cend(), std::out_of_range); - CHECK_THROWS_AS(* j5_const.end(), std::out_of_range); - CHECK_THROWS_AS(* j5_const.cend(), std::out_of_range); - CHECK_THROWS_AS(* j6_const.end(), std::out_of_range); - CHECK_THROWS_AS(* j6_const.cend(), std::out_of_range); - CHECK_THROWS_AS(* j7_const.end(), std::out_of_range); - CHECK_THROWS_AS(* j7_const.cend(), std::out_of_range); - - // operator -> - CHECK(j1.begin()->type() == json::value_t::number); - CHECK(j1.cbegin()->type() == json::value_t::number); - CHECK(j2.begin()->type() == json::value_t::string); - CHECK(j2.cbegin()->type() == json::value_t::string); - CHECK(j3.begin()->type() == json::value_t::boolean); - CHECK(j3.cbegin()->type() == json::value_t::boolean); - CHECK(j4.begin()->type() == json::value_t::null); - CHECK(j4.cbegin()->type() == json::value_t::null); - CHECK(j5.begin()->type() == json::value_t::number); - CHECK(j5.cbegin()->type() == json::value_t::number); - CHECK(j6.begin()->type() == json::value_t::number_float); - CHECK(j6.cbegin()->type() == json::value_t::number_float); - CHECK(j7.begin()->type() == json::value_t::string); - CHECK(j7.cbegin()->type() == json::value_t::string); - - CHECK(j1_const.begin()->type() == json::value_t::number); - CHECK(j1_const.cbegin()->type() == json::value_t::number); - CHECK(j2_const.begin()->type() == json::value_t::string); - CHECK(j2_const.cbegin()->type() == json::value_t::string); - CHECK(j3_const.begin()->type() == json::value_t::boolean); - CHECK(j3_const.cbegin()->type() == json::value_t::boolean); - CHECK(j4_const.begin()->type() == json::value_t::null); - CHECK(j4_const.cbegin()->type() == json::value_t::null); - CHECK(j5_const.begin()->type() == json::value_t::number); - CHECK(j5_const.cbegin()->type() == json::value_t::number); - CHECK(j6_const.begin()->type() == json::value_t::number_float); - CHECK(j6_const.cbegin()->type() == json::value_t::number_float); - CHECK(j7_const.begin()->type() == json::value_t::string); - CHECK(j7_const.cbegin()->type() == json::value_t::string); - - CHECK_THROWS_AS(j1.end()->type(), std::out_of_range); - CHECK_THROWS_AS(j1.cend()->type(), std::out_of_range); - CHECK_THROWS_AS(j2.end()->type(), std::out_of_range); - CHECK_THROWS_AS(j2.cend()->type(), std::out_of_range); - CHECK_THROWS_AS(j3.end()->type(), std::out_of_range); - CHECK_THROWS_AS(j3.cend()->type(), std::out_of_range); - CHECK_THROWS_AS(j4.end()->type(), std::out_of_range); - CHECK_THROWS_AS(j4.cend()->type(), std::out_of_range); - CHECK_THROWS_AS(j5.end()->type(), std::out_of_range); - CHECK_THROWS_AS(j5.cend()->type(), std::out_of_range); - CHECK_THROWS_AS(j6.end()->type(), std::out_of_range); - CHECK_THROWS_AS(j6.cend()->type(), std::out_of_range); - CHECK_THROWS_AS(j7.end()->type(), std::out_of_range); - CHECK_THROWS_AS(j7.cend()->type(), std::out_of_range); - - CHECK_THROWS_AS(j1_const.end()->type(), std::out_of_range); - CHECK_THROWS_AS(j1_const.cend()->type(), std::out_of_range); - CHECK_THROWS_AS(j2_const.end()->type(), std::out_of_range); - CHECK_THROWS_AS(j2_const.cend()->type(), std::out_of_range); - CHECK_THROWS_AS(j3_const.end()->type(), std::out_of_range); - CHECK_THROWS_AS(j3_const.cend()->type(), std::out_of_range); - CHECK_THROWS_AS(j4_const.end()->type(), std::out_of_range); - CHECK_THROWS_AS(j4_const.cend()->type(), std::out_of_range); - CHECK_THROWS_AS(j5_const.end()->type(), std::out_of_range); - CHECK_THROWS_AS(j5_const.cend()->type(), std::out_of_range); - CHECK_THROWS_AS(j6_const.end()->type(), std::out_of_range); - CHECK_THROWS_AS(j6_const.cend()->type(), std::out_of_range); - CHECK_THROWS_AS(j7_const.end()->type(), std::out_of_range); - CHECK_THROWS_AS(j7_const.cend()->type(), std::out_of_range); - - // value - CHECK(j1.begin().value().type() == json::value_t::number); - CHECK(j1.cbegin().value().type() == json::value_t::number); - CHECK(j2.begin().value().type() == json::value_t::string); - CHECK(j2.cbegin().value().type() == json::value_t::string); - CHECK(j3.begin().value().type() == json::value_t::boolean); - CHECK(j3.cbegin().value().type() == json::value_t::boolean); - CHECK(j4.begin().value().type() == json::value_t::null); - CHECK(j4.cbegin().value().type() == json::value_t::null); - CHECK(j5.begin().value().type() == json::value_t::number); - CHECK(j5.cbegin().value().type() == json::value_t::number); - CHECK(j6.begin().value().type() == json::value_t::number_float); - CHECK(j6.cbegin().value().type() == json::value_t::number_float); - CHECK(j7.begin().value().type() == json::value_t::string); - CHECK(j7.cbegin().value().type() == json::value_t::string); - - CHECK(j1_const.begin().value().type() == json::value_t::number); - CHECK(j1_const.cbegin().value().type() == json::value_t::number); - CHECK(j2_const.begin().value().type() == json::value_t::string); - CHECK(j2_const.cbegin().value().type() == json::value_t::string); - CHECK(j3_const.begin().value().type() == json::value_t::boolean); - CHECK(j3_const.cbegin().value().type() == json::value_t::boolean); - CHECK(j4_const.begin().value().type() == json::value_t::null); - CHECK(j4_const.cbegin().value().type() == json::value_t::null); - CHECK(j5_const.begin().value().type() == json::value_t::number); - CHECK(j5_const.cbegin().value().type() == json::value_t::number); - CHECK(j6_const.begin().value().type() == json::value_t::number_float); - CHECK(j6_const.cbegin().value().type() == json::value_t::number_float); - CHECK(j7_const.begin().value().type() == json::value_t::string); - CHECK(j7_const.cbegin().value().type() == json::value_t::string); - - CHECK_THROWS_AS(j1.end().value(), std::out_of_range); - CHECK_THROWS_AS(j1.cend().value(), std::out_of_range); - CHECK_THROWS_AS(j2.end().value(), std::out_of_range); - CHECK_THROWS_AS(j2.cend().value(), std::out_of_range); - CHECK_THROWS_AS(j3.end().value(), std::out_of_range); - CHECK_THROWS_AS(j3.cend().value(), std::out_of_range); - CHECK_THROWS_AS(j4.end().value(), std::out_of_range); - CHECK_THROWS_AS(j4.cend().value(), std::out_of_range); - CHECK_THROWS_AS(j5.end().value(), std::out_of_range); - CHECK_THROWS_AS(j5.cend().value(), std::out_of_range); - CHECK_THROWS_AS(j6.end().value(), std::out_of_range); - CHECK_THROWS_AS(j6.cend().value(), std::out_of_range); - CHECK_THROWS_AS(j7.end().value(), std::out_of_range); - CHECK_THROWS_AS(j7.cend().value(), std::out_of_range); - - CHECK_THROWS_AS(j1_const.end().value(), std::out_of_range); - CHECK_THROWS_AS(j1_const.cend().value(), std::out_of_range); - CHECK_THROWS_AS(j2_const.end().value(), std::out_of_range); - CHECK_THROWS_AS(j2_const.cend().value(), std::out_of_range); - CHECK_THROWS_AS(j3_const.end().value(), std::out_of_range); - CHECK_THROWS_AS(j3_const.cend().value(), std::out_of_range); - CHECK_THROWS_AS(j4_const.end().value(), std::out_of_range); - CHECK_THROWS_AS(j4_const.cend().value(), std::out_of_range); - CHECK_THROWS_AS(j5_const.end().value(), std::out_of_range); - CHECK_THROWS_AS(j5_const.cend().value(), std::out_of_range); - CHECK_THROWS_AS(j6_const.end().value(), std::out_of_range); - CHECK_THROWS_AS(j6_const.cend().value(), std::out_of_range); - CHECK_THROWS_AS(j7_const.end().value(), std::out_of_range); - CHECK_THROWS_AS(j7_const.cend().value(), std::out_of_range); - - // iterator comparison - CHECK(j1.begin() != j2.begin()); - CHECK(j1.begin() != j3.begin()); - CHECK(j1.begin() != j4.begin()); - CHECK(j1.begin() != j5.begin()); - CHECK(j1.begin() != j6.begin()); - CHECK(j1.begin() != j7.begin()); - - CHECK(j1.cbegin() != j2.cbegin()); - CHECK(j1.cbegin() != j3.cbegin()); - CHECK(j1.cbegin() != j4.cbegin()); - CHECK(j1.cbegin() != j5.cbegin()); - CHECK(j1.cbegin() != j6.cbegin()); - CHECK(j1.cbegin() != j7.cbegin()); - - CHECK(j2.begin() != j1.begin()); - CHECK(j2.begin() != j3.begin()); - CHECK(j2.begin() != j4.begin()); - CHECK(j2.begin() != j5.begin()); - CHECK(j2.begin() != j6.begin()); - CHECK(j2.begin() != j7.begin()); - - CHECK(j2.cbegin() != j1.cbegin()); - CHECK(j2.cbegin() != j3.cbegin()); - CHECK(j2.cbegin() != j4.cbegin()); - CHECK(j2.cbegin() != j5.cbegin()); - CHECK(j2.cbegin() != j6.cbegin()); - CHECK(j2.cbegin() != j7.cbegin()); - - CHECK(j3.begin() != j1.begin()); - CHECK(j3.begin() != j2.begin()); - CHECK(j3.begin() != j4.begin()); - CHECK(j3.begin() != j5.begin()); - CHECK(j3.begin() != j6.begin()); - CHECK(j3.begin() != j7.begin()); - - CHECK(j3.cbegin() != j1.cbegin()); - CHECK(j3.cbegin() != j2.cbegin()); - CHECK(j3.cbegin() != j4.cbegin()); - CHECK(j3.cbegin() != j5.cbegin()); - CHECK(j3.cbegin() != j6.cbegin()); - CHECK(j3.cbegin() != j7.cbegin()); - - CHECK(j4.begin() != j1.begin()); - CHECK(j4.begin() != j2.begin()); - CHECK(j4.begin() != j3.begin()); - CHECK(j4.begin() != j5.begin()); - CHECK(j4.begin() != j6.begin()); - CHECK(j4.begin() != j7.begin()); - - CHECK(j4.cbegin() != j1.cbegin()); - CHECK(j4.cbegin() != j2.cbegin()); - CHECK(j4.cbegin() != j3.cbegin()); - CHECK(j4.cbegin() != j5.cbegin()); - CHECK(j4.cbegin() != j6.cbegin()); - CHECK(j4.cbegin() != j7.cbegin()); - - CHECK(j5.begin() != j1.begin()); - CHECK(j5.begin() != j2.begin()); - CHECK(j5.begin() != j3.begin()); - CHECK(j5.begin() != j4.begin()); - CHECK(j5.begin() != j6.begin()); - CHECK(j5.begin() != j7.begin()); - - CHECK(j5.cbegin() != j1.cbegin()); - CHECK(j5.cbegin() != j2.cbegin()); - CHECK(j5.cbegin() != j3.cbegin()); - CHECK(j5.cbegin() != j4.cbegin()); - CHECK(j5.cbegin() != j6.cbegin()); - CHECK(j5.cbegin() != j7.cbegin()); - - CHECK(j6.begin() != j1.begin()); - CHECK(j6.begin() != j2.begin()); - CHECK(j6.begin() != j3.begin()); - CHECK(j6.begin() != j4.begin()); - CHECK(j6.begin() != j5.begin()); - CHECK(j6.begin() != j7.begin()); - - CHECK(j6.cbegin() != j1.cbegin()); - CHECK(j6.cbegin() != j2.cbegin()); - CHECK(j6.cbegin() != j3.cbegin()); - CHECK(j6.cbegin() != j4.cbegin()); - CHECK(j6.cbegin() != j5.cbegin()); - CHECK(j6.cbegin() != j7.cbegin()); - - CHECK(j7.begin() != j1.begin()); - CHECK(j7.begin() != j2.begin()); - CHECK(j7.begin() != j3.begin()); - CHECK(j7.begin() != j4.begin()); - CHECK(j7.begin() != j5.begin()); - CHECK(j7.begin() != j6.begin()); - - CHECK(j7.cbegin() != j1.cbegin()); - CHECK(j7.cbegin() != j2.cbegin()); - CHECK(j7.cbegin() != j3.cbegin()); - CHECK(j7.cbegin() != j4.cbegin()); - CHECK(j7.cbegin() != j5.cbegin()); - CHECK(j7.cbegin() != j6.cbegin()); - - // iterator copy constructors - { - json::iterator tmp1(j1.begin()); - json::const_iterator tmp2(j1.cbegin()); - } - { - json::iterator tmp1(j2.begin()); - json::const_iterator tmp2(j2.cbegin()); - } - { - json::iterator tmp1(j3.begin()); - json::const_iterator tmp2(j3.cbegin()); - } - { - json::iterator tmp1(j4.begin()); - json::const_iterator tmp2(j4.cbegin()); - } - { - json::iterator tmp1(j5.begin()); - json::const_iterator tmp2(j5.cbegin()); - } - { - json::iterator tmp1(j6.begin()); - json::const_iterator tmp2(j6.cbegin()); - } - { - json::iterator tmp1(j7.begin()); - json::const_iterator tmp2(j7.cbegin()); - } - { - json j_array = {0, 1, 2, 3, 4, 5}; - - json::iterator i1 = j_array.begin(); - ++i1; - json::iterator i2(i1); - json::iterator i3; - i3 = i2; - CHECK(i1 == i1); - - json::const_iterator i4 = j_array.begin(); - ++i4; - json::const_iterator i5(i4); - json::const_iterator i6; - i6 = i5; - CHECK(i4 == i4); - } - { - json j_object = {{"1", 1}, {"2", 2}, {"3", 3}}; - - json::iterator i1 = j_object.begin(); - ++i1; - json::iterator i11 = j_object.begin(); - CHECK((i1 == i11) == false); - json::iterator i2(i1); - json::iterator i3; - i3 = i2; - CHECK(i1 == i1); - - json::const_iterator i4 = j_object.begin(); - ++i4; - json::iterator i41 = j_object.begin(); - CHECK((i4 == i41) == false); - json::const_iterator i5(i4); - json::const_iterator i6; - i6 = i5; - CHECK(i4 == i4); - } - - // iterator copy assignment - { - json::iterator i1 = j2.begin(); - json::const_iterator i2 = j2.cbegin(); - json::iterator i3 = i1; - json::const_iterator i4 = i2; - } - - // operator++ - { - json j; - const json j_const = j; - { - json::iterator i = j.begin(); - ++i; - CHECK(i == j.end()); - ++i; - CHECK(i == j.end()); - } - { - json::const_iterator i = j.begin(); - ++i; - CHECK(i == j.end()); - ++i; - CHECK(i == j.end()); - } - { - json::const_iterator i = j_const.begin(); - ++i; - CHECK(i == j_const.end()); - ++i; - CHECK(i == j_const.end()); - } - { - json::const_iterator i = j.cbegin(); - ++i; - CHECK(i == j.cend()); - ++i; - CHECK(i == j.cend()); - } - { - json::const_iterator i = j_const.cbegin(); - ++i; - CHECK(i == j_const.cend()); - ++i; - CHECK(i == j_const.cend()); - } - } -} - -TEST_CASE("Comparisons") -{ - json j1 = {0, 1, 2, 3, 4}; - json j2 = {{"foo", "bar"}, {"baz", "bam"}}; - json j3 = true; - json j4 = nullptr; - json j5 = 42; - json j6 = 23.42; - json j7 = "hello"; - - CHECK((j1 == j1) == true); - CHECK((j1 == j2) == false); - CHECK((j1 == j3) == false); - CHECK((j1 == j4) == false); - CHECK((j1 == j5) == false); - CHECK((j1 == j6) == false); - CHECK((j1 == j7) == false); - - CHECK((j2 == j1) == false); - CHECK((j2 == j2) == true); - CHECK((j2 == j3) == false); - CHECK((j2 == j4) == false); - CHECK((j2 == j5) == false); - CHECK((j2 == j6) == false); - CHECK((j2 == j7) == false); - - CHECK((j3 == j1) == false); - CHECK((j3 == j2) == false); - CHECK((j3 == j3) == true); - CHECK((j3 == j4) == false); - CHECK((j3 == j5) == false); - CHECK((j3 == j6) == false); - CHECK((j3 == j7) == false); - - CHECK((j4 == j1) == false); - CHECK((j4 == j2) == false); - CHECK((j4 == j3) == false); - CHECK((j4 == j4) == true); - CHECK((j4 == j5) == false); - CHECK((j4 == j6) == false); - CHECK((j4 == j7) == false); - - CHECK((j5 == j1) == false); - CHECK((j5 == j2) == false); - CHECK((j5 == j3) == false); - CHECK((j5 == j4) == false); - CHECK((j5 == j5) == true); - CHECK((j5 == j6) == false); - CHECK((j5 == j7) == false); - - CHECK((j6 == j1) == false); - CHECK((j6 == j2) == false); - CHECK((j6 == j3) == false); - CHECK((j6 == j4) == false); - CHECK((j6 == j5) == false); - CHECK((j6 == j6) == true); - CHECK((j6 == j7) == false); - - CHECK((j7 == j1) == false); - CHECK((j7 == j2) == false); - CHECK((j7 == j3) == false); - CHECK((j7 == j4) == false); - CHECK((j7 == j5) == false); - CHECK((j7 == j6) == false); - CHECK((j7 == j7) == true); - - CHECK((j1 != j1) == false); - CHECK((j1 != j2) == true); - CHECK((j1 != j3) == true); - CHECK((j1 != j4) == true); - CHECK((j1 != j5) == true); - CHECK((j1 != j6) == true); - CHECK((j1 != j7) == true); - - CHECK((j2 != j1) == true); - CHECK((j2 != j2) == false); - CHECK((j2 != j3) == true); - CHECK((j2 != j4) == true); - CHECK((j2 != j5) == true); - CHECK((j2 != j6) == true); - CHECK((j2 != j7) == true); - - CHECK((j3 != j1) == true); - CHECK((j3 != j2) == true); - CHECK((j3 != j3) == false); - CHECK((j3 != j4) == true); - CHECK((j3 != j5) == true); - CHECK((j3 != j6) == true); - CHECK((j3 != j7) == true); - - CHECK((j4 != j1) == true); - CHECK((j4 != j2) == true); - CHECK((j4 != j3) == true); - CHECK((j4 != j4) == false); - CHECK((j4 != j5) == true); - CHECK((j4 != j6) == true); - CHECK((j4 != j7) == true); - - CHECK((j5 != j1) == true); - CHECK((j5 != j2) == true); - CHECK((j5 != j3) == true); - CHECK((j5 != j4) == true); - CHECK((j5 != j5) == false); - CHECK((j5 != j6) == true); - CHECK((j5 != j7) == true); - - CHECK((j6 != j1) == true); - CHECK((j6 != j2) == true); - CHECK((j6 != j3) == true); - CHECK((j6 != j4) == true); - CHECK((j6 != j5) == true); - CHECK((j6 != j6) == false); - CHECK((j6 != j7) == true); - - CHECK((j7 != j1) == true); - CHECK((j7 != j2) == true); - CHECK((j7 != j3) == true); - CHECK((j7 != j4) == true); - CHECK((j7 != j5) == true); - CHECK((j7 != j6) == true); - CHECK((j7 != j7) == false); -} - -TEST_CASE("Parser") -{ - SECTION("null") - { - // accept the exact values - CHECK(json::parse("null") == json(nullptr)); - - // ignore whitespace - CHECK(json::parse(" null ") == json(nullptr)); - CHECK(json::parse("\tnull\n") == json(nullptr)); - - // respect capitalization - CHECK_THROWS_AS(json::parse("Null"), std::invalid_argument); - CHECK_THROWS_AS(json::parse("NULL"), std::invalid_argument); - - // do not accept prefixes - CHECK_THROWS_AS(json::parse("n"), std::invalid_argument); - CHECK_THROWS_AS(json::parse("nu"), std::invalid_argument); - CHECK_THROWS_AS(json::parse("nul"), std::invalid_argument); - } - - SECTION("string") - { - // accept some values - CHECK(json::parse("\"\"") == json("")); - CHECK(json::parse("\"foo\"") == json("foo")); - - // escaping quotes - CHECK_THROWS_AS(json::parse("\"\\\""), std::invalid_argument); - CHECK_NOTHROW(json::parse("\"\\\"\"")); - - // escaping backslashes - CHECK(json::parse("\"a\\\\z\"") == json("a\\z")); - CHECK(json::parse("\"\\\\\"") == json("\\")); - CHECK(json::parse("\"\\\\a\\\\\"") == json("\\a\\")); - CHECK(json::parse("\"\\\\\\\\\"") == json("\\\\")); - - // escaping slash - CHECK(json::parse("\"a\\/z\"") == json("a/z")); - CHECK(json::parse("\"\\/\"") == json("/")); - - // escaping tabs - CHECK(json::parse("\"a\\tz\"") == json("a\tz")); - CHECK(json::parse("\"\\t\"") == json("\t")); - - // escaping formfeed - CHECK(json::parse("\"a\\fz\"") == json("a\fz")); - CHECK(json::parse("\"\\f\"") == json("\f")); - - // escaping carriage return - CHECK(json::parse("\"a\\rz\"") == json("a\rz")); - CHECK(json::parse("\"\\r\"") == json("\r")); - - // escaping backspace - CHECK(json::parse("\"a\\bz\"") == json("a\bz")); - CHECK(json::parse("\"\\b\"") == json("\b")); - - // escaping newline - CHECK(json::parse("\"a\\nz\"") == json("a\nz")); - CHECK(json::parse("\"\\n\"") == json("\n")); - - // escaping senseless stuff - CHECK_THROWS_AS(json::parse("\"\\z\""), std::invalid_argument); - CHECK_THROWS_AS(json::parse("\"\\ \""), std::invalid_argument); - CHECK_THROWS_AS(json::parse("\"\\9\""), std::invalid_argument); - - // quotes must be closed - CHECK_THROWS_AS(json::parse("\""), std::invalid_argument); - } - - SECTION("unicode_escaping") - { - // two tests for uppercase and lowercase hex - - // normal forward slash in ASCII range - CHECK(json::parse("\"\\u002F\"") == json("/")); - CHECK(json::parse("\"\\u002f\"") == json("/")); - // german a umlaut - CHECK(json::parse("\"\\u00E4\"") == json(u8"\u00E4")); - CHECK(json::parse("\"\\u00e4\"") == json(u8"\u00E4")); - // weird d - CHECK(json::parse("\"\\u0111\"") == json(u8"\u0111")); - // unicode arrow left - CHECK(json::parse("\"\\u2190\"") == json(u8"\u2190")); - // pleasing osiris by testing hieroglyph support - CHECK(json::parse("\"\\uD80C\\uDC60\"") == json(u8"\U00013060")); - CHECK(json::parse("\"\\ud80C\\udc60\"") == json(u8"\U00013060")); - - - // no hex numbers behind the \u - CHECK_THROWS_AS(json::parse("\"\\uD80v\""), std::invalid_argument); - CHECK_THROWS_AS(json::parse("\"\\uD80 A\""), std::invalid_argument); - CHECK_THROWS_AS(json::parse("\"\\uD8v\""), std::invalid_argument); - CHECK_THROWS_AS(json::parse("\"\\uDv\""), std::invalid_argument); - CHECK_THROWS_AS(json::parse("\"\\uv\""), std::invalid_argument); - CHECK_THROWS_AS(json::parse("\"\\u\""), std::invalid_argument); - CHECK_THROWS_AS(json::parse("\"\\u\\u\""), std::invalid_argument); - CHECK_THROWS_AS(json::parse("\"a\\uD80vAz\""), std::invalid_argument); - // missing part of a surrogate pair - CHECK_THROWS_AS(json::parse("\"bla \\uD80C bla\""), std::invalid_argument); - CHECK_THROWS_AS(json::parse("\"\\uD80C bla bla\""), std::invalid_argument); - CHECK_THROWS_AS(json::parse("\"bla bla \\uD80C bla bla\""), std::invalid_argument); - // senseless surrogate pair - CHECK_THROWS_AS(json::parse("\"\\uD80C\\uD80C\""), std::invalid_argument); - CHECK_THROWS_AS(json::parse("\"\\uD80C\\u0000\""), std::invalid_argument); - CHECK_THROWS_AS(json::parse("\"\\uD80C\\uFFFF\""), std::invalid_argument); - - // test private code point converter function - CHECK_NOTHROW(json::parser("").codePointToUTF8(0x10FFFE)); - CHECK_NOTHROW(json::parser("").codePointToUTF8(0x10FFFF)); - CHECK_THROWS_AS(json::parser("").codePointToUTF8(0x110000), std::invalid_argument); - CHECK_THROWS_AS(json::parser("").codePointToUTF8(0x110001), std::invalid_argument); - } - - SECTION("boolean") - { - // accept the exact values - CHECK(json::parse("true") == json(true)); - CHECK(json::parse("false") == json(false)); - - // ignore whitespace - CHECK(json::parse(" true ") == json(true)); - CHECK(json::parse("\tfalse\n") == json(false)); - - // respect capitalization - CHECK_THROWS_AS(json::parse("True"), std::invalid_argument); - CHECK_THROWS_AS(json::parse("False"), std::invalid_argument); - - // do not accept prefixes - CHECK_THROWS_AS(json::parse("t"), std::invalid_argument); - CHECK_THROWS_AS(json::parse("tr"), std::invalid_argument); - CHECK_THROWS_AS(json::parse("tru"), std::invalid_argument); - CHECK_THROWS_AS(json::parse("f"), std::invalid_argument); - CHECK_THROWS_AS(json::parse("fa"), std::invalid_argument); - CHECK_THROWS_AS(json::parse("fal"), std::invalid_argument); - CHECK_THROWS_AS(json::parse("fals"), std::invalid_argument); - } - - SECTION("number (int)") - { - // accept the exact values - CHECK(json::parse("0") == json(0)); - CHECK(json::parse("-0") == json(0)); - CHECK(json::parse("1") == json(1)); - CHECK(json::parse("-1") == json(-1)); - CHECK(json::parse("12345678") == json(12345678)); - CHECK(json::parse("-12345678") == json(-12345678)); - - CHECK(json::parse("0.0") == json(0)); - CHECK(json::parse("-0.0") == json(0)); - CHECK(json::parse("1.0") == json(1)); - CHECK(json::parse("-1.0") == json(-1)); - CHECK(json::parse("12345678.0") == json(12345678)); - CHECK(json::parse("-12345678.0") == json(-12345678)); - - CHECK(json::parse("17e0") == json(17)); - CHECK(json::parse("17e1") == json(170)); - CHECK(json::parse("17e3") == json(17000)); - CHECK(json::parse("17e+0") == json(17)); - CHECK(json::parse("17e+1") == json(170)); - CHECK(json::parse("17e+3") == json(17000)); - CHECK(json::parse("17E0") == json(17)); - CHECK(json::parse("17E1") == json(170)); - CHECK(json::parse("17E3") == json(17000)); - CHECK(json::parse("17E+0") == json(17)); - CHECK(json::parse("17E+1") == json(170)); - CHECK(json::parse("17E+3") == json(17000)); - CHECK(json::parse("10000e-0") == json(10000)); - CHECK(json::parse("10000e-1") == json(1000)); - CHECK(json::parse("10000e-4") == json(1)); - CHECK(json::parse("10000E-0") == json(10000)); - CHECK(json::parse("10000E-1") == json(1000)); - CHECK(json::parse("10000E-4") == json(1)); - - CHECK(json::parse("17.0e0") == json(17)); - CHECK(json::parse("17.0e1") == json(170)); - CHECK(json::parse("17.0e3") == json(17000)); - CHECK(json::parse("17.0e+0") == json(17)); - CHECK(json::parse("17.0e+1") == json(170)); - CHECK(json::parse("17.0e+3") == json(17000)); - CHECK(json::parse("17.0E0") == json(17)); - CHECK(json::parse("17.0E1") == json(170)); - CHECK(json::parse("17.0E3") == json(17000)); - CHECK(json::parse("17.0E+0") == json(17)); - CHECK(json::parse("17.0E+1") == json(170)); - CHECK(json::parse("17.0E+3") == json(17000)); - CHECK(json::parse("10000.0e-0") == json(10000)); - CHECK(json::parse("10000.0e-1") == json(1000)); - CHECK(json::parse("10000.0e-4") == json(1)); - CHECK(json::parse("10000.0E-0") == json(10000)); - CHECK(json::parse("10000.0E-1") == json(1000)); - CHECK(json::parse("10000.0E-4") == json(1)); - - // 64 bit integers - CHECK(json::parse("9000000000000000000") == json(9000000000000000000)); - CHECK(json::parse("-9000000000000000000") == json(-9000000000000000000)); - - // trailing zero is not allowed - //CHECK_THROWS_AS(json::parse("01"), std::invalid_argument); - - // whitespace inbetween is an error - //CHECK_THROWS_AS(json::parse("1 0"), std::invalid_argument); - - // only one minus is allowd - CHECK_THROWS_AS(json::parse("--1"), std::invalid_argument); - - // string representations are not allowed - CHECK_THROWS_AS(json::parse("NAN"), std::invalid_argument); - CHECK_THROWS_AS(json::parse("nan"), std::invalid_argument); - CHECK_THROWS_AS(json::parse("INF"), std::invalid_argument); - CHECK_THROWS_AS(json::parse("inf"), std::invalid_argument); - CHECK_THROWS_AS(json::parse("INFINITY"), std::invalid_argument); - CHECK_THROWS_AS(json::parse("infinity"), std::invalid_argument); - } - - SECTION("number (float)") - { - // accept the exact values - CHECK(json::parse("0.5") == json(0.5)); - CHECK(json::parse("-0.5") == json(-0.5)); - CHECK(json::parse("1.5") == json(1.5)); - CHECK(json::parse("-1.5") == json(-1.5)); - CHECK(json::parse("12345678.5") == json(12345678.5)); - CHECK(json::parse("-12345678.5") == json(-12345678.5)); - - CHECK(json::parse("17.5e0") == json(17.5)); - CHECK(json::parse("17.5e1") == json(175)); - CHECK(json::parse("17.5e3") == json(17500)); - CHECK(json::parse("17.5e+0") == json(17.5)); - CHECK(json::parse("17.5e+1") == json(175)); - CHECK(json::parse("17.5e+3") == json(17500)); - CHECK(json::parse("17.5E0") == json(17.5)); - CHECK(json::parse("17.5E1") == json(175)); - CHECK(json::parse("17.5E3") == json(17500)); - CHECK(json::parse("17.5E+0") == json(17.5)); - CHECK(json::parse("17.5E+1") == json(175)); - CHECK(json::parse("17.5E+3") == json(17500)); - CHECK(json::parse("10000.5e-0") == json(10000.5)); - CHECK(json::parse("10000.5e-1") == json(1000.05)); - CHECK(json::parse("10000.5e-4") == json(1.00005)); - CHECK(json::parse("10000.5E-0") == json(10000.5)); - CHECK(json::parse("10000.5E-1") == json(1000.05)); - CHECK(json::parse("10000.5E-4") == json(1.00005)); - } - - SECTION("parse from C++ string") - { - std::string s = "{ \"foo\": [1,2,true] }"; - json j = json::parse(s); - CHECK(j["foo"].size() == 3); - } - - SECTION("parse from stream") - { - std::stringstream s; - s << "{ \"foo\": [1,2,true] }"; - json j; - j << s; - CHECK(j["foo"].size() == 3); - } - - SECTION("user-defined string literal operator") - { - auto j1 = "[1,2,3]"_json; - json j2 = {1, 2, 3}; - CHECK(j1 == j2); - - auto j3 = "{\"key\": \"value\"}"_json; - CHECK(j3["key"] == "value"); - - auto j22 = R"({ - "pi": 3.141, - "happy": true - })"_json; - auto j23 = "{ \"pi\": 3.141, \"happy\": true }"_json; - CHECK(j22 == j23); - } - - SECTION("serialization") - { - auto j23 = "{ \"a\": null, \"b\": true, \"c\": [1,2,3], \"d\": {\"a\": 0} }"_json; - - CHECK(j23.dump() == "{\"a\":null,\"b\":true,\"c\":[1,2,3],\"d\":{\"a\":0}}"); - CHECK(j23.dump(-1) == "{\"a\":null,\"b\":true,\"c\":[1,2,3],\"d\":{\"a\":0}}"); - CHECK(j23.dump(0) == - "{\n\"a\": null,\n\"b\": true,\n\"c\": [\n1,\n2,\n3\n],\n\"d\": {\n\"a\": 0\n}\n}"); - CHECK(j23.dump(4) == - "{\n \"a\": null,\n \"b\": true,\n \"c\": [\n 1,\n 2,\n 3\n ],\n \"d\": {\n \"a\": 0\n }\n}"); - } - - SECTION("Errors") - { - CHECK_THROWS_AS(json::parse(""), std::invalid_argument); - CHECK_THROWS_AS(json::parse(std::string("")), std::invalid_argument); - CHECK_THROWS_AS(json::parse("[1,2"), std::invalid_argument); - } -} diff --git a/test/unit.cpp b/test/unit.cpp new file mode 100644 index 00000000..1ac0fcf4 --- /dev/null +++ b/test/unit.cpp @@ -0,0 +1,2413 @@ +#define CATCH_CONFIG_MAIN +#include "catch.hpp" + +#include "json.hpp" + +#include <sstream> + +using nlohmann::json; + +TEST_CASE() +{ + { + json j = {1, 2, 3, 4}; + std::cerr << j << std::endl; + } + + { + json j = {{}}; + std::cerr << j << std::endl; + } + + { + json j = {{"foo", nullptr}}; + std::cerr << j << std::endl; + } + { + json j = + { + {"pi", 3.141}, + {"happy", true}, + {"name", "Niels"}, + {"nothing", nullptr}, + { + "answer", { + {"everything", 42} + } + }, + {"list", {1, 0, 2}}, + { + "object", { + {"currency", "USD"}, + {"value", 42.99} + } + } + }; + std::cerr << j.dump(4) << std::endl; + j["pi"] = {3, 1, 4, 1}; + std::cerr << j << std::endl; + } + { + // ways to express the empty array [] + json empty_array_implicit = {{}}; + std::cerr << "empty_array_implicit: " << empty_array_implicit << std::endl; + json empty_array_explicit = json::array(); + std::cerr << "empty_array_explicit: " << empty_array_explicit << std::endl; + + // a way to express the empty object {} + json empty_object_explicit = json::object(); + std::cerr << "empty_object_explicit: " << empty_object_explicit << std::endl; + + // 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}) }; + std::cerr << "array_not_object: " << array_not_object << std::endl; + } +} + +TEST_CASE("null") +{ + SECTION("constructors") + { + SECTION("no arguments") + { + { + json j; + CHECK(j.m_type == json::value_t::null); + } + { + json j{}; + CHECK(j.m_type == json::value_t::null); + } + } + + SECTION("nullptr_t argument") + { + { + json j(nullptr); + CHECK(j.m_type == json::value_t::null); + } + } + + SECTION("value_t::null argument") + { + { + json j(json::value_t::null); + CHECK(j.m_type == json::value_t::null); + } + } + + SECTION("copy constructor") + { + { + json other; + json j(other); + CHECK(j.m_type == json::value_t::null); + } + { + json j = nullptr; + CHECK(j.m_type == json::value_t::null); + } + } + + SECTION("move constructor") + { + { + json other; + json j(std::move(other)); + CHECK(j.m_type == json::value_t::null); + CHECK(other.m_type == json::value_t::null); + } + } + + SECTION("copy assignment") + { + { + json other; + json j = other; + CHECK(j.m_type == json::value_t::null); + } + } + } + + SECTION("object inspection") + { + json j; + + SECTION("dump()") + { + CHECK(j.dump() == "null"); + CHECK(j.dump(-1) == "null"); + CHECK(j.dump(4) == "null"); + } + + SECTION("type()") + { + CHECK(j.type() == j.m_type); + } + + SECTION("operator value_t()") + { + json::value_t t = j; + CHECK(t == j.m_type); + } + } + + SECTION("value conversion") + { + json j; + + SECTION("get()/operator() for objects") + { + CHECK_THROWS_AS(auto o = j.get<json::object_t>(), std::logic_error); + CHECK_THROWS_AS(json::object_t o = j, std::logic_error); + } + + SECTION("get()/operator() for arrays") + { + CHECK_THROWS_AS(auto o = j.get<json::array_t>(), std::logic_error); + CHECK_THROWS_AS(json::array_t o = j, std::logic_error); + } + + SECTION("get()/operator() for strings") + { + CHECK_THROWS_AS(auto o = j.get<json::string_t>(), std::logic_error); + CHECK_THROWS_AS(json::string_t o = j, std::logic_error); + } + + SECTION("get()/operator() for booleans") + { + CHECK_THROWS_AS(auto o = j.get<json::boolean_t>(), std::logic_error); + CHECK_THROWS_AS(json::boolean_t o = j, std::logic_error); + } + + SECTION("get()/operator() for integer numbers") + { + CHECK_THROWS_AS(auto o = j.get<json::number_integer_t>(), std::logic_error); + CHECK_THROWS_AS(json::number_integer_t o = j, std::logic_error); + } + + SECTION("get()/operator() for floating point numbers") + { + CHECK_THROWS_AS(auto o = j.get<json::number_float_t>(), std::logic_error); + CHECK_THROWS_AS(json::number_float_t o = j, std::logic_error); + } + } + + SECTION("element access") + { + json j; + const json jc; + + SECTION("operator[size_type]") + { + CHECK_THROWS_AS(auto o = j[0], std::runtime_error); + CHECK_THROWS_AS(auto o = jc[0], std::runtime_error); + } + + SECTION("at(size_type)") + { + CHECK_THROWS_AS(auto o = j.at(0), std::runtime_error); + CHECK_THROWS_AS(auto o = jc.at(0), std::runtime_error); + } + + SECTION("operator[object_t::key_type]") + { + CHECK_THROWS_AS(j["key"], std::runtime_error); + CHECK_THROWS_AS(j[std::string("key")], std::runtime_error); + } + + SECTION("at(object_t::key_type)") + { + CHECK_THROWS_AS(auto o = j.at("key"), std::runtime_error); + CHECK_THROWS_AS(auto o = j.at(std::string("key")), std::runtime_error); + CHECK_THROWS_AS(auto o = jc.at("key"), std::runtime_error); + CHECK_THROWS_AS(auto o = jc.at(std::string("key")), std::runtime_error); + } + } + + SECTION("iterators") + { + json j; + const json jc; + + SECTION("begin()") + { + { + json::iterator it = j.begin(); + CHECK_THROWS_AS(*it, std::out_of_range); + } + { + json::const_iterator it = jc.begin(); + CHECK_THROWS_AS(*it, std::out_of_range); + } + } + + SECTION("cbegin()") + { + { + json::const_iterator it = j.cbegin(); + CHECK_THROWS_AS(*it, std::out_of_range); + } + { + json::const_iterator it = jc.cbegin(); + CHECK_THROWS_AS(*it, std::out_of_range); + } + { + // check semantics definition of cbegin() + CHECK(const_cast<json::const_reference>(j).begin() == j.cbegin()); + CHECK(const_cast<json::const_reference>(jc).begin() == jc.cbegin()); + } + } + + SECTION("end()") + { + { + json::iterator it = j.end(); + CHECK_THROWS_AS(*it, std::out_of_range); + } + { + json::const_iterator it = jc.end(); + CHECK_THROWS_AS(*it, std::out_of_range); + } + } + + SECTION("cend()") + { + { + json::const_iterator it = j.cend(); + CHECK_THROWS_AS(*it, std::out_of_range); + } + { + json::const_iterator it = jc.cend(); + CHECK_THROWS_AS(*it, std::out_of_range); + } + { + // check semantics definition of cend() + CHECK(const_cast<json::const_reference>(j).end() == j.cend()); + CHECK(const_cast<json::const_reference>(jc).end() == jc.cend()); + } + } + } + + SECTION("capacity") + { + json j; + const json jc; + + SECTION("empty()") + { + // null values are empty + CHECK(j.empty()); + CHECK(jc.empty()); + + // check semantics definition of empty() + CHECK(j.begin() == j.end()); + CHECK(j.cbegin() == j.cend()); + } + + SECTION("size()") + { + // null values have size 0 + CHECK(j.size() == 0); + CHECK(jc.size() == 0); + + // check semantics definition of size() + CHECK(std::distance(j.begin(), j.end()) == 0); + CHECK(std::distance(j.cbegin(), j.cend()) == 0); + } + + SECTION("max_size()") + { + // null values have max_size 0 + CHECK(j.max_size() == 0); + CHECK(jc.max_size() == 0); + } + } + + SECTION("modifiers") + { + json j; + + SECTION("clear()") + { + j.clear(); + CHECK(j.empty()); + } + + SECTION("push_back") + { + SECTION("const json&") + { + const json v; + j.push_back(v); + CHECK(j.type() == json::value_t::array); + CHECK(j.empty() == false); + CHECK(j.size() == 1); + CHECK(j.max_size() >= 1); + } + + SECTION("json&&") + { + j.push_back(nullptr); + CHECK(j.type() == json::value_t::array); + CHECK(j.empty() == false); + CHECK(j.size() == 1); + CHECK(j.max_size() >= 1); + } + + SECTION("object_t::value_type&") + { + json::object_t::value_type v { "foo", nullptr }; + j.push_back(v); + CHECK(j.type() == json::value_t::object); + CHECK(j.empty() == false); + CHECK(j.size() == 1); + CHECK(j.max_size() >= 1); + } + } + + SECTION("emplace_back") + { + j.emplace_back(nullptr); + CHECK(j.type() == json::value_t::array); + CHECK(j.empty() == false); + CHECK(j.size() == 1); + CHECK(j.max_size() >= 1); + } + + /* + SECTION("operator+=") + { + SECTION("const json&") + { + const json v; + j += v; + CHECK(j.type() == json::value_t::array); + CHECK(j.empty() == false); + CHECK(j.size() == 1); + CHECK(j.max_size() >= 1); + } + + SECTION("json&&") + { + j += nullptr; + CHECK(j.type() == json::value_t::array); + CHECK(j.empty() == false); + CHECK(j.size() == 1); + CHECK(j.max_size() >= 1); + } + + SECTION("object_t::value_type&") + { + json::object_t::value_type v { "foo", nullptr }; + j += v; + CHECK(j.type() == json::value_t::object); + CHECK(j.empty() == false); + CHECK(j.size() == 1); + CHECK(j.max_size() >= 1); + } + } + */ + + SECTION("swap") + { + SECTION("array_t&") + { + json::array_t other = {nullptr, nullptr, nullptr}; + CHECK_THROWS_AS(j.swap(other), std::runtime_error); + } + + SECTION("object_t&") + { + json::object_t other = {{"key1", nullptr}, {"key2", nullptr}}; + CHECK_THROWS_AS(j.swap(other), std::runtime_error); + } + + SECTION("string_t&") + { + json::string_t other = "string"; + CHECK_THROWS_AS(j.swap(other), std::runtime_error); + } + } + } + + SECTION("lexicographical comparison operators") + { + json j1, j2; + + CHECK(j1 == j2); + CHECK(not(j1 != j2)); + CHECK(not(j1 < j2)); + CHECK(j1 <= j2); + CHECK(not(j1 > j2)); + CHECK(j1 >= j2); + } + + SECTION("serialization") + { + json j; + + SECTION("operator<<") + { + std::stringstream s; + s << j; + CHECK(s.str() == "null"); + } + + SECTION("operator>>") + { + std::stringstream s; + j >> s; + CHECK(s.str() == "null"); + } + } + + SECTION("convenience functions") + { + json j; + + SECTION("type_name") + { + CHECK(j.type_name() == "null"); + } + } + + SECTION("nonmember functions") + { + json j1, j2; + + SECTION("swap") + { + std::swap(j1, j2); + } + + SECTION("hash") + { + std::hash<json> hash_fn; + auto h1 = hash_fn(j1); + auto h2 = hash_fn(j2); + CHECK(h1 == h2); + } + } +} + +TEST_CASE("boolean") +{ + SECTION("constructors") + { + SECTION("booleant_t argument") + { + { + json j(true); + CHECK(j.m_type == json::value_t::boolean); + CHECK(j.m_value.boolean == true); + } + { + json j(false); + CHECK(j.m_type == json::value_t::boolean); + CHECK(j.m_value.boolean == false); + } + } + + SECTION("value_t::boolean argument") + { + { + json j(json::value_t::boolean); + CHECK(j.m_type == json::value_t::boolean); + CHECK(j.m_value.boolean == false); + } + } + + SECTION("copy constructor") + { + { + json other(true); + json j(other); + CHECK(j.m_type == json::value_t::boolean); + CHECK(j.m_value.boolean == true); + } + { + json other(false); + json j(other); + CHECK(j.m_type == json::value_t::boolean); + CHECK(j.m_value.boolean == false); + } + { + json j = true; + CHECK(j.m_type == json::value_t::boolean); + CHECK(j.m_value.boolean == true); + } + { + json j = false; + CHECK(j.m_type == json::value_t::boolean); + CHECK(j.m_value.boolean == false); + } + } + + SECTION("move constructor") + { + { + json other = true; + json j(std::move(other)); + CHECK(j.m_type == json::value_t::boolean); + CHECK(j.m_value.boolean == true); + CHECK(other.m_type == json::value_t::null); + } + } + + SECTION("copy assignment") + { + { + json other = true; + json j = other; + CHECK(j.m_type == json::value_t::boolean); + CHECK(j.m_value.boolean == true); + } + { + json other = false; + json j = other; + CHECK(j.m_type == json::value_t::boolean); + CHECK(j.m_value.boolean == false); + } + } + } + + SECTION("object inspection") + { + json jt = true; + json jf = false; + + SECTION("dump()") + { + CHECK(jt.dump() == "true"); + CHECK(jt.dump(-1) == "true"); + CHECK(jt.dump(4) == "true"); + CHECK(jf.dump() == "false"); + CHECK(jf.dump(-1) == "false"); + CHECK(jf.dump(4) == "false"); + } + + SECTION("type()") + { + CHECK(jt.type() == jt.m_type); + CHECK(jf.type() == jf.m_type); + } + + SECTION("operator value_t()") + { + { + json::value_t t = jt; + CHECK(t == jt.m_type); + } + { + json::value_t t = jf; + CHECK(t == jf.m_type); + } + } + } + + SECTION("value conversion") + { + json j = true; + + SECTION("get()/operator() for objects") + { + CHECK_THROWS_AS(auto o = j.get<json::object_t>(), std::logic_error); + CHECK_THROWS_AS(json::object_t o = j, std::logic_error); + } + + SECTION("get()/operator() for arrays") + { + CHECK_THROWS_AS(auto o = j.get<json::array_t>(), std::logic_error); + CHECK_THROWS_AS(json::array_t o = j, std::logic_error); + } + + SECTION("get()/operator() for strings") + { + CHECK_THROWS_AS(auto o = j.get<json::string_t>(), std::logic_error); + CHECK_THROWS_AS(json::string_t o = j, std::logic_error); + } + + SECTION("get()/operator() for booleans") + { + { + auto o = j.get<json::boolean_t>(); + CHECK(o == true); + } + { + json::boolean_t o = j; + CHECK(o == true); + } + } + + SECTION("get()/operator() for integer numbers") + { + CHECK_THROWS_AS(auto o = j.get<json::number_integer_t>(), std::logic_error); + CHECK_THROWS_AS(json::number_integer_t o = j, std::logic_error); + } + + SECTION("get()/operator() for floating point numbers") + { + CHECK_THROWS_AS(auto o = j.get<json::number_float_t>(), std::logic_error); + CHECK_THROWS_AS(json::number_float_t o = j, std::logic_error); + } + } + + SECTION("element access") + { + json j = true; + const json jc = false; + + SECTION("operator[size_type]") + { + CHECK_THROWS_AS(auto o = j[0], std::runtime_error); + CHECK_THROWS_AS(auto o = jc[0], std::runtime_error); + } + + SECTION("at(size_type)") + { + CHECK_THROWS_AS(auto o = j.at(0), std::runtime_error); + CHECK_THROWS_AS(auto o = jc.at(0), std::runtime_error); + } + + SECTION("operator[object_t::key_type]") + { + CHECK_THROWS_AS(j["key"], std::runtime_error); + CHECK_THROWS_AS(j[std::string("key")], std::runtime_error); + } + + SECTION("at(object_t::key_type)") + { + CHECK_THROWS_AS(auto o = j.at("key"), std::runtime_error); + CHECK_THROWS_AS(auto o = j.at(std::string("key")), std::runtime_error); + CHECK_THROWS_AS(auto o = jc.at("key"), std::runtime_error); + CHECK_THROWS_AS(auto o = jc.at(std::string("key")), std::runtime_error); + } + } + + SECTION("iterators") + { + json j = true; + const json jc = false; + + SECTION("begin()") + { + { + json::iterator it = j.begin(); + CHECK(*it == j); + } + { + json::const_iterator it = jc.begin(); + CHECK(*it == jc); + } + } + + SECTION("cbegin()") + { + { + json::const_iterator it = j.cbegin(); + CHECK(*it == j); + } + { + json::const_iterator it = jc.cbegin(); + CHECK(*it == jc); + } + { + // check semantics definition of cbegin() + CHECK(const_cast<json::const_reference>(j).begin() == j.cbegin()); + CHECK(const_cast<json::const_reference>(jc).begin() == jc.cbegin()); + } + } + + SECTION("end()") + { + { + json::iterator it = j.end(); + CHECK_THROWS_AS(*it, std::out_of_range); + } + { + json::const_iterator it = jc.end(); + CHECK_THROWS_AS(*it, std::out_of_range); + } + } + + SECTION("cend()") + { + { + json::const_iterator it = j.cend(); + CHECK_THROWS_AS(*it, std::out_of_range); + } + { + json::const_iterator it = jc.cend(); + CHECK_THROWS_AS(*it, std::out_of_range); + } + { + // check semantics definition of cend() + CHECK(const_cast<json::const_reference>(j).end() == j.cend()); + CHECK(const_cast<json::const_reference>(jc).end() == jc.cend()); + } + } + } + + SECTION("capacity") + { + json j = true; + const json jc = false; + + SECTION("empty()") + { + // null values are empty + CHECK(not j.empty()); + CHECK(not jc.empty()); + + // check semantics definition of empty() + CHECK(j.begin() != j.end()); + CHECK(j.cbegin() != j.cend()); + } + + SECTION("size()") + { + // boolean values have size 1 + CHECK(j.size() == 1); + CHECK(jc.size() == 1); + + // check semantics definition of size() + CHECK(std::distance(j.begin(), j.end()) == 1); + CHECK(std::distance(j.cbegin(), j.cend()) == 1); + } + + SECTION("max_size()") + { + // null values have max_size 0 + CHECK(j.max_size() == 1); + CHECK(jc.max_size() == 1); + } + } + + SECTION("modifiers") + { + json j = true; + + SECTION("clear()") + { + j.clear(); + CHECK(not j.empty()); + CHECK(j.m_value.boolean == false); + } + + SECTION("push_back") + { + SECTION("const json&") + { + const json v = true; + CHECK_THROWS_AS(j.push_back(v), std::runtime_error); + } + + SECTION("json&&") + { + CHECK_THROWS_AS(j.push_back(false), std::runtime_error); + } + + SECTION("object_t::value_type&") + { + json::object_t::value_type v { "foo", true }; + CHECK_THROWS_AS(j.push_back(v), std::runtime_error); + } + } + + SECTION("emplace_back") + { + CHECK_THROWS_AS(j.emplace_back(true), std::runtime_error); + } + + /* + SECTION("operator+=") + { + SECTION("const json&") + { + const json v = true; + CHECK_THROWS_AS(j += v, std::runtime_error); + } + + SECTION("json&&") + { + CHECK_THROWS_AS(j += true, std::runtime_error); + } + + SECTION("object_t::value_type&") + { + json::object_t::value_type v { "foo", true }; + CHECK_THROWS_AS(j += v, std::runtime_error); + } + } + */ + + SECTION("swap") + { + SECTION("array_t&") + { + json::array_t other = {true, false}; + CHECK_THROWS_AS(j.swap(other), std::runtime_error); + } + + SECTION("object_t&") + { + json::object_t other = {{"key1", true}, {"key2", false}}; + CHECK_THROWS_AS(j.swap(other), std::runtime_error); + } + + SECTION("string_t&") + { + json::string_t other = "string"; + CHECK_THROWS_AS(j.swap(other), std::runtime_error); + } + } + } + + SECTION("lexicographical comparison operators") + { + json j1 = true; + json j2 = false; + + CHECK(j1 == j1); + CHECK(not(j1 != j1)); + CHECK(not(j1 < j1)); + CHECK(j1 <= j1); + CHECK(not(j1 > j1)); + CHECK(j1 >= j1); + + CHECK(j2 == j2); + CHECK(not(j2 != j2)); + CHECK(not(j2 < j2)); + CHECK(j2 <= j2); + CHECK(not(j2 > j2)); + CHECK(j2 >= j2); + + CHECK(not(j1 == j2)); + CHECK(j1 != j2); + CHECK(not(j1 < j2)); + CHECK(not(j1 <= j2)); + CHECK(j1 > j2); + CHECK(j1 >= j2); + } + + SECTION("serialization") + { + json j1 = true; + json j2 = false; + + SECTION("operator<<") + { + std::stringstream s; + s << j1 << " " << j2; + CHECK(s.str() == "true false"); + } + + SECTION("operator>>") + { + std::stringstream s; + j1 >> s; + j2 >> s; + CHECK(s.str() == "truefalse"); + } + } + + SECTION("convenience functions") + { + json j = true; + + SECTION("type_name") + { + CHECK(j.type_name() == "boolean"); + } + } + + SECTION("nonmember functions") + { + json j1 = true; + json j2 = false; + + SECTION("swap") + { + std::swap(j1, j2); + CHECK(j1 == json(false)); + CHECK(j2 == json(true)); + } + + SECTION("hash") + { + std::hash<json> hash_fn; + auto h1 = hash_fn(j1); + auto h2 = hash_fn(j2); + CHECK(h1 != h2); + } + } +} + +TEST_CASE("number (integer)") +{ + SECTION("constructors") + { + SECTION("number_integer_t argument") + { + { + json j(17); + CHECK(j.m_type == json::value_t::number_integer); + CHECK(j.m_value.number_integer == 17); + } + { + json j(0); + CHECK(j.m_type == json::value_t::number_integer); + CHECK(j.m_value.number_integer == 0); + } + { + json j(-42); + CHECK(j.m_type == json::value_t::number_integer); + CHECK(j.m_value.number_integer == -42); + } + } + + SECTION("integer type argument") + { + { + int8_t v = -128; + json j(v); + CHECK(j.m_type == json::value_t::number_integer); + CHECK(j.m_value.number_integer == v); + } + { + uint8_t v = 255; + json j(v); + CHECK(j.m_type == json::value_t::number_integer); + CHECK(j.m_value.number_integer == v); + } + { + int16_t v = -32768; + json j(v); + CHECK(j.m_type == json::value_t::number_integer); + CHECK(j.m_value.number_integer == v); + } + { + uint16_t v = 65535; + json j(v); + CHECK(j.m_type == json::value_t::number_integer); + CHECK(j.m_value.number_integer == v); + } + { + int32_t v = -2147483648; + json j(v); + CHECK(j.m_type == json::value_t::number_integer); + CHECK(j.m_value.number_integer == v); + } + { + uint32_t v = 4294967295; + json j(v); + CHECK(j.m_type == json::value_t::number_integer); + CHECK(j.m_value.number_integer == v); + } + { + int64_t v = INT64_MIN; + json j(v); + CHECK(j.m_type == json::value_t::number_integer); + CHECK(j.m_value.number_integer == v); + } + { + int64_t v = INT64_MAX; + json j(v); + CHECK(j.m_type == json::value_t::number_integer); + CHECK(j.m_value.number_integer == v); + } + } + + SECTION("value_t::number_integer argument") + { + { + json j(json::value_t::number_integer); + CHECK(j.m_type == json::value_t::number_integer); + CHECK(j.m_value.number_integer == 0); + } + } + + SECTION("copy constructor") + { + { + json other(117); + json j(other); + CHECK(j.m_type == json::value_t::number_integer); + CHECK(j.m_value.number_integer == 117); + } + { + json other(-49); + json j(other); + CHECK(j.m_type == json::value_t::number_integer); + CHECK(j.m_value.number_integer == -49); + } + { + json j = 110; + CHECK(j.m_type == json::value_t::number_integer); + CHECK(j.m_value.number_integer == 110); + } + { + json j = 112; + CHECK(j.m_type == json::value_t::number_integer); + CHECK(j.m_value.number_integer == 112); + } + } + + SECTION("move constructor") + { + { + json other = 7653434; + json j(std::move(other)); + CHECK(j.m_type == json::value_t::number_integer); + CHECK(j.m_value.number_integer == 7653434); + CHECK(other.m_type == json::value_t::null); + } + } + + SECTION("copy assignment") + { + { + json other = 333; + json j = other; + CHECK(j.m_type == json::value_t::number_integer); + CHECK(j.m_value.number_integer == 333); + } + { + json other = 555; + json j = other; + CHECK(j.m_type == json::value_t::number_integer); + CHECK(j.m_value.number_integer == 555); + } + } + } + + SECTION("object inspection") + { + json jp = 4294967295; + json jn = -4294967295; + + SECTION("dump()") + { + CHECK(jp.dump() == "4294967295"); + CHECK(jn.dump(-1) == "-4294967295"); + CHECK(jp.dump(4) == "4294967295"); + CHECK(jn.dump() == "-4294967295"); + CHECK(jp.dump(-1) == "4294967295"); + CHECK(jn.dump(4) == "-4294967295"); + } + + SECTION("type()") + { + CHECK(jp.type() == jp.m_type); + CHECK(jn.type() == jn.m_type); + } + + SECTION("operator value_t()") + { + { + json::value_t t = jp; + CHECK(t == jp.m_type); + } + { + json::value_t t = jn; + CHECK(t == jn.m_type); + } + } + } + + SECTION("value conversion") + { + json j = 1003; + + SECTION("get()/operator() for objects") + { + CHECK_THROWS_AS(auto o = j.get<json::object_t>(), std::logic_error); + CHECK_THROWS_AS(json::object_t o = j, std::logic_error); + } + + SECTION("get()/operator() for arrays") + { + CHECK_THROWS_AS(auto o = j.get<json::array_t>(), std::logic_error); + CHECK_THROWS_AS(json::array_t o = j, std::logic_error); + } + + SECTION("get()/operator() for strings") + { + CHECK_THROWS_AS(auto o = j.get<json::string_t>(), std::logic_error); + CHECK_THROWS_AS(json::string_t o = j, std::logic_error); + } + + SECTION("get()/operator() for booleans") + { + CHECK_THROWS_AS(auto o = j.get<json::boolean_t>(), std::logic_error); + CHECK_THROWS_AS(json::boolean_t o = j, std::logic_error); + } + + SECTION("get()/operator() for integer numbers") + { + { + auto o = j.get<json::number_integer_t>(); + CHECK(o == 1003); + } + { + json::number_integer_t o = j; + CHECK(o == 1003); + } + } + + SECTION("get()/operator() for floating point numbers") + { + { + auto o = j.get<json::number_float_t>(); + CHECK(o == 1003); + } + { + json::number_float_t o = j; + CHECK(o == 1003); + } + } + } + + SECTION("element access") + { + json j = 119; + const json jc = -65433; + + SECTION("operator[size_type]") + { + CHECK_THROWS_AS(auto o = j[0], std::runtime_error); + CHECK_THROWS_AS(auto o = jc[0], std::runtime_error); + } + + SECTION("at(size_type)") + { + CHECK_THROWS_AS(auto o = j.at(0), std::runtime_error); + CHECK_THROWS_AS(auto o = jc.at(0), std::runtime_error); + } + + SECTION("operator[object_t::key_type]") + { + CHECK_THROWS_AS(j["key"], std::runtime_error); + CHECK_THROWS_AS(j[std::string("key")], std::runtime_error); + } + + SECTION("at(object_t::key_type)") + { + CHECK_THROWS_AS(auto o = j.at("key"), std::runtime_error); + CHECK_THROWS_AS(auto o = j.at(std::string("key")), std::runtime_error); + CHECK_THROWS_AS(auto o = jc.at("key"), std::runtime_error); + CHECK_THROWS_AS(auto o = jc.at(std::string("key")), std::runtime_error); + } + } + + SECTION("iterators") + { + json j = 0; + const json jc = 666; + + SECTION("begin()") + { + { + json::iterator it = j.begin(); + CHECK(*it == j); + } + { + json::const_iterator it = jc.begin(); + CHECK(*it == jc); + } + } + + SECTION("cbegin()") + { + { + json::const_iterator it = j.cbegin(); + CHECK(*it == j); + } + { + json::const_iterator it = jc.cbegin(); + CHECK(*it == jc); + } + { + // check semantics definition of cbegin() + CHECK(const_cast<json::const_reference>(j).begin() == j.cbegin()); + CHECK(const_cast<json::const_reference>(jc).begin() == jc.cbegin()); + } + } + + SECTION("end()") + { + { + json::iterator it = j.end(); + CHECK_THROWS_AS(*it, std::out_of_range); + } + { + json::const_iterator it = jc.end(); + CHECK_THROWS_AS(*it, std::out_of_range); + } + } + + SECTION("cend()") + { + { + json::const_iterator it = j.cend(); + CHECK_THROWS_AS(*it, std::out_of_range); + } + { + json::const_iterator it = jc.cend(); + CHECK_THROWS_AS(*it, std::out_of_range); + } + { + // check semantics definition of cend() + CHECK(const_cast<json::const_reference>(j).end() == j.cend()); + CHECK(const_cast<json::const_reference>(jc).end() == jc.cend()); + } + } + } + + SECTION("capacity") + { + json j = 4344; + const json jc = -255; + + SECTION("empty()") + { + // null values are empty + CHECK(not j.empty()); + CHECK(not jc.empty()); + + // check semantics definition of empty() + CHECK(j.begin() != j.end()); + CHECK(j.cbegin() != j.cend()); + } + + SECTION("size()") + { + // number values have size 1 + CHECK(j.size() == 1); + CHECK(jc.size() == 1); + + // check semantics definition of size() + CHECK(std::distance(j.begin(), j.end()) == 1); + CHECK(std::distance(j.cbegin(), j.cend()) == 1); + } + + SECTION("max_size()") + { + // null values have max_size 0 + CHECK(j.max_size() == 1); + CHECK(jc.max_size() == 1); + } + } + + SECTION("modifiers") + { + json j = 1119; + + SECTION("clear()") + { + j.clear(); + CHECK(not j.empty()); + CHECK(j.m_value.number_integer == 0); + } + + SECTION("push_back") + { + SECTION("const json&") + { + const json v = 6; + CHECK_THROWS_AS(j.push_back(v), std::runtime_error); + } + + SECTION("json&&") + { + CHECK_THROWS_AS(j.push_back(56), std::runtime_error); + } + + SECTION("object_t::value_type&") + { + json::object_t::value_type v { "foo", 12 }; + CHECK_THROWS_AS(j.push_back(v), std::runtime_error); + } + } + + SECTION("emplace_back") + { + CHECK_THROWS_AS(j.emplace_back(-42), std::runtime_error); + } + + /* + SECTION("operator+=") + { + SECTION("const json&") + { + const json v = 8; + CHECK_THROWS_AS(j += v, std::runtime_error); + } + + SECTION("json&&") + { + CHECK_THROWS_AS(j += 0, std::runtime_error); + } + + SECTION("object_t::value_type&") + { + json::object_t::value_type v { "foo", 42 }; + CHECK_THROWS_AS(j += v, std::runtime_error); + } + } + */ + + SECTION("swap") + { + SECTION("array_t&") + { + json::array_t other = {11, 2}; + CHECK_THROWS_AS(j.swap(other), std::runtime_error); + } + + SECTION("object_t&") + { + json::object_t other = {{"key1", 4}, {"key2", 33}}; + CHECK_THROWS_AS(j.swap(other), std::runtime_error); + } + + SECTION("string_t&") + { + json::string_t other = "string"; + CHECK_THROWS_AS(j.swap(other), std::runtime_error); + } + } + } + + SECTION("lexicographical comparison operators") + { + json j1 = -100; + json j2 = 100; + + CHECK(j1 == j1); + CHECK(not(j1 != j1)); + CHECK(not(j1 < j1)); + CHECK(j1 <= j1); + CHECK(not(j1 > j1)); + CHECK(j1 >= j1); + + CHECK(j2 == j2); + CHECK(not(j2 != j2)); + CHECK(not(j2 < j2)); + CHECK(j2 <= j2); + CHECK(not(j2 > j2)); + CHECK(j2 >= j2); + + CHECK(not(j1 == j2)); + CHECK(j1 != j2); + CHECK(j1 < j2); + CHECK(j1 <= j2); + CHECK(not(j1 > j2)); + CHECK(not(j1 >= j2)); + } + + SECTION("serialization") + { + json j1 = 42; + json j2 = 66; + + SECTION("operator<<") + { + std::stringstream s; + s << j1 << " " << j2; + CHECK(s.str() == "42 66"); + } + + SECTION("operator>>") + { + std::stringstream s; + j1 >> s; + j2 >> s; + CHECK(s.str() == "4266"); + } + } + + SECTION("convenience functions") + { + json j = 2354; + + SECTION("type_name") + { + CHECK(j.type_name() == "number"); + } + } + + SECTION("nonmember functions") + { + json j1 = 23; + json j2 = 32; + + SECTION("swap") + { + std::swap(j1, j2); + CHECK(j1 == json(32)); + CHECK(j2 == json(23)); + } + + SECTION("hash") + { + std::hash<json> hash_fn; + auto h1 = hash_fn(j1); + auto h2 = hash_fn(j2); + CHECK(h1 != h2); + } + } +} + +TEST_CASE("number (floating point)") +{ + SECTION("constructors") + { + SECTION("number_float_t argument") + { + { + json j(17.23); + CHECK(j.m_type == json::value_t::number_float); + CHECK(j.m_value.number_float == 17.23); + } + { + json j(0.0); + CHECK(j.m_type == json::value_t::number_float); + CHECK(j.m_value.number_float == 0.0); + } + { + json j(-42.1211); + CHECK(j.m_type == json::value_t::number_float); + CHECK(j.m_value.number_float == -42.1211); + } + } + + SECTION("floating type argument") + { + { + float v = 3.14159265359; + json j(v); + CHECK(j.m_type == json::value_t::number_float); + CHECK(j.m_value.number_float == v); + } + { + double v = 2.71828182846; + json j(v); + CHECK(j.m_type == json::value_t::number_float); + CHECK(j.m_value.number_float == v); + } + { + long double v = 1.57079632679; + json j(v); + CHECK(j.m_type == json::value_t::number_float); + CHECK(j.m_value.number_float == v); + } + } + + SECTION("value_t::number_float argument") + { + { + json j(json::value_t::number_float); + CHECK(j.m_type == json::value_t::number_float); + CHECK(j.m_value.number_float == 0.0); + } + } + + SECTION("copy constructor") + { + { + json other(117.1); + json j(other); + CHECK(j.m_type == json::value_t::number_float); + CHECK(j.m_value.number_float == 117.1); + } + { + json other(-49.00); + json j(other); + CHECK(j.m_type == json::value_t::number_float); + CHECK(j.m_value.number_float == -49.00); + } + { + json j = 110.22; + CHECK(j.m_type == json::value_t::number_float); + CHECK(j.m_value.number_float == 110.22); + } + { + json j = 112.5; + CHECK(j.m_type == json::value_t::number_float); + CHECK(j.m_value.number_float == 112.5); + } + } + + SECTION("move constructor") + { + { + json other = 7653434.99999; + json j(std::move(other)); + CHECK(j.m_type == json::value_t::number_float); + CHECK(j.m_value.number_float == 7653434.99999); + CHECK(other.m_type == json::value_t::null); + } + } + + SECTION("copy assignment") + { + { + json other = 333.444; + json j = other; + CHECK(j.m_type == json::value_t::number_float); + CHECK(j.m_value.number_float == 333.444); + } + { + json other = 555.333; + json j = other; + CHECK(j.m_type == json::value_t::number_float); + CHECK(j.m_value.number_float == 555.333); + } + } + } + + SECTION("object inspection") + { + json jp = 4294967295.333; + json jn = -4294967249.222; + + SECTION("dump()") + { + CHECK(jp.dump() == "4294967295.333000"); + CHECK(jn.dump(-1) == "-4294967249.222000"); + CHECK(jp.dump(4) == "4294967295.333000"); + CHECK(jn.dump() == "-4294967249.222000"); + CHECK(jp.dump(-1) == "4294967295.333000"); + CHECK(jn.dump(4) == "-4294967249.222000"); + } + + SECTION("type()") + { + CHECK(jp.type() == jp.m_type); + CHECK(jn.type() == jn.m_type); + } + + SECTION("operator value_t()") + { + { + json::value_t t = jp; + CHECK(t == jp.m_type); + } + { + json::value_t t = jn; + CHECK(t == jn.m_type); + } + } + } + + SECTION("value conversion") + { + json j = 10203.444344; + + SECTION("get()/operator() for objects") + { + CHECK_THROWS_AS(auto o = j.get<json::object_t>(), std::logic_error); + CHECK_THROWS_AS(json::object_t o = j, std::logic_error); + } + + SECTION("get()/operator() for arrays") + { + CHECK_THROWS_AS(auto o = j.get<json::array_t>(), std::logic_error); + CHECK_THROWS_AS(json::array_t o = j, std::logic_error); + } + + SECTION("get()/operator() for strings") + { + CHECK_THROWS_AS(auto o = j.get<json::string_t>(), std::logic_error); + CHECK_THROWS_AS(json::string_t o = j, std::logic_error); + } + + SECTION("get()/operator() for booleans") + { + CHECK_THROWS_AS(auto o = j.get<json::boolean_t>(), std::logic_error); + CHECK_THROWS_AS(json::boolean_t o = j, std::logic_error); + } + + SECTION("get()/operator() for integer numbers") + { + { + auto o = j.get<json::number_integer_t>(); + CHECK(o == 10203); + } + { + json::number_integer_t o = j; + CHECK(o == 10203); + } + } + + SECTION("get()/operator() for floating point numbers") + { + { + auto o = j.get<json::number_float_t>(); + CHECK(o == 10203.444344); + } + { + json::number_float_t o = j; + CHECK(o == 10203.444344); + } + } + } + + SECTION("element access") + { + json j = 119.3333; + const json jc = -65433.55343; + + SECTION("operator[size_type]") + { + CHECK_THROWS_AS(auto o = j[0], std::runtime_error); + CHECK_THROWS_AS(auto o = jc[0], std::runtime_error); + } + + SECTION("at(size_type)") + { + CHECK_THROWS_AS(auto o = j.at(0), std::runtime_error); + CHECK_THROWS_AS(auto o = jc.at(0), std::runtime_error); + } + + SECTION("operator[object_t::key_type]") + { + CHECK_THROWS_AS(j["key"], std::runtime_error); + CHECK_THROWS_AS(j[std::string("key")], std::runtime_error); + } + + SECTION("at(object_t::key_type)") + { + CHECK_THROWS_AS(auto o = j.at("key"), std::runtime_error); + CHECK_THROWS_AS(auto o = j.at(std::string("key")), std::runtime_error); + CHECK_THROWS_AS(auto o = jc.at("key"), std::runtime_error); + CHECK_THROWS_AS(auto o = jc.at(std::string("key")), std::runtime_error); + } + } + + SECTION("iterators") + { + json j = 0.0; + const json jc = -666.22233322; + + SECTION("begin()") + { + { + json::iterator it = j.begin(); + CHECK(*it == j); + } + { + json::const_iterator it = jc.begin(); + CHECK(*it == jc); + } + } + + SECTION("cbegin()") + { + { + json::const_iterator it = j.cbegin(); + CHECK(*it == j); + } + { + json::const_iterator it = jc.cbegin(); + CHECK(*it == jc); + } + { + // check semantics definition of cbegin() + CHECK(const_cast<json::const_reference>(j).begin() == j.cbegin()); + CHECK(const_cast<json::const_reference>(jc).begin() == jc.cbegin()); + } + } + + SECTION("end()") + { + { + json::iterator it = j.end(); + CHECK_THROWS_AS(*it, std::out_of_range); + } + { + json::const_iterator it = jc.end(); + CHECK_THROWS_AS(*it, std::out_of_range); + } + } + + SECTION("cend()") + { + { + json::const_iterator it = j.cend(); + CHECK_THROWS_AS(*it, std::out_of_range); + } + { + json::const_iterator it = jc.cend(); + CHECK_THROWS_AS(*it, std::out_of_range); + } + { + // check semantics definition of cend() + CHECK(const_cast<json::const_reference>(j).end() == j.cend()); + CHECK(const_cast<json::const_reference>(jc).end() == jc.cend()); + } + } + } + + SECTION("capacity") + { + json j = 4344.0; + const json jc = -255.1; + + SECTION("empty()") + { + // null values are empty + CHECK(not j.empty()); + CHECK(not jc.empty()); + + // check semantics definition of empty() + CHECK(j.begin() != j.end()); + CHECK(j.cbegin() != j.cend()); + } + + SECTION("size()") + { + // number values have size 1 + CHECK(j.size() == 1); + CHECK(jc.size() == 1); + + // check semantics definition of size() + CHECK(std::distance(j.begin(), j.end()) == 1); + CHECK(std::distance(j.cbegin(), j.cend()) == 1); + } + + SECTION("max_size()") + { + // null values have max_size 0 + CHECK(j.max_size() == 1); + CHECK(jc.max_size() == 1); + } + } + + SECTION("modifiers") + { + json j = 1119.12; + + SECTION("clear()") + { + j.clear(); + CHECK(not j.empty()); + CHECK(j.m_value.number_float == 0.0); + } + + SECTION("push_back") + { + SECTION("const json&") + { + const json v = 6.2; + CHECK_THROWS_AS(j.push_back(v), std::runtime_error); + } + + SECTION("json&&") + { + CHECK_THROWS_AS(j.push_back(56.11), std::runtime_error); + } + + SECTION("object_t::value_type&") + { + json::object_t::value_type v { "foo", 12.2 }; + CHECK_THROWS_AS(j.push_back(v), std::runtime_error); + } + } + + SECTION("emplace_back") + { + CHECK_THROWS_AS(j.emplace_back(-42.55), std::runtime_error); + } + + /* + SECTION("operator+=") + { + SECTION("const json&") + { + const json v = 8.4; + CHECK_THROWS_AS(j += v, std::runtime_error); + } + + SECTION("json&&") + { + CHECK_THROWS_AS(j += 0, std::runtime_error); + } + + SECTION("object_t::value_type&") + { + json::object_t::value_type v { "foo", 4.42 }; + CHECK_THROWS_AS(j += v, std::runtime_error); + } + } + */ + + SECTION("swap") + { + SECTION("array_t&") + { + json::array_t other = {11.2, 2.4}; + CHECK_THROWS_AS(j.swap(other), std::runtime_error); + } + + SECTION("object_t&") + { + json::object_t other = {{"key1", 44.4}, {"key2", 23.2}}; + CHECK_THROWS_AS(j.swap(other), std::runtime_error); + } + + SECTION("string_t&") + { + json::string_t other = "string"; + CHECK_THROWS_AS(j.swap(other), std::runtime_error); + } + } + } + + SECTION("lexicographical comparison operators") + { + json j1 = -100.55; + json j2 = 100.4; + + CHECK(j1 == j1); + CHECK(not(j1 != j1)); + CHECK(not(j1 < j1)); + CHECK(j1 <= j1); + CHECK(not(j1 > j1)); + CHECK(j1 >= j1); + + CHECK(j2 == j2); + CHECK(not(j2 != j2)); + CHECK(not(j2 < j2)); + CHECK(j2 <= j2); + CHECK(not(j2 > j2)); + CHECK(j2 >= j2); + + CHECK(not(j1 == j2)); + CHECK(j1 != j2); + CHECK(j1 < j2); + CHECK(j1 <= j2); + CHECK(not(j1 > j2)); + CHECK(not(j1 >= j2)); + } + + SECTION("serialization") + { + json j1 = 42.23; + json j2 = 66.66; + + SECTION("operator<<") + { + std::stringstream s; + s << j1 << " " << j2; + auto res = s.str(); + CHECK(res.find("42.23") != std::string::npos); + CHECK(res.find("66.66") != std::string::npos); + } + + SECTION("operator>>") + { + std::stringstream s; + j1 >> s; + j2 >> s; + auto res = s.str(); + CHECK(res.find("42.23") != std::string::npos); + CHECK(res.find("66.66") != std::string::npos); + } + } + + SECTION("convenience functions") + { + json j = 2354.222; + + SECTION("type_name") + { + CHECK(j.type_name() == "number"); + } + } + + SECTION("nonmember functions") + { + json j1 = 23.44; + json j2 = 32.44; + + SECTION("swap") + { + std::swap(j1, j2); + CHECK(j1 == json(32.44)); + CHECK(j2 == json(23.44)); + } + + SECTION("hash") + { + std::hash<json> hash_fn; + auto h1 = hash_fn(j1); + auto h2 = hash_fn(j2); + CHECK(h1 != h2); + } + } +} + +TEST_CASE("string") +{ + SECTION("constructors") + { + SECTION("string_t argument") + { + { + json j("hello"); + CHECK(j.m_type == json::value_t::string); + CHECK(*(j.m_value.string) == "hello"); + } + { + json j("world"); + CHECK(j.m_type == json::value_t::string); + CHECK(*(j.m_value.string) == "world"); + } + { + json j("this"); + CHECK(j.m_type == json::value_t::string); + CHECK(*(j.m_value.string) == "this"); + } + } + + SECTION("string type argument") + { + { + std::string v = "3.14159265359"; + json j(v); + CHECK(j.m_type == json::value_t::string); + CHECK(*(j.m_value.string) == v); + } + { + const char* v = "3.14159265359"; + json j(v); + CHECK(j.m_type == json::value_t::string); + CHECK(*(j.m_value.string) == v); + } + { + char v[14] = "3.14159265359"; + json j(v); + CHECK(j.m_type == json::value_t::string); + CHECK(*(j.m_value.string) == v); + } + { + const char v[14] = "3.14159265359"; + json j(v); + CHECK(j.m_type == json::value_t::string); + CHECK(*(j.m_value.string) == v); + } + } + + SECTION("value_t::string argument") + { + { + json j(json::value_t::string); + CHECK(j.m_type == json::value_t::string); + CHECK(*(j.m_value.string) == ""); + } + } + + SECTION("copy constructor") + { + { + json other("foo"); + json j(other); + CHECK(j.m_type == json::value_t::string); + CHECK(*(j.m_value.string) == "foo"); + } + { + json j = "baz"; + CHECK(j.m_type == json::value_t::string); + CHECK(*(j.m_value.string) == "baz"); + } + } + + SECTION("move constructor") + { + { + json other = "ß"; + json j(std::move(other)); + CHECK(j.m_type == json::value_t::string); + CHECK(*(j.m_value.string) == "ß"); + CHECK(other.m_type == json::value_t::null); + } + } + + SECTION("copy assignment") + { + { + json other = "a string"; + json j = other; + CHECK(j.m_type == json::value_t::string); + CHECK(*(j.m_value.string) == "a string"); + } + } + } + + SECTION("object inspection") + { + json j = "This is a string."; + + SECTION("dump()") + { + CHECK(j.dump() == "\"This is a string.\""); + CHECK(j.dump(-1) == "\"This is a string.\""); + CHECK(j.dump(4) == "\"This is a string.\""); + } + + SECTION("type()") + { + CHECK(j.type() == j.m_type); + } + + SECTION("operator value_t()") + { + { + json::value_t t = j; + CHECK(t == j.m_type); + } + } + } + + SECTION("value conversion") + { + json j = "another example string"; + + SECTION("get()/operator() for objects") + { + CHECK_THROWS_AS(auto o = j.get<json::object_t>(), std::logic_error); + CHECK_THROWS_AS(json::object_t o = j, std::logic_error); + } + + SECTION("get()/operator() for arrays") + { + CHECK_THROWS_AS(auto o = j.get<json::array_t>(), std::logic_error); + CHECK_THROWS_AS(json::array_t o = j, std::logic_error); + } + + SECTION("get()/operator() for strings") + { + { + auto o = j.get<json::string_t>(); + CHECK(o == "another example string"); + } + { + json::string_t o = j; + CHECK(o == "another example string"); + } + } + + SECTION("get()/operator() for booleans") + { + CHECK_THROWS_AS(auto o = j.get<json::boolean_t>(), std::logic_error); + CHECK_THROWS_AS(json::boolean_t o = j, std::logic_error); + } + + SECTION("get()/operator() for integer numbers") + { + CHECK_THROWS_AS(auto o = j.get<json::number_integer_t>(), std::logic_error); + CHECK_THROWS_AS(json::number_integer_t o = j, std::logic_error); + } + + SECTION("get()/operator() for floating point numbers") + { + CHECK_THROWS_AS(auto o = j.get<json::number_float_t>(), std::logic_error); + CHECK_THROWS_AS(json::number_float_t o = j, std::logic_error); + } + } + + SECTION("element access") + { + json j = "!§$&/()="; + const json jc = "!§$&/()="; + + SECTION("operator[size_type]") + { + CHECK_THROWS_AS(auto o = j[0], std::runtime_error); + CHECK_THROWS_AS(auto o = jc[0], std::runtime_error); + } + + SECTION("at(size_type)") + { + CHECK_THROWS_AS(auto o = j.at(0), std::runtime_error); + CHECK_THROWS_AS(auto o = jc.at(0), std::runtime_error); + } + + SECTION("operator[object_t::key_type]") + { + CHECK_THROWS_AS(j["key"], std::runtime_error); + CHECK_THROWS_AS(j[std::string("key")], std::runtime_error); + } + + SECTION("at(object_t::key_type)") + { + CHECK_THROWS_AS(auto o = j.at("key"), std::runtime_error); + CHECK_THROWS_AS(auto o = j.at(std::string("key")), std::runtime_error); + CHECK_THROWS_AS(auto o = jc.at("key"), std::runtime_error); + CHECK_THROWS_AS(auto o = jc.at(std::string("key")), std::runtime_error); + } + } + + SECTION("iterators") + { + json j = "@"; + const json jc = "€"; + + SECTION("begin()") + { + { + json::iterator it = j.begin(); + CHECK(*it == j); + } + { + json::const_iterator it = jc.begin(); + CHECK(*it == jc); + } + } + + SECTION("cbegin()") + { + { + json::const_iterator it = j.cbegin(); + CHECK(*it == j); + } + { + json::const_iterator it = jc.cbegin(); + CHECK(*it == jc); + } + { + // check semantics definition of cbegin() + CHECK(const_cast<json::const_reference>(j).begin() == j.cbegin()); + CHECK(const_cast<json::const_reference>(jc).begin() == jc.cbegin()); + } + } + + SECTION("end()") + { + { + json::iterator it = j.end(); + CHECK_THROWS_AS(*it, std::out_of_range); + } + { + json::const_iterator it = jc.end(); + CHECK_THROWS_AS(*it, std::out_of_range); + } + } + + SECTION("cend()") + { + { + json::const_iterator it = j.cend(); + CHECK_THROWS_AS(*it, std::out_of_range); + } + { + json::const_iterator it = jc.cend(); + CHECK_THROWS_AS(*it, std::out_of_range); + } + { + // check semantics definition of cend() + CHECK(const_cast<json::const_reference>(j).end() == j.cend()); + CHECK(const_cast<json::const_reference>(jc).end() == jc.cend()); + } + } + } + + SECTION("capacity") + { + json j = "Franz jagt im komplett verwahrlosten Taxi quer durch Bayern."; + const json jc = "The quick brown fox jumps over the lazy dog."; + + SECTION("empty()") + { + // null values are empty + CHECK(not j.empty()); + CHECK(not jc.empty()); + + // check semantics definition of empty() + CHECK(j.begin() != j.end()); + CHECK(j.cbegin() != j.cend()); + } + + SECTION("size()") + { + // string values have size 1 + CHECK(j.size() == 1); + CHECK(jc.size() == 1); + + // check semantics definition of size() + CHECK(std::distance(j.begin(), j.end()) == 1); + CHECK(std::distance(j.cbegin(), j.cend()) == 1); + } + + SECTION("max_size()") + { + // null values have max_size 0 + CHECK(j.max_size() == 1); + CHECK(jc.max_size() == 1); + } + } + + SECTION("modifiers") + { + json j = "YOLO"; + + SECTION("clear()") + { + j.clear(); + CHECK(not j.empty()); + CHECK(*(j.m_value.string) == ""); + } + + SECTION("push_back") + { + SECTION("const json&") + { + const json v = 6.2; + CHECK_THROWS_AS(j.push_back(v), std::runtime_error); + } + + SECTION("json&&") + { + CHECK_THROWS_AS(j.push_back(56.11), std::runtime_error); + } + + SECTION("object_t::value_type&") + { + json::object_t::value_type v { "foo", 12.2 }; + CHECK_THROWS_AS(j.push_back(v), std::runtime_error); + } + } + + SECTION("emplace_back") + { + CHECK_THROWS_AS(j.emplace_back(-42.55), std::runtime_error); + } + + /* + SECTION("operator+=") + { + SECTION("const json&") + { + const json v = 8.4; + CHECK_THROWS_AS(j += v, std::runtime_error); + } + + SECTION("json&&") + { + CHECK_THROWS_AS(j += 0, std::runtime_error); + } + + SECTION("object_t::value_type&") + { + json::object_t::value_type v { "foo", 4.42 }; + CHECK_THROWS_AS(j += v, std::runtime_error); + } + } + */ + + SECTION("swap") + { + SECTION("array_t&") + { + json::array_t other = {11.2, 2.4}; + CHECK_THROWS_AS(j.swap(other), std::runtime_error); + } + + SECTION("object_t&") + { + json::object_t other = {{"key1", 44.4}, {"key2", 23.2}}; + CHECK_THROWS_AS(j.swap(other), std::runtime_error); + } + + SECTION("string_t&") + { + json::string_t other = "string"; + j.swap(other); + CHECK(other == json("YOLO")); + CHECK(j == json("string")); + } + } + } + + SECTION("lexicographical comparison operators") + { + json j1 = "Alpha"; + json j2 = "Omega"; + + CHECK(j1 == j1); + CHECK(not(j1 != j1)); + CHECK(not(j1 < j1)); + CHECK(j1 <= j1); + CHECK(not(j1 > j1)); + CHECK(j1 >= j1); + + CHECK(j2 == j2); + CHECK(not(j2 != j2)); + CHECK(not(j2 < j2)); + CHECK(j2 <= j2); + CHECK(not(j2 > j2)); + CHECK(j2 >= j2); + + CHECK(not(j1 == j2)); + CHECK(j1 != j2); + CHECK(j1 < j2); + CHECK(j1 <= j2); + CHECK(not(j1 > j2)); + CHECK(not(j1 >= j2)); + } + + SECTION("serialization") + { + json j1 = "flip"; + json j2 = "flop"; + + SECTION("operator<<") + { + std::stringstream s; + s << j1 << " " << j2; + CHECK(s.str() == "\"flip\" \"flop\""); + } + + SECTION("operator>>") + { + std::stringstream s; + j1 >> s; + j2 >> s; + CHECK(s.str() == "\"flip\"\"flop\""); + } + } + + SECTION("convenience functions") + { + json j = "I am a string, believe me!"; + + SECTION("type_name") + { + CHECK(j.type_name() == "string"); + } + } + + SECTION("nonmember functions") + { + json j1 = "A"; + json j2 = "B"; + + SECTION("swap") + { + std::swap(j1, j2); + CHECK(j1 == json("B")); + CHECK(j2 == json("A")); + } + + SECTION("hash") + { + std::hash<json> hash_fn; + auto h1 = hash_fn(j1); + auto h2 = hash_fn(j2); + CHECK(h1 != h2); + } + } +}