简体   繁体   English

JsonCpp包装器

[英]JsonCpp wrapper

Problem Description 问题描述

I am trying to write wrapper for JsonCpp . 我正在尝试为JsonCpp编写包装器。 My wrapper must have following functions 我的包装器必须具有以下功能

  • 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)

I have call my wrapper class Parser 我已经调用了我的包装器类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);
};

In the code I want to write following: 在代码中,我想编写以下代码:

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

by calling this I want to create following JSON 通过调用它,我想创建以下JSON

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

Question

How I must implement GetObject and SetString function in order to get expected result ? 如何必须实现GetObjectSetString函数才能获得预期的结果?

First of all, good luck :) 首先,祝你好运:)

I'm not sure what exactly you're having trouble with, but here are some things you need to do: 我不确定您到底在遇到什么问题,但是您需要执行以下操作:

  • GetObject returns *this , so that you can chain GetObject calls GetObject返回*this ,以便您可以链接GetObject调用
  • Json::Value contains an operator[] which does what you expect - get the associated value, creating it if it doesn't exist. Json::Value包含一个operator[] ,它可以完成您期望的操作-获取关联的值,如果不存在则创建它。 GetObject can simply wrap that. GetObject可以简单地包装它。 Remember to update your local mJsonObject with the child object. 请记住使用子对象更新本地mJsonObject
  • SetString simply wraps GetObject followed by constructing a new Json::Value via string argument SetString只是包装GetObject然后通过字符串参数构造一个新的Json::Value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM