Question

Problem Description

I am trying to write wrapper for 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

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

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

Question

How I must implement GetObject and SetString function in order to get expected result ?

Was it helpful?

Solution

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
  • Json::Value contains an operator[] which does what you expect - get the associated value, creating it if it doesn't exist. GetObject can simply wrap that. Remember to update your local mJsonObject with the child object.
  • SetString simply wraps GetObject followed by constructing a new Json::Value via string argument
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top