【问题标题】:JsonCpp wrapperJsonCpp 包装器
【发布时间】:2013-02-28 06:25:05
【问题描述】:

问题描述

我正在尝试为JsonCpp 编写包装器。我的包装器必须具有以下功能

  • Parse(const string& input)
  • GetString(string& output, const string name, bool optional = true)
  • SetString(const string& value, const string name, bool optional = true)
  • GetObject(const string& objectName)

我已经调用我的包装类Parser

class Parser
{
private:
    Json::Value mJsonObject;

public:
    bool Parse(const string& input);
    bool GetString(string& output, const string name, bool optional = true);
    bool SetString(const string& value, const string name, bool optional = true);
    Parser& GetObject(const string& objectName);
};

在我想写的代码中:

void foo()
{
    Parser::GetObject("IN").GetObject("Params").SetString("Param1", "this is json");
}

通过调用这个我想创建以下 JSON

{
    "IN" : {
        "Params" : {
            "Param1":"this is json"
        }
    }
}

问题

我必须如何实现GetObjectSetString 函数才能获得预期的结果?

【问题讨论】:

    标签: c++ class implementation wrapper jsoncpp


    【解决方案1】:

    首先,祝你好运:)

    我不确定您到底遇到了什么问题,但这里有一些您需要做的事情:

    • GetObject 返回*this,这样你就可以链接GetObject 调用
    • Json::Value 包含一个 operator[] ,它可以满足您的期望 - 获取关联的值,如果它不存在则创建它。 GetObject 可以简单地包装它。请记住使用子对象更新您的本地 mJsonObject
    • SetString 只是简单地将 GetObject 包装起来,然后通过字符串参数构造一个新的 Json::Value

    【讨论】:

    • 其实我建议GetObject返回*this,而是返回另一个代理对象(可能是同一类型)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 2016-11-26
    • 1970-01-01
    • 1970-01-01
    • 2021-05-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多