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 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; }
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; }
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(); } }
btVector3 toBullet(Json::Value& jValue) { if(jValue.isArray()) { btVector3 retVal(0, 0, 0); Json::Value& jx = jValue[0]; Json::Value& jy = jValue[1]; Json::Value& jz = jValue[2]; if(jx.isDouble()) { retVal.setX(jx.asDouble()); } if(jy.isDouble()) { retVal.setY(jy.asDouble()); } if(jz.isDouble()) { retVal.setZ(jz.asDouble()); } return retVal; } if(jValue.isDouble()) { btDouble n = jValue.asDouble(); return btVector3(n, n, n); } else { return btVector3(0, 0, 0); } }
double readDouble(std::string key, double def) { Json::Value val = root.get(key, def); if (val.isDouble()) return val.asDouble(); 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, float & obj_val ) { if ( json_val.isDouble () ) { obj_val = json_val.asFloat (); 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 GetFloat64(const Json::Value& value, float64_t* out) { if (value.isNull()) { return false; } if (!value.isDouble()) { return false; } *out = value.asDouble(); return true; }
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; }
/***************以下函数用于读取json静态数据***************/ unsigned int to_uint(const Json::Value& val) { if ( val.isIntegral() ) return val.asUInt(); if ( val.isDouble() ) return (unsigned int)val.asDouble(); if ( val.isString() ) return strtoul(val.asCString(), NULL, 0 ); return 0; }
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() ); }
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() ); } }
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; }
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; }
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); } }
int64_t BtcJsonLegacy::GetBalance(const char *account, const int32_t &minConfirmations, const bool &includeWatchonly) { // note: json and bitcoind make a difference between NULL-strings and empty strings. Json::Value params = Json::Value(); params.append(account != NULL ? account : Json::Value()); // account params.append(minConfirmations); BtcRpcPacketPtr reply = this->modules->btcRpc->SendRpc(CreateJsonQuery(METHOD_GETBALANCE)); Json::Value result; if(!ProcessRpcString(reply, result) || !result.isDouble()) { return 0; } return this->modules->btcHelper->CoinsToSatoshis(result.asDouble()); }
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; }
void LuaModule::pushValue(const Json::Value &val, lua_State * stack) { if (val.isIntegral()) { lua_pushinteger(stack, val.asInt()); } else if (val.isDouble()) { lua_pushnumber(stack, val.asDouble()); } else if (val.isBool()) { lua_pushboolean(stack, val.asBool()); } else if (val.isString()) { lua_pushstring(stack, val.asString().c_str()); } else if (val.isNull()) { //lua_pushstring(stack, val.asString().c_str()); lua_pushnil(stack); } else { lua_pop(stack, 1); std::stringstream ss; ss << val.type(); std::string typeNum; ss >> typeNum; throw std::runtime_error("Value type error: value of type " + typeNum); } }
osc::OutboundPacketStream LogReplayer::constructSyncArg(char* buffer,std::string name, int index, Json::Value value) { // char buffer[1024]; osc::OutboundPacketStream p(buffer,1024); p << osc::BeginMessage("/sA"); p << client->getServerPassword().toStdString().c_str(); p << name.c_str(); p << index; if(value.isDouble()) { p << (float)value.asDouble(); } else if(value.isString()) { p << value.asString().c_str(); } else if(value.isInt()) { p << value.asInt(); } p << osc::EndMessage; return p; }
void TrexRpcCommand::check_field_type_common(const Json::Value &field, const std::string &name, field_type_e type, Json::Value &result) { std::stringstream ss; /* first check if field exists */ if (field == Json::Value::null) { ss << "field '" << name << "' is missing"; generate_parse_err(result, ss.str()); } bool rc = true; switch (type) { case FIELD_TYPE_BYTE: if ( (!field.isUInt()) || (field.asInt() > 0xFF)) { rc = false; } break; case FIELD_TYPE_UINT16: if ( (!field.isUInt()) || (field.asInt() > 0xFFFF)) { rc = false; } break; case FIELD_TYPE_UINT32: if ( (!field.isUInt()) || (field.asUInt() > 0xFFFFFFFF)) { rc = false; } break; case FIELD_TYPE_UINT64: if (!field.isUInt64()) { rc = false; } break; case FIELD_TYPE_BOOL: if (!field.isBool()) { rc = false; } break; case FIELD_TYPE_INT: if (!field.isInt()) { rc = false; } break; case FIELD_TYPE_DOUBLE: if (!field.isDouble()) { rc = false; } break; case FIELD_TYPE_OBJ: if (!field.isObject()) { rc = false; } break; case FIELD_TYPE_STR: if (!field.isString()) { rc = false; } break; case FIELD_TYPE_ARRAY: if (!field.isArray()) { rc = false; } break; default: throw TrexRpcException("unhandled type"); break; } if (!rc) { ss << "error at offset: " << field.getOffsetStart() << " - '" << name << "' is '" << json_type_to_name(field) << "', expecting '" << type_to_str(type) << "'"; generate_parse_err(result, ss.str()); } }
void Deserialize_Double( const Json::Value& srcValue, F8 &dstValue ) { CHK_VRET_IF_NOT( srcValue.isDouble() ); dstValue = srcValue.asDouble(); }
void ApplyUserAttributes(std::string name, AtNode* node,std::vector<std::string> tags,ProcArgs & args) { bool foundInPath = false; for(std::vector<std::string>::iterator it=args.userAttributes.begin(); it!=args.userAttributes.end(); ++it) { Json::Value userAttributes; if(it->find("/") != std::string::npos) // Based on path { if(name.find(*it) != std::string::npos) { userAttributes = args.userAttributesRoot[*it]; foundInPath = true; } } else if(matchPattern(name,*it)) // based on wildcard expression { userAttributes = args.userAttributesRoot[*it]; foundInPath = true; } else if(foundInPath == false) { if (std::find(tags.begin(), tags.end(), *it) != tags.end()) { userAttributes = args.userAttributesRoot[*it]; } } if(userAttributes.size() > 0) { for( Json::ValueIterator itr = userAttributes.begin() ; itr != userAttributes.end() ; itr++ ) { std::string attribute = itr.key().asString(); if( AiNodeLookUpUserParameter(node,attribute.c_str())) continue; const AtNodeEntry* nodeEntry = AiNodeGetNodeEntry(node); Json::Value val = args.userAttributesRoot[*it][attribute]; if( val.isString() ) { AddUserGeomParams(node,attribute.c_str(),AI_TYPE_STRING); AiNodeSetStr(node, attribute.c_str(), val.asCString()); } else if( val.isBool() ) { AddUserGeomParams(node,attribute.c_str(),AI_TYPE_BOOLEAN); AiNodeSetBool(node, attribute.c_str(), val.asBool()); } else if( val.isInt() ) { AddUserGeomParams(node,attribute.c_str(),AI_TYPE_INT); AiNodeSetInt(node, attribute.c_str(), val.asInt()); } else if( val.isUInt() ) { AddUserGeomParams(node,attribute.c_str(),AI_TYPE_UINT); AiNodeSetUInt(node, attribute.c_str(), val.asUInt()); } else if(val.isDouble()) { AddUserGeomParams(node,attribute.c_str(),AI_TYPE_FLOAT); AiNodeSetFlt(node, attribute.c_str(), val.asDouble()); } else if(val.isArray()) { // in the future we will convert to an arnold array type for now lets just // write out a json string AddUserGeomParams(node,attribute.c_str(),AI_TYPE_STRING); Json::FastWriter writer; AiNodeSetStr(node, attribute.c_str(), writer.write(val).c_str()); // AddUserGeomParams(node,attribute.c_str(),AI_TYPE_ARRAY ); // // get the type of the first entry, this will be our key as to // // what type of data is in this array // Json::Value firstValue = val[0]; // if (firstValue.isString()) // { // AtArray* arrayValues = AiArrayAllocate( val.size() , 1, AI_TYPE_STRING); // for( uint idx = 0 ; idx != val.size() ; idx++ ) // { // AiMsgInfo("[ABC] adding string %s to user array attribute '%s'",val[idx].asCString(),attribute.c_str()); // AiArraySetStr(arrayValues,idx,val[idx].asCString()); // } // AiNodeSetArray(node, attribute.c_str(), arrayValues); // } } // TODO color, matrix, vector } } } }
void ApplyOverrides(std::string name, AtNode* node, std::vector<std::string> tags, ProcArgs & args) { bool foundInPath = false; for(std::vector<std::string>::iterator it=args.overrides.begin(); it!=args.overrides.end(); ++it) { Json::Value overrides; if(it->find("/") != std::string::npos) // Based on path { if(name.find(*it) != std::string::npos) { overrides = args.overrideRoot[*it]; foundInPath = true; } } else if(matchPattern(name,*it)) // based on wildcard expression { overrides = args.overrideRoot[*it]; foundInPath = true; } else if(foundInPath == false) { if (std::find(tags.begin(), tags.end(), *it) != tags.end()) { overrides = args.overrideRoot[*it]; } } if(overrides.size() > 0) { for( Json::ValueIterator itr = overrides.begin() ; itr != overrides.end() ; itr++ ) { std::string attribute = itr.key().asString(); const AtNodeEntry* nodeEntry = AiNodeGetNodeEntry(node); const AtParamEntry* paramEntry = AiNodeEntryLookUpParameter(nodeEntry, attribute.c_str()); if ( paramEntry != NULL && attribute!="invert_normals" || attribute == "matte") { Json::Value val = args.overrideRoot[*it][itr.key().asString()]; if( val.isString() ) AiNodeSetStr(node, attribute.c_str(), val.asCString()); else if( val.isBool() ) { if(attribute == "matte") { AiMsgDebug("[ABC] adding enable_matte to %s", AiNodeGetName(node)); AddUserGeomParams(node,"enable_matte",AI_TYPE_BOOLEAN); AiNodeSetBool(node,"enable_matte", val.asBool()); } else { AiNodeSetBool(node, attribute.c_str(), val.asBool()); } } else if( val.isInt() ) { //make the difference between Byte & int! int typeEntry = AiParamGetType(paramEntry); if(typeEntry == AI_TYPE_BYTE) { if(attribute=="visibility") { AtByte attrViz = val.asInt(); // special case, we must determine it against the general viz. AtByte procViz = AiNodeGetByte( args.proceduralNode, "visibility" ); AtByte compViz = AI_RAY_ALL; { compViz &= ~AI_RAY_GLOSSY; if(procViz > compViz) procViz &= ~AI_RAY_GLOSSY; else attrViz &= ~AI_RAY_GLOSSY; compViz &= ~AI_RAY_DIFFUSE; if(procViz > compViz) procViz &= ~AI_RAY_DIFFUSE; else attrViz &= ~AI_RAY_DIFFUSE; compViz &= ~AI_RAY_REFRACTED; if(procViz > compViz) procViz &= ~AI_RAY_REFRACTED; else attrViz &= ~AI_RAY_REFRACTED; compViz &= ~AI_RAY_REFLECTED; if(procViz > compViz) procViz &= ~AI_RAY_REFLECTED; else attrViz &= ~AI_RAY_REFLECTED; compViz &= ~AI_RAY_SHADOW; if(procViz > compViz) procViz &= ~AI_RAY_SHADOW; else attrViz &= ~AI_RAY_SHADOW; compViz &= ~AI_RAY_CAMERA; if(procViz > compViz) procViz &= ~AI_RAY_CAMERA; else attrViz &= ~AI_RAY_CAMERA; } AiNodeSetByte(node, attribute.c_str(), attrViz); } else AiNodeSetByte(node, attribute.c_str(), val.asInt()); } else AiNodeSetInt(node, attribute.c_str(), val.asInt()); } else if( val.isUInt() ) AiNodeSetUInt(node, attribute.c_str(), val.asUInt()); else if( val.isDouble() ) AiNodeSetFlt(node, attribute.c_str(), val.asDouble()); } } } } }
void LuaContext::PushJson(const Json::Value& value) { if (value.isString()) { const std::string s = value.asString(); lua_pushlstring(lua_, s.c_str(), s.size()); } else if (value.isDouble()) { lua_pushnumber(lua_, value.asDouble()); } else if (value.isInt()) { lua_pushinteger(lua_, value.asInt()); } else if (value.isUInt()) { lua_pushinteger(lua_, value.asUInt()); } else if (value.isBool()) { lua_pushboolean(lua_, value.asBool()); } else if (value.isNull()) { lua_pushnil(lua_); } else if (value.isArray()) { lua_newtable(lua_); // http://lua-users.org/wiki/SimpleLuaApiExample for (Json::Value::ArrayIndex i = 0; i < value.size(); i++) { // Push the table index (note the "+1" because of Lua conventions) lua_pushnumber(lua_, i + 1); // Push the value of the cell PushJson(value[i]); // Stores the pair in the table lua_rawset(lua_, -3); } } else if (value.isObject()) { lua_newtable(lua_); Json::Value::Members members = value.getMemberNames(); for (Json::Value::Members::const_iterator it = members.begin(); it != members.end(); ++it) { // Push the index of the cell lua_pushlstring(lua_, it->c_str(), it->size()); // Push the value of the cell PushJson(value[*it]); // Stores the pair in the table lua_rawset(lua_, -3); } } else { throw OrthancException(ErrorCode_JsonToLuaTable); } }