Values Factory::jsonToValues(const Json::Value& values) { Values outValues; if (values.isInt()) outValues.emplace_back(values.asInt()); else if (values.isDouble()) outValues.emplace_back(values.asFloat()); else if (values.isArray()) for (const auto& v : values) { if (v.isInt()) outValues.emplace_back(v.asInt()); else if (v.isDouble()) outValues.emplace_back(v.asFloat()); else if (v.isArray()) outValues.emplace_back(jsonToValues(v)); else outValues.emplace_back(v.asString()); } else outValues.emplace_back(values.asString()); return outValues; }
bool JsonHelper::ToBool( const Json::Value& value, bool defaultResult ) { if ( value.isBool() ) { return value.asBool(); } if ( value.isInt() ) { return (bool)value.asInt(); } if ( value.isDouble() ) { return (bool)value.asDouble(); } if ( value.isUInt() ) { return (bool)value.asUInt(); } if ( value.isString() ) { const std::string& str = value.asString(); return ( str == "true" || str == "1" ); } return defaultResult; }
/** * Expected input: * { * alg: "aes", * keySize : an int - 128/192/256 * } * * Output: * { * key: generated key data * } */ Json::Value AES::generateKey(const std::string & algorithm, Json::Value & args) { size_t keySize = 128; if (args.isMember("keySize")) { Json::Value keySizeV = args["keySize"]; if (!keySizeV.isInt()) { throw std::string("keySize must be an int"); } keySize = keySizeV.asInt(); switch (keySize) { case 128: case 192: case 256: // awesome! break; default: throw errorMessage("Not a valid key size: ", keySize); } } AESParams params(*this, SB_AES_CBC, SB_AES_128_BLOCK_BITS, true); DataTracker dt; AESKey key(params, keySize); key.get(dt); Json::Value keyData = toJson(dt.data, dt.dataLen); Json::Value toReturn; toReturn["key"] = keyData; return toReturn; }
// Read a natural Natural natural( Optizelle::Messaging const & msg, Json::Value const & json, std::string const & name ) { // Set the error message std::string const err_msg = "Invalid JSON parameter: " + name + " contains an invalid natural."; // As long as we have an unsigned integer, grab it if(json.isUInt()) return Natural(Json::Value::UInt64(json.asUInt64())); // If we have an integer, grab it if it's positive else if(json.isInt()) { Integer val(json.asInt64()); if(val>=0) return Natural(val); else msg.error(err_msg); // Anything else is an error } else msg.error(err_msg); // We should not hit this point throw; }
FB::variant jsonValueToVariant( Json::Value root ) { Json::Value def; if (root.isString()) return root.asString(); else if (root.isBool()) return root.asBool(); else if (root.isDouble()) return root.asDouble(); else if (root.isInt()) return root.asInt(); else if (root.isUInt()) return root.asUInt(); else if (root.isNull()) return FB::FBNull(); else if (root.isArray()) { FB::VariantList outList; for (size_t i = 0; i < root.size(); ++i) { outList.push_back(jsonValueToVariant(root.get(i, def))); } return outList; } else if (root.isObject()) { Json::Value::Members members = root.getMemberNames(); FB::VariantMap outMap; for (Json::Value::Members::iterator it = members.begin(); it != members.end(); ++it) { outMap[*it] = jsonValueToVariant(root.get(*it, def)); } return outMap; } else { return FB::FBVoid(); } }
static void add( MetadataNode& parent, const std::string& name, const Json::Value& node ) { if (node.isNull()) { parent.add(name, ""); } else if (node.isBool()) { parent.add(name, node.asBool()); } else if (node.isInt()) { parent.add(name, node.asInt64()); } else if (node.isUInt()) { parent.add(name, node.asUInt64()); } else if (node.isDouble()) { parent.add(name, node.asDouble()); } else if (node.isString()) { parent.add(name, node.asString()); } else if (node.isObject()) { MetadataNode object = parent.add(name); for (const std::string& name: node.getMemberNames()) { add(object, name, node[name]); } } else if (node.isArray()) { for (const Json::Value& item: node) { add(parent, name, item); } } }
bool ConfigFile::load_config() { std::ifstream file; file.open(get_config_file()); if (!file) { LOGE("Config file %s not found", get_config_file().c_str()); return false; } Json::Reader reader; bool success = reader.parse(file, m_root, false); file.close(); if (!success) { LOGE("Failed to parse configuration file"); return false; } const Json::Value jsonversion = m_root[CONF_VERSION]; if (jsonversion.isInt()) { m_version = jsonversion.asInt(); } else { return false; } return true; }
bool Deserialize ( const Json::Value& json_val, bool& obj_val ) { /** * Warning: the default type may be int, even you Serialize a bool value */ if ( json_val.isBool () ) { obj_val = json_val.asBool (); return true; } else if ( json_val.isInt () ) { int tmp = json_val.asInt (); if ( ! tmp ) { obj_val = false; } else { obj_val = true; } return true; } return false; }
int readInt(std::string key, int def) { Json::Value val = root.get(key, def); if (val.isInt()) return val.asInt(); else return def; }
/// 从 Json 解析数据到 dictNode 中 static void parseFromJsonToDictionary(const Json::Value & jsonNode, CCDictionary * dictNode) { Json::Value::Members members = jsonNode.getMemberNames(); for (Json::Value::Members::iterator beg = members.begin(); beg != members.end(); ++beg) { std::string name = *beg; Json::Value child = jsonNode[name]; if (child.isArray()) { CCArray * arr = CCArray::create(); parseFromJsonToArray(child, arr); dictNode->setObject(arr, name); } else if (child.isObject()) { CCDictionary * dict = CCDictionary::create(); parseFromJsonToDictionary(child, dict); dictNode->setObject(dict, name); } else if (child.isString()) { CCString * str = CCString::createWithFormat("%s", child.asCString()); dictNode->setObject(str, name); } else if (child.isInt()) { CCString * str = CCString::createWithFormat("%d", child.asInt()); dictNode->setObject(str, name); } else if (child.isUInt()) { CCString * str = CCString::createWithFormat("%u", child.asUInt()); dictNode->setObject(str, name); } else if (child.isInt64()) { CCString * str = CCString::createWithFormat("%lld", child.asInt64()); dictNode->setObject(str, name); } else if (child.isUInt64()) { CCString * str = CCString::createWithFormat("%llu", child.asUInt64()); dictNode->setObject(str, name); } else if (child.isDouble()) { CCString * str = CCString::createWithFormat("%f", child.asDouble()); dictNode->setObject(str, name); } else if (child.isBool()) { CCString * str = CCString::createWithFormat("%d", child.asInt()); dictNode->setObject(str, name); } } }
void JsonTree::init( const string &key, const Json::Value &value, bool setType, NodeType nodeType, ValueType valueType ) { mKey = key; mNodeType = nodeType; mParent = 0; mValue = ""; mValueType = valueType; if( ! value.isNull() && ( value.isArray() || value.isObject() ) ) { if( value.isArray() ) { mNodeType = NODE_ARRAY; for ( uint32_t i = 0; i < value.size(); i++ ) { pushBack( JsonTree( "", value[ i ] ) ); } } else if( value.isObject() ) { mNodeType = NODE_OBJECT; Json::Value::Members members = value.getMemberNames(); for( Json::Value::Members::const_iterator memberIt = members.begin(); memberIt != members.end(); ++memberIt ) { string key = *memberIt; pushBack( JsonTree( key, value[ key ] ) ); } } } else { if( value.isBool() ) { mValue = toString( value.asBool() ); if( setType ) { mValueType = VALUE_BOOL; } } else if ( value.isDouble() ) { mValue = toString( value.asDouble() ); if ( setType ) { mValueType = VALUE_DOUBLE; } } else if ( value.isInt() ) { mValue = toString( value.asInt() ); if ( setType ) { mValueType = VALUE_INT; } } else if ( value.isString() ) { mValue = toString( value.asString() ); if ( setType ) { mValueType = VALUE_STRING; } } else if ( value.isUInt() ) { mValue = toString( value.asUInt() ); if ( setType ) { mValueType = VALUE_UINT; } } } }
bool Deserialize ( const Json::Value& json_val, int& obj_val ) { if ( json_val.isInt () ) { obj_val = json_val.asInt (); return true; } return false; }
bool QSanProtocol::Utils::tryParse(const Json::Value &arg, double &result) { if (arg.isDouble()) result = arg.asDouble(); else if (arg.isInt()) result = arg.asInt(); else return false; return true; }
bool GetUInt32(const Json::Value& value, uint32_t* out) { if (value.isNull()) { return false; } if (!(value.isInt() || value.isUInt())) { return false; } *out = value.asUInt(); return true; }
bool GetInt32(const Json::Value& value, int32_t* out) { if (value.isNull()) { return false; } if (!value.isInt()) { return false; } *out = value.asInt(); return true; }
int Utils::GetIntFromJsonValue(Json::Value &value, int defaultValue) { int res = defaultValue; // some json responses have ints formated as strings if (value.isString()) res = StringToInt(value.asString()); else if (value.isInt()) res = value.asInt(); return res; }
double Utils::GetDoubleFromJsonValue(Json::Value &value, double defaultValue) { double res = defaultValue; /* some json responses have doubles formated as strings, or an expected double is formated as an int */ if (value.isString()) res = StringToDouble(value.asString()); else if (value.isInt() || value.isDouble()) res = value.asDouble(); return res; }
void ValueTest::checkIs( const Json::Value &value, const IsCheck &check ) { JSONTEST_ASSERT_EQUAL( check.isObject_, value.isObject() ); JSONTEST_ASSERT_EQUAL( check.isArray_, value.isArray() ); JSONTEST_ASSERT_EQUAL( check.isBool_, value.isBool() ); JSONTEST_ASSERT_EQUAL( check.isDouble_, value.isDouble() ); JSONTEST_ASSERT_EQUAL( check.isInt_, value.isInt() ); JSONTEST_ASSERT_EQUAL( check.isUInt_, value.isUInt() ); JSONTEST_ASSERT_EQUAL( check.isIntegral_, value.isIntegral() ); JSONTEST_ASSERT_EQUAL( check.isNumeric_, value.isNumeric() ); JSONTEST_ASSERT_EQUAL( check.isString_, value.isString() ); JSONTEST_ASSERT_EQUAL( check.isNull_, value.isNull() ); }
sf::Color jsonNodeAs<sf::Color>(const Json::Value& node) { sf::Color c = sf::Color::White; if (node.isArray()) { c.r = static_cast<uint8_t>(node.get(0u, 255).asUInt()); c.g = static_cast<uint8_t>(node.get(1u, 255).asUInt()); c.b = static_cast<uint8_t>(node.get(2u, 255).asUInt()); c.a = static_cast<uint8_t>(node.get(3u, 255).asUInt()); } else if (node.isInt()) { unsigned int ic = node.asUInt(); c.r = static_cast<uint8_t>(ic & 0xFF0000 >> 16); c.g = static_cast<uint8_t>(ic & 0xFF00 >> 8); c.b = static_cast<uint8_t>(ic & 0xFF); c.a = static_cast<uint8_t>(ic & 0xFF000000 >> 24); }
void util::PrintJSONValue( Json::Value val ){ if( val.isString() ) { printf( "string(%s)", val.asString().c_str() ); } else if( val.isBool() ) { printf( "bool(%d)", val.asBool() ); } else if( val.isInt() ) { printf( "int(%d)", val.asInt() ); } else if( val.isUInt() ) { printf( "uint(%u)", val.asUInt() ); } else if( val.isDouble() ) { printf( "double(%f)", val.asDouble() ); } else { printf( "unknown type=[%d]", val.type() ); } }
int TConfigParser::ToInt(const Json::Value& v, const std::string& title) { if (v.isInt()) return v.asInt(); if (v.isString()) { try { return std::stoi(v.asString(), /*pos= */ 0, /*base= */ 0); } catch (const std::logic_error& e) {} } throw TConfigParserException( title + ": plain integer or '0x..' hex string expected instead of '" + v.asString() + "'"); // v.asString() should give a bit more information what config this exception came from }
int JsonHelper::ToInt( const Json::Value& value, int defaultResult ) { if ( value.isInt() ) { return value.asInt(); } if ( value.isDouble() ) { return (int) value.asDouble(); } if ( value.isString() ) { return Core::Convert::ToInt( value.asString() ); } return defaultResult; }
std::string JsonHelper::ToString( const Json::Value& value, const std::string& defaultResult ) { if ( value.isString() ) { return value.asString(); } if ( value.isInt() ) { return Convert::ToString( value.asInt() ); } if ( value.isDouble() ) { return Convert::ToString( (float)value.asDouble() ); } return defaultResult; }
float JsonHelper::ToFloat( const Json::Value& value, float defaultResult ) { if ( value.isDouble() ) { return (float) value.asDouble(); } if ( value.isInt() ) { return (float) value.asInt(); } if ( value.isString() ) { return Convert::ToFloat( value.asString() ); } return defaultResult; }
void Log::execute(fsm::Context * ctx, const log4cplus::Logger & log, const std::string & sessionId)const { std::string loginfo = m_strExpr; if (ctx && m_Type.compare("script") == 0) { Json::Value jsonval = ctx->eval(m_strExpr, m_strFileName, m_lineNo/*,m_node*/); if (jsonval.isString() || jsonval.isBool() || jsonval.isNull()){ loginfo = jsonval.asString(); } else if (jsonval.isInt()){ loginfo = std::to_string(jsonval.asInt()); } else if (jsonval.isUInt()){ loginfo = std::to_string(jsonval.asUInt()); } else if (jsonval.isDouble()){ loginfo = std::to_string(jsonval.asDouble()); } else if (jsonval.isObject()) { loginfo = jsonval.toStyledString(); } } if (m_strLevel.compare("trace") == 0){ LOG4CPLUS_TRACE(log, "." + sessionId, m_strFileName << ":" << m_lineNo << "," << loginfo); } else if (m_strLevel.compare("debug") == 0){ LOG4CPLUS_DEBUG(log, "." + sessionId, m_strFileName << ":" << m_lineNo << "," << loginfo); } else if (m_strLevel.compare("info") == 0){ LOG4CPLUS_INFO(log, "." + sessionId, m_strFileName << ":" << m_lineNo << "," << loginfo); } else if (m_strLevel.compare("warn") == 0){ LOG4CPLUS_WARN(log, "." + sessionId, m_strFileName << ":" << m_lineNo << "," << loginfo); } else if (m_strLevel.compare("error") == 0){ LOG4CPLUS_ERROR(log, "." + sessionId, m_strFileName << ":" << m_lineNo << "," << loginfo); } else if (m_strLevel.compare("fatal") == 0){ LOG4CPLUS_FATAL(log, "." + sessionId, m_strFileName << ":" << m_lineNo << "," << loginfo); } else{ LOG4CPLUS_INFO(log, "." + sessionId, m_strFileName << ":" << m_lineNo << "," << loginfo); } }
void Deserialize_UInt32( const Json::Value& srcValue, UINT32 &dstValue ) { //HACK: Json Cpp saves small unsigned integers as signed ints if( srcValue.isInt() ) { const Json::Int signedIntValue = srcValue.asInt(); if( signedIntValue >= Json::Value::minInt && signedIntValue <= Json::Value::maxInt ) { dstValue = signedIntValue; return; } } CHK_VRET_IF_NOT( srcValue.isUInt() ); Assert( srcValue.asUInt() < MAX_UINT32 ); dstValue = srcValue.asUInt(); }
bool commom::check_one_passin_class(Json::Value& val_one, ClassType class_type) { if (val_one == Json::Value::null) return false; bool result = false; switch(class_type) { case int_value: result = val_one.isInt(); break; case string_value: result = val_one.isString(); break; default: result = false; break; } return result; }
long JsonHelper::ToLong( const Json::Value& value, long defaultResult ) { if ( value.isUInt() ) { return value.asUInt(); } if ( value.isInt() ) { return ((long)value.asInt()); } if ( value.isDouble() ) { return value.asDouble(); } if ( value.isString() ) { return Core::Convert::ToLong( value.asString() ); } return defaultResult; }
size_t k8s_component::extract_pod_restart_count(const Json::Value& item) { size_t restart_count = 0; Json::Value status = item["status"]; if(!status.isNull()) { Json::Value containers = status["containerStatuses"]; if(!containers.isNull()) { for (auto& container : containers) { Json::Value rc = container["restartCount"]; if(!rc.isNull() && rc.isInt()) { restart_count += rc.asInt(); } } } } return restart_count; }
void UploadThread::uploadFile(const std::string& fileName) { std::ifstream file(fileName.c_str(), std::ios::in); if(file) { file.seekg(0,std::ios::end); std::streampos fileLength = file.tellg(); file.seekg(0,std::ios::beg); std::string fileData; fileData.resize(fileLength); file.read(&fileData[0],fileLength); fileData.insert(0, "data="); GamePlatform::WebRequest request; request.Request( mServerRequestType, mServerHost, mServerPort, mServerRequestPath, fileData); // If there was a socket error exit the thread. Updates // will try again the next time report data is committed if(request.mError < 0) return; if(!request.mMessageBody.empty()) { Json::Value resultValue; Json::Reader resultReader; if(resultReader.parse(request.GetMessageBodyString(), resultValue, false)) { Json::Value errorValue = resultValue["error"]; if(errorValue.isInt() && errorValue.asInt() == 0) { // Guaranteed success delete the file unlink(fileName.c_str()); } } } } }