本文整理汇总了C++中json类的典型用法代码示例。如果您正苦于以下问题:C++ json类的具体用法?C++ json怎么用?C++ json使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了json类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: from_json
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 = {};
}
}
示例2: read_message
void read_message(int clientID, string message){
// Deserialize message from the string
json msg = json::parse(message);
if (msg["MESSAGE_TYPE"] == "CLIENT_UPDATE") {
// Send a time stamp reply for client-side latency estimation
json tStamps;
tStamps["MESSAGE_TYPE"] = "TIME_STAMP_REPLY";
tStamps["T2"] = chrono::system_clock::now().time_since_epoch() / chrono::milliseconds(1); // Time received
tStamps["T1"] = msg["TIME_STAMP"]; // Origination time
send_message(clientID, tStamps);
// Now, process the client update:
// Scenario A: We don't have a game ready yet. This is a pre-game message.
if (game_p == NULL) {
// Step 1: Put the message in the correct bin.
pregame_player_msgs[clientID] = msg;
// Step 2: If both bins are filled (2 players ready),
// then start a new game.
if (pregame_player_msgs.size() == 2) {
game_p = new SnakeGame(pregame_player_msgs[0], pregame_player_msgs[1]);
pregame_player_msgs.clear();
}
}
// Scenario B: A game already exists. Just forward the message to it.
else {
game_p->handleClientInput(msg);
}
}
}
示例3: merge
json merge(const json &a, const json &b) {
json result = a.flatten();
json tmp = b.flatten();
for ( auto it = tmp.begin(); it != tmp.end(); ++it )
result[it.key()] = it.value();
return result.unflatten();
}
示例4: from_json
void from_json(const json &j, AtomData &a) {
if (j.is_object()==false || j.size()!=1)
throw std::runtime_error("Invalid JSON data for AtomData");
for (auto it : j.items()) {
a.name = it.key();
auto& val = it.value();
a.activity = val.value("activity", a.activity) * 1.0_molar;
a.alphax = val.value("alphax", a.alphax);
a.charge = val.value("q", a.charge);
a.dp = val.value("dp", a.dp) * 1.0_angstrom;
a.dprot = val.value("dprot", a.dprot) * 1.0_rad;
a.eps = val.value("eps", a.eps) * 1.0_kJmol;
a.id() = val.value("id", a.id());
a.mu = val.value("mu", a.mu);
a.mulen = val.value("mulen", a.mulen);
a.scdir = val.value("scdir", a.scdir);
a.sclen = val.value("sclen", a.sclen);
a.mw = val.value("mw", a.mw);
a.sigma = val.value("sigma", 0.0) * 1.0_angstrom;
if (fabs(a.sigma)<1e-20)
a.sigma = 2.0*val.value("r", 0.0) * 1.0_angstrom;
a.tension = val.value("tension", a.tension) * 1.0_kJmol / (1.0_angstrom*1.0_angstrom);
a.tfe = val.value("tfe", a.tfe) * 1.0_kJmol / (1.0_angstrom*1.0_angstrom*1.0_molar);
a.hydrophobic = val.value("hydrophobic", false);
}
}
示例5: fromJSON
void Colour::fromJSON(json& jvalue)
{
//Will accept integer colour or [r,g,b,a] array or
// string containing x11 name or hex #rrggbb or html rgba(r,g,b,a)
if (jvalue.is_number())
{
value = jvalue;
}
else if (jvalue.is_array())
{
float R = jvalue[0];
float G = jvalue[1];
float B = jvalue[2];
float A = 1.0;
if (jvalue.size() > 3) A = jvalue[3];
if (R <= 1.0 && G <= 1.0 && B <= 1.0 && A <= 1.0)
{
r = R*255.0;
g = G*255.0;
b = B*255.0;
a = A*255.0;
}
else
{
r = R;
g = G;
b = B;
}
}
else if (jvalue.is_string())
{
fromString(jvalue);
}
//std::cout << "COLOUR: " << std::setw(2) << jvalue << " ==> " << *this << std::endl;
}
示例6: parseJson
void LogEntryRootFilter::parseJson(const json &j) {
auto filterType_json = j.find("filterType");
if (filterType_json != j.end()) {
filterType = (FilterType) filterType_json->get<int>();
} else {
filterType = filterIn;
}
}
示例7: ensureType
void JsonUtil::ensureType(const string& aFieldName, const json& aNew, const json& aExisting) {
if (aExisting.is_number()) {
if (!aNew.is_number()) {
throwError(aFieldName, ERROR_INVALID, "The new value must be a number");
}
} else if (aNew.type() != aExisting.type()) {
throwError(aFieldName, ERROR_INVALID, "Type of the new value doesn't match with the existing type");
}
}
示例8: to_dict
QM_NAMESPACE
boost::python::dict to_dict(json const& self) {
boost::python::dict d;
for(json::const_iterator it=self.begin();it!=self.end();++it) {
d[it->first] = it->second;
}
return d;
}
示例9: read_meas_array
std::shared_ptr<dariadb::MeasArray>
read_meas_array(const dariadb::scheme::IScheme_Ptr &scheme, const json &js) {
auto result = std::make_shared<dariadb::MeasArray>();
auto js_iter = js.find("append_values");
if (js_iter == js.end()) {
dariadb::logger_fatal("append_values not found");
return nullptr;
}
auto values = *js_iter;
for (auto it = values.begin(); it != values.end(); ++it) {
dariadb::MeasArray sub_result;
auto id_str = it.key();
auto val4id = it.value();
dariadb::Id id = scheme->addParam(id_str);
auto flag_js = val4id["F"];
auto val_js = val4id["V"];
auto time_js = val4id["T"];
if (flag_js.size() != val_js.size() || time_js.size() != val_js.size()) {
THROW_EXCEPTION("bad query format, flags:", flag_js.size(),
" values:", val_js.size(), " times:", time_js.size(),
" query: ", js.dump(1));
}
sub_result.resize(flag_js.size());
size_t pos = 0;
for (auto f_it = flag_js.begin(); f_it != flag_js.end(); ++f_it) {
dariadb::Flag f = *f_it;
sub_result[pos++].flag = f;
}
pos = 0;
for (auto v_it = val_js.begin(); v_it != val_js.end(); ++v_it) {
dariadb::Value v = *v_it;
sub_result[pos++].value = v;
}
pos = 0;
for (auto t_it = time_js.begin(); t_it != time_js.end(); ++t_it) {
dariadb::Time t = *t_it;
sub_result[pos++].time = t;
}
dariadb::logger("in query ", sub_result.size(), " values for id:", id);
for (auto v : sub_result) {
v.id = id;
result->push_back(v);
}
}
return result;
}
示例10:
RenderableSpriteBlueprint::RenderableSpriteBlueprint(json& j) {
if (j.count("diffuse")) {
_diffuse = j["diffuse"].get<std::string>();
}
if (j.count("normal")) {
_normal = j["normal"].get<std::string>();
}
if (j.count("lit")) {
_lit = j["lit"].get<bool>();
}
}
示例11: filterExactValues
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;
}
示例12:
optional<int> FavoriteHubUtils::deserializeIntHubSetting(const string& aFieldName, const json& aJson) {
auto p = aJson.find(aFieldName);
if (p == aJson.end()) {
return boost::none;
}
if ((*p).is_null()) {
return HUB_SETTING_DEFAULT_INT;
}
return JsonUtil::parseValue<int>(aFieldName, *p);
}
示例13: ValidateArguments
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;
}
示例14:
RenderableBackgroundBlueprint::RenderableBackgroundBlueprint(json& j) {
if (j.count("diffuse")) {
_diffuse = j["diffuse"].get<std::string>();
}
if (j.count("normal")) {
_normal = j["normal"].get<std::string>();
}
if (j.count("scale")) {
_scale = j["scale"].get<float>();
} else {
_scale = 1;
}
}
示例15: Color
LightBlueprint::LightBlueprint(json& j) {
_type = j["type"];
if (j.count("x")) _x = j["x"].get<float>();
if (j.count("y")) _y = j["y"].get<float>();
if (j.count("z")) _z = j["z"].get<float>();
if (j.count("drop")) _drop = j["drop"].get<float>();
if (j.count("end")) _end = j["end"].get<float>();
if (j.count("depth")) _depth = j["depth"].get<float>();
if (j.count("color")) {
_color = Color(j["color"][0], j["color"][1], j["color"][2]);
}
}