input = {'[', '1', ',', '2', ',', '3', ']', 'o', 't', 'h', 'e', 'r'};
11 |
12 | // parse and serialize JSON
13 | json j_complete = json::parse(input.begin(), input.begin() + 7);
14 | std::cout << std::setw(4) << j_complete << "\n\n";
15 | }
16 |
--------------------------------------------------------------------------------
/tests/reports/2016-08-29-fuzz/index.html:
--------------------------------------------------------------------------------
1 |
2 | | Banner: | fuzz |
3 | | Directory: | fuzz-testing/out |
4 | | Generated on: | Mo 29 Aug 2016 22:14:22 CEST |
5 |
6 |
7 | 
8 | 
9 |
10 |
11 |
--------------------------------------------------------------------------------
/tests/reports/2016-10-02-fuzz/index.html:
--------------------------------------------------------------------------------
1 |
2 | | Banner: | fuzz |
3 | | Directory: | fuzz-testing/out |
4 | | Generated on: | Sun Oct 2 08:51:02 CEST 2016 |
5 |
6 |
7 | 
8 | 
9 |
10 |
11 |
--------------------------------------------------------------------------------
/tests/cmake_add_subdirectory/project/main.cpp:
--------------------------------------------------------------------------------
1 | // __ _____ _____ _____
2 | // __| | __| | | | JSON for Modern C++ (supporting code)
3 | // | | |__ | | | | | | version 3.11.3
4 | // |_____|_____|_____|_|___| https://github.com/nlohmann/json
5 | //
6 | // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann
7 | // SPDX-License-Identifier: MIT
8 |
9 | #include
10 |
11 | int main(int argc, char** argv)
12 | {
13 | nlohmann::json j;
14 |
15 | return 0;
16 | }
17 |
--------------------------------------------------------------------------------
/tests/cmake_fetch_content/project/main.cpp:
--------------------------------------------------------------------------------
1 | // __ _____ _____ _____
2 | // __| | __| | | | JSON for Modern C++ (supporting code)
3 | // | | |__ | | | | | | version 3.11.3
4 | // |_____|_____|_____|_|___| https://github.com/nlohmann/json
5 | //
6 | // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann
7 | // SPDX-License-Identifier: MIT
8 |
9 | #include
10 |
11 | int main(int argc, char** argv)
12 | {
13 | nlohmann::json j;
14 |
15 | return 0;
16 | }
17 |
--------------------------------------------------------------------------------
/tests/cmake_fetch_content2/project/main.cpp:
--------------------------------------------------------------------------------
1 | // __ _____ _____ _____
2 | // __| | __| | | | JSON for Modern C++ (supporting code)
3 | // | | |__ | | | | | | version 3.11.3
4 | // |_____|_____|_____|_|___| https://github.com/nlohmann/json
5 | //
6 | // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann
7 | // SPDX-License-Identifier: MIT
8 |
9 | #include
10 |
11 | int main(int argc, char** argv)
12 | {
13 | nlohmann::json j;
14 |
15 | return 0;
16 | }
17 |
--------------------------------------------------------------------------------
/tests/cmake_import_minver/project/main.cpp:
--------------------------------------------------------------------------------
1 | // __ _____ _____ _____
2 | // __| | __| | | | JSON for Modern C++ (supporting code)
3 | // | | |__ | | | | | | version 3.11.3
4 | // |_____|_____|_____|_|___| https://github.com/nlohmann/json
5 | //
6 | // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann
7 | // SPDX-License-Identifier: MIT
8 |
9 | #include
10 |
11 | int main(int argc, char** argv)
12 | {
13 | nlohmann::json j;
14 |
15 | return 0;
16 | }
17 |
--------------------------------------------------------------------------------
/docs/examples/at__json_pointer.output:
--------------------------------------------------------------------------------
1 | 1
2 | "foo"
3 | [1,2]
4 | 2
5 | "bar"
6 | [1,21]
7 | [json.exception.parse_error.106] parse error: array index '01' must not begin with '0'
8 | [json.exception.parse_error.109] parse error: array index 'one' is not a number
9 | [json.exception.out_of_range.401] array index 4 is out of range
10 | [json.exception.out_of_range.402] array index '-' (2) is out of range
11 | [json.exception.out_of_range.403] key 'foo' not found
12 | [json.exception.out_of_range.404] unresolved reference token 'foo'
13 |
--------------------------------------------------------------------------------
/docs/examples/out_of_range.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | using json = nlohmann::json;
5 |
6 | int main()
7 | {
8 | try
9 | {
10 | // calling at() for an invalid index
11 | json j = {1, 2, 3, 4};
12 | j.at(4) = 10;
13 | }
14 | catch (const json::out_of_range& e)
15 | {
16 | // output exception information
17 | std::cout << "message: " << e.what() << '\n'
18 | << "exception id: " << e.id << std::endl;
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/tests/cmake_target_include_directories/project/main.cpp:
--------------------------------------------------------------------------------
1 | // __ _____ _____ _____
2 | // __| | __| | | | JSON for Modern C++ (supporting code)
3 | // | | |__ | | | | | | version 3.11.3
4 | // |_____|_____|_____|_|___| https://github.com/nlohmann/json
5 | //
6 | // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann
7 | // SPDX-License-Identifier: MIT
8 |
9 | #include
10 |
11 | int main(int argc, char** argv)
12 | {
13 | nlohmann::json j;
14 |
15 | return 0;
16 | }
17 |
--------------------------------------------------------------------------------
/tests/thirdparty/Fuzzer/test/fuzzer-customcrossover.test:
--------------------------------------------------------------------------------
1 | RUN: rm -rf %t/CustomCrossover
2 | RUN: mkdir -p %t/CustomCrossover
3 | RUN: echo "0123456789" > %t/CustomCrossover/digits
4 | RUN: echo "abcdefghij" > %t/CustomCrossover/chars
5 | RUN: not LLVMFuzzer-CustomCrossOverTest -seed=1 -use_memcmp=0 -runs=100000 %t/CustomCrossover 2>&1 | FileCheck %s --check-prefix=LLVMFuzzerCustomCrossover
6 | RUN: rm -rf %t/CustomCrossover
7 |
8 | LLVMFuzzerCustomCrossover: In LLVMFuzzerCustomCrossover
9 | LLVMFuzzerCustomCrossover: BINGO
10 |
11 |
--------------------------------------------------------------------------------
/tests/thirdparty/Fuzzer/test/NthRunCrashTest.cpp:
--------------------------------------------------------------------------------
1 | // This file is distributed under the University of Illinois Open Source
2 | // License. See LICENSE.TXT for details.
3 |
4 | // Crash on the N-th execution.
5 | #include
6 | #include
7 | #include
8 |
9 | static int Counter;
10 |
11 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
12 | if (Counter++ == 1000) {
13 | std::cout << "BINGO; Found the target, exiting\n";
14 | exit(1);
15 | }
16 | return 0;
17 | }
18 |
19 |
--------------------------------------------------------------------------------
/docs/examples/basic_json__size_type_basic_json.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | using json = nlohmann::json;
5 |
6 | int main()
7 | {
8 | // create an array by creating copies of a JSON value
9 | json value = "Hello";
10 | json array_0 = json(0, value);
11 | json array_1 = json(1, value);
12 | json array_5 = json(5, value);
13 |
14 | // serialize the JSON arrays
15 | std::cout << array_0 << '\n';
16 | std::cout << array_1 << '\n';
17 | std::cout << array_5 << '\n';
18 | }
19 |
--------------------------------------------------------------------------------
/docs/examples/type_error.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | using json = nlohmann::json;
5 |
6 | int main()
7 | {
8 | try
9 | {
10 | // calling push_back() on a string value
11 | json j = "string";
12 | j.push_back("another string");
13 | }
14 | catch (const json::type_error& e)
15 | {
16 | // output exception information
17 | std::cout << "message: " << e.what() << '\n'
18 | << "exception id: " << e.id << std::endl;
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/docs/examples/README.output:
--------------------------------------------------------------------------------
1 | {
2 | "answer": {
3 | "everything": 42
4 | },
5 | "happy": true,
6 | "list": [
7 | 1,
8 | 0,
9 | 2
10 | ],
11 | "name": "Niels",
12 | "new": {
13 | "key": {
14 | "value": [
15 | "another",
16 | "list"
17 | ]
18 | }
19 | },
20 | "nothing": null,
21 | "object": {
22 | "currency": "USD",
23 | "value": 42.99
24 | },
25 | "pi": 3.141,
26 | "size": 8
27 | }
28 |
--------------------------------------------------------------------------------
/docs/examples/exception.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | using json = nlohmann::json;
5 |
6 | int main()
7 | {
8 | try
9 | {
10 | // calling at() for a non-existing key
11 | json j = {{"foo", "bar"}};
12 | json k = j.at("non-existing");
13 | }
14 | catch (const json::exception& e)
15 | {
16 | // output exception information
17 | std::cout << "message: " << e.what() << '\n'
18 | << "exception id: " << e.id << std::endl;
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/docs/examples/json_pointer__operator_add_binary.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | using json = nlohmann::json;
5 |
6 | int main()
7 | {
8 | // create a JSON pointer
9 | json::json_pointer ptr("/foo");
10 |
11 | // append a JSON Pointer
12 | std::cout << "\"" << ptr / json::json_pointer("/bar/baz") << "\"\n";
13 |
14 | // append a string
15 | std::cout << "\"" << ptr / "fob" << "\"\n";
16 |
17 | // append an array index
18 | std::cout << "\"" << ptr / 42 << "\"" << std::endl;
19 | }
20 |
--------------------------------------------------------------------------------
/docs/examples/json_pointer__pop_back.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | using json = nlohmann::json;
5 |
6 | int main()
7 | {
8 | // create empty JSON Pointer
9 | json::json_pointer ptr("/foo/bar/baz");
10 | std::cout << "\"" << ptr << "\"\n";
11 |
12 | // call pop_back()
13 | ptr.pop_back();
14 | std::cout << "\"" << ptr << "\"\n";
15 |
16 | ptr.pop_back();
17 | std::cout << "\"" << ptr << "\"\n";
18 |
19 | ptr.pop_back();
20 | std::cout << "\"" << ptr << "\"\n";
21 | }
22 |
--------------------------------------------------------------------------------
/docs/examples/json_pointer__push_back.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | using json = nlohmann::json;
5 |
6 | int main()
7 | {
8 | // create empty JSON Pointer
9 | json::json_pointer ptr;
10 | std::cout << "\"" << ptr << "\"\n";
11 |
12 | // call push_back()
13 | ptr.push_back("foo");
14 | std::cout << "\"" << ptr << "\"\n";
15 |
16 | ptr.push_back("0");
17 | std::cout << "\"" << ptr << "\"\n";
18 |
19 | ptr.push_back("bar");
20 | std::cout << "\"" << ptr << "\"\n";
21 | }
22 |
--------------------------------------------------------------------------------
/docs/examples/swap__binary_t.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | using json = nlohmann::json;
5 |
6 | int main()
7 | {
8 | // create a binary value
9 | json value = json::binary({1, 2, 3});
10 |
11 | // create a binary_t
12 | json::binary_t binary = {{4, 5, 6}};
13 |
14 | // swap the object stored in the JSON value
15 | value.swap(binary);
16 |
17 | // output the values
18 | std::cout << "value = " << value << '\n';
19 | std::cout << "binary = " << json(binary) << '\n';
20 | }
21 |
--------------------------------------------------------------------------------
/tests/thirdparty/Fuzzer/test/trace-malloc.test:
--------------------------------------------------------------------------------
1 | RUN: LLVMFuzzer-TraceMallocTest -seed=1 -trace_malloc=1 -runs=10000 2>&1 | FileCheck %s
2 | CHECK-DAG: MallocFreeTracer: STOP 0 0 (same)
3 | CHECK-DAG: MallocFreeTracer: STOP 0 1 (DIFFERENT)
4 | CHECK-DAG: MallocFreeTracer: STOP 1 0 (DIFFERENT)
5 | CHECK-DAG: MallocFreeTracer: STOP 1 1 (same)
6 |
7 | RUN: LLVMFuzzer-TraceMallocTest -seed=1 -trace_malloc=2 -runs=1000 2>&1 | FileCheck %s --check-prefix=TRACE2
8 | TRACE2-DAG: FREE[0]
9 | TRACE2-DAG: MALLOC[0]
10 | TRACE2-DAG: in LLVMFuzzerTestOneInput
11 |
--------------------------------------------------------------------------------
/docs/examples/swap__string_t.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | using json = nlohmann::json;
5 |
6 | int main()
7 | {
8 | // create a JSON value
9 | json value = { "the good", "the bad", "the ugly" };
10 |
11 | // create string_t
12 | json::string_t string = "the fast";
13 |
14 | // swap the object stored in the JSON value
15 | value[1].swap(string);
16 |
17 | // output the values
18 | std::cout << "value = " << value << '\n';
19 | std::cout << "string = " << string << '\n';
20 | }
21 |
--------------------------------------------------------------------------------
/docs/examples/swap__array_t.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | using json = nlohmann::json;
5 |
6 | int main()
7 | {
8 | // create a JSON value
9 | json value = {{"array", {1, 2, 3, 4}}};
10 |
11 | // create an array_t
12 | json::array_t array = {"Snap", "Crackle", "Pop"};
13 |
14 | // swap the array stored in the JSON value
15 | value["array"].swap(array);
16 |
17 | // output the values
18 | std::cout << "value = " << value << '\n';
19 | std::cout << "array = " << array << '\n';
20 | }
21 |
--------------------------------------------------------------------------------
/include/nlohmann/detail/meta/call_std/end.hpp:
--------------------------------------------------------------------------------
1 | // __ _____ _____ _____
2 | // __| | __| | | | JSON for Modern C++
3 | // | | |__ | | | | | | version 3.11.3
4 | // |_____|_____|_____|_|___| https://github.com/nlohmann/json
5 | //
6 | // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann
7 | // SPDX-License-Identifier: MIT
8 |
9 | #pragma once
10 |
11 | #include
12 |
13 | NLOHMANN_JSON_NAMESPACE_BEGIN
14 |
15 | NLOHMANN_CAN_CALL_STD_FUNC_IMPL(end);
16 |
17 | NLOHMANN_JSON_NAMESPACE_END
18 |
--------------------------------------------------------------------------------
/include/nlohmann/detail/meta/call_std/begin.hpp:
--------------------------------------------------------------------------------
1 | // __ _____ _____ _____
2 | // __| | __| | | | JSON for Modern C++
3 | // | | |__ | | | | | | version 3.11.3
4 | // |_____|_____|_____|_|___| https://github.com/nlohmann/json
5 | //
6 | // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann
7 | // SPDX-License-Identifier: MIT
8 |
9 | #pragma once
10 |
11 | #include
12 |
13 | NLOHMANN_JSON_NAMESPACE_BEGIN
14 |
15 | NLOHMANN_CAN_CALL_STD_FUNC_IMPL(begin);
16 |
17 | NLOHMANN_JSON_NAMESPACE_END
18 |
--------------------------------------------------------------------------------
/docs/examples/parse_error.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | using json = nlohmann::json;
5 |
6 | int main()
7 | {
8 | try
9 | {
10 | // parsing input with a syntax error
11 | json::parse("[1,2,3,]");
12 | }
13 | catch (const json::parse_error& e)
14 | {
15 | // output exception information
16 | std::cout << "message: " << e.what() << '\n'
17 | << "exception id: " << e.id << '\n'
18 | << "byte position of error: " << e.byte << std::endl;
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/docs/examples/push_back.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | using json = nlohmann::json;
5 |
6 | int main()
7 | {
8 | // create JSON values
9 | json array = {1, 2, 3, 4, 5};
10 | json null;
11 |
12 | // print values
13 | std::cout << array << '\n';
14 | std::cout << null << '\n';
15 |
16 | // add values
17 | array.push_back(6);
18 | array += 7;
19 | null += "first";
20 | null += "second";
21 |
22 | // print values
23 | std::cout << array << '\n';
24 | std::cout << null << '\n';
25 | }
26 |
--------------------------------------------------------------------------------
/docs/examples/count__object_t_key_type.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | using json = nlohmann::json;
5 |
6 | int main()
7 | {
8 | // create a JSON object
9 | json j_object = {{"one", 1}, {"two", 2}};
10 |
11 | // call count()
12 | auto count_two = j_object.count("two");
13 | auto count_three = j_object.count("three");
14 |
15 | // print values
16 | std::cout << "number of elements with key \"two\": " << count_two << '\n';
17 | std::cout << "number of elements with key \"three\": " << count_three << '\n';
18 | }
19 |
--------------------------------------------------------------------------------
/tests/thirdparty/Fuzzer/test/ThreadedLeakTest.cpp:
--------------------------------------------------------------------------------
1 | // This file is distributed under the University of Illinois Open Source
2 | // License. See LICENSE.TXT for details.
3 |
4 | // The fuzzer should find a leak in a non-main thread.
5 | #include
6 | #include
7 | #include
8 |
9 | static volatile int *Sink;
10 |
11 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
12 | if (Size == 0) return 0;
13 | if (Data[0] != 'F') return 0;
14 | std::thread T([&] { Sink = new int; });
15 | T.join();
16 | return 0;
17 | }
18 |
19 |
--------------------------------------------------------------------------------
/docs/examples/emplace_back.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | using json = nlohmann::json;
5 |
6 | int main()
7 | {
8 | // create JSON values
9 | json array = {1, 2, 3, 4, 5};
10 | json null;
11 |
12 | // print values
13 | std::cout << array << '\n';
14 | std::cout << null << '\n';
15 |
16 | // add values
17 | array.emplace_back(6);
18 | null.emplace_back("first");
19 | null.emplace_back(3, "second");
20 |
21 | // print values
22 | std::cout << array << '\n';
23 | std::cout << null << '\n';
24 | }
25 |
--------------------------------------------------------------------------------
/docs/examples/insert__range_object.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | using json = nlohmann::json;
5 |
6 | int main()
7 | {
8 | // create two JSON objects
9 | json j1 = {{"one", "eins"}, {"two", "zwei"}};
10 | json j2 = {{"eleven", "elf"}, {"seventeen", "siebzehn"}};
11 |
12 | // output objects
13 | std::cout << j1 << '\n';
14 | std::cout << j2 << '\n';
15 |
16 | // insert range from j2 to j1
17 | j1.insert(j2.begin(), j2.end());
18 |
19 | // output result of insert call
20 | std::cout << j1 << '\n';
21 | }
22 |
--------------------------------------------------------------------------------
/docs/examples/to_string.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | using json = nlohmann::json;
5 | using std::to_string;
6 |
7 | int main()
8 | {
9 | // create values
10 | json j = {{"one", 1}, {"two", 2}};
11 | int i = 42;
12 |
13 | // use ADL to select best to_string function
14 | auto j_str = to_string(j); // calling nlohmann::to_string
15 | auto i_str = to_string(i); // calling std::to_string
16 |
17 | // serialize without indentation
18 | std::cout << j_str << "\n\n"
19 | << i_str << std::endl;
20 | }
21 |
--------------------------------------------------------------------------------
/tests/thirdparty/Fuzzer/test/SingleStrcmpTest.cpp:
--------------------------------------------------------------------------------
1 | // This file is distributed under the University of Illinois Open Source
2 | // License. See LICENSE.TXT for details.
3 |
4 | // Simple test for a fuzzer. The fuzzer must find a particular string.
5 | #include
6 | #include
7 | #include
8 | #include
9 |
10 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
11 | char *S = (char*)Data;
12 | if (Size >= 7 && !strcmp(S, "qwerty")) {
13 | fprintf(stderr, "BINGO\n");
14 | exit(1);
15 | }
16 | return 0;
17 | }
18 |
--------------------------------------------------------------------------------
/docs/examples/erase__keytype.c++17.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 |
5 | using namespace std::string_view_literals;
6 | using json = nlohmann::json;
7 |
8 | int main()
9 | {
10 | // create a JSON object
11 | json j_object = {{"one", 1}, {"two", 2}};
12 |
13 | // call erase()
14 | auto count_one = j_object.erase("one"sv);
15 | auto count_three = j_object.erase("three"sv);
16 |
17 | // print values
18 | std::cout << j_object << '\n';
19 | std::cout << count_one << " " << count_three << '\n';
20 | }
21 |
--------------------------------------------------------------------------------
/docs/examples/insert__range.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | using json = nlohmann::json;
5 |
6 | int main()
7 | {
8 | // create a JSON array
9 | json v = {1, 2, 3, 4};
10 |
11 | // create a JSON array to copy values from
12 | json v2 = {"one", "two", "three", "four"};
13 |
14 | // insert range from v2 before the end of array v
15 | auto new_pos = v.insert(v.end(), v2.begin(), v2.end());
16 |
17 | // output new array and result of insert call
18 | std::cout << *new_pos << '\n';
19 | std::cout << v << '\n';
20 | }
21 |
--------------------------------------------------------------------------------
/docs/examples/operator__equal__specializations.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 |
5 | using json = nlohmann::json;
6 |
7 | int main()
8 | {
9 | nlohmann::json uj1 = {{"version", 1}, {"type", "integer"}};
10 | nlohmann::json uj2 = {{"type", "integer"}, {"version", 1}};
11 |
12 | nlohmann::ordered_json oj1 = {{"version", 1}, {"type", "integer"}};
13 | nlohmann::ordered_json oj2 = {{"type", "integer"}, {"version", 1}};
14 |
15 | std::cout << std::boolalpha << (uj1 == uj2) << '\n' << (oj1 == oj2) << std::endl;
16 | }
17 |
--------------------------------------------------------------------------------
/tests/thirdparty/Fuzzer/test/SingleMemcmpTest.cpp:
--------------------------------------------------------------------------------
1 | // This file is distributed under the University of Illinois Open Source
2 | // License. See LICENSE.TXT for details.
3 |
4 | // Simple test for a fuzzer. The fuzzer must find a particular string.
5 | #include
6 | #include
7 | #include
8 | #include
9 |
10 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
11 | char *S = (char*)Data;
12 | if (Size >= 6 && !memcmp(S, "qwerty", 6)) {
13 | fprintf(stderr, "BINGO\n");
14 | exit(1);
15 | }
16 | return 0;
17 | }
18 |
--------------------------------------------------------------------------------
/tests/thirdparty/Fuzzer/test/SingleStrncmpTest.cpp:
--------------------------------------------------------------------------------
1 | // This file is distributed under the University of Illinois Open Source
2 | // License. See LICENSE.TXT for details.
3 |
4 | // Simple test for a fuzzer. The fuzzer must find a particular string.
5 | #include
6 | #include
7 | #include
8 | #include
9 |
10 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
11 | char *S = (char*)Data;
12 | if (Size >= 6 && !strncmp(S, "qwerty", 6)) {
13 | fprintf(stderr, "BINGO\n");
14 | exit(1);
15 | }
16 | return 0;
17 | }
18 |
--------------------------------------------------------------------------------
/docs/examples/invalid_iterator.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | using json = nlohmann::json;
5 |
6 | int main()
7 | {
8 | try
9 | {
10 | // calling iterator::key() on non-object iterator
11 | json j = "string";
12 | json::iterator it = j.begin();
13 | auto k = it.key();
14 | }
15 | catch (const json::invalid_iterator& e)
16 | {
17 | // output exception information
18 | std::cout << "message: " << e.what() << '\n'
19 | << "exception id: " << e.id << std::endl;
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/docs/mkdocs/docs/api/json_sax/null.md:
--------------------------------------------------------------------------------
1 | # nlohmann::json_sax::null
2 |
3 | ```cpp
4 | virtual bool null() = 0;
5 | ```
6 |
7 | A null value was read.
8 |
9 | ## Return value
10 |
11 | Whether parsing should proceed.
12 |
13 | ## Examples
14 |
15 | ??? example
16 |
17 | The example below shows how the SAX interface is used.
18 |
19 | ```cpp
20 | --8<-- "examples/sax_parse.cpp"
21 | ```
22 |
23 | Output:
24 |
25 | ```json
26 | --8<-- "examples/sax_parse.output"
27 | ```
28 |
29 | ## Version history
30 |
31 | - Added in version 3.2.0.
32 |
--------------------------------------------------------------------------------
/tests/thirdparty/Fuzzer/test/CounterTest.cpp:
--------------------------------------------------------------------------------
1 | // This file is distributed under the University of Illinois Open Source
2 | // License. See LICENSE.TXT for details.
3 |
4 | // Test for a fuzzer: must find the case where a particular basic block is
5 | // executed many times.
6 | #include
7 |
8 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
9 | int Num = 0;
10 | for (size_t i = 0; i < Size; i++)
11 | if (Data[i] == 'A' + i)
12 | Num++;
13 | if (Num >= 4) {
14 | std::cerr << "BINGO!\n";
15 | exit(1);
16 | }
17 | return 0;
18 | }
19 |
--------------------------------------------------------------------------------
/tests/thirdparty/Fuzzer/test/fuzzer-flags.test:
--------------------------------------------------------------------------------
1 | RUN: LLVMFuzzer-SimpleTest -foo_bar=1 2>&1 | FileCheck %s --check-prefix=FOO_BAR
2 | FOO_BAR: WARNING: unrecognized flag '-foo_bar=1'; use -help=1 to list all flags
3 | FOO_BAR: BINGO
4 |
5 | RUN: LLVMFuzzer-SimpleTest -runs=10 --max_len=100 2>&1 | FileCheck %s --check-prefix=DASH_DASH
6 | DASH_DASH: WARNING: did you mean '-max_len=100' (single dash)?
7 | DASH_DASH: INFO: A corpus is not provided, starting from an empty corpus
8 |
9 | RUN: LLVMFuzzer-SimpleTest -help=1 2>&1 | FileCheck %s --check-prefix=NO_INTERNAL
10 | NO_INTERNAL-NOT: internal flag
11 |
--------------------------------------------------------------------------------
/docs/examples/swap__object_t.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | using json = nlohmann::json;
5 |
6 | int main()
7 | {
8 | // create a JSON value
9 | json value = { {"translation", {{"one", "eins"}, {"two", "zwei"}}} };
10 |
11 | // create an object_t
12 | json::object_t object = {{"cow", "Kuh"}, {"dog", "Hund"}};
13 |
14 | // swap the object stored in the JSON value
15 | value["translation"].swap(object);
16 |
17 | // output the values
18 | std::cout << "value = " << value << '\n';
19 | std::cout << "object = " << object << '\n';
20 | }
21 |
--------------------------------------------------------------------------------
/tests/thirdparty/Fuzzer/test/NullDerefOnEmptyTest.cpp:
--------------------------------------------------------------------------------
1 | // This file is distributed under the University of Illinois Open Source
2 | // License. See LICENSE.TXT for details.
3 |
4 | // Simple test for a fuzzer. The fuzzer must find the empty string.
5 | #include
6 | #include
7 | #include
8 | #include
9 |
10 | static volatile int *Null = 0;
11 |
12 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
13 | if (Size == 0) {
14 | std::cout << "Found the target, dereferencing NULL\n";
15 | *Null = 1;
16 | }
17 | return 0;
18 | }
19 |
20 |
--------------------------------------------------------------------------------
/docs/examples/accept__string.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 |
5 | using json = nlohmann::json;
6 |
7 | int main()
8 | {
9 | // a valid JSON text
10 | auto valid_text = R"(
11 | {
12 | "numbers": [1, 2, 3]
13 | }
14 | )";
15 |
16 | // an invalid JSON text
17 | auto invalid_text = R"(
18 | {
19 | "strings": ["extra", "comma", ]
20 | }
21 | )";
22 |
23 | std::cout << std::boolalpha
24 | << json::accept(valid_text) << ' '
25 | << json::accept(invalid_text) << '\n';
26 | }
27 |
--------------------------------------------------------------------------------
/tests/thirdparty/Fuzzer/test/DivTest.cpp:
--------------------------------------------------------------------------------
1 | // This file is distributed under the University of Illinois Open Source
2 | // License. See LICENSE.TXT for details.
3 |
4 | // Simple test for a fuzzer: find the interesting argument for div.
5 | #include
6 | #include
7 | #include
8 | #include
9 | #include