1

So I am working on an assignment and I need to parser several properties into a json to send over a network. Here is what the final value should look like.

{"__type":"Login:#Messages","Identity":{"sNumber":"value","Alias":"value","FirstName":"value","LastName":"value"}}

so the code I have is

boost::property_tree::ptree pt;
pt.put("__type", "Login:#Messages");
pt.put("Identity", myPlayer.Encode());

myplayer is a class that contains snumber, alias, firstname, lastname. the encode function returns a ptree but when I go to write_json it seems it doesnt know how to handle a ptree in a ptree. I tried parsing myPlayer into a json and puting that in the tree but it gives me this

   {"__type":"Login:#Messages","Identity":"{\"sNumber\":\"value\",\"Alias\":\"value\",\"FirstName\":\"value\",\"LastName\":\"value\"}"}

so how to I get boost to parse a ptree such that it can do a ptree in a ptree or doesnt add the escapes for the quotes and doesnt put the Identity property's value as a string? Thanks

lesyriad
  • 21
  • 1
  • 2

1 Answers1

0

Okay so boost ptree to json is a little weird but I figured out how to make subtrees.

Example output: {"__type":"Login:#Messages","Identity":"{\"sNumber\":\"value\"}}

Solution:

boost::property_tree::ptree pt;
pt.put("__type", "Login:#Messages");
pt.put("Identity.sNumber", value);

Solution 2:

boost::property_tree::ptree pt;
boost::property_tree::ptree subtree;
subtree.put("sNumber", value);
pt.put("__type", "Login:#Messages");
pt.put_child("Identity", subtree);

These 2 examples will both make a subtree in the ptree.

lesyriad
  • 21
  • 1
  • 2