Merge branch 'develop' of https://github.com/nlohmann/json into bon8

This commit is contained in:
Niels Lohmann
2022-08-13 11:42:08 +02:00
411 changed files with 32052 additions and 5692 deletions

View File

@@ -2,6 +2,7 @@
#include <nlohmann/json.hpp>
using json = nlohmann::json;
using namespace nlohmann::literals;
int main()
{

View File

@@ -2,6 +2,7 @@
#include <nlohmann/json.hpp>
using json = nlohmann::json;
using namespace nlohmann::literals;
int main()
{

View File

@@ -0,0 +1,50 @@
#include <iostream>
#include <string_view>
#include <nlohmann/json.hpp>
using namespace std::string_view_literals;
using json = nlohmann::json;
int main()
{
// create JSON object
json object =
{
{"the good", "il buono"},
{"the bad", "il cattivo"},
{"the ugly", "il brutto"}
};
// output element with key "the ugly" using string_view
std::cout << object.at("the ugly"sv) << '\n';
// change element with key "the bad" using string_view
object.at("the bad"sv) = "il cattivo";
// output changed array
std::cout << object << '\n';
// exception type_error.304
try
{
// use at() with string_view on a non-object type
json str = "I am a string";
str.at("the good"sv) = "Another string";
}
catch (json::type_error& e)
{
std::cout << e.what() << '\n';
}
// exception out_of_range.401
try
{
// try to write at a nonexisting key using string_view
object.at("the fast"sv) = "il rapido";
}
catch (json::out_of_range& e)
{
std::cout << e.what() << '\n';
}
}

View File

@@ -0,0 +1,4 @@
"il brutto"
{"the bad":"il cattivo","the good":"il buono","the ugly":"il brutto"}
[json.exception.type_error.304] cannot use at() with string
[json.exception.out_of_range.403] key 'the fast' not found

View File

@@ -0,0 +1,44 @@
#include <iostream>
#include <string_view>
#include <nlohmann/json.hpp>
using namespace std::string_view_literals;
using json = nlohmann::json;
int main()
{
// create JSON object
const json object =
{
{"the good", "il buono"},
{"the bad", "il cattivo"},
{"the ugly", "il brutto"}
};
// output element with key "the ugly" using string_view
std::cout << object.at("the ugly"sv) << '\n';
// exception type_error.304
try
{
// use at() with string_view on a non-object type
const json str = "I am a string";
std::cout << str.at("the good"sv) << '\n';
}
catch (json::type_error& e)
{
std::cout << e.what() << '\n';
}
// exception out_of_range.401
try
{
// try to read from a nonexisting key using string_view
std::cout << object.at("the fast"sv) << '\n';
}
catch (json::out_of_range)
{
std::cout << "out of range" << '\n';
}
}

View File

@@ -0,0 +1,3 @@
"il brutto"
[json.exception.type_error.304] cannot use at() with string
out of range

View File

@@ -2,6 +2,7 @@
#include <nlohmann/json.hpp>
using json = nlohmann::json;
using namespace nlohmann::literals;
int main()
{

View File

@@ -0,0 +1,20 @@
#include <iostream>
#include <string_view>
#include <nlohmann/json.hpp>
using namespace std::string_view_literals;
using json = nlohmann::json;
using namespace nlohmann::literals;
int main()
{
// create some JSON values
json j_object = R"( {"key": "value"} )"_json;
json j_array = R"( [1, 2, 3] )"_json;
// call contains
std::cout << std::boolalpha <<
"j_object contains 'key': " << j_object.contains("key"sv) << '\n' <<
"j_object contains 'another': " << j_object.contains("another"sv) << '\n' <<
"j_array contains 'key': " << j_array.contains("key"sv) << std::endl;
}

View File

@@ -2,6 +2,7 @@
#include <nlohmann/json.hpp>
using json = nlohmann::json;
using namespace nlohmann::literals;
int main()
{

View File

@@ -0,0 +1,3 @@
j_object contains 'key': true
j_object contains 'another': false
j_array contains 'key': false

View File

@@ -0,0 +1,20 @@
#include <iostream>
#include <string_view>
#include <nlohmann/json.hpp>
using namespace std::string_view_literals;
using json = nlohmann::json;
int main()
{
// create a JSON object
json j_object = {{"one", 1}, {"two", 2}};
// call count()
auto count_two = j_object.count("two"sv);
auto count_three = j_object.count("three"sv);
// print values
std::cout << "number of elements with key \"two\": " << count_two << '\n';
std::cout << "number of elements with key \"three\": " << count_three << '\n';
}

View File

@@ -0,0 +1,2 @@
number of elements with key "two": 1
number of elements with key "three": 0

View File

@@ -3,6 +3,7 @@
#include <nlohmann/json.hpp>
using json = nlohmann::json;
using namespace nlohmann::literals;
int main()
{

View File

@@ -0,0 +1,20 @@
#include <iostream>
#include <string_view>
#include <nlohmann/json.hpp>
using namespace std::string_view_literals;
using json = nlohmann::json;
int main()
{
// create a JSON object
json j_object = {{"one", 1}, {"two", 2}};
// call erase()
auto count_one = j_object.erase("one"sv);
auto count_three = j_object.erase("three"sv);
// print values
std::cout << j_object << '\n';
std::cout << count_one << " " << count_three << '\n';
}

View File

@@ -0,0 +1,2 @@
{"two":2}
1 0

View File

@@ -0,0 +1,22 @@
#include <iostream>
#include <string_view>
#include <nlohmann/json.hpp>
using namespace std::string_view_literals;
using json = nlohmann::json;
int main()
{
// create a JSON object
json j_object = {{"one", 1}, {"two", 2}};
// call find
auto it_two = j_object.find("two"sv);
auto it_three = j_object.find("three"sv);
// print values
std::cout << std::boolalpha;
std::cout << "\"two\" was found: " << (it_two != j_object.end()) << '\n';
std::cout << "value at key \"two\": " << *it_two << '\n';
std::cout << "\"three\" was found: " << (it_three != j_object.end()) << '\n';
}

View File

@@ -0,0 +1,3 @@
"two" was found: true
value at key "two": 2
"three" was found: false

View File

@@ -0,0 +1,37 @@
#include <iostream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
namespace ns
{
// a simple struct to model a person
struct person
{
std::string name;
std::string address;
int age;
};
} // namespace ns
namespace ns
{
void from_json(const json& j, person& p)
{
j.at("name").get_to(p.name);
j.at("address").get_to(p.address);
j.at("age").get_to(p.age);
}
} // namespace ns
int main()
{
json j;
j["name"] = "Ned Flanders";
j["address"] = "744 Evergreen Terrace";
j["age"] = 60;
auto p = j.get<ns::person>();
std::cout << p.name << " (" << p.age << ") lives in " << p.address << std::endl;
}

View File

@@ -0,0 +1 @@
Ned Flanders (60) lives in 744 Evergreen Terrace

View File

@@ -0,0 +1,53 @@
#include <iostream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
namespace ns
{
// a simple struct to model a person (not default constructible)
struct person
{
person(std::string n, std::string a, int aa)
: name(std::move(n)), address(std::move(a)), age(aa)
{}
std::string name;
std::string address;
int age;
};
} // namespace ns
namespace nlohmann
{
template <>
struct adl_serializer<ns::person>
{
static ns::person from_json(const json& j)
{
return {j.at("name"), j.at("address"), j.at("age")};
}
// Here's the catch! You must provide a to_json method! Otherwise, you
// will not be able to convert person to json, since you fully
// specialized adl_serializer on that type
static void to_json(json& j, ns::person p)
{
j["name"] = p.name;
j["address"] = p.address;
j["age"] = p.age;
}
};
} // namespace nlohmann
int main()
{
json j;
j["name"] = "Ned Flanders";
j["address"] = "744 Evergreen Terrace";
j["age"] = 60;
auto p = j.get<ns::person>();
std::cout << p.name << " (" << p.age << ") lives in " << p.address << std::endl;
}

View File

@@ -0,0 +1 @@
Ned Flanders (60) lives in 744 Evergreen Terrace

View File

@@ -10,6 +10,6 @@ int main()
json::json_pointer ptr2("/foo/0");
// call empty()
std::cout << "last reference token of " << ptr1 << " is " << ptr1.back() << '\n'
<< "last reference token of " << ptr2 << " is " << ptr2.back() << std::endl;
std::cout << "last reference token of \"" << ptr1 << "\" is \"" << ptr1.back() << "\"\n"
<< "last reference token of \"" << ptr2 << "\" is \"" << ptr2.back() << "\"" << std::endl;
}

View File

@@ -1,2 +1,2 @@
last reference token of "/foo" is foo
last reference token of "/foo/0" is 0
last reference token of "/foo" is "foo"
last reference token of "/foo/0" is "0"

View File

@@ -13,8 +13,8 @@ int main()
// call empty()
std::cout << std::boolalpha
<< ptr0 << ": " << ptr0.empty() << '\n'
<< ptr1 << ": " << ptr1.empty() << '\n'
<< ptr2 << ": " << ptr2.empty() << '\n'
<< ptr3 << ": " << ptr3.empty() << std::endl;
<< "\"" << ptr0 << "\": " << ptr0.empty() << '\n'
<< "\"" << ptr1 << "\": " << ptr1.empty() << '\n'
<< "\"" << ptr2 << "\": " << ptr2.empty() << '\n'
<< "\"" << ptr3 << "\": " << ptr3.empty() << std::endl;
}

View File

@@ -0,0 +1,19 @@
#include <iostream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
int main()
{
// different JSON pointers
json::json_pointer ptr0;
json::json_pointer ptr1("");
json::json_pointer ptr2("/foo");
// compare JSON pointers
std::cout << std::boolalpha
<< "\"" << ptr0 << "\" == \"" << ptr0 << "\": " << (ptr0 == ptr0) << '\n'
<< "\"" << ptr0 << "\" == \"" << ptr1 << "\": " << (ptr0 == ptr1) << '\n'
<< "\"" << ptr1 << "\" == \"" << ptr2 << "\": " << (ptr1 == ptr2) << '\n'
<< "\"" << ptr2 << "\" == \"" << ptr2 << "\": " << (ptr2 == ptr2) << std::endl;
}

View File

@@ -0,0 +1,4 @@
"" == "": true
"" == "": true
"" == "/foo": false
"/foo" == "/foo": true

View File

@@ -0,0 +1,33 @@
#include <exception>
#include <iostream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
int main()
{
// different JSON pointers
json::json_pointer ptr0;
json::json_pointer ptr1("");
json::json_pointer ptr2("/foo");
// different strings
std::string str0("");
std::string str1("/foo");
std::string str2("bar");
// compare JSON pointers and strings
std::cout << std::boolalpha
<< "\"" << ptr0 << "\" == \"" << str0 << "\": " << (ptr0 == str0) << '\n'
<< "\"" << str0 << "\" == \"" << ptr1 << "\": " << (str0 == ptr1) << '\n'
<< "\"" << ptr2 << "\" == \"" << str1 << "\": " << (ptr2 == str1) << std::endl;
try
{
std::cout << "\"" << str2 << "\" == \"" << ptr2 << "\": " << (str2 == ptr2) << std::endl;
}
catch (const json::parse_error& ex)
{
std::cout << ex.what() << std::endl;
}
}

View File

@@ -0,0 +1,4 @@
"" == "": true
"" == "": true
"/foo" == "/foo": true
"bar" == "/foo": [json.exception.parse_error.107] parse error at byte 1: JSON pointer must be empty or begin with '/' - was: 'bar'

View File

@@ -0,0 +1,19 @@
#include <iostream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
int main()
{
// different JSON pointers
json::json_pointer ptr0;
json::json_pointer ptr1("");
json::json_pointer ptr2("/foo");
// compare JSON pointers
std::cout << std::boolalpha
<< "\"" << ptr0 << "\" != \"" << ptr0 << "\": " << (ptr0 != ptr0) << '\n'
<< "\"" << ptr0 << "\" != \"" << ptr1 << "\": " << (ptr0 != ptr1) << '\n'
<< "\"" << ptr1 << "\" != \"" << ptr2 << "\": " << (ptr1 != ptr2) << '\n'
<< "\"" << ptr2 << "\" != \"" << ptr2 << "\": " << (ptr2 != ptr2) << std::endl;
}

View File

@@ -0,0 +1,4 @@
"" != "": false
"" != "": false
"" != "/foo": true
"/foo" != "/foo": false

View File

@@ -0,0 +1,32 @@
#include <iostream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
int main()
{
// different JSON pointers
json::json_pointer ptr0;
json::json_pointer ptr1("");
json::json_pointer ptr2("/foo");
// different strings
std::string str0("");
std::string str1("/foo");
std::string str2("bar");
// compare JSON pointers and strings
std::cout << std::boolalpha
<< "\"" << ptr0 << "\" != \"" << str0 << "\": " << (ptr0 != str0) << '\n'
<< "\"" << str0 << "\" != \"" << ptr1 << "\": " << (str0 != ptr1) << '\n'
<< "\"" << ptr2 << "\" != \"" << str1 << "\": " << (ptr2 != str1) << std::endl;
try
{
std::cout << "\"" << str2 << "\" != \"" << ptr2 << "\": " << (str2 != ptr2) << std::endl;
}
catch (const json::parse_error& ex)
{
std::cout << ex.what() << std::endl;
}
}

View File

@@ -0,0 +1,4 @@
"" != "": false
"" != "": false
"/foo" != "/foo": false
"bar" != "/foo": [json.exception.parse_error.107] parse error at byte 1: JSON pointer must be empty or begin with '/' - was: 'bar'

View File

@@ -7,17 +7,17 @@ int main()
{
// create a JSON pointer
json::json_pointer ptr("/foo");
std::cout << ptr << '\n';
std::cout << "\"" << ptr << "\"\n";
// append a JSON Pointer
ptr /= json::json_pointer("/bar/baz");
std::cout << ptr << '\n';
std::cout << "\"" << ptr << "\"\n";
// append a string
ptr /= "fob";
std::cout << ptr << '\n';
std::cout << "\"" << ptr << "\"\n";
// append an array index
ptr /= 42;
std::cout << ptr << std::endl;
std::cout << "\"" << ptr << "\"" << std::endl;
}

View File

@@ -9,11 +9,11 @@ int main()
json::json_pointer ptr("/foo");
// append a JSON Pointer
std::cout << ptr / json::json_pointer("/bar/baz") << '\n';
std::cout << "\"" << ptr / json::json_pointer("/bar/baz") << "\"\n";
// append a string
std::cout << ptr / "fob" << '\n';
std::cout << "\"" << ptr / "fob" << "\"\n";
// append an array index
std::cout << ptr / 42 << std::endl;
std::cout << "\"" << ptr / 42 << "\"" << std::endl;
}

View File

@@ -12,7 +12,7 @@ int main()
// call parent_pointer()
std::cout << std::boolalpha
<< "parent of " << ptr1 << " is " << ptr1.parent_pointer() << '\n'
<< "parent of " << ptr2 << " is " << ptr2.parent_pointer() << '\n'
<< "parent of " << ptr3 << " is " << ptr3.parent_pointer() << std::endl;
<< "parent of \"" << ptr1 << "\" is \"" << ptr1.parent_pointer() << "\"\n"
<< "parent of \"" << ptr2 << "\" is \"" << ptr2.parent_pointer() << "\"\n"
<< "parent of \"" << ptr3 << "\" is \"" << ptr3.parent_pointer() << "\"" << std::endl;
}

View File

@@ -7,15 +7,15 @@ int main()
{
// create empty JSON Pointer
json::json_pointer ptr("/foo/bar/baz");
std::cout << ptr << '\n';
std::cout << "\"" << ptr << "\"\n";
// call pop_back()
ptr.pop_back();
std::cout << ptr << '\n';
std::cout << "\"" << ptr << "\"\n";
ptr.pop_back();
std::cout << ptr << '\n';
std::cout << "\"" << ptr << "\"\n";
ptr.pop_back();
std::cout << ptr << '\n';
std::cout << "\"" << ptr << "\"\n";
}

View File

@@ -7,15 +7,15 @@ int main()
{
// create empty JSON Pointer
json::json_pointer ptr;
std::cout << ptr << '\n';
std::cout << "\"" << ptr << "\"\n";
// call push_back()
ptr.push_back("foo");
std::cout << ptr << '\n';
std::cout << "\"" << ptr << "\"\n";
ptr.push_back("0");
std::cout << ptr << '\n';
std::cout << "\"" << ptr << "\"\n";
ptr.push_back("bar");
std::cout << ptr << '\n';
std::cout << "\"" << ptr << "\"\n";
}

View File

@@ -19,16 +19,16 @@ int main()
json::json_pointer ptr11("/ ");
json::json_pointer ptr12("/m~0n");
std::cout << ptr1.to_string() << '\n'
<< ptr2.to_string() << '\n'
<< ptr3.to_string() << '\n'
<< ptr4.to_string() << '\n'
<< ptr5.to_string() << '\n'
<< ptr6.to_string() << '\n'
<< ptr7.to_string() << '\n'
<< ptr8.to_string() << '\n'
<< ptr9.to_string() << '\n'
<< ptr10.to_string() << '\n'
<< ptr11.to_string() << '\n'
<< ptr12.to_string() << std::endl;
std::cout << "\"" << ptr1.to_string() << "\"\n"
<< "\"" << ptr2.to_string() << "\"\n"
<< "\"" << ptr3.to_string() << "\"\n"
<< "\"" << ptr4.to_string() << "\"\n"
<< "\"" << ptr5.to_string() << "\"\n"
<< "\"" << ptr6.to_string() << "\"\n"
<< "\"" << ptr7.to_string() << "\"\n"
<< "\"" << ptr8.to_string() << "\"\n"
<< "\"" << ptr9.to_string() << "\"\n"
<< "\"" << ptr10.to_string() << "\"\n"
<< "\"" << ptr11.to_string() << "\"\n"
<< "\"" << ptr12.to_string() << "\"" << std::endl;
}

