1

How to create a boost ptree which can be encoded to below JSON? i.e. I want to know how JSON Array of JSON Objects can be represented in boost ptree..

[
{"3":"SomeValue"},
{"40":"AnotherValue"},
{"23":"SomethingElse"},
{"9":"AnotherOne"},
{"1":"LastOne"}
]

I must say the below link doesn't answer: Creating JSON arrays in Boost using Property Trees

1 Answers1

1

The link does answer it. All the answers clearly show that you should use push_back (or insert, actually), not put_child.

You also have to read past the "how to make the array" and realize that you cannot have arrays as document root.

This is a symptom of the fact that Boost Ptree is not a JSON library. It's a Property Tree library, and it supports only property trees. The limitations are documented:

https://www.boost.org/doc/libs/1_74_0/doc/html/property_tree/parsers.html#property_tree.parsers.json_parser

enter image description here

DEMO

Here's the best you can do, assuming you did not really need the array as document root:

Live On Coliru

#define BOOST_BIND_GLOBAL_PLACEHOLDERS 
#include <boost/property_tree/json_parser.hpp>
#include <iostream>

using boost::property_tree::ptree;

int main() {
    ptree arr;

    for (auto [k,v]: { std::pair
            {"3",  "SomeValue"},
            {"40", "AnotherValue"},
            {"23", "SomethingElse"},
            {"9",  "AnotherOne"},
            {"1",  "LastOne"} })
    {
        ptree element;
        element.put(k, v);
        arr.push_back({"", element});
    }

    // can't have array at root of doc...
    ptree doc;
    doc.put_child("arr", arr);
    write_json(std::cout, doc);
}

Prints

{
    "arr": [
        {
            "3": "SomeValue"
        },
        {
            "40": "AnotherValue"
        },
        {
            "23": "SomethingElse"
        },
        {
            "9": "AnotherOne"
        },
        {
            "1": "LastOne"
        }
    ]
}

sehe
  • 374,641
  • 47
  • 450
  • 633
  • You can workaround this with some effort in not referring to the ptree/wptree but to the basic property tree in combination with variants for your custom types. For sure, this also requires some extensions for the reader and you'll have to write your own JSon-Writer (simple task in general). – Secundi Dec 04 '20 at 15:40
  • @Secundi That's not a simple task in general, and by the time you did all that you would be far better off with a JSON library anyways. I can't imagine what property [sic] of the Property Tree library keeps you interested in the added coupling/awkwardness – sehe Dec 04 '20 at 21:06
  • The implementation for a property_tree> is an easy task for any intermediate C++ developer. For the other stuff, there might be some extra efforts required I admit. Took me 1 1/2 days to make this tree fit for the requirements in total and I'm not a master developer (yet...). Why refering to the tree? It's a widely used data type as fas as I can see. The refactorization would be an easy task without a total redesign of your software. And the tree properties themselves are quite useful. – Secundi Dec 04 '20 at 21:49
  • "It's a widely used data type as fas as I can see." - not where I live. The only times I see it used is when abused for XML or JSON "support". I write my own tree datastructures routinely, and I don't see any appeal of using btree over a direct spelling with variant+container. Trees are widespread. Property Trees are not. But thanks for providing another POV – sehe Dec 04 '20 at 22:14
  • In fact, boost's property has several drawbacks and issues (bad debugable, boost header/symbol explosion...) but at least if you search within stackoverflow, there are thousands of questions about it and dozends even only for this particular issue discussed here about the JSON format. Writing an own tree structure might be sufficient for a lot of local purposes but if you have to provide it for a huger project world, things can become non trivial very fast since the data type has to ensure several higher aspects (iterator behavior, conversion behavior, getter/setter traits/policies). – Secundi Dec 04 '20 at 23:24