Rapidjson::Type的开关大小写.我尝试解析的
Switch case for Rapidjson::Type
JSON看起来像这样:
{
"testBool": true,
"testString": "eu"
}
而且我目前的解析器看起来真的很丑,真的感觉有一种更优雅的方法可以解决这个问题。我尝试使用 document.GetObject().GetType()
研究开关盒的rapidjson::Type
,但它不能提供与使用Get%TypeName%()
函数可以实现的相同类型精度。 hashmap
只不过是std::unordered_map<std::string, std::any>
的包装纸。
rapidjson::Document document;
document.Parse(tmp_string.c_str());
for (auto& member : document.GetObject())
{
if (member.value.IsBool())
{
hashmap->addEntry<bool>(member.name.GetString(), member.value.GetBool());
}
else if (member.value.IsString())
{
hashmap->addEntry<std::string>(member.name.GetString(), member.value.GetString());
}
else if (member.value.IsInt())
{
hashmap->addEntry<int>(member.name.GetString(), member.value.GetInt());
}
.....
//And so on
.....
}
我目前的解析器看起来真的很丑
美在被(呃)持有者的眼中...这是我的代码:
static void
printField(const Value& e, const string& fld, bool print_newline = true) {
const Value &v = fld.empty() ? e : e[fld];
if (print_newline)
cout << endl << "t";
if (not fld.empty())
cout << fld << ": ";
if ( /* custom stuff required? */ ) {
// Do custom stuff
else {
switch (v.GetType()) {
case kNullType:
cout << "Null";
break;
case kFalseType:
case kTrueType:
cout << v.GetBool();
break;
case kObjectType: {
bool first = true;
cout << "{ ";
for (const auto &subfield: v.GetObject()) {
if (first)
first = false;
else
cout << ", ";
printField(v, subfield.name.GetString(), false);
}
cout << " }";
break;
}
case kArrayType: {
bool first = true;
cout << "[ ";
for (const auto &arrEntry: v.GetArray()) {
if (first)
first = false;
else
cout << ", ";
printField(arrEntry, "", false);
}
cout << " ]";
break;
}
case kStringType:
cout << v.GetString();
break;
case kNumberType:
if (v.IsInt64())
cout << v.GetInt64();
else if (v.IsUint64())
cout << v.GetUint64();
else
cout << v.GetDouble();
break;
default:
stringstream msg;
msg << "Unexpected RapidJSON Value type: " << v.GetType();
throw logic_error(msg.str());
}
}
}
这使用字符串化的东西来解决一些问题,但是,如果你不喜欢这样,你可以手动获得相同的效果。 它使用级联if
细分IsNumber
情况;如果您需要更多分辨率,可以将其他情况添加到其中。
相关文章:
- 使用 [] 运算符时"binding reference of type discards qualifiers"
- 在 QVector<std::unique_ptr 上使用 std::find<Type>>
- 在类型和包装器之间reinterpret_cast是否安全<Type>?
- 使用 cmake 的 Linux 终端上的"Conversion to non-scalar type is requested"错误
- 控制到达非空函数clang(-Wreturn-type)的末尾
- 切换大小写后如何阻止变量重置?
- std::unordered_map 类型对象声明期间出现"field has incomplete type"错误
- 无法找到简单的开关大小写枚举错误
- 将系数存储在头文件的数组中("does not name a type"错误)
- 仿射密码解密,输出大小写不同
- 尝试打开 ifstream 时出现"Incomplete type"错误
- 将成员函数作为构造函数参数调用时出错 "Variable is not a type name"
- 在"结构提升::enable_if<提升::is_pod<T>,无效>"中没有名为"type"的类型
- 编写宏函数来更改字符串的大小写?
- "Missing type specifier - int assumed"无法通过向主函数添加"return 0"来解决
- OpenCV CV_16F type
- 使字母检查不区分大小写
- 如何执行不区分大小写的字符串比较?
- C++ "错误:在'类 std::result_of< ... >"中没有名为'type'的类型"
- Rapidjson::Type的开关大小写.我尝试解析的