void CompareJson(json::Value& lhs, json::Value& rhs, json::Value& output) { std::string lstr, rstr; if (rhs.type() != lhs.type()) return; switch (lhs.type()) { case json::Value::tString: lstr = lhs.getString(); rstr = rhs.getString(); if (!lstr.empty() && lstr[0] == '$' && lstr.back() == '$') { size_t slash = lstr.find('/'); std::string cat = lstr.substr(1, slash - 1); std::string name = lstr.substr(slash + 1, lstr.length() - slash - 2); output[cat][name] = rstr; } break; case json::Value::tArray: for (size_t i = 0; i < lhs.length(); ++i) { if (i < rhs.length()) { CompareJson(lhs[i], rhs[i], output); } } break; case json::Value::tObject: for (auto it = lhs.begin(); it != lhs.end(); ++it) { if (rhs.has(it.key())) { CompareJson(*it, rhs[it.key()], output); } } break; } }
void Session::read(const JSON::Value &value) { creationTime = value.has("created") ? (uint64_t)Time::parse(value.getString("created")) : 0; lastUsed = value.has("last_used") ? (uint64_t)Time::parse(value.getString("last_used")) : 0; user = value.getString("user", ""); ip = value.has("ip") ? IPAddress(value.getString("ip")) : IPAddress(); }
static void numberToWide(PrintTextA &print) { JSON::PFactory f = JSON::create(); JSON::Value v = f->newValue((natural)123); String tst1 = v->getString(); String tst2 = v->getString(); print("%1,%2") << tst1 << tst2; }
void Tool::read(const JSON::Value &value) { setNumber(value.getU32("number", number)); if (value.hasString("units")) units = ToolUnits::parse(value.getString("units")); double scale = units == ToolUnits::UNITS_INCH ? 25.4 : 1; if (value.hasString("shape")) shape = ToolShape::parse(value.getString("shape")); if (value.hasNumber("length")) setLength(value.getNumber("length") * scale); if (value.hasNumber("diameter")) setDiameter(value.getNumber("diameter") * scale); if (value.hasNumber("snub_diameter")) setSnubDiameter(value.getNumber("snub_diameter") * scale); setDescription(value.getString("description", "")); }
void TransformJson(json::Value& value, json::Value& data, Func const& func) { switch (value.type()) { case json::Value::tString: value = func(value.getString(), data); break; case json::Value::tArray: case json::Value::tObject: for (auto& sub : value) { TransformJson(sub, data, func); } break; } }
void Atom::loadJSON(const JSON::Value &value, float scale) { if (0 < value.size()) type = value.getString(0); else THROWS("Atom expected list of at least length 1"); if (String::toUpper(type) == "UNKNOWN") type = "?"; if (1 < value.size()) charge = (float)value.getNumber(1); if (2 < value.size()) radius = (float)value.getNumber(2) * scale; if (3 < value.size()) mass = (float)value.getNumber(3); if (4 < value.size()) number = (unsigned)value.getNumber(4); else number = numberFromName(type); if (!number) number = numberFromName(type); if (!charge) charge = chargeFromNumber(number); if (!radius) radius = radiusFromNumber(number); if (!mass) mass = massFromNumber(number); }