poco icon indicating copy to clipboard operation
poco copied to clipboard

Condensed JSON creation code

Open ElephantInTheRoomFirst opened this issue 9 years ago • 2 comments

I want to produce following JSON {"lib":100,"status":{"code":1000,"msg":"Success."}}

This is my code which works fine

Poco::JSON::Object RetJson(true); RetJson.set("lib", 100); Poco::JSON::Object JStatus(true); JStatus.set("code", 1000); JStatus.set("msg", "Success"); RetJson.set("status", JStatus);

In above code I do not like creation of JStatus object. Therefore I want to use reduced JSON creation if available like following. Poco::JSON::Object RetJson(true); RetJson.set("lib", 100); RetJson.get("status").set("code", 1000); //will not compile RetJson.get("status").set("msg", "Success"); //will not compile

RetJson.get("status")["code"]= 1000; //will throw exception RetJson.get("status")["msg"]= "Success"; //will throw exception

Please, let me know if you have alternatives.

ElephantInTheRoomFirst avatar May 14 '17 20:05 ElephantInTheRoomFirst

A new method "addObject" which returns the newly object could help and the set method must be changed so that it returns the current object.

Poco::JSON::Object` RetJson(true);
RetJson.set("lib", 100);
RetJson.addObject("status")
     .set("code", 1000)
     .set("msg", "Success");

Is this what you are looking for?

fbraem avatar May 15 '17 11:05 fbraem

Thanks your help. I decided to use another library for JSON.

ElephantInTheRoomFirst avatar Jun 26 '17 20:06 ElephantInTheRoomFirst