[英]JsonCpp wrapper
我正在嘗試為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"
}
}
}
如何必須實現GetObject
和SetString
函數才能獲得預期的結果?
首先,祝你好運:)
我不確定您到底在遇到什么問題,但是您需要執行以下操作:
GetObject
返回*this
,以便您可以鏈接GetObject
調用 Json::Value
包含一個operator[]
,它可以完成您期望的操作-獲取關聯的值,如果不存在則創建它。 GetObject
可以簡單地包裝它。 請記住使用子對象更新本地mJsonObject
。 SetString
只是包裝GetObject
然后通過字符串參數構造一個新的Json::Value
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.