Boost Variant:如何建模JSON?

时间:2011-07-03 07:25:01

标签: c++ json boost boost-spirit boost-variant

我正在尝试使用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&amp; 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);

只是张贴以便每个人都可以从中受益。

1 个答案:

答案 0 :(得分:7)

你必须使用递归包装器(你不应该从boost::variant派生):

struct JsonValue;

typedef boost::variant</*types*/, boost::recursive_wrapper<JsonValue> > JsonDataValue;

struct JsonValue
{
    JsonDataValue value;
};

要使Boost.Spirit采用JsonValue,您需要编写其中一个Fusion适配器以使原始变体类型适应结构。


  

此外,如何安全地将JsonValue转换为JsonObject?当我尝试做的时候:

这不是变体的工作方式。如果要将它们设置为值,只需将它们设置为任何其他值:

JsonValue val;
val.value = JsonValue();