title
stringclasses
1 value
text
stringlengths
30
1.11M
id
stringlengths
27
31
doc/examples/basic_json__size_type_basic_json.cpp/main 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'; }
negative_train_query0_00198
doc/examples/insert.cpp/main 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'; }
negative_train_query0_00199
doc/examples/get_to.cpp/main 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} }; bool v1; int v2; short v3; float v4; int v5; std::string v6; std::vector<short> v7; std::unordered_map<std::string, json> v8; // use explicit conversions json_types["boolean"].get_to(v1); json_types["number"]["integer"].get_to(v2); json_types["number"]["integer"].get_to(v3); json_types["number"]["floating-point"].get_to(v4); json_types["number"]["floating-point"].get_to(v5); json_types["string"].get_to(v6); json_types["array"].get_to(v7); json_types.get_to(v8); // 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'; } }
negative_train_query0_00200
doc/examples/is_discarded.cpp/main 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"; // call is_discarded() std::cout << std::boolalpha; std::cout << j_null.is_discarded() << '\n'; std::cout << j_boolean.is_discarded() << '\n'; std::cout << j_number_integer.is_discarded() << '\n'; std::cout << j_number_unsigned_integer.is_discarded() << '\n'; std::cout << j_number_float.is_discarded() << '\n'; std::cout << j_object.is_discarded() << '\n'; std::cout << j_array.is_discarded() << '\n'; std::cout << j_string.is_discarded() << '\n'; }
negative_train_query0_00201
doc/examples/push_back.cpp/main 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'; }
negative_train_query0_00202
doc/examples/back.cpp/main 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_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 back() std::cout << j_boolean.back() << '\n'; std::cout << j_number_integer.back() << '\n'; std::cout << j_number_float.back() << '\n'; std::cout << j_object.back() << '\n'; //std::cout << j_object_empty.back() << '\n'; // undefined behavior std::cout << j_array.back() << '\n'; //std::cout << j_array_empty.back() << '\n'; // undefined behavior std::cout << j_string.back() << '\n'; // back() called on a null value try { json j_null; j_null.back(); } catch (json::invalid_iterator& e) { std::cout << e.what() << '\n'; } }
negative_train_query0_00203
doc/examples/count.cpp/main int main() { // create a JSON object json j_object = {{"one", 1}, {"two", 2}}; // call count() auto count_two = j_object.count("two"); auto count_three = j_object.count("three"); // print values std::cout << "number of elements with key \"two\": " << count_two << '\n'; std::cout << "number of elements with key \"three\": " << count_three << '\n'; }
negative_train_query0_00204
doc/examples/object.cpp/main 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'; } }
negative_train_query0_00205
doc/examples/is_number_integer.cpp/main 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"; // call is_number_integer() std::cout << std::boolalpha; std::cout << j_null.is_number_integer() << '\n'; std::cout << j_boolean.is_number_integer() << '\n'; std::cout << j_number_integer.is_number_integer() << '\n'; std::cout << j_number_unsigned_integer.is_number_integer() << '\n'; std::cout << j_number_float.is_number_integer() << '\n'; std::cout << j_object.is_number_integer() << '\n'; std::cout << j_array.is_number_integer() << '\n'; std::cout << j_string.is_number_integer() << '\n'; }
negative_train_query0_00206
doc/examples/to_ubjson.cpp/print_byte void print_byte(uint8_t byte) { if (32 < byte and byte < 128) { std::cout << (char)byte; } else { std::cout << (int)byte; } }
negative_train_query0_00207
doc/examples/to_ubjson.cpp/main int main() { // create a JSON value json j = R"({"compact": true, "schema": false})"_json; // serialize it to UBJSON std::vector<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<uint8_t> v_array = json::to_ubjson(array); // serialize it to UBJSON using size optimization std::vector<uint8_t> v_array_size = json::to_ubjson(array, true); // serialize it to UBJSON using type optimization std::vector<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; }
negative_train_query0_00208
doc/examples/invalid_iterator.cpp/main int main() { try { // calling iterator::key() on non-object iterator json j = "string"; json::iterator it = j.begin(); auto k = it.key(); } catch (json::invalid_iterator& e) { // output exception information std::cout << "message: " << e.what() << '\n' << "exception id: " << e.id << std::endl; } }
negative_train_query0_00209
doc/examples/basic_json__nullptr_t.cpp/main 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'; }
negative_train_query0_00210
doc/examples/parse__array__parser_callback_t.cpp/main 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"; }
negative_train_query0_00211
doc/examples/operatorarray__key_type_const.cpp/main 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'; }
negative_train_query0_00212
doc/examples/crend.cpp/main int main() { // create an array value json array = {1, 2, 3, 4, 5}; // get an iterator to the reverse-end json::const_reverse_iterator it = array.crend(); // increment the iterator to point to the first element --it; // serialize the element that the iterator points to std::cout << *it << '\n'; }
negative_train_query0_00213
doc/examples/operator__less.cpp/main 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'; }
negative_train_query0_00214
doc/examples/operator_deserialize.cpp/main 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'; }
negative_train_query0_00215
doc/examples/README.cpp/main int main() { // create a JSON object json j = { {"pi", 3.141}, {"happy", true}, {"name", "Niels"}, {"nothing", nullptr}, { "answer", { {"everything", 42} } }, {"list", {1, 0, 2}}, { "object", { {"currency", "USD"}, {"value", 42.99} } } }; // add new values j["new"]["key"]["value"] = {"another", "list"}; // count elements auto s = j.size(); j["size"] = s; // pretty print with indent of 4 spaces std::cout << std::setw(4) << j << '\n'; }
negative_train_query0_00216
doc/examples/is_primitive.cpp/main 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"; // 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'; }
negative_train_query0_00217
doc/examples/operator__notequal.cpp/main 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.000000000000001L; 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'; }
negative_train_query0_00218
doc/examples/from_cbor.cpp/main int main() { // create byte vector std::vector<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; }
negative_train_query0_00219
doc/examples/is_string.cpp/main 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"; // 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'; }
negative_train_query0_00220
doc/examples/from_ubjson.cpp/main int main() { // create byte vector std::vector<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 UBJSON json j = json::from_ubjson(v); // print the deserialized JSON value std::cout << std::setw(2) << j << std::endl; }
negative_train_query0_00221
doc/examples/swap__string_t.cpp/main int main() { // create a JSON value json value = { "the good", "the bad", "the ugly" }; // create string_t json::string_t string = "the fast"; // swap the object stored in the JSON value value[1].swap(string); // output the values std::cout << "value = " << value << '\n'; std::cout << "string = " << string << '\n'; }
negative_train_query0_00222
doc/examples/type.cpp/main 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'; }
negative_train_query0_00223
doc/examples/json_pointer__back.cpp/main 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; }
negative_train_query0_00224
doc/examples/operatorjson_pointer.cpp/main 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'; }
negative_train_query0_00225
doc/examples/update__range.cpp/main int main() { // create two JSON objects json o1 = R"( {"color": "red", "price": 17.99} )"_json; json o2 = R"( {"color": "blue", "speed": 100} )"_json; // add all keys from o2 to o1 (updating "color") o1.update(o2.begin(), o2.end()); // output updated object o1 std::cout << std::setw(2) << o1 << '\n'; }
negative_train_query0_00226
doc/examples/is_number_unsigned.cpp/main 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"; // call is_number_unsigned() std::cout << std::boolalpha; std::cout << j_null.is_number_unsigned() << '\n'; std::cout << j_boolean.is_number_unsigned() << '\n'; std::cout << j_number_integer.is_number_unsigned() << '\n'; std::cout << j_number_unsigned_integer.is_number_unsigned() << '\n'; std::cout << j_number_float.is_number_unsigned() << '\n'; std::cout << j_object.is_number_unsigned() << '\n'; std::cout << j_array.is_number_unsigned() << '\n'; std::cout << j_string.is_number_unsigned() << '\n'; }
negative_train_query0_00227
doc/examples/at_json_pointer.cpp/main 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.at("/number"_json_pointer) << '\n'; // output element with JSON pointer "/string" std::cout << j.at("/string"_json_pointer) << '\n'; // output element with JSON pointer "/array" std::cout << j.at("/array"_json_pointer) << '\n'; // output element with JSON pointer "/array/1" std::cout << j.at("/array/1"_json_pointer) << '\n'; // writing access // change the string j.at("/string"_json_pointer) = "bar"; // output the changed string std::cout << j["string"] << '\n'; // change an array element j.at("/array/1"_json_pointer) = 21; // output the changed array std::cout << j["array"] << '\n'; // out_of_range.106 try { // try to use an array index with leading '0' json::reference ref = j.at("/array/01"_json_pointer); } catch (json::parse_error& e) { std::cout << e.what() << '\n'; } // out_of_range.109 try { // try to use an array index that is not a number json::reference ref = j.at("/array/one"_json_pointer); } catch (json::parse_error& e) { std::cout << e.what() << '\n'; } // out_of_range.401 try { // try to use a an invalid array index json::reference ref = j.at("/array/4"_json_pointer); } catch (json::out_of_range& e) { std::cout << e.what() << '\n'; } // out_of_range.402 try { // try to use the array index '-' json::reference ref = j.at("/array/-"_json_pointer); } catch (json::out_of_range& e) { std::cout << e.what() << '\n'; } // out_of_range.403 try { // try to use a JSON pointer to an nonexistent object key json::const_reference ref = j.at("/foo"_json_pointer); } catch (json::out_of_range& e) { std::cout << e.what() << '\n'; } // out_of_range.404 try { // try to use a JSON pointer that cannot be resolved json::reference ref = j.at("/number/foo"_json_pointer); } catch (json::out_of_range& e) { std::cout << e.what() << '\n'; } }
negative_train_query0_00228
doc/examples/emplace_back.cpp/main 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'; }
negative_train_query0_00229
doc/examples/basic_json__value_ptr.cpp/main 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"; }
negative_train_query0_00230
doc/examples/basic_json__CompatibleType.cpp/main 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<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'; }
negative_train_query0_00231
doc/examples/swap__reference.cpp/main int main() { // create two JSON values json j1 = {1, 2, 3, 4, 5}; json j2 = {{"pi", 3.141592653589793}, {"e", 2.718281828459045}}; // swap the values j1.swap(j2); // output the values std::cout << "j1 = " << j1 << '\n'; std::cout << "j2 = " << j2 << '\n'; }
negative_train_query0_00232
doc/examples/from_msgpack.cpp/main int main() { // create byte vector std::vector<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; }
negative_train_query0_00233
doc/examples/cbegin.cpp/main int main() { // create an array value const json array = {1, 2, 3, 4, 5}; // get an iterator to the first element json::const_iterator it = array.cbegin(); // serialize the element that the iterator points to std::cout << *it << '\n'; }
negative_train_query0_00234
doc/examples/basic_json__copyassignment.cpp/main int main() { // create JSON values json a = 23; json b = 42; // copy-assign a to b b = a; // serialize the JSON arrays std::cout << a << '\n'; std::cout << b << '\n'; }
negative_train_query0_00235
doc/examples/from_bson.cpp/main int main() { // create byte vector std::vector<uint8_t> v = {0x1b, 0x00, 0x00, 0x00, 0x08, 0x63, 0x6f, 0x6d, 0x70, 0x61, 0x63, 0x74, 0x00, 0x01, 0x10, 0x73, 0x63, 0x68, 0x65, 0x6d, 0x61, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; // deserialize it with BSON json j = json::from_bson(v); // print the deserialized JSON value std::cout << std::setw(2) << j << std::endl; }
negative_train_query0_00236
doc/examples/at__object_t_key_type_const.cpp/main 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" std::cout << object.at("the ugly") << '\n'; // exception type_error.304 try { // use at() on a non-object type const json str = "I am a string"; std::cout << str.at("the good") << '\n'; } catch (json::type_error& e) { std::cout << e.what() << '\n'; } // exception out_of_range.401 try { // try to read from a nonexisting key std::cout << object.at("the fast") << '\n'; } catch (json::out_of_range) { std::cout << "out of range" << '\n'; } }
negative_train_query0_00237
doc/examples/operator__ValueType.cpp/main 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 implicit conversions bool v1 = json_types["boolean"]; int v2 = json_types["number"]["integer"]; short v3 = json_types["number"]["integer"]; float v4 = json_types["number"]["floating-point"]; int v5 = json_types["number"]["floating-point"]; std::string v6 = json_types["string"]; std::vector<short> v7 = json_types["array"]; std::unordered_map<std::string, json> v8 = json_types; // 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'; } // example for an exception try { bool v1 = json_types["string"]; } catch (json::type_error& e) { std::cout << e.what() << '\n'; } }
negative_train_query0_00238
doc/examples/json_pointer__to_string.cpp/main 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; }
negative_train_query0_00239
doc/examples/at__size_type_const.cpp/main 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'; // exception type_error.304 try { // use at() on a non-array type const json str = "I am a string"; std::cout << str.at(0) << '\n'; } catch (json::type_error& e) { std::cout << e.what() << '\n'; } // exception out_of_range.401 try { // try to read beyond the array limit std::cout << array.at(5) << '\n'; } catch (json::out_of_range& e) { std::cout << e.what() << '\n'; } }
negative_train_query0_00240
doc/examples/get_ptr.cpp/main int main() { // create a JSON number json value = 17; // explicitly getting pointers auto p1 = value.get_ptr<const json::number_integer_t*>(); auto p2 = value.get_ptr<json::number_integer_t*>(); auto p3 = value.get_ptr<json::number_integer_t* const>(); auto p4 = value.get_ptr<const json::number_integer_t* const>(); auto p5 = value.get_ptr<json::number_float_t*>(); // print the pointees std::cout << *p1 << ' ' << *p2 << ' ' << *p3 << ' ' << *p4 << '\n'; std::cout << std::boolalpha << (p5 == nullptr) << '\n'; }
negative_train_query0_00241
doc/examples/is_null.cpp/main 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"; // 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'; }
negative_train_query0_00242
doc/examples/cend.cpp/main 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'; }
negative_train_query0_00243
doc/examples/basic_json__basic_json.cpp/main 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'; }
negative_train_query0_00244
doc/examples/is_structured.cpp/main 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"; // call is_structured() std::cout << std::boolalpha; std::cout << j_null.is_structured() << '\n'; std::cout << j_boolean.is_structured() << '\n'; std::cout << j_number_integer.is_structured() << '\n'; std::cout << j_number_unsigned_integer.is_structured() << '\n'; std::cout << j_number_float.is_structured() << '\n'; std::cout << j_object.is_structured() << '\n'; std::cout << j_array.is_structured() << '\n'; std::cout << j_string.is_structured() << '\n'; }
negative_train_query0_00245
doc/examples/array.cpp/main int main() { // create JSON arrays json j_no_init_list = json::array(); json j_empty_init_list = json::array({}); json j_nonempty_init_list = json::array({1, 2, 3, 4}); json j_list_of_pairs = json::array({ {"one", 1}, {"two", 2} }); // serialize the JSON arrays std::cout << j_no_init_list << '\n'; std::cout << j_empty_init_list << '\n'; std::cout << j_nonempty_init_list << '\n'; std::cout << j_list_of_pairs << '\n'; }
negative_train_query0_00246
doc/examples/at__size_type.cpp/main 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'; } }
negative_train_query0_00247
doc/examples/erase__IteratorType_IteratorType.cpp/main 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'; }
negative_train_query0_00248
doc/examples/is_number_float.cpp/main 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"; // 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'; }
negative_train_query0_00249
doc/examples/operator__value_t.cpp/main 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'; }
negative_train_query0_00250
doc/examples/out_of_range.cpp/main 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; } }
negative_train_query0_00251
doc/examples/crbegin.cpp/main 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'; }
negative_train_query0_00252
doc/examples/json_pointer__parent_pointer.cpp/main 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; }
negative_train_query0_00253
doc/examples/push_back__initializer_list.cpp/main 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: object.push_back({"three", 3}); // object is extended object += {"four", 4}; // object is extended null.push_back({"five", 5}); // null is converted to array // print values std::cout << object << '\n'; std::cout << null << '\n'; // would throw: //object.push_back({1, 2, 3}); }
negative_train_query0_00254
doc/examples/json_pointer.cpp/main 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'; } }
negative_train_query0_00255
doc/examples/find__key_type.cpp/main int main() { // create a JSON object json j_object = {{"one", 1}, {"two", 2}}; // call find auto it_two = j_object.find("two"); auto it_three = j_object.find("three"); // 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'; }
negative_train_query0_00256
single_include/nlohmann/json.hpp/get_arithmetic_value void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val) { switch (static_cast<value_t>(j)) { case value_t::number_unsigned: { val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>()); break; } case value_t::number_integer: { val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_integer_t*>()); break; } case value_t::number_float: { val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_float_t*>()); break; } default: JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name()))); } }
negative_train_query0_00257
single_include/nlohmann/json.hpp/from_json void from_json(const BasicJsonType& j, ConstructibleObjectType& obj) { if (JSON_HEDLEY_UNLIKELY(not j.is_object())) { JSON_THROW(type_error::create(302, "type must be object, but is " + std::string(j.type_name()))); } ConstructibleObjectType ret; auto inner_object = j.template get_ptr<const typename BasicJsonType::object_t*>(); using value_type = typename ConstructibleObjectType::value_type; std::transform( inner_object->begin(), inner_object->end(), std::inserter(ret, ret.begin()), [](typename BasicJsonType::object_t::value_type const & p) { return value_type(p.first, p.second.template get<typename ConstructibleObjectType::mapped_type>()); }); obj = std::move(ret); }
negative_train_query0_00258
single_include/nlohmann/json.hpp/split static std::vector<std::string> split(const std::string& reference_string) { std::vector<std::string> result; // special case: empty reference string -> no reference tokens if (reference_string.empty()) { return result; } // check if nonempty reference string begins with slash if (JSON_HEDLEY_UNLIKELY(reference_string[0] != '/')) { JSON_THROW(detail::parse_error::create(107, 1, "JSON pointer must be empty or begin with '/' - was: '" + reference_string + "'")); } // extract the reference tokens: // - slash: position of the last read slash (or end of string) // - start: position after the previous slash for ( // search for the first slash after the first character std::size_t slash = reference_string.find_first_of('/', 1), // set the beginning of the first reference token start = 1; // we can stop if start == 0 (if slash == std::string::npos) start != 0; // set the beginning of the next reference token // (will eventually be 0 if slash == std::string::npos) start = (slash == std::string::npos) ? 0 : slash + 1, // find next slash slash = reference_string.find_first_of('/', start)) { // use the text between the beginning of the reference token // (start) and the last slash (slash). auto reference_token = reference_string.substr(start, slash - start); // check reference tokens are properly escaped for (std::size_t pos = reference_token.find_first_of('~'); pos != std::string::npos; pos = reference_token.find_first_of('~', pos + 1)) { assert(reference_token[pos] == '~'); // ~ must be followed by 0 or 1 if (JSON_HEDLEY_UNLIKELY(pos == reference_token.size() - 1 or (reference_token[pos + 1] != '0' and reference_token[pos + 1] != '1'))) { JSON_THROW(detail::parse_error::create(108, 0, "escape character '~' must be followed with '0' or '1'")); } } // finally, store the reference token unescape(reference_token); result.push_back(reference_token); } return result; }
negative_train_query0_00259
single_include/nlohmann/json.hpp/begin const_iterator begin() const noexcept { return cbegin(); }
negative_train_query0_00260
single_include/nlohmann/json.hpp/insert iterator insert(const_iterator pos, initializer_list_t ilist) { // insert only works for arrays if (JSON_HEDLEY_UNLIKELY(not is_array())) { JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()))); } // check if iterator pos fits to this JSON value if (JSON_HEDLEY_UNLIKELY(pos.m_object != this)) { JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value")); } // insert to array and return iterator return insert_iterator(pos, ilist.begin(), ilist.end()); }
negative_train_query0_00261
single_include/nlohmann/json.hpp/swap void swap(string_t& other) { // swap only works for strings if (JSON_HEDLEY_LIKELY(is_string())) { std::swap(*(m_value.string), other); } else { JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()))); } }
negative_train_query0_00262
single_include/nlohmann/json.hpp/to_ubjson static void to_ubjson(const basic_json& j, detail::output_adapter<char> o, const bool use_size = false, const bool use_type = false) { binary_writer<char>(o).write_ubjson(j, use_size, use_type); }
negative_train_query0_00263
single_include/nlohmann/json.hpp/JSON_INTERNAL_CATCH ValueType value(const json_pointer& ptr, const ValueType& default_value) const { // at only works for objects if (JSON_HEDLEY_LIKELY(is_object())) { // if pointer resolves a value, return it or use default value JSON_TRY { return ptr.get_checked(this); } JSON_INTERNAL_CATCH (out_of_range&) { return default_value; } }
negative_train_query0_00264
single_include/nlohmann/json.hpp/get auto get(const nlohmann::detail::iteration_proxy_value<IteratorType>& i) -> decltype(i.key()) { return i.key(); }
negative_train_query0_00265
single_include/nlohmann/json.hpp/basic_json basic_json(CompatibleType && val) noexcept(noexcept( JSONSerializer<U>::to_json(std::declval<basic_json_t&>(), std::forward<CompatibleType>(val)))) { JSONSerializer<U>::to_json(*this, std::forward<CompatibleType>(val)); assert_invariant(); }
negative_train_query0_00266
single_include/nlohmann/json.hpp/update void update(const_reference j) { // implicitly convert null value to an empty object if (is_null()) { m_type = value_t::object; m_value.object = create<object_t>(); assert_invariant(); } if (JSON_HEDLEY_UNLIKELY(not is_object())) { JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(type_name()))); } if (JSON_HEDLEY_UNLIKELY(not j.is_object())) { JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(j.type_name()))); } for (auto it = j.cbegin(); it != j.cend(); ++it) { m_value.object->operator[](it.key()) = it.value(); } }
negative_train_query0_00267
single_include/nlohmann/json.hpp/object JSON_HEDLEY_WARN_UNUSED_RESULT static basic_json object(initializer_list_t init = {}) { return basic_json(init, false, value_t::object); }
negative_train_query0_00268
single_include/nlohmann/json.hpp/front const_reference front() const { return *cbegin(); }
negative_train_query0_00269
single_include/nlohmann/json.hpp/erase void erase(const size_type idx) { // this erase only works for arrays if (JSON_HEDLEY_LIKELY(is_array())) { if (JSON_HEDLEY_UNLIKELY(idx >= size())) { JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range")); } m_value.array->erase(m_value.array->begin() + static_cast<difference_type>(idx)); } else { JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()))); } }
negative_train_query0_00270
single_include/nlohmann/json.hpp/push_back void push_back(const typename object_t::value_type& val) { // push_back only works for null objects or objects if (JSON_HEDLEY_UNLIKELY(not(is_null() or is_object()))) { JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name()))); } // transform null object into an object if (is_null()) { m_type = value_t::object; m_value = value_t::object; assert_invariant(); } // add element to array m_value.object->insert(val); }
negative_train_query0_00271
single_include/nlohmann/json.hpp/accept static bool accept(detail::input_adapter&& i) { return parser(i).accept(true); }
negative_train_query0_00272
single_include/nlohmann/json.hpp/from_json_array_impl void from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr, priority_tag<0> /*unused*/) { using std::end; ConstructibleArrayType ret; std::transform( j.begin(), j.end(), std::inserter(ret, end(ret)), [](const BasicJsonType & i) { // get<BasicJsonType>() returns *this, this won't call a from_json // method when value_type is BasicJsonType return i.template get<typename ConstructibleArrayType::value_type>(); }); arr = std::move(ret); }
negative_train_query0_00273
single_include/nlohmann/json.hpp/construct static void construct(BasicJsonType& j, typename BasicJsonType::boolean_t b) noexcept { j.m_type = value_t::boolean; j.m_value = b; j.assert_invariant(); }
negative_train_query0_00274
single_include/nlohmann/json.hpp/to_json void to_json(BasicJsonType& j, const std::valarray<T>& arr) { external_constructor<value_t::array>::construct(j, std::move(arr)); }
negative_train_query0_00275
single_include/nlohmann/json.hpp/unflatten basic_json unflatten() const { return json_pointer::unflatten(*this); }
negative_train_query0_00276
single_include/nlohmann/json.hpp/is_null constexpr bool is_null() const noexcept { return m_type == value_t::null; }
negative_train_query0_00277
single_include/nlohmann/json.hpp/from_ubjson JSON_HEDLEY_WARN_UNUSED_RESULT static basic_json from_ubjson(A1 && a1, A2 && a2, const bool strict = true, const bool allow_exceptions = true) { basic_json result; detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions); const bool res = binary_reader(detail::input_adapter(std::forward<A1>(a1), std::forward<A2>(a2))).sax_parse(input_format_t::ubjson, &sdp, strict); return res ? result : basic_json(value_t::discarded); }
negative_train_query0_00278
single_include/nlohmann/json.hpp/flatten basic_json flatten() const { basic_json result(value_t::object); json_pointer::flatten("", *this, result); return result; }
negative_train_query0_00279
single_include/nlohmann/json.hpp/is_discarded constexpr bool is_discarded() const noexcept { return m_type == value_t::discarded; }
negative_train_query0_00280
single_include/nlohmann/json.hpp/back const_reference back() const { auto tmp = cend(); --tmp; return *tmp; }
negative_train_query0_00281
single_include/nlohmann/json.hpp/is_string constexpr bool is_string() const noexcept { return m_type == value_t::string; }
negative_train_query0_00282
single_include/nlohmann/json.hpp/JSON_CATCH JSON_CATCH (std::out_of_range&) { // create better exception explanation JSON_THROW(out_of_range::create(403, "key '" + key + "' not found")); }
negative_train_query0_00283
single_include/nlohmann/json.hpp/to_msgpack static void to_msgpack(const basic_json& j, detail::output_adapter<uint8_t> o) { binary_writer<uint8_t>(o).write_msgpack(j); }
negative_train_query0_00284
single_include/nlohmann/json.hpp/replace_substring static void replace_substring(std::string& s, const std::string& f, const std::string& t) { assert(not f.empty()); for (auto pos = s.find(f); // find first occurrence of f pos != std::string::npos; // make sure f was found s.replace(pos, f.size(), t), // replace with t, and pos = s.find(f, pos + t.size())) // find next occurrence of f {} }
negative_train_query0_00285
single_include/nlohmann/json.hpp/parse static basic_json parse(IteratorType first, IteratorType last, const parser_callback_t cb = nullptr, const bool allow_exceptions = true) { basic_json result; parser(detail::input_adapter(first, last), cb, allow_exceptions).parse(true, result); return result; }
negative_train_query0_00286
single_include/nlohmann/json.hpp/compute_boundaries boundaries compute_boundaries(FloatType value) { assert(std::isfinite(value)); assert(value > 0); // Convert the IEEE representation into a diyfp. // // If v is denormal: // value = 0.F * 2^(1 - bias) = ( F) * 2^(1 - bias - (p-1)) // If v is normalized: // value = 1.F * 2^(E - bias) = (2^(p-1) + F) * 2^(E - bias - (p-1)) static_assert(std::numeric_limits<FloatType>::is_iec559, "internal error: dtoa_short requires an IEEE-754 floating-point implementation"); constexpr int kPrecision = std::numeric_limits<FloatType>::digits; // = p (includes the hidden bit) constexpr int kBias = std::numeric_limits<FloatType>::max_exponent - 1 + (kPrecision - 1); constexpr int kMinExp = 1 - kBias; constexpr std::uint64_t kHiddenBit = std::uint64_t{1} << (kPrecision - 1); // = 2^(p-1) using bits_type = typename std::conditional<kPrecision == 24, std::uint32_t, std::uint64_t >::type; const std::uint64_t bits = reinterpret_bits<bits_type>(value); const std::uint64_t E = bits >> (kPrecision - 1); const std::uint64_t F = bits & (kHiddenBit - 1); const bool is_denormal = E == 0; const diyfp v = is_denormal ? diyfp(F, kMinExp) : diyfp(F + kHiddenBit, static_cast<int>(E) - kBias); // Compute the boundaries m- and m+ of the floating-point value // v = f * 2^e. // // Determine v- and v+, the floating-point predecessor and successor if v, // respectively. // // v- = v - 2^e if f != 2^(p-1) or e == e_min (A) // = v - 2^(e-1) if f == 2^(p-1) and e > e_min (B) // // v+ = v + 2^e // // Let m- = (v- + v) / 2 and m+ = (v + v+) / 2. All real numbers _strictly_ // between m- and m+ round to v, regardless of how the input rounding // algorithm breaks ties. // // ---+-------------+-------------+-------------+-------------+--- (A) // v- m- v m+ v+ // // -----------------+------+------+-------------+-------------+--- (B) // v- m- v m+ v+ const bool lower_boundary_is_closer = F == 0 and E > 1; const diyfp m_plus = diyfp(2 * v.f + 1, v.e - 1); const diyfp m_minus = lower_boundary_is_closer ? diyfp(4 * v.f - 1, v.e - 2) // (B) : diyfp(2 * v.f - 1, v.e - 1); // (A) // Determine the normalized w+ = m+. const diyfp w_plus = diyfp::normalize(m_plus); // Determine w- = m- such that e_(w-) = e_(w+). const diyfp w_minus = diyfp::normalize_to(m_minus, w_plus.e); return {diyfp::normalize(v), w_minus, w_plus}; }
negative_train_query0_00287
single_include/nlohmann/json.hpp/patch basic_json patch(const basic_json& json_patch) const { // make a working copy to apply the patch to basic_json result = *this; // the valid JSON Patch operations enum class patch_operations {add, remove, replace, move, copy, test, invalid}; const auto get_op = [](const std::string & op) { if (op == "add") { return patch_operations::add; } if (op == "remove") { return patch_operations::remove; } if (op == "replace") { return patch_operations::replace; } if (op == "move") { return patch_operations::move; } if (op == "copy") { return patch_operations::copy; } if (op == "test") { return patch_operations::test; } return patch_operations::invalid; }; // wrapper for "add" operation; add value at ptr const auto operation_add = [&result](json_pointer & ptr, basic_json val) { // adding to the root of the target document means replacing it if (ptr.empty()) { result = val; return; } // make sure the top element of the pointer exists json_pointer top_pointer = ptr.top(); if (top_pointer != ptr) { result.at(top_pointer); } // get reference to parent of JSON pointer ptr const auto last_path = ptr.back(); ptr.pop_back(); basic_json& parent = result[ptr]; switch (parent.m_type) { case value_t::null: case value_t::object: { // use operator[] to add value parent[last_path] = val; break; } case value_t::array: { if (last_path == "-") { // special case: append to back parent.push_back(val); } else { const auto idx = json_pointer::array_index(last_path); if (JSON_HEDLEY_UNLIKELY(static_cast<size_type>(idx) > parent.size())) { // avoid undefined behavior JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range")); } // default case: insert add offset parent.insert(parent.begin() + static_cast<difference_type>(idx), val); } break; } // if there exists a parent it cannot be primitive default: // LCOV_EXCL_LINE assert(false); // LCOV_EXCL_LINE } }; // wrapper for "remove" operation; remove value at ptr const auto operation_remove = [&result](json_pointer & ptr) { // get reference to parent of JSON pointer ptr const auto last_path = ptr.back(); ptr.pop_back(); basic_json& parent = result.at(ptr); // remove child if (parent.is_object()) { // perform range check auto it = parent.find(last_path); if (JSON_HEDLEY_LIKELY(it != parent.end())) { parent.erase(it); } else { JSON_THROW(out_of_range::create(403, "key '" + last_path + "' not found")); } } else if (parent.is_array()) { // note erase performs range check parent.erase(static_cast<size_type>(json_pointer::array_index(last_path))); } }; // type check: top level value must be an array if (JSON_HEDLEY_UNLIKELY(not json_patch.is_array())) { JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects")); } // iterate and apply the operations for (const auto& val : json_patch) { // wrapper to get a value for an operation const auto get_value = [&val](const std::string & op, const std::string & member, bool string_type) -> basic_json & { // find value auto it = val.m_value.object->find(member); // context-sensitive error message const auto error_msg = (op == "op") ? "operation" : "operation '" + op + "'"; // check if desired value is present if (JSON_HEDLEY_UNLIKELY(it == val.m_value.object->end())) { JSON_THROW(parse_error::create(105, 0, error_msg + " must have member '" + member + "'")); } // check if result is of type string if (JSON_HEDLEY_UNLIKELY(string_type and not it->second.is_string())) { JSON_THROW(parse_error::create(105, 0, error_msg + " must have string member '" + member + "'")); } // no error: return value return it->second; }; // type check: every element of the array must be an object if (JSON_HEDLEY_UNLIKELY(not val.is_object())) { JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects")); } // collect mandatory members const std::string op = get_value("op", "op", true); const std::string path = get_value(op, "path", true); json_pointer ptr(path); switch (get_op(op)) { case patch_operations::add: { operation_add(ptr, get_value("add", "value", false)); break; } case patch_operations::remove: { operation_remove(ptr); break; } case patch_operations::replace: { // the "path" location must exist - use at() result.at(ptr) = get_value("replace", "value", false); break; } case patch_operations::move: { const std::string from_path = get_value("move", "from", true); json_pointer from_ptr(from_path); // the "from" location must exist - use at() basic_json v = result.at(from_ptr); // The move operation is functionally identical to a // "remove" operation on the "from" location, followed // immediately by an "add" operation at the target // location with the value that was just removed. operation_remove(from_ptr); operation_add(ptr, v); break; } case patch_operations::copy: { const std::string from_path = get_value("copy", "from", true); const json_pointer from_ptr(from_path); // the "from" location must exist - use at() basic_json v = result.at(from_ptr); // The copy is functionally identical to an "add" // operation at the target location using the value // specified in the "from" member. operation_add(ptr, v); break; } case patch_operations::test: { bool success = false; JSON_TRY { // check if "value" matches the one at "path" // the "path" location must exist - use at() success = (result.at(ptr) == get_value("test", "value", false)); } JSON_INTERNAL_CATCH (out_of_range&) { // ignore out of range errors: success remains false } // throw an exception if test fails if (JSON_HEDLEY_UNLIKELY(not success)) { JSON_THROW(other_error::create(501, "unsuccessful: " + val.dump())); } break; } default: { // op must be "add", "remove", "replace", "move", "copy", or // "test" JSON_THROW(parse_error::create(105, 0, "operation value '" + op + "' is invalid")); } } }
negative_train_query0_00288
single_include/nlohmann/json.hpp/crend const_reverse_iterator crend() const noexcept { return const_reverse_iterator(cbegin()); }
negative_train_query0_00289
single_include/nlohmann/json.hpp/emplace_back reference emplace_back(Args&& ... args) { // emplace_back only works for null objects or arrays if (JSON_HEDLEY_UNLIKELY(not(is_null() or is_array()))) { JSON_THROW(type_error::create(311, "cannot use emplace_back() with " + std::string(type_name()))); } // transform null object into an array if (is_null()) { m_type = value_t::array; m_value = value_t::array; assert_invariant(); } // add element to array (perfect forwarding) #ifdef JSON_HAS_CPP_17 return m_value.array->emplace_back(std::forward<Args>(args)...); #else m_value.array->emplace_back(std::forward<Args>(args)...); return m_value.array->back(); #endif }
negative_train_query0_00290
single_include/nlohmann/json.hpp/to_bson static void to_bson(const basic_json& j, detail::output_adapter<uint8_t> o) { binary_writer<uint8_t>(o).write_bson(j); }
negative_train_query0_00291
single_include/nlohmann/json.hpp/at reference at(const json_pointer& ptr) { return ptr.get_checked(this); }
negative_train_query0_00292
single_include/nlohmann/json.hpp/value ValueType value(const typename object_t::key_type& key, const ValueType& default_value) const { // at only works for objects if (JSON_HEDLEY_LIKELY(is_object())) { // if key is found, return value and given default value otherwise const auto it = find(key); if (it != end()) { return *it; } return default_value; } JSON_THROW(type_error::create(306, "cannot use value() with " + std::string(type_name()))); }
negative_train_query0_00293
single_include/nlohmann/json.hpp/from_msgpack JSON_HEDLEY_WARN_UNUSED_RESULT static basic_json from_msgpack(detail::input_adapter&& i, const bool strict = true, const bool allow_exceptions = true) { basic_json result; detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions); const bool res = binary_reader(detail::input_adapter(i)).sax_parse(input_format_t::msgpack, &sdp, strict); return res ? result : basic_json(value_t::discarded); }
negative_train_query0_00294
single_include/nlohmann/json.hpp/from_bson JSON_HEDLEY_WARN_UNUSED_RESULT static basic_json from_bson(A1 && a1, A2 && a2, const bool strict = true, const bool allow_exceptions = true) { basic_json result; detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions); const bool res = binary_reader(detail::input_adapter(std::forward<A1>(a1), std::forward<A2>(a2))).sax_parse(input_format_t::bson, &sdp, strict); return res ? result : basic_json(value_t::discarded); }
negative_train_query0_00295
single_include/nlohmann/json.hpp/is_number_float constexpr bool is_number_float() const noexcept { return m_type == value_t::number_float; }
negative_train_query0_00296
single_include/nlohmann/json.hpp/reinterpret_bits Target reinterpret_bits(const Source source) { static_assert(sizeof(Target) == sizeof(Source), "size mismatch"); Target target; std::memcpy(&target, &source, sizeof(Source)); return target; }
negative_train_query0_00297