View File

@@ -1,12 +1,12 @@
/foo
/foo/0
/
/a~1b
/c%d
/e^f
/g|h
/i\j
/k"l
/
/m~0n
""
"/foo"
"/foo/0"
"/"
"/a~1b"
"/c%d"
"/e^f"
"/g|h"
"/i\j"
"/k"l"
"/ "
"/m~0n"

View File

@@ -2,6 +2,6 @@
1
1
1
256204778801521550
1152921504606846975
115292150460684697
576460752303423487
1

View File

@@ -3,6 +3,7 @@
#include <iomanip> // for std::setw
using json = nlohmann::json;
using namespace nlohmann::literals;
int main()
{

View File

@@ -1,8 +1,8 @@
{
"compiler": {
"c++": "201103",
"family": "clang",
"version": "13.0.0 (clang-1300.0.29.30)"
"family": "gcc",
"version": "12.1.0"
},
"copyright": "(C) 2013-2022 Niels Lohmann",
"name": "JSON for Modern C++",
@@ -10,8 +10,8 @@
"url": "https://github.com/nlohmann/json",
"version": {
"major": 3,
"minor": 10,
"patch": 5,
"string": "3.10.5"
"minor": 11,
"patch": 2,
"string": "3.11.2"
}
}

View File

@@ -2,6 +2,7 @@
#include <nlohmann/json.hpp>
using json = nlohmann::json;
using namespace nlohmann::literals;
namespace ns
{

View File

@@ -2,6 +2,7 @@
#include <nlohmann/json.hpp>
using json = nlohmann::json;
using namespace nlohmann::literals;
namespace ns
{

View File

@@ -2,6 +2,7 @@
#include <nlohmann/json.hpp>
using json = nlohmann::json;
using namespace nlohmann::literals;
namespace ns
{

View File

@@ -2,6 +2,7 @@
#include <nlohmann/json.hpp>
using json = nlohmann::json;
using namespace nlohmann::literals;
namespace ns
{

View File

@@ -2,6 +2,7 @@
#include <nlohmann/json.hpp>
using json = nlohmann::json;
using namespace nlohmann::literals;
namespace ns
{

View File

@@ -2,6 +2,7 @@
#include <nlohmann/json.hpp>
using json = nlohmann::json;
using namespace nlohmann::literals;
namespace ns
{

View File

@@ -2,6 +2,7 @@
#include <nlohmann/json.hpp>
using json = nlohmann::json;
using namespace nlohmann::literals;
namespace ns
{

View File

@@ -2,6 +2,7 @@
#include <nlohmann/json.hpp>
using json = nlohmann::json;
using namespace nlohmann::literals;
namespace ns
{

View File

@@ -0,0 +1,14 @@
#include <iostream>
#include <nlohmann/json.hpp>
// possible use case: use NLOHMANN_JSON_NAMESPACE instead of nlohmann
using json = NLOHMANN_JSON_NAMESPACE::json;
// macro needed to output the NLOHMANN_JSON_NAMESPACE as string literal
#define Q(x) #x
#define QUOTE(x) Q(x)
int main()
{
std::cout << QUOTE(NLOHMANN_JSON_NAMESPACE) << std::endl;
}

View File

@@ -0,0 +1 @@
nlohmann::json_abi_v3_11_2

View File

@@ -0,0 +1,33 @@
#include <iostream>
#include <optional>
#include <nlohmann/json.hpp>
// partial specialization (see https://json.nlohmann.me/features/arbitrary_types/)
NLOHMANN_JSON_NAMESPACE_BEGIN
template <typename T>
struct adl_serializer<std::optional<T>>
{
static void to_json(json& j, const std::optional<T>& opt)
{
if (opt == std::nullopt)
{
j = nullptr;
}
else
{
j = *opt;
}
}
};
NLOHMANN_JSON_NAMESPACE_END
int main()
{
std::optional<int> o1 = 1;
std::optional<int> o2 = std::nullopt;
NLOHMANN_JSON_NAMESPACE::json j;
j.push_back(o1);
j.push_back(o2);
std::cout << j << std::endl;
}

View File

@@ -0,0 +1 @@
[1,null]

View File

@@ -0,0 +1,13 @@
#include <iostream>
#define NLOHMANN_JSON_NAMESPACE_NO_VERSION 1
#include <nlohmann/json.hpp>
// macro needed to output the NLOHMANN_JSON_NAMESPACE as string literal
#define Q(x) #x
#define QUOTE(x) Q(x)
int main()
{
std::cout << QUOTE(NLOHMANN_JSON_NAMESPACE) << std::endl;
}

View File

@@ -0,0 +1 @@
nlohmann::json_abi

View File

@@ -1 +1 @@
JSON for Modern C++ version 3.10.5
JSON for Modern C++ version 3.11.2

View File

@@ -0,0 +1,16 @@
#include <iostream>
#include <iomanip>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
int main()
{
nlohmann::json uj1 = {{"version", 1}, {"type", "integer"}};
nlohmann::json uj2 = {{"type", "integer"}, {"version", 1}};
nlohmann::ordered_json oj1 = {{"version", 1}, {"type", "integer"}};
nlohmann::ordered_json oj2 = {{"type", "integer"}, {"version", 1}};
std::cout << std::boolalpha << (uj1 == uj2) << '\n' << (oj1 == oj2) << std::endl;
}

View File

@@ -0,0 +1,2 @@
true
false

View File

@@ -2,6 +2,7 @@
#include <nlohmann/json.hpp>
using json = nlohmann::json;
using namespace nlohmann::literals;
int main()
{

View File

@@ -2,6 +2,7 @@
#include <nlohmann/json.hpp>
using json = nlohmann::json;
using namespace nlohmann::literals;
int main()
{

View File

@@ -0,0 +1,34 @@
#include <iostream>
#include <iomanip>
#include <string_view>
#include <nlohmann/json.hpp>
using namespace std::string_view_literals;
using json = nlohmann::json;
int main()
{
// create a JSON object
json object =
{
{"one", 1}, {"two", 2}, {"three", 2.9}
};
// output element with key "two"
std::cout << object["two"sv] << "\n\n";
// change element with key "three"
object["three"sv] = 3;
// output changed array
std::cout << std::setw(4) << object << "\n\n";
// mention nonexisting key
object["four"sv];
// write to nonexisting key
object["five"sv]["really"sv]["nested"sv] = true;
// output changed object
std::cout << std::setw(4) << object << '\n';
}

View File

@@ -0,0 +1,18 @@
#include <iostream>
#include <string_view>
#include <nlohmann/json.hpp>
using namespace std::string_view_literals;
using json = nlohmann::json;
int main()
{
// create a JSON object
const json object =
{
{"one", 1}, {"two", 2}, {"three", 2.9}
};
// output element with key "two"
std::cout << object["two"sv] << '\n';
}

View File

@@ -0,0 +1,19 @@
2
{
"one": 1,
"three": 3,
"two": 2
}
{
"five": {
"really": {
"nested": true
}
},
"four": null,
"one": 1,
"three": 3,
"two": 2
}

View File

@@ -0,0 +1 @@
2

View File

@@ -3,6 +3,7 @@
#include <nlohmann/json.hpp>
using json = nlohmann::json;
using namespace nlohmann::literals;
int main()
{

View File

@@ -3,6 +3,7 @@
#include <nlohmann/json.hpp>
using json = nlohmann::json;
using namespace nlohmann::literals;
int main()
{

View File

@@ -0,0 +1,13 @@
#include <iostream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
int main()
{
// create JSON poiner
json::json_pointer ptr("/foo/bar/baz");
// write string representation to stream
std::cout << ptr << std::endl;
}

View File

@@ -0,0 +1 @@
/foo/bar/baz

View File

@@ -0,0 +1,41 @@
#include <compare>
#include <iostream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
const char* to_string(const std::partial_ordering& po)
{
if (std::is_lt(po))
{
return "less";
}
else if (std::is_gt(po))
{
return "greater";
}
else if (std::is_eq(po))
{
return "equivalent";
}
return "unordered";
}
int main()
{
// create several JSON values
json array_1 = {1, 2, 3};
json array_2 = {1, 2, 4};
json object_1 = {{"A", "a"}, {"B", "b"}};
json object_2 = {{"B", "b"}, {"A", "a"}};
json number = 17;
json string = "foo";
json discarded = json(json::value_t::discarded);
// output values and comparisons
std::cout << array_1 << " <=> " << array_2 << " := " << to_string(array_1 <=> array_2) << '\n'; // *NOPAD*
std::cout << object_1 << " <=> " << object_2 << " := " << to_string(object_1 <=> object_2) << '\n'; // *NOPAD*
std::cout << string << " <=> " << number << " := " << to_string(string <=> number) << '\n'; // *NOPAD*
std::cout << string << " <=> " << discarded << " := " << to_string(string <=> discarded) << '\n'; // *NOPAD*
}

View File

@@ -0,0 +1,4 @@
[1,2,3] <=> [1,2,4] := less
{"A":"a","B":"b"} <=> {"A":"a","B":"b"} := equivalent
"foo" <=> 17 := greater
"foo" <=> <discarded> := unordered

View File

@@ -0,0 +1,41 @@
#include <compare>
#include <iostream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
const char* to_string(const std::partial_ordering& po)
{
if (std::is_lt(po))
{
return "less";
}
else if (std::is_gt(po))
{
return "greater";
}
else if (std::is_eq(po))
{
return "equivalent";
}
return "unordered";
}
int main()
{
using float_limits = std::numeric_limits<json::number_float_t>;
constexpr auto nan = float_limits::quiet_NaN();
// create several JSON values
json boolean = false;
json number = 17;
json string = "17";
// output values and comparisons
std::cout << std::boolalpha << std::fixed;
std::cout << boolean << " <=> " << true << " := " << to_string(boolean <=> true) << '\n'; // *NOPAD*
std::cout << number << " <=> " << 17.0 << " := " << to_string(number <=> 17.0) << '\n'; // *NOPAD*
std::cout << number << " <=> " << nan << " := " << to_string(number <=> nan) << '\n'; // *NOPAD*
std::cout << string << " <=> " << 17 << " := " << to_string(string <=> 17) << '\n'; // *NOPAD*
}

View File

@@ -0,0 +1,4 @@
false <=> true := less
17 <=> 17.000000 := equivalent
17 <=> nan := unordered
"17" <=> 17 := greater

View File

@@ -2,6 +2,7 @@
#include <nlohmann/json.hpp>
using json = nlohmann::json;
using namespace nlohmann::literals;
int main()
{

View File

@@ -3,6 +3,7 @@
#include <nlohmann/json.hpp>
using json = nlohmann::json;
using namespace nlohmann::literals;
int main()
{

View File

@@ -0,0 +1,35 @@
#include <iostream>
#include <iomanip>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
using namespace nlohmann::literals;
int main()
{
// the original document
json doc = R"(
{
"baz": "qux",
"foo": "bar"
}
)"_json;
// the patch
json patch = R"(
[
{ "op": "replace", "path": "/baz", "value": "boo" },
{ "op": "add", "path": "/hello", "value": ["world"] },
{ "op": "remove", "path": "/foo"}
]
)"_json;
// output original document
std::cout << "Before\n" << std::setw(4) << doc << std::endl;
// apply the patch
doc.patch_inplace(patch);
// output patched document
std::cout << "\nAfter\n" << std::setw(4) << doc << std::endl;
}

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