Boost变体:如何对JSON建模
Boost Variant: How to model JSON?
我正在尝试使用Boost Spirit将JSON对象存储为递归数据结构来解析JSON字符串:
Value <== [null, bool, long, double, std::string, Array, Object];
Array <== [Value, Value, Value, ...];
Object <== ["name1": Value, "name2": Value, ...];
这是我的代码:
#include <map>
#include <vector>
#include <string>
#include <boost/variant.hpp>
#include <boost/shared_array.hpp>
#include <boost/shared_ptr.hpp>
struct JsonNull {};
struct JsonValue;
typedef std::map<std::string, JsonValue *> JsonObject;
typedef std::vector<JsonValue *> JsonArray;
struct JsonValue : boost::variant<JsonNull, bool, long, double, std::string, JsonArray, JsonObject>
{
};
JsonValue aval = JsonObject();
编译时我得到错误:
Error C2440: 'initializing' : cannot convert from 'std::map<_Kty,_Ty>' to 'JsonValue'
此外,如何将JsonValue安全地转换为JsonObject?当我尝试做:
boost::get<JsonObject>(aval) = JsonObject();
这会导致运行时异常/致命故障。
非常感谢您的帮助。
编辑:
根据@Nicol的建议,我得出了以下代码:
struct JsonNull {};
struct JsonValue;
typedef std::map<std::string, JsonValue *> JsonObject;
typedef std::vector<JsonValue *> JsonArray;
typedef boost::variant<
JsonNull, bool, long, double, std::string,
JsonObject, JsonArray,
boost::recursive_wrapper<JsonValue>
> JsonDataValue;
struct JsonValue
{
JsonDataValue data;
};
我可以在JsonObject&JsonArray就这么简单:
JsonValue *pJsonVal = new JsonValue();
boost::get<JsonObject>(pCurrVal->data).insert(
std::pair<std::string, JsonValue *>("key", pJsonVal)
);
boost::get<JsonArray>(pCurrVal->data).push_back(pJsonVal);
只是为了让每个人都能从中受益。
您必须使用递归包装器(并且不应该从boost::variant
派生(:
struct JsonValue;
typedef boost::variant</*types*/, boost::recursive_wrapper<JsonValue> > JsonDataValue;
struct JsonValue
{
JsonDataValue value;
};
要使BoostSpirit获得JsonValue,您需要编写其中一个Fusion适配器,以将原始变体类型调整为结构。
此外,如何将JsonValue安全地转换为JsonObject?当我尝试做:
变体不是这样工作的。如果你想将它们设置为一个值,只需像设置其他值一样设置它们:
JsonValue val;
val.value = JsonValue();
相关文章:
- 在c++中使用nlohmann从类到json的转换
- C++json插入数组
- nlohmann-json将一个数组插入到另一个数组中
- 如何使用curlpp通过POST方法上传文件和json数据
- JSON转换为nlohmann JSON-lib中的结构数组
- NLOHMANN 的 JSON 库将数组转换为结构向量
- 在nlohmann json中,如何将嵌套对象的数组转换为嵌套结构的向量
- 如何在 nlohmann 的 json 库中获取数组长度?
- UE4 C++ 我无法将 JSON 嵌套值获取到 TArray
- 如何在特定位置追加 json 文件
- 有没有办法让编译器在我放置字符串而不是 nlohmann::json 对象时抛出错误?
- 使用 jsoncpp 解析 json 数组字符串
- 将 FlatBuffer 对象序列化为 JSON,而不使用其架构文件
- 如何在 c++ 非托管代码中反序列化 byte[] 的 json 字符串?
- 使用 cmake 生成 compile_commands.json
- 如何使用 c++ 和提升库生成 json
- 满意和建模的概念?
- 使用 minijson-reader 库读取 JSON 字符串时出现问题
- 在c++中建模JSon的简单方法
- Boost变体:如何对JSON建模