keyword
stringclasses
7 values
repo_name
stringlengths
8
98
file_path
stringlengths
4
244
file_extension
stringclasses
29 values
file_size
int64
0
84.1M
line_count
int64
0
1.6M
content
stringlengths
1
84.1M
language
stringclasses
14 values
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/erase__IteratorType.cpp
.cpp
862
32
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { // create JSON values json j_boolean = true; json j_number_integer = 17; json j_number_float = 23.42; json j_object = {{"one", 1}, {"two", 2}}; json j_array = {1, 2, 4, 8, 16}; json j_string = "Hello, world"; // call erase() j_boolean.erase(j_boolean.begin()); j_number_integer.erase(j_number_integer.begin()); j_number_float.erase(j_number_float.begin()); j_object.erase(j_object.find("two")); j_array.erase(j_array.begin() + 2); j_string.erase(j_string.begin()); // print values std::cout << j_boolean << '\n'; std::cout << j_number_integer << '\n'; std::cout << j_number_float << '\n'; std::cout << j_object << '\n'; std::cout << j_array << '\n'; std::cout << j_string << '\n'; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/from_json__non_default_constructible.cpp
.cpp
1,182
54
#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.template get<ns::person>(); std::cout << p.name << " (" << p.age << ") lives in " << p.address << std::endl; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/is_string.cpp
.cpp
961
31
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { // create JSON values json j_null; json j_boolean = true; json j_number_integer = 17; json j_number_float = 23.42; json j_number_unsigned_integer = 12345678987654321u; json j_object = {{"one", 1}, {"two", 2}}; json j_array = {1, 2, 4, 8, 16}; json j_string = "Hello, world"; json j_binary = json::binary({1, 2, 3}); // call is_string() std::cout << std::boolalpha; std::cout << j_null.is_string() << '\n'; std::cout << j_boolean.is_string() << '\n'; std::cout << j_number_integer.is_string() << '\n'; std::cout << j_number_unsigned_integer.is_string() << '\n'; std::cout << j_number_float.is_string() << '\n'; std::cout << j_object.is_string() << '\n'; std::cout << j_array.is_string() << '\n'; std::cout << j_string.is_string() << '\n'; std::cout << j_binary.is_string() << '\n'; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/basic_json__nullptr_t.cpp
.cpp
298
17
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { // implicitly create a JSON null value json j1; // explicitly create a JSON null value json j2(nullptr); // serialize the JSON null value std::cout << j1 << '\n' << j2 << '\n'; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/nlohmann_json_namespace_begin.c++17.cpp
.cpp
687
34
#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; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/value__json_ptr.cpp
.cpp
907
32
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; using namespace nlohmann::literals; int main() { // create a JSON object with different entry types json j = { {"integer", 1}, {"floating", 42.23}, {"string", "hello world"}, {"boolean", true}, {"object", {{"key1", 1}, {"key2", 2}}}, {"array", {1, 2, 3}} }; // access existing values int v_integer = j.value("/integer"_json_pointer, 0); double v_floating = j.value("/floating"_json_pointer, 47.11); // access nonexisting values and rely on default value std::string v_string = j.value("/nonexisting"_json_pointer, "oops"); bool v_boolean = j.value("/nonexisting"_json_pointer, false); // output values std::cout << std::boolalpha << v_integer << " " << v_floating << " " << v_string << " " << v_boolean << "\n"; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/meta.cpp
.cpp
188
12
#include <iostream> #include <iomanip> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { // call meta() std::cout << std::setw(4) << json::meta() << '\n'; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/binary.cpp
.cpp
408
17
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { // create a binary vector std::vector<std::uint8_t> vec = {0xCA, 0xFE, 0xBA, 0xBE}; // create a binary JSON value with subtype 42 json j = json::binary(vec, 42); // output type and subtype std::cout << "type: " << j.type_name() << ", subtype: " << j.get_binary().subtype() << std::endl; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/diagnostics_standard.cpp
.cpp
349
21
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { json j; j["address"]["street"] = "Fake Street"; j["address"]["housenumber"] = "12"; try { int housenumber = j["address"]["housenumber"]; } catch (json::exception& e) { std::cout << e.what() << '\n'; } }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/parse__contiguouscontainer__parser_callback_t.cpp
.cpp
363
16
#include <iostream> #include <iomanip> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { // a JSON text given as std::vector std::vector<std::uint8_t> text = {'[', '1', ',', '2', ',', '3', ']', '\0'}; // parse and serialize JSON json j_complete = json::parse(text); std::cout << std::setw(4) << j_complete << "\n\n"; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/ordered_json.cpp
.cpp
224
15
#include <iostream> #include <nlohmann/json.hpp> using ordered_json = nlohmann::ordered_json; int main() { ordered_json j; j["one"] = 1; j["two"] = 2; j["three"] = 3; std::cout << j.dump(2) << '\n'; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/basic_json__InputIt_InputIt.cpp
.cpp
848
33
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { // create JSON values json j_array = {"alpha", "bravo", "charly", "delta", "easy"}; json j_number = 42; json j_object = {{"one", "eins"}, {"two", "zwei"}}; // create copies using iterators json j_array_range(j_array.begin() + 1, j_array.end() - 2); json j_number_range(j_number.begin(), j_number.end()); json j_object_range(j_object.begin(), j_object.find("two")); // serialize the values std::cout << j_array_range << '\n'; std::cout << j_number_range << '\n'; std::cout << j_object_range << '\n'; // example for an exception try { json j_invalid(j_number.begin() + 1, j_number.end()); } catch (json::invalid_iterator& e) { std::cout << e.what() << '\n'; } }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/json_pointer__operator_add_binary.cpp
.cpp
421
20
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { // create a JSON pointer json::json_pointer ptr("/foo"); // append a JSON Pointer std::cout << "\"" << ptr / json::json_pointer("/bar/baz") << "\"\n"; // append a string std::cout << "\"" << ptr / "fob" << "\"\n"; // append an array index std::cout << "\"" << ptr / 42 << "\"" << std::endl; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/operator_array__object_t_key_type_const.cpp
.cpp
285
17
#include <iostream> #include <nlohmann/json.hpp> 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"] << '\n'; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/nlohmann_define_type_non_intrusive_with_default_explicit.cpp
.cpp
1,669
54
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; using namespace nlohmann::literals; namespace ns { struct person { std::string name = "John Doe"; std::string address = "123 Fake St"; int age = -1; person() = default; person(std::string name_, std::string address_, int age_) : name(std::move(name_)), address(std::move(address_)), age(age_) {} }; void to_json(nlohmann::json& nlohmann_json_j, const person& nlohmann_json_t) { nlohmann_json_j["name"] = nlohmann_json_t.name; nlohmann_json_j["address"] = nlohmann_json_t.address; nlohmann_json_j["age"] = nlohmann_json_t.age; } void from_json(const nlohmann::json& nlohmann_json_j, person& nlohmann_json_t) { person nlohmann_json_default_obj; nlohmann_json_t.name = nlohmann_json_j.value("name", nlohmann_json_default_obj.name); nlohmann_json_t.address = nlohmann_json_j.value("address", nlohmann_json_default_obj.address); nlohmann_json_t.age = nlohmann_json_j.value("age", nlohmann_json_default_obj.age); } } // namespace ns int main() { ns::person p = {"Ned Flanders", "744 Evergreen Terrace", 60}; // serialization: person -> json json j = p; std::cout << "serialization: " << j << std::endl; // deserialization: json -> person json j2 = R"({"address": "742 Evergreen Terrace", "age": 40, "name": "Homer Simpson"})"_json; auto p2 = j2.template get<ns::person>(); // incomplete deserialization: json j3 = R"({"address": "742 Evergreen Terrace", "name": "Maggie Simpson"})"_json; auto p3 = j3.template get<ns::person>(); std::cout << "roundtrip: " << json(p3) << std::endl; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/nlohmann_json_serialize_enum.cpp
.cpp
1,486
60
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; namespace ns { enum TaskState { TS_STOPPED, TS_RUNNING, TS_COMPLETED, TS_INVALID = -1 }; NLOHMANN_JSON_SERIALIZE_ENUM(TaskState, { { TS_INVALID, nullptr }, { TS_STOPPED, "stopped" }, { TS_RUNNING, "running" }, { TS_COMPLETED, "completed" } }) enum class Color { red, green, blue, unknown }; NLOHMANN_JSON_SERIALIZE_ENUM(Color, { { Color::unknown, "unknown" }, { Color::red, "red" }, { Color::green, "green" }, { Color::blue, "blue" } }) } // namespace ns int main() { // serialization json j_stopped = ns::TS_STOPPED; json j_red = ns::Color::red; std::cout << "ns::TS_STOPPED -> " << j_stopped << ", ns::Color::red -> " << j_red << std::endl; // deserialization json j_running = "running"; json j_blue = "blue"; auto running = j_running.template get<ns::TaskState>(); auto blue = j_blue.template get<ns::Color>(); std::cout << j_running << " -> " << running << ", " << j_blue << " -> " << static_cast<int>(blue) << std::endl; // deserializing undefined JSON value to enum // (where the first map entry above is the default) json j_pi = 3.14; auto invalid = j_pi.template get<ns::TaskState>(); auto unknown = j_pi.template get<ns::Color>(); std::cout << j_pi << " -> " << invalid << ", " << j_pi << " -> " << static_cast<int>(unknown) << std::endl; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/json_lines.cpp
.cpp
569
23
#include <sstream> #include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { // JSON Lines (see https://jsonlines.org) std::stringstream input; input << R"({"name": "Gilbert", "wins": [["straight", "7♣"], ["one pair", "10♥"]]} {"name": "Alexa", "wins": [["two pair", "4♠"], ["two pair", "9♠"]]} {"name": "May", "wins": []} {"name": "Deloise", "wins": [["three of a kind", "5♣"]]} )"; std::string line; while (std::getline(input, line)) { std::cout << json::parse(line) << std::endl; } }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/at__object_t_key_type.cpp
.cpp
982
49
#include <iostream> #include <nlohmann/json.hpp> 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" std::cout << object.at("the ugly") << '\n'; // change element with key "the bad" object.at("the bad") = "il cattivo"; // output changed array std::cout << object << '\n'; // exception type_error.304 try { // use at() on a non-object type json str = "I am a string"; str.at("the good") = "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 object.at("the fast") = "il rapido"; } catch (json::out_of_range& e) { std::cout << e.what() << '\n'; } }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/cend.cpp
.cpp
413
20
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { // create an array value json array = {1, 2, 3, 4, 5}; // get an iterator to one past the last element json::const_iterator it = array.cend(); // decrement the iterator to point to the last element --it; // serialize the element that the iterator points to std::cout << *it << '\n'; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/contains__keytype.c++17.cpp
.cpp
635
21
#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; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/operator_array__json_pointer.cpp
.cpp
1,366
50
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; using namespace nlohmann::literals; int main() { // create a JSON value json j = { {"number", 1}, {"string", "foo"}, {"array", {1, 2}} }; // read-only access // output element with JSON pointer "/number" std::cout << j["/number"_json_pointer] << '\n'; // output element with JSON pointer "/string" std::cout << j["/string"_json_pointer] << '\n'; // output element with JSON pointer "/array" std::cout << j["/array"_json_pointer] << '\n'; // output element with JSON pointer "/array/1" std::cout << j["/array/1"_json_pointer] << '\n'; // writing access // change the string j["/string"_json_pointer] = "bar"; // output the changed string std::cout << j["string"] << '\n'; // "change" a nonexisting object entry j["/boolean"_json_pointer] = true; // output the changed object std::cout << j << '\n'; // change an array element j["/array/1"_json_pointer] = 21; // "change" an array element with nonexisting index j["/array/4"_json_pointer] = 44; // output the changed array std::cout << j["array"] << '\n'; // "change" the array element past the end j["/array/-"_json_pointer] = 55; // output the changed array std::cout << j["array"] << '\n'; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/crbegin.cpp
.cpp
350
17
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { // create an array value json array = {1, 2, 3, 4, 5}; // get an iterator to the reverse-beginning json::const_reverse_iterator it = array.crbegin(); // serialize the element that the iterator points to std::cout << *it << '\n'; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/emplace.cpp
.cpp
775
32
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { // create JSON values json object = {{"one", 1}, {"two", 2}}; json null; // print values std::cout << object << '\n'; std::cout << null << '\n'; // add values auto res1 = object.emplace("three", 3); null.emplace("A", "a"); null.emplace("B", "b"); // the following call will not add an object, because there is already // a value stored at key "B" auto res2 = null.emplace("B", "c"); // print values std::cout << object << '\n'; std::cout << *res1.first << " " << std::boolalpha << res1.second << '\n'; std::cout << null << '\n'; std::cout << *res2.first << " " << std::boolalpha << res2.second << '\n'; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/get__ValueType_const.cpp
.cpp
1,417
51
#include <iostream> #include <unordered_map> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { // create a JSON value with different types json json_types = { {"boolean", true}, { "number", { {"integer", 42}, {"floating-point", 17.23} } }, {"string", "Hello, world!"}, {"array", {1, 2, 3, 4, 5}}, {"null", nullptr} }; // use explicit conversions auto v1 = json_types["boolean"].template get<bool>(); auto v2 = json_types["number"]["integer"].template get<int>(); auto v3 = json_types["number"]["integer"].template get<short>(); auto v4 = json_types["number"]["floating-point"].template get<float>(); auto v5 = json_types["number"]["floating-point"].template get<int>(); auto v6 = json_types["string"].template get<std::string>(); auto v7 = json_types["array"].template get<std::vector<short>>(); auto v8 = json_types.template get<std::unordered_map<std::string, json>>(); // print the conversion results std::cout << v1 << '\n'; std::cout << v2 << ' ' << v3 << '\n'; std::cout << v4 << ' ' << v5 << '\n'; std::cout << v6 << '\n'; for (auto i : v7) { std::cout << i << ' '; } std::cout << "\n\n"; for (auto i : v8) { std::cout << i.first << ": " << i.second << '\n'; } }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/is_null.cpp
.cpp
941
31
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { // create JSON values json j_null; json j_boolean = true; json j_number_integer = 17; json j_number_unsigned_integer = 12345678987654321u; json j_number_float = 23.42; json j_object = {{"one", 1}, {"two", 2}}; json j_array = {1, 2, 4, 8, 16}; json j_string = "Hello, world"; json j_binary = json::binary({1, 2, 3}); // call is_null() std::cout << std::boolalpha; std::cout << j_null.is_null() << '\n'; std::cout << j_boolean.is_null() << '\n'; std::cout << j_number_integer.is_null() << '\n'; std::cout << j_number_unsigned_integer.is_null() << '\n'; std::cout << j_number_float.is_null() << '\n'; std::cout << j_object.is_null() << '\n'; std::cout << j_array.is_null() << '\n'; std::cout << j_string.is_null() << '\n'; std::cout << j_binary.is_null() << '\n'; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/size.cpp
.cpp
864
30
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { // create JSON values json j_null; json j_boolean = true; json j_number_integer = 17; json j_number_float = 23.42; json j_object = {{"one", 1}, {"two", 2}}; json j_object_empty(json::value_t::object); json j_array = {1, 2, 4, 8, 16}; json j_array_empty(json::value_t::array); json j_string = "Hello, world"; // call size() std::cout << j_null.size() << '\n'; std::cout << j_boolean.size() << '\n'; std::cout << j_number_integer.size() << '\n'; std::cout << j_number_float.size() << '\n'; std::cout << j_object.size() << '\n'; std::cout << j_object_empty.size() << '\n'; std::cout << j_array.size() << '\n'; std::cout << j_array_empty.size() << '\n'; std::cout << j_string.size() << '\n'; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/json_pointer__operator__notequal.cpp
.cpp
645
20
#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; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/erase__keytype.c++17.cpp
.cpp
466
21
#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'; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/to_bson.cpp
.cpp
507
23
#include <iostream> #include <iomanip> #include <nlohmann/json.hpp> using json = nlohmann::json; using namespace nlohmann::literals; int main() { // create a JSON value json j = R"({"compact": true, "schema": 0})"_json; // serialize it to BSON std::vector<std::uint8_t> v = json::to_bson(j); // print the vector content for (auto& byte : v) { std::cout << "0x" << std::hex << std::setw(2) << std::setfill('0') << (int)byte << " "; } std::cout << std::endl; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/object.cpp
.cpp
701
29
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { // create JSON objects json j_no_init_list = json::object(); json j_empty_init_list = json::object({}); json j_list_of_pairs = json::object({ {"one", 1}, {"two", 2} }); // serialize the JSON objects std::cout << j_no_init_list << '\n'; std::cout << j_empty_init_list << '\n'; std::cout << j_list_of_pairs << '\n'; // example for an exception try { // can only create an object from a list of pairs json j_invalid_object = json::object({{ "one", 1, 2 }}); } catch (json::type_error& e) { std::cout << e.what() << '\n'; } }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/dump.cpp
.cpp
1,472
49
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { // create JSON values json j_object = {{"one", 1}, {"two", 2}}; json j_array = {1, 2, 4, 8, 16}; json j_string = "Hellö 😀!"; // call dump() std::cout << "objects:" << '\n' << j_object.dump() << "\n\n" << j_object.dump(-1) << "\n\n" << j_object.dump(0) << "\n\n" << j_object.dump(4) << "\n\n" << j_object.dump(1, '\t') << "\n\n"; std::cout << "arrays:" << '\n' << j_array.dump() << "\n\n" << j_array.dump(-1) << "\n\n" << j_array.dump(0) << "\n\n" << j_array.dump(4) << "\n\n" << j_array.dump(1, '\t') << "\n\n"; std::cout << "strings:" << '\n' << j_string.dump() << '\n' << j_string.dump(-1, ' ', true) << '\n'; // create JSON value with invalid UTF-8 byte sequence json j_invalid = "ä\xA9ü"; try { std::cout << j_invalid.dump() << std::endl; } catch (json::type_error& e) { std::cout << e.what() << std::endl; } std::cout << "string with replaced invalid characters: " << j_invalid.dump(-1, ' ', false, json::error_handler_t::replace) << "\nstring with ignored invalid characters: " << j_invalid.dump(-1, ' ', false, json::error_handler_t::ignore) << '\n'; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/diagnostics_extended.cpp
.cpp
378
23
#include <iostream> # define JSON_DIAGNOSTICS 1 #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { json j; j["address"]["street"] = "Fake Street"; j["address"]["housenumber"] = "12"; try { int housenumber = j["address"]["housenumber"]; } catch (json::exception& e) { std::cout << e.what() << '\n'; } }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/json_pointer__parent_pointer.cpp
.cpp
576
19
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { // different JSON Pointers json::json_pointer ptr1(""); json::json_pointer ptr2("/foo"); json::json_pointer ptr3("/foo/0"); // 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; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/operator_array__json_pointer_const.cpp
.cpp
684
26
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; using namespace nlohmann::literals; int main() { // create a JSON value const json j = { {"number", 1}, {"string", "foo"}, {"array", {1, 2}} }; // read-only access // output element with JSON pointer "/number" std::cout << j["/number"_json_pointer] << '\n'; // output element with JSON pointer "/string" std::cout << j["/string"_json_pointer] << '\n'; // output element with JSON pointer "/array" std::cout << j["/array"_json_pointer] << '\n'; // output element with JSON pointer "/array/1" std::cout << j["/array/1"_json_pointer] << '\n'; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/from_json__default_constructible.cpp
.cpp
664
38
#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.template get<ns::person>(); std::cout << p.name << " (" << p.age << ") lives in " << p.address << std::endl; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/json_pointer__operator__equal_stringtype.cpp
.cpp
917
34
#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; } }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/diff.cpp
.cpp
741
38
#include <iostream> #include <iomanip> #include <nlohmann/json.hpp> using json = nlohmann::json; using namespace nlohmann::literals; int main() { // the source document json source = R"( { "baz": "qux", "foo": "bar" } )"_json; // the target document json target = R"( { "baz": "boo", "hello": [ "world" ] } )"_json; // create the patch json patch = json::diff(source, target); // roundtrip json patched_source = source.patch(patch); // output patch and roundtrip result std::cout << std::setw(4) << patch << "\n\n" << std::setw(4) << patched_source << std::endl; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/swap__object_t.cpp
.cpp
494
21
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { // create a JSON value json value = { {"translation", {{"one", "eins"}, {"two", "zwei"}}} }; // create an object_t json::object_t object = {{"cow", "Kuh"}, {"dog", "Hund"}}; // swap the object stored in the JSON value value["translation"].swap(object); // output the values std::cout << "value = " << value << '\n'; std::cout << "object = " << object << '\n'; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/parse__string__parser_callback_t.cpp
.cpp
1,193
50
#include <iostream> #include <iomanip> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { // a JSON text auto text = R"( { "Image": { "Width": 800, "Height": 600, "Title": "View from 15th Floor", "Thumbnail": { "Url": "http://www.example.com/image/481989943", "Height": 125, "Width": 100 }, "Animated" : false, "IDs": [116, 943, 234, 38793] } } )"; // parse and serialize JSON json j_complete = json::parse(text); std::cout << std::setw(4) << j_complete << "\n\n"; // define parser callback json::parser_callback_t cb = [](int depth, json::parse_event_t event, json & parsed) { // skip object elements with key "Thumbnail" if (event == json::parse_event_t::key and parsed == json("Thumbnail")) { return false; } else { return true; } }; // parse (with callback) and serialize JSON json j_filtered = json::parse(text, cb); std::cout << std::setw(4) << j_filtered << '\n'; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/json_pointer__to_string.cpp
.cpp
1,210
35
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { // different JSON Pointers json::json_pointer ptr1(""); json::json_pointer ptr2("/foo"); json::json_pointer ptr3("/foo/0"); json::json_pointer ptr4("/"); json::json_pointer ptr5("/a~1b"); json::json_pointer ptr6("/c%d"); json::json_pointer ptr7("/e^f"); json::json_pointer ptr8("/g|h"); json::json_pointer ptr9("/i\\j"); json::json_pointer ptr10("/k\"l"); 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; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/at__size_type.cpp
.cpp
883
44
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { // create JSON array json array = {"first", "2nd", "third", "fourth"}; // output element at index 2 (third element) std::cout << array.at(2) << '\n'; // change element at index 1 (second element) to "second" array.at(1) = "second"; // output changed array std::cout << array << '\n'; // exception type_error.304 try { // use at() on a non-array type json str = "I am a string"; str.at(0) = "Another string"; } catch (json::type_error& e) { std::cout << e.what() << '\n'; } // exception out_of_range.401 try { // try to write beyond the array limit array.at(5) = "sixth"; } catch (json::out_of_range& e) { std::cout << e.what() << '\n'; } }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/erase__object_t_key_type.cpp
.cpp
396
19
#include <iostream> #include <nlohmann/json.hpp> 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"); auto count_three = j_object.erase("three"); // print values std::cout << j_object << '\n'; std::cout << count_one << " " << count_three << '\n'; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/operator_array__keytype.c++17.cpp
.cpp
734
35
#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'; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/get__PointerType.cpp
.cpp
666
22
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { // create a JSON number json value = 17; // explicitly getting pointers auto p1 = value.template get<const json::number_integer_t*>(); auto p2 = value.template get<json::number_integer_t*>(); auto p3 = value.template get<json::number_integer_t* const>(); auto p4 = value.template get<const json::number_integer_t* const>(); auto p5 = value.template get<json::number_float_t*>(); // print the pointees std::cout << *p1 << ' ' << *p2 << ' ' << *p3 << ' ' << *p4 << '\n'; std::cout << std::boolalpha << (p5 == nullptr) << '\n'; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/type.cpp
.cpp
1,031
29
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { // create JSON values json j_null; json j_boolean = true; json j_number_integer = -17; json j_number_unsigned = 42u; json j_number_float = 23.42; json j_object = {{"one", 1}, {"two", 2}}; json j_array = {1, 2, 4, 8, 16}; json j_string = "Hello, world"; // call type() std::cout << std::boolalpha; std::cout << (j_null.type() == json::value_t::null) << '\n'; std::cout << (j_boolean.type() == json::value_t::boolean) << '\n'; std::cout << (j_number_integer.type() == json::value_t::number_integer) << '\n'; std::cout << (j_number_unsigned.type() == json::value_t::number_unsigned) << '\n'; std::cout << (j_number_float.type() == json::value_t::number_float) << '\n'; std::cout << (j_object.type() == json::value_t::object) << '\n'; std::cout << (j_array.type() == json::value_t::array) << '\n'; std::cout << (j_string.type() == json::value_t::string) << '\n'; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/empty.cpp
.cpp
907
31
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { // create JSON values json j_null; json j_boolean = true; json j_number_integer = 17; json j_number_float = 23.42; json j_object = {{"one", 1}, {"two", 2}}; json j_object_empty(json::value_t::object); json j_array = {1, 2, 4, 8, 16}; json j_array_empty(json::value_t::array); json j_string = "Hello, world"; // call empty() std::cout << std::boolalpha; std::cout << j_null.empty() << '\n'; std::cout << j_boolean.empty() << '\n'; std::cout << j_number_integer.empty() << '\n'; std::cout << j_number_float.empty() << '\n'; std::cout << j_object.empty() << '\n'; std::cout << j_object_empty.empty() << '\n'; std::cout << j_array.empty() << '\n'; std::cout << j_array_empty.empty() << '\n'; std::cout << j_string.empty() << '\n'; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/default_object_comparator_t.cpp
.cpp
325
12
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { std::cout << std::boolalpha << "one < two : " << json::default_object_comparator_t{}("one", "two") << "\n" << "three < four : " << json::default_object_comparator_t{}("three", "four") << std::endl; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/swap__binary_t.cpp
.cpp
432
21
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { // create a binary value json value = json::binary({1, 2, 3}); // create a binary_t json::binary_t binary = {{4, 5, 6}}; // swap the object stored in the JSON value value.swap(binary); // output the values std::cout << "value = " << value << '\n'; std::cout << "binary = " << json(binary) << '\n'; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/from_bjdata.cpp
.cpp
574
21
#include <iostream> #include <iomanip> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { // create byte vector std::vector<std::uint8_t> v = {0x7B, 0x69, 0x07, 0x63, 0x6F, 0x6D, 0x70, 0x61, 0x63, 0x74, 0x54, 0x69, 0x06, 0x73, 0x63, 0x68, 0x65, 0x6D, 0x61, 0x69, 0x00, 0x7D }; // deserialize it with BJData json j = json::from_bjdata(v); // print the deserialized JSON value std::cout << std::setw(2) << j << std::endl; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/to_bjdata.cpp
.cpp
1,481
65
#include <iostream> #include <iomanip> #include <nlohmann/json.hpp> using json = nlohmann::json; using namespace nlohmann::literals; // function to print BJData's diagnostic format void print_byte(uint8_t byte) { if (32 < byte and byte < 128) { std::cout << (char)byte; } else { std::cout << (int)byte; } } int main() { // create a JSON value json j = R"({"compact": true, "schema": false})"_json; // serialize it to BJData std::vector<std::uint8_t> v = json::to_bjdata(j); // print the vector content for (auto& byte : v) { print_byte(byte); } std::cout << std::endl; // create an array of numbers json array = {1, 2, 3, 4, 5, 6, 7, 8}; // serialize it to BJData using default representation std::vector<std::uint8_t> v_array = json::to_bjdata(array); // serialize it to BJData using size optimization std::vector<std::uint8_t> v_array_size = json::to_bjdata(array, true); // serialize it to BJData using type optimization std::vector<std::uint8_t> v_array_size_and_type = json::to_bjdata(array, true, true); // print the vector contents for (auto& byte : v_array) { print_byte(byte); } std::cout << std::endl; for (auto& byte : v_array_size) { print_byte(byte); } std::cout << std::endl; for (auto& byte : v_array_size_and_type) { print_byte(byte); } std::cout << std::endl; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/parse__array__parser_callback_t.cpp
.cpp
678
31
#include <iostream> #include <iomanip> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { // a JSON text char text[] = R"( { "Image": { "Width": 800, "Height": 600, "Title": "View from 15th Floor", "Thumbnail": { "Url": "http://www.example.com/image/481989943", "Height": 125, "Width": 100 }, "Animated" : false, "IDs": [116, 943, 234, 38793] } } )"; // parse and serialize JSON json j_complete = json::parse(text); std::cout << std::setw(4) << j_complete << "\n\n"; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/json_pointer.cpp
.cpp
1,038
48
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { // correct JSON pointers json::json_pointer p1; json::json_pointer p2(""); json::json_pointer p3("/"); json::json_pointer p4("//"); json::json_pointer p5("/foo/bar"); json::json_pointer p6("/foo/bar/-"); json::json_pointer p7("/foo/~0"); json::json_pointer p8("/foo/~1"); // error: JSON pointer does not begin with a slash try { json::json_pointer p9("foo"); } catch (json::parse_error& e) { std::cout << e.what() << '\n'; } // error: JSON pointer uses escape symbol ~ not followed by 0 or 1 try { json::json_pointer p10("/foo/~"); } catch (json::parse_error& e) { std::cout << e.what() << '\n'; } // error: JSON pointer uses escape symbol ~ not followed by 0 or 1 try { json::json_pointer p11("/foo/~3"); } catch (json::parse_error& e) { std::cout << e.what() << '\n'; } }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/nlohmann_define_type_intrusive_macro.cpp
.cpp
1,197
49
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; using namespace nlohmann::literals; namespace ns { class person { private: std::string name = "John Doe"; std::string address = "123 Fake St"; int age = -1; public: person() = default; person(std::string name_, std::string address_, int age_) : name(std::move(name_)), address(std::move(address_)), age(age_) {} NLOHMANN_DEFINE_TYPE_INTRUSIVE(person, name, address, age) }; } // namespace ns int main() { ns::person p = {"Ned Flanders", "744 Evergreen Terrace", 60}; // serialization: person -> json json j = p; std::cout << "serialization: " << j << std::endl; // deserialization: json -> person json j2 = R"({"address": "742 Evergreen Terrace", "age": 40, "name": "Homer Simpson"})"_json; auto p2 = j2.template get<ns::person>(); // incomplete deserialization: json j3 = R"({"address": "742 Evergreen Terrace", "name": "Maggie Simpson"})"_json; try { auto p3 = j3.template get<ns::person>(); } catch (json::exception& e) { std::cout << "deserialization failed: " << e.what() << std::endl; } }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/nlohmann_json_serialize_enum_2.cpp
.cpp
810
34
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; namespace ns { enum class Color { red, green, blue, unknown }; NLOHMANN_JSON_SERIALIZE_ENUM(Color, { { Color::unknown, "unknown" }, { Color::red, "red" }, { Color::green, "green" }, { Color::blue, "blue" }, { Color::red, "rot" } // a second conversion for Color::red }) } int main() { // serialization json j_red = ns::Color::red; std::cout << static_cast<int>(ns::Color::red) << " -> " << j_red << std::endl; // deserialization json j_rot = "rot"; auto rot = j_rot.template get<ns::Color>(); auto red = j_red.template get<ns::Color>(); std::cout << j_rot << " -> " << static_cast<int>(rot) << std::endl; std::cout << j_red << " -> " << static_cast<int>(red) << std::endl; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/operator__value_t.cpp
.cpp
1,361
39
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { // create JSON values json j_null; json j_boolean = true; json j_number_integer = -17; json j_number_unsigned = 42u; json j_number_float = 23.42; json j_object = {{"one", 1}, {"two", 2}}; json j_array = {1, 2, 4, 8, 16}; json j_string = "Hello, world"; // call operator value_t() json::value_t t_null = j_null; json::value_t t_boolean = j_boolean; json::value_t t_number_integer = j_number_integer; json::value_t t_number_unsigned = j_number_unsigned; json::value_t t_number_float = j_number_float; json::value_t t_object = j_object; json::value_t t_array = j_array; json::value_t t_string = j_string; // print types std::cout << std::boolalpha; std::cout << (t_null == json::value_t::null) << '\n'; std::cout << (t_boolean == json::value_t::boolean) << '\n'; std::cout << (t_number_integer == json::value_t::number_integer) << '\n'; std::cout << (t_number_unsigned == json::value_t::number_unsigned) << '\n'; std::cout << (t_number_float == json::value_t::number_float) << '\n'; std::cout << (t_object == json::value_t::object) << '\n'; std::cout << (t_array == json::value_t::array) << '\n'; std::cout << (t_string == json::value_t::string) << '\n'; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/std_hash.cpp
.cpp
803
20
#include <iostream> #include <iomanip> #include <nlohmann/json.hpp> using json = nlohmann::json; using namespace nlohmann::literals; int main() { std::cout << "hash(null) = " << std::hash<json> {}(json(nullptr)) << '\n' << "hash(false) = " << std::hash<json> {}(json(false)) << '\n' << "hash(0) = " << std::hash<json> {}(json(0)) << '\n' << "hash(0U) = " << std::hash<json> {}(json(0U)) << '\n' << "hash(\"\") = " << std::hash<json> {}(json("")) << '\n' << "hash({}) = " << std::hash<json> {}(json::object()) << '\n' << "hash([]) = " << std::hash<json> {}(json::array()) << '\n' << "hash({\"hello\": \"world\"}) = " << std::hash<json> {}("{\"hello\": \"world\"}"_json) << std::endl; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/get_allocator.cpp
.cpp
401
19
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { auto alloc = json::get_allocator(); using traits_t = std::allocator_traits<decltype(alloc)>; json* j = traits_t::allocate(alloc, 1); traits_t::construct(alloc, j, "Hello, world!"); std::cout << *j << std::endl; traits_t::destroy(alloc, j); traits_t::deallocate(alloc, j, 1); }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/push_back.cpp
.cpp
440
26
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { // create JSON values json array = {1, 2, 3, 4, 5}; json null; // print values std::cout << array << '\n'; std::cout << null << '\n'; // add values array.push_back(6); array += 7; null += "first"; null += "second"; // print values std::cout << array << '\n'; std::cout << null << '\n'; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/to_json.cpp
.cpp
513
33
#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 to_json(json& j, const person& p) { j = json{ {"name", p.name}, {"address", p.address}, {"age", p.age} }; } } // namespace ns int main() { ns::person p = {"Ned Flanders", "744 Evergreen Terrace", 60}; json j = p; std::cout << j << std::endl; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/operator__less.cpp
.cpp
821
25
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; 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_1 = 17; json number_2 = 17.0000000000001L; json string_1 = "foo"; json string_2 = "bar"; // output values and comparisons std::cout << std::boolalpha; std::cout << array_1 << " == " << array_2 << " " << (array_1 < array_2) << '\n'; std::cout << object_1 << " == " << object_2 << " " << (object_1 < object_2) << '\n'; std::cout << number_1 << " == " << number_2 << " " << (number_1 < number_2) << '\n'; std::cout << string_1 << " == " << string_2 << " " << (string_1 < string_2) << '\n'; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/operator__greater.cpp
.cpp
817
25
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; 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_1 = 17; json number_2 = 17.0000000000001L; json string_1 = "foo"; json string_2 = "bar"; // output values and comparisons std::cout << std::boolalpha; std::cout << array_1 << " > " << array_2 << " " << (array_1 > array_2) << '\n'; std::cout << object_1 << " > " << object_2 << " " << (object_1 > object_2) << '\n'; std::cout << number_1 << " > " << number_2 << " " << (number_1 > number_2) << '\n'; std::cout << string_1 << " > " << string_2 << " " << (string_1 > string_2) << '\n'; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/exception.cpp
.cpp
432
21
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { try { // calling at() for a non-existing key json j = {{"foo", "bar"}}; json k = j.at("non-existing"); } catch (json::exception& e) { // output exception information std::cout << "message: " << e.what() << '\n' << "exception id: " << e.id << std::endl; } }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/items.cpp
.cpp
514
24
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { // create JSON values json j_object = {{"one", 1}, {"two", 2}}; json j_array = {1, 2, 4, 8, 16}; // example for an object for (auto& x : j_object.items()) { std::cout << "key: " << x.key() << ", value: " << x.value() << '\n'; } // example for an array for (auto& x : j_array.items()) { std::cout << "key: " << x.key() << ", value: " << x.value() << '\n'; } }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/is_boolean.cpp
.cpp
971
31
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { // create JSON values json j_null; json j_boolean = true; json j_number_integer = 17; json j_number_unsigned_integer = 12345678987654321u; json j_number_float = 23.42; json j_object = {{"one", 1}, {"two", 2}}; json j_array = {1, 2, 4, 8, 16}; json j_string = "Hello, world"; json j_binary = json::binary({1, 2, 3}); // call is_boolean() std::cout << std::boolalpha; std::cout << j_null.is_boolean() << '\n'; std::cout << j_boolean.is_boolean() << '\n'; std::cout << j_number_integer.is_boolean() << '\n'; std::cout << j_number_unsigned_integer.is_boolean() << '\n'; std::cout << j_number_float.is_boolean() << '\n'; std::cout << j_object.is_boolean() << '\n'; std::cout << j_array.is_boolean() << '\n'; std::cout << j_string.is_boolean() << '\n'; std::cout << j_binary.is_boolean() << '\n'; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/json_pointer__pop_back.cpp
.cpp
418
22
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { // create empty JSON Pointer json::json_pointer ptr("/foo/bar/baz"); std::cout << "\"" << ptr << "\"\n"; // call pop_back() ptr.pop_back(); std::cout << "\"" << ptr << "\"\n"; ptr.pop_back(); std::cout << "\"" << ptr << "\"\n"; ptr.pop_back(); std::cout << "\"" << ptr << "\"\n"; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/nlohmann_define_type_non_intrusive_explicit.cpp
.cpp
1,406
54
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; using namespace nlohmann::literals; namespace ns { struct person { std::string name; std::string address; int age; }; void to_json(nlohmann::json& nlohmann_json_j, const person& nlohmann_json_t) { nlohmann_json_j["name"] = nlohmann_json_t.name; nlohmann_json_j["address"] = nlohmann_json_t.address; nlohmann_json_j["age"] = nlohmann_json_t.age; } void from_json(const nlohmann::json& nlohmann_json_j, person& nlohmann_json_t) { nlohmann_json_t.name = nlohmann_json_j.at("name"); nlohmann_json_t.address = nlohmann_json_j.at("address"); nlohmann_json_t.age = nlohmann_json_j.at("age"); } } // namespace ns int main() { ns::person p = {"Ned Flanders", "744 Evergreen Terrace", 60}; // serialization: person -> json json j = p; std::cout << "serialization: " << j << std::endl; // deserialization: json -> person json j2 = R"({"address": "742 Evergreen Terrace", "age": 40, "name": "Homer Simpson"})"_json; auto p2 = j2.template get<ns::person>(); // incomplete deserialization: json j3 = R"({"address": "742 Evergreen Terrace", "name": "Maggie Simpson"})"_json; try { auto p3 = j3.template get<ns::person>(); } catch (json::exception& e) { std::cout << "deserialization failed: " << e.what() << std::endl; } }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/erase__IteratorType_IteratorType.cpp
.cpp
978
32
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { // create JSON values json j_boolean = true; json j_number_integer = 17; json j_number_float = 23.42; json j_object = {{"one", 1}, {"two", 2}}; json j_array = {1, 2, 4, 8, 16}; json j_string = "Hello, world"; // call erase() j_boolean.erase(j_boolean.begin(), j_boolean.end()); j_number_integer.erase(j_number_integer.begin(), j_number_integer.end()); j_number_float.erase(j_number_float.begin(), j_number_float.end()); j_object.erase(j_object.find("two"), j_object.end()); j_array.erase(j_array.begin() + 1, j_array.begin() + 3); j_string.erase(j_string.begin(), j_string.end()); // print values std::cout << j_boolean << '\n'; std::cout << j_number_integer << '\n'; std::cout << j_number_float << '\n'; std::cout << j_object << '\n'; std::cout << j_array << '\n'; std::cout << j_string << '\n'; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/to_msgpack.cpp
.cpp
517
23
#include <iostream> #include <iomanip> #include <nlohmann/json.hpp> using json = nlohmann::json; using namespace nlohmann::literals; int main() { // create a JSON value json j = R"({"compact": true, "schema": 0})"_json; // serialize it to MessagePack std::vector<std::uint8_t> v = json::to_msgpack(j); // print the vector content for (auto& byte : v) { std::cout << "0x" << std::hex << std::setw(2) << std::setfill('0') << (int)byte << " "; } std::cout << std::endl; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/object_comparator_t.cpp
.cpp
377
12
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { std::cout << std::boolalpha << "json::object_comparator_t(\"one\", \"two\") = " << json::object_comparator_t{}("one", "two") << "\n" << "json::object_comparator_t(\"three\", \"four\") = " << json::object_comparator_t{}("three", "four") << std::endl; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/insert__count.cpp
.cpp
372
18
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { // create a JSON array json v = {1, 2, 3, 4}; // insert number 7 copies of number 7 before number 3 auto new_pos = v.insert(v.begin() + 2, 7, 7); // output new array and result of insert call std::cout << *new_pos << '\n'; std::cout << v << '\n'; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/byte_container_with_subtype__set_subtype.cpp
.cpp
594
23
#include <iostream> #include <nlohmann/json.hpp> // define a byte container based on std::vector using byte_container_with_subtype = nlohmann::byte_container_with_subtype<std::vector<std::uint8_t>>; using json = nlohmann::json; int main() { std::vector<std::uint8_t> bytes = {{0xca, 0xfe, 0xba, 0xbe}}; // create container without subtype auto c = byte_container_with_subtype(bytes); std::cout << "before calling set_subtype(42): " << json(c) << '\n'; // set the subtype c.set_subtype(42); std::cout << "after calling set_subtype(42): " << json(c) << '\n'; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/from_cbor.cpp
.cpp
546
21
#include <iostream> #include <iomanip> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { // create byte vector std::vector<std::uint8_t> v = {0xa2, 0x67, 0x63, 0x6f, 0x6d, 0x70, 0x61, 0x63, 0x74, 0xf5, 0x66, 0x73, 0x63, 0x68, 0x65, 0x6d, 0x61, 0x00 }; // deserialize it with CBOR json j = json::from_cbor(v); // print the deserialized JSON value std::cout << std::setw(2) << j << std::endl; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/out_of_range.cpp
.cpp
412
21
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { try { // calling at() for an invalid index json j = {1, 2, 3, 4}; j.at(4) = 10; } catch (json::out_of_range& e) { // output exception information std::cout << "message: " << e.what() << '\n' << "exception id: " << e.id << std::endl; } }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/operator_array__size_type_const.cpp
.cpp
268
14
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { // create JSON array const json array = {"first", "2nd", "third", "fourth"}; // output element at index 2 (third element) std::cout << array.at(2) << '\n'; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/json_pointer__back.cpp
.cpp
421
16
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { // different JSON Pointers json::json_pointer ptr1("/foo"); 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; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/error_handler_t.cpp
.cpp
652
25
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { // create JSON value with invalid UTF-8 byte sequence json j_invalid = "ä\xA9ü"; try { std::cout << j_invalid.dump() << std::endl; } catch (json::type_error& e) { std::cout << e.what() << std::endl; } std::cout << "string with replaced invalid characters: " << j_invalid.dump(-1, ' ', false, json::error_handler_t::replace) << "\nstring with ignored invalid characters: " << j_invalid.dump(-1, ' ', false, json::error_handler_t::ignore) << '\n'; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/std_swap.cpp
.cpp
382
20
#include <iostream> #include <iomanip> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { // create JSON values json j1 = {{"one", 1}, {"two", 2}}; json j2 = {1, 2, 4, 8, 16}; std::cout << "j1 = " << j1 << " | j2 = " << j2 << '\n'; // swap values std::swap(j1, j2); std::cout << "j1 = " << j1 << " | j2 = " << j2 << std::endl; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/nlohmann_define_type_intrusive_explicit.cpp
.cpp
1,690
61
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; using namespace nlohmann::literals; namespace ns { class person { private: std::string name = "John Doe"; std::string address = "123 Fake St"; int age = -1; public: person() = default; person(std::string name_, std::string address_, int age_) : name(std::move(name_)), address(std::move(address_)), age(age_) {} friend void to_json(nlohmann::json& nlohmann_json_j, const person& nlohmann_json_t) { nlohmann_json_j["name"] = nlohmann_json_t.name; nlohmann_json_j["address"] = nlohmann_json_t.address; nlohmann_json_j["age"] = nlohmann_json_t.age; } friend void from_json(const nlohmann::json& nlohmann_json_j, person& nlohmann_json_t) { nlohmann_json_t.name = nlohmann_json_j.at("name"); nlohmann_json_t.address = nlohmann_json_j.at("address"); nlohmann_json_t.age = nlohmann_json_j.at("age"); } }; } // namespace ns int main() { ns::person p = {"Ned Flanders", "744 Evergreen Terrace", 60}; // serialization: person -> json json j = p; std::cout << "serialization: " << j << std::endl; // deserialization: json -> person json j2 = R"({"address": "742 Evergreen Terrace", "age": 40, "name": "Homer Simpson"})"_json; auto p2 = j2.template get<ns::person>(); // incomplete deserialization: json j3 = R"({"address": "742 Evergreen Terrace", "name": "Maggie Simpson"})"_json; try { auto p3 = j3.template get<ns::person>(); } catch (json::exception& e) { std::cout << "deserialization failed: " << e.what() << std::endl; } }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/operator__equal__specializations.cpp
.cpp
474
17
#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; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/is_array.cpp
.cpp
951
31
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { // create JSON values json j_null; json j_boolean = true; json j_number_integer = 17; json j_number_unsigned_integer = 12345678987654321u; json j_number_float = 23.42; json j_object = {{"one", 1}, {"two", 2}}; json j_array = {1, 2, 4, 8, 16}; json j_string = "Hello, world"; json j_binary = json::binary({1, 2, 3}); // call is_array() std::cout << std::boolalpha; std::cout << j_null.is_array() << '\n'; std::cout << j_boolean.is_array() << '\n'; std::cout << j_number_integer.is_array() << '\n'; std::cout << j_number_unsigned_integer.is_array() << '\n'; std::cout << j_number_float.is_array() << '\n'; std::cout << j_object.is_array() << '\n'; std::cout << j_array.is_array() << '\n'; std::cout << j_string.is_array() << '\n'; std::cout << j_binary.is_array() << '\n'; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/json_pointer__operator__notequal_stringtype.cpp
.cpp
896
33
#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; } }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/emplace_back.cpp
.cpp
452
25
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { // create JSON values json array = {1, 2, 3, 4, 5}; json null; // print values std::cout << array << '\n'; std::cout << null << '\n'; // add values array.emplace_back(6); null.emplace_back("first"); null.emplace_back(3, "second"); // print values std::cout << array << '\n'; std::cout << null << '\n'; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/number_float_t.cpp
.cpp
213
11
#include <iostream> #include <iomanip> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { std::cout << std::boolalpha << std::is_same<double, json::number_float_t>::value << std::endl; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/nlohmann_define_type_non_intrusive_with_default_macro.cpp
.cpp
1,106
41
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; using namespace nlohmann::literals; namespace ns { struct person { std::string name = "John Doe"; std::string address = "123 Fake St"; int age = -1; person() = default; person(std::string name_, std::string address_, int age_) : name(std::move(name_)), address(std::move(address_)), age(age_) {} }; NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(person, name, address, age) } // namespace ns int main() { ns::person p = {"Ned Flanders", "744 Evergreen Terrace", 60}; // serialization: person -> json json j = p; std::cout << "serialization: " << j << std::endl; // deserialization: json -> person json j2 = R"({"address": "742 Evergreen Terrace", "age": 40, "name": "Homer Simpson"})"_json; auto p2 = j2.template get<ns::person>(); // incomplete deserialization: json j3 = R"({"address": "742 Evergreen Terrace", "name": "Maggie Simpson"})"_json; auto p3 = j3.template get<ns::person>(); std::cout << "roundtrip: " << json(p3) << std::endl; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/byte_container_with_subtype__clear_subtype.cpp
.cpp
576
22
#include <iostream> #include <nlohmann/json.hpp> // define a byte container based on std::vector using byte_container_with_subtype = nlohmann::byte_container_with_subtype<std::vector<std::uint8_t>>; using json = nlohmann::json; int main() { std::vector<std::uint8_t> bytes = {{0xca, 0xfe, 0xba, 0xbe}}; // create container with subtype auto c1 = byte_container_with_subtype(bytes, 42); std::cout << "before calling clear_subtype(): " << json(c1) << '\n'; c1.clear_subtype(); std::cout << "after calling clear_subtype(): " << json(c1) << '\n'; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/insert.cpp
.cpp
352
18
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { // create a JSON array json v = {1, 2, 3, 4}; // insert number 10 before number 3 auto new_pos = v.insert(v.begin() + 2, 10); // output new array and result of insert call std::cout << *new_pos << '\n'; std::cout << v << '\n'; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/is_number_float.cpp
.cpp
1,021
31
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { // create JSON values json j_null; json j_boolean = true; json j_number_integer = 17; json j_number_unsigned_integer = 12345678987654321u; json j_number_float = 23.42; json j_object = {{"one", 1}, {"two", 2}}; json j_array = {1, 2, 4, 8, 16}; json j_string = "Hello, world"; json j_binary = json::binary({1, 2, 3}); // call is_number_float() std::cout << std::boolalpha; std::cout << j_null.is_number_float() << '\n'; std::cout << j_boolean.is_number_float() << '\n'; std::cout << j_number_integer.is_number_float() << '\n'; std::cout << j_number_unsigned_integer.is_number_float() << '\n'; std::cout << j_number_float.is_number_float() << '\n'; std::cout << j_object.is_number_float() << '\n'; std::cout << j_array.is_number_float() << '\n'; std::cout << j_string.is_number_float() << '\n'; std::cout << j_binary.is_number_float() << '\n'; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/update__range.cpp
.cpp
727
25
#include <iostream> #include <iomanip> #include <nlohmann/json.hpp> using json = nlohmann::json; using namespace nlohmann::literals; int main() { // create two JSON objects json o1 = R"( {"color": "red", "price": 17.99, "names": {"de": "Flugzeug"}} )"_json; json o2 = R"( {"color": "blue", "speed": 100, "names": {"en": "plane"}} )"_json; json o3 = o1; // add all keys from o2 to o1 (updating "color", replacing "names") o1.update(o2.begin(), o2.end()); // add all keys from o2 to o1 (updating "color", merging "names") o3.update(o2.begin(), o2.end(), true); // output updated object o1 and o3 std::cout << std::setw(2) << o1 << '\n'; std::cout << std::setw(2) << o3 << '\n'; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/basic_json__basic_json.cpp
.cpp
338
18
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { // create a JSON array json j1 = {"one", "two", 3, 4.5, false}; // create a copy json j2(j1); // serialize the JSON array std::cout << j1 << " = " << j2 << '\n'; std::cout << std::boolalpha << (j1 == j2) << '\n'; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/from_msgpack.cpp
.cpp
556
21
#include <iostream> #include <iomanip> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { // create byte vector std::vector<std::uint8_t> v = {0x82, 0xa7, 0x63, 0x6f, 0x6d, 0x70, 0x61, 0x63, 0x74, 0xc3, 0xa6, 0x73, 0x63, 0x68, 0x65, 0x6d, 0x61, 0x00 }; // deserialize it with MessagePack json j = json::from_msgpack(v); // print the deserialized JSON value std::cout << std::setw(2) << j << std::endl; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/operator_deserialize.cpp
.cpp
524
27
#include <iostream> #include <iomanip> #include <sstream> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { // create stream with serialized JSON std::stringstream ss; ss << R"({ "number": 23, "string": "Hello, world!", "array": [1, 2, 3, 4, 5], "boolean": false, "null": null })"; // create JSON value and read the serialization from the stream json j; ss >> j; // serialize JSON std::cout << std::setw(2) << j << '\n'; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/rend.cpp
.cpp
406
20
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { // create an array value json array = {1, 2, 3, 4, 5}; // get an iterator to the reverse-end json::reverse_iterator it = array.rend(); // increment the iterator to point to the first element --it; // serialize the element that the iterator points to std::cout << *it << '\n'; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/to_ubjson.cpp
.cpp
1,481
65
#include <iostream> #include <iomanip> #include <nlohmann/json.hpp> using json = nlohmann::json; using namespace nlohmann::literals; // function to print UBJSON's diagnostic format void print_byte(uint8_t byte) { if (32 < byte and byte < 128) { std::cout << (char)byte; } else { std::cout << (int)byte; } } int main() { // create a JSON value json j = R"({"compact": true, "schema": false})"_json; // serialize it to UBJSON std::vector<std::uint8_t> v = json::to_ubjson(j); // print the vector content for (auto& byte : v) { print_byte(byte); } std::cout << std::endl; // create an array of numbers json array = {1, 2, 3, 4, 5, 6, 7, 8}; // serialize it to UBJSON using default representation std::vector<std::uint8_t> v_array = json::to_ubjson(array); // serialize it to UBJSON using size optimization std::vector<std::uint8_t> v_array_size = json::to_ubjson(array, true); // serialize it to UBJSON using type optimization std::vector<std::uint8_t> v_array_size_and_type = json::to_ubjson(array, true, true); // print the vector contents for (auto& byte : v_array) { print_byte(byte); } std::cout << std::endl; for (auto& byte : v_array_size) { print_byte(byte); } std::cout << std::endl; for (auto& byte : v_array_size_and_type) { print_byte(byte); } std::cout << std::endl; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/get_ref.cpp
.cpp
573
28
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { // create a JSON number json value = 17; // explicitly getting references auto r1 = value.get_ref<const json::number_integer_t&>(); auto r2 = value.get_ref<json::number_integer_t&>(); // print the values std::cout << r1 << ' ' << r2 << '\n'; // incompatible type throws exception try { auto r3 = value.get_ref<json::number_float_t&>(); } catch (json::type_error& ex) { std::cout << ex.what() << '\n'; } }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/parse__istream__parser_callback_t.cpp
.cpp
1,355
58
#include <iostream> #include <iomanip> #include <sstream> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { // a JSON text auto text = R"( { "Image": { "Width": 800, "Height": 600, "Title": "View from 15th Floor", "Thumbnail": { "Url": "http://www.example.com/image/481989943", "Height": 125, "Width": 100 }, "Animated" : false, "IDs": [116, 943, 234, 38793] } } )"; // fill a stream with JSON text std::stringstream ss; ss << text; // parse and serialize JSON json j_complete = json::parse(ss); std::cout << std::setw(4) << j_complete << "\n\n"; // define parser callback json::parser_callback_t cb = [](int depth, json::parse_event_t event, json & parsed) { // skip object elements with key "Thumbnail" if (event == json::parse_event_t::key and parsed == json("Thumbnail")) { return false; } else { return true; } }; // fill a stream with JSON text ss.clear(); ss << text; // parse (with callback) and serialize JSON json j_filtered = json::parse(ss, cb); std::cout << std::setw(4) << j_filtered << '\n'; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/basic_json__CompatibleType.cpp
.cpp
6,452
219
#include <iostream> #include <deque> #include <list> #include <forward_list> #include <set> #include <unordered_map> #include <unordered_set> #include <valarray> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { // ============ // object types // ============ // create an object from an object_t value json::object_t object_value = { {"one", 1}, {"two", 2} }; json j_object_t(object_value); // create an object from std::map std::map<std::string, int> c_map { {"one", 1}, {"two", 2}, {"three", 3} }; json j_map(c_map); // create an object from std::unordered_map std::unordered_map<const char*, double> c_umap { {"one", 1.2}, {"two", 2.3}, {"three", 3.4} }; json j_umap(c_umap); // create an object from std::multimap 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 // create an object from std::unordered_multimap 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 // serialize the JSON objects std::cout << j_object_t << '\n'; std::cout << j_map << '\n'; std::cout << j_umap << '\n'; std::cout << j_mmap << '\n'; std::cout << j_ummap << "\n\n"; // =========== // array types // =========== // create an array from an array_t value json::array_t array_value = {"one", "two", 3, 4.5, false}; json j_array_t(array_value); // create an array from std::vector std::vector<int> c_vector {1, 2, 3, 4}; json j_vec(c_vector); // create an array from std::valarray std::valarray<short> c_valarray {10, 9, 8, 7}; json j_valarray(c_valarray); // create an array from std::deque std::deque<double> c_deque {1.2, 2.3, 3.4, 5.6}; json j_deque(c_deque); // create an array from std::list std::list<bool> c_list {true, true, false, true}; json j_list(c_list); // create an array from std::forward_list std::forward_list<std::int64_t> c_flist {12345678909876, 23456789098765, 34567890987654, 45678909876543}; json j_flist(c_flist); // create an array from std::array std::array<unsigned long, 4> c_array {{1, 2, 3, 4}}; json j_array(c_array); // create an array from std::set std::set<std::string> c_set {"one", "two", "three", "four", "one"}; json j_set(c_set); // only one entry for "one" is used // create an array from std::unordered_set std::unordered_set<std::string> c_uset {"one", "two", "three", "four", "one"}; json j_uset(c_uset); // only one entry for "one" is used // create an array from std::multiset std::multiset<std::string> c_mset {"one", "two", "one", "four"}; json j_mset(c_mset); // both entries for "one" are used // create an array from std::unordered_multiset std::unordered_multiset<std::string> c_umset {"one", "two", "one", "four"}; json j_umset(c_umset); // both entries for "one" are used // serialize the JSON arrays std::cout << j_array_t << '\n'; std::cout << j_vec << '\n'; std::cout << j_valarray << '\n'; std::cout << j_deque << '\n'; std::cout << j_list << '\n'; std::cout << j_flist << '\n'; std::cout << j_array << '\n'; std::cout << j_set << '\n'; std::cout << j_uset << '\n'; std::cout << j_mset << '\n'; std::cout << j_umset << "\n\n"; // ============ // string types // ============ // create string from a string_t value json::string_t string_value = "The quick brown fox jumps over the lazy dog."; json j_string_t(string_value); // create a JSON string directly from a string literal json j_string_literal("The quick brown fox jumps over the lazy dog."); // create string from std::string std::string s_stdstring = "The quick brown fox jumps over the lazy dog."; json j_stdstring(s_stdstring); // serialize the JSON strings std::cout << j_string_t << '\n'; std::cout << j_string_literal << '\n'; std::cout << j_stdstring << "\n\n"; // ============ // number types // ============ // create a JSON number from number_integer_t json::number_integer_t value_integer_t = -42; json j_integer_t(value_integer_t); // create a JSON number from number_unsigned_t json::number_integer_t value_unsigned_t = 17; json j_unsigned_t(value_unsigned_t); // create a JSON number from an anonymous enum enum { enum_value = 17 }; json j_enum(enum_value); // create values of different integer types short n_short = 42; int n_int = -23; long n_long = 1024; int_least32_t n_int_least32_t = -17; uint8_t n_uint8_t = 8; // create (integer) JSON numbers json j_short(n_short); json j_int(n_int); json j_long(n_long); json j_int_least32_t(n_int_least32_t); json j_uint8_t(n_uint8_t); // create values of different floating-point types json::number_float_t v_ok = 3.141592653589793; json::number_float_t v_nan = NAN; json::number_float_t v_infinity = INFINITY; // create values of different floating-point types float n_float = 42.23; float n_float_nan = 1.0f / 0.0f; double n_double = 23.42; // create (floating point) JSON numbers json j_ok(v_ok); json j_nan(v_nan); json j_infinity(v_infinity); json j_float(n_float); json j_float_nan(n_float_nan); json j_double(n_double); // serialize the JSON numbers std::cout << j_integer_t << '\n'; std::cout << j_unsigned_t << '\n'; std::cout << j_enum << '\n'; std::cout << j_short << '\n'; std::cout << j_int << '\n'; std::cout << j_long << '\n'; std::cout << j_int_least32_t << '\n'; std::cout << j_uint8_t << '\n'; std::cout << j_ok << '\n'; std::cout << j_nan << '\n'; std::cout << j_infinity << '\n'; std::cout << j_float << '\n'; std::cout << j_float_nan << '\n'; std::cout << j_double << "\n\n"; // ============= // boolean types // ============= // create boolean values json j_truth = true; json j_falsity = false; // serialize the JSON booleans std::cout << j_truth << '\n'; std::cout << j_falsity << '\n'; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/basic_json__size_type_basic_json.cpp
.cpp
420
19
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { // create an array by creating copies of a JSON value json value = "Hello"; json array_0 = json(0, value); json array_1 = json(1, value); json array_5 = json(5, value); // serialize the JSON arrays std::cout << array_0 << '\n'; std::cout << array_1 << '\n'; std::cout << array_5 << '\n'; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/parse_error.cpp
.cpp
457
21
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { try { // parsing input with a syntax error json::parse("[1,2,3,]"); } catch (json::parse_error& e) { // output exception information std::cout << "message: " << e.what() << '\n' << "exception id: " << e.id << '\n' << "byte position of error: " << e.byte << std::endl; } }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/json_pointer__operator_string_t.cpp
.cpp
339
20
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { // different JSON Pointers json::json_pointer ptr1("/foo/0"); json::json_pointer ptr2("/a~1b"); // implicit conversion to string std::string s; s += ptr1; s += "\n"; s += ptr2; std::cout << s << std::endl; }
C++
3D
OpenMS/OpenMS
src/openms/extern/nlohmann_json/docs/examples/is_primitive.cpp
.cpp
991
31
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { // create JSON values json j_null; json j_boolean = true; json j_number_integer = 17; json j_number_float = 23.42; json j_number_unsigned_integer = 12345678987654321u; json j_object = {{"one", 1}, {"two", 2}}; json j_array = {1, 2, 4, 8, 16}; json j_string = "Hello, world"; json j_binary = json::binary({1, 2, 3}); // call is_primitive() std::cout << std::boolalpha; std::cout << j_null.is_primitive() << '\n'; std::cout << j_boolean.is_primitive() << '\n'; std::cout << j_number_integer.is_primitive() << '\n'; std::cout << j_number_unsigned_integer.is_primitive() << '\n'; std::cout << j_number_float.is_primitive() << '\n'; std::cout << j_object.is_primitive() << '\n'; std::cout << j_array.is_primitive() << '\n'; std::cout << j_string.is_primitive() << '\n'; std::cout << j_binary.is_primitive() << '\n'; }
C++