void from_json(const json& j, ToolConfig& a) { std::vector<std::string>& files = a.getSourceFiles(); try { files = j.at("source_files").get<std::vector<std::string>>(); } catch (...) { files = {}; } try { a.setExecutable(j.at("executable")); } catch (...) { a.setExecutable(""); } try { a.setToolID(j.at("tool_id")); } catch (...) { a.setToolID("None"); } try { a.setVersion(j.at("version")); } catch (...) { a.setVersion(newVersion); } std::vector<ToolAction>& actions = a.getActions(); try { actions = j.at("actions").get<std::vector<ToolAction>>(); } catch (...) { actions = {}; } }
void Faunus::Energy::from_json(const json &j, EwaldData &d) { d.alpha = j.at("alpha"); d.rc = j.at("cutoff"); d.kc = j.at("kcutoff"); d.ipbc = j.value("ipbc", false); d.spherical_sum = j.value("spherical_sum", true); d.lB = pc::lB( j.at("epsr") ); d.eps_surf = j.value("epss", 0.0); d.const_inf = (d.eps_surf < 1) ? 0 : 1; // if unphysical (<1) use epsr infinity for surrounding medium }
json JsonUtil::filterExactValues(const json& aNew, const json& aCompareTo) noexcept { json ret = aNew; for (const auto& v: json::iterator_wrapper(aCompareTo)) { auto key = v.key(); auto i = aNew.find(key); if (i != aNew.end() && aNew.at(key) == aCompareTo.at(key)) { ret.erase(key); } } return ret; }
void PPM (const json &j) { PMParams params; params.LoadFrom(j.at("pm_params")); omp_set_num_threads(params.n_threads); Scene scene = Scene::Load(j); Image image(scene.Width() * 2, scene.Height(), "tmp/ph_last", params.display); PM pm(scene, image, params); if (j.at("pm_params").at("algo").get<string>() == "ppm") pm.PPM(); else pm.PPPM(); image.Show(); image.Dump(-1); image.Show(); }
bool RPCMethod::ValidateArguments(json j) { if (j.size() != this->paramTypes->size()) { return false; } for (unsigned int c = 0; c < this->paramTypes->size(); c++) { if (!this->checkType(this->paramTypes->at(c), j.at(c))) { return false; } } return true; }
Srs::Srs(const json& j) { if (j.is_null()) return; if (j.is_string()) { *this = Srs(j.get<std::string>()); return; } m_authority = j.value("authority", ""); m_horizontal = j.value("horizontal", ""); m_vertical = j.value("vertical", ""); if (j.count("wkt")) { m_wkt = j.at("wkt").get<std::string>(); m_spatialReference.set(m_wkt); } }
void PT (const json &j) { PTParams params; params.LoadFrom(j.at("pt_params")); omp_set_num_threads(params.n_threads); Scene scene = Scene::Load(j); Image image(scene.Width() * 2, scene.Height(), "tmp/im_last", params.display); PathTracer pathTracer(scene, image, params); Ray ray; rand_data rd; rand_init(&rd); scene.GetCamera()->Project(&rd, 818, 1800 - 1043, ray); pathTracer.Trace(&rd, ray); pathTracer.PT(); image.Show(); image.Dump(-1); image.Show(); }
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'; // 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'; } }
void IPCClientPrivate::processSignalJSON(const json& jobj) { try { const std::string name = jobj["_s"]; if (name == "DeviceInserted") { const json attributes_json = jobj.at("attributes"); std::map<std::string,std::string> attributes; for (auto it = attributes_json.begin(); it != attributes_json.end(); ++it) { const std::string key = it.key(); const std::string value = it.value(); attributes[key] = value; } std::vector<USBInterfaceType> interfaces; for (auto const& jitem : jobj["interfaces"]) { const std::string type_string = jitem; interfaces.push_back(USBInterfaceType(type_string)); } _p_instance.DeviceInserted(jobj["seqn"], attributes, interfaces, jobj["rule_match"], jobj["rule_seqn"]); } else if (name == "DevicePresent") { const json attributes_json = jobj.at("attributes"); std::map<std::string,std::string> attributes; for (auto it = attributes_json.begin(); it != attributes_json.end(); ++it) { const std::string key = it.key(); const std::string value = it.value(); attributes[key] = value; } std::vector<USBInterfaceType> interfaces; for (auto const& jitem : jobj["interfaces"]) { const std::string type_string = jitem; interfaces.push_back(USBInterfaceType(type_string)); } _p_instance.DevicePresent(jobj["seqn"], attributes, interfaces, Rule::targetFromString(jobj["target"])); } else if (name == "DeviceRemoved") { const json attributes_json = jobj.at("attributes"); std::map<std::string,std::string> attributes; for (auto it = attributes_json.begin(); it != attributes_json.end(); ++it) { const std::string key = it.key(); const std::string value = it.value(); attributes[key] = value; } _p_instance.DeviceRemoved(jobj["seqn"], attributes); } else if (name == "DeviceAllowed") { const json attributes_json = jobj.at("attributes"); std::map<std::string,std::string> attributes; for (auto it = attributes_json.begin(); it != attributes_json.end(); ++it) { const std::string key = it.key(); const std::string value = it.value(); attributes[key] = value; } _p_instance.DeviceAllowed(jobj["seqn"], attributes, jobj["rule_match"], jobj["rule_seqn"]); } else if (name == "DeviceBlocked") { const json attributes_json = jobj.at("attributes"); std::map<std::string,std::string> attributes; for (auto it = attributes_json.begin(); it != attributes_json.end(); ++it) { const std::string key = it.key(); const std::string value = it.value(); attributes[key] = value; } _p_instance.DeviceBlocked(jobj["seqn"], attributes, jobj["rule_match"], jobj["rule_seqn"]); } else if (name == "DeviceRejected") { const json attributes_json = jobj.at("attributes"); std::map<std::string,std::string> attributes; for (auto it = attributes_json.begin(); it != attributes_json.end(); ++it) { const std::string key = it.key(); const std::string value = it.value(); attributes[key] = value; } _p_instance.DeviceRejected(jobj["seqn"], attributes, jobj["rule_match"], jobj["rule_seqn"]); } else { throw 0; } } catch(...) { disconnect(); throw IPCException(IPCException::ProtocolError, "Invalid IPC signal data"); } return; }
void ChainRotationMovebase::_from_json(const json &j) { molname = j.at("molecule"); dprot = j.at("dprot"); allow_small_box = j.value("skiplarge", true); // todo rename the json attribute and make false default }
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.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'; // out_of_range.109 try { // try to use an array index that is not a number json::const_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::const_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::const_reference ref = j.at("/array/-"_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::const_reference ref = j.at("/number/foo"_json_pointer); } catch (json::out_of_range& e) { std::cout << e.what() << '\n'; } }
#include "json.hpp" using nlohmann::json; TEST_CASE("element access 1") { SECTION("array") { json j = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}}; const json j_const = j; SECTION("access specified element with bounds checking") { SECTION("access within bounds") { CHECK(j.at(0) == json(1)); CHECK(j.at(1) == json(1u)); CHECK(j.at(2) == json(true)); CHECK(j.at(3) == json(nullptr)); CHECK(j.at(4) == json("string")); CHECK(j.at(5) == json(42.23)); CHECK(j.at(6) == json(json::object())); CHECK(j.at(7) == json({1, 2, 3})); CHECK(j_const.at(0) == json(1)); CHECK(j_const.at(1) == json(1u)); CHECK(j_const.at(2) == json(true)); CHECK(j_const.at(3) == json(nullptr)); CHECK(j_const.at(4) == json("string")); CHECK(j_const.at(5) == json(42.23)); CHECK(j_const.at(6) == json(json::object()));
void from_json(const json& j, ToolAction& a) { try { a.setActionType(j.at("action")); } catch(...) { a.setActionType("None"); } try { a.setName(j.at("name")); } catch(...) { a.setName(""); } try { char buff[MAXPATHLEN]; std::string pwd = getcwd(buff, MAXPATHLEN); std::string handle = j.at("handle"); size_t loc = handle.find("${PWD}"); if (loc != std::string::npos) { handle.replace(loc, 6, pwd); } a.setHandle(handle); } catch(...) { a.setHandle(""); } try { a.setScope(j.at("scope")); } catch(...) { a.setScope(""); } try { a.setSourceInfo(j.at("source_info")); } catch(...) { a.setSourceInfo(""); } try { a.setFromType(j.at("from_type")); } catch(...) { a.setFromType(""); } try { a.setToType(j.at("to_type")); } catch(...) { a.setToType(""); } try { a.setError(j.at("error")); } catch(...) { a.setError(-1); } try { a.setAssignments(j.at("dynamic_assignments")); } catch(...) { a.setAssignments(-1); } }
void from_json(const json& j, Post& p) { p.userId = j.at("userId").get<int>(); p.id = j.at("id").get<int>(); p.title = j.at("title").get<string>(); p.body = boost::algorithm::replace_all_copy(j.at("body").get<string>(), "\n", "\\n"); }
*/ #include "catch.hpp" #include <nlohmann/json.hpp> using nlohmann::json; TEST_CASE("version information") { SECTION("meta()") { json j = json::meta(); CHECK(j["name"] == "JSON for Modern C++"); CHECK(j["copyright"] == "(C) 2013-2017 Niels Lohmann"); CHECK(j["url"] == "https://github.com/nlohmann/json"); CHECK(j["version"] == json( { {"string", "3.2.0"}, {"major", 3}, {"minor", 2}, {"patch", 0} })); CHECK(j.find("platform") != j.end()); CHECK(j.at("compiler").find("family") != j.at("compiler").end()); CHECK(j.at("compiler").find("version") != j.at("compiler").end()); CHECK(j.at("compiler").find("c++") != j.at("compiler").end()); } }
ReadQuery(const Reader& reader, const json& j) : Query(reader, j) , m_schema(j.count("schema") ? Schema(j.at("schema")) : m_metadata.outSchema()) { }