簡體   English   中英

JsonCpp包裝器

[英]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"
        }
    }
}

如何必須實現GetObjectSetString函數才能獲得預期的結果?

首先,祝你好運:)

我不確定您到底在遇到什么問題,但是您需要執行以下操作:

  • GetObject返回*this ,以便您可以鏈接GetObject調用
  • Json::Value包含一個operator[] ,它可以完成您期望的操作-獲取關聯的值,如果不存在則創建它。 GetObject可以簡單地包裝它。 請記住使用子對象更新本地mJsonObject
  • SetString只是包裝GetObject然后通過字符串參數構造一個新的Json::Value

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM