Opened 12 years ago

Closed 11 years ago

#5210 closed Bugs (invalid)

JSON writer does not allow attributes and children in the same node

Reported by: Christian Ceelen <christian.ceelen@…> Owned by: Sebastian Redl
Milestone: To Be Determined Component: property_tree
Version: Boost 1.46.0 Severity: Problem
Keywords: Cc:

Description

The JSON serialization of property_tree is currently restricts to instances of property_tree that do contain data attributes only in leaf node.

    // Verify if ptree does not contain information that cannot be written to json
    template<class Ptree>
    bool verify_json(const Ptree &pt, int depth)
    {

        typedef typename Ptree::key_type::value_type Ch;
        typedef typename std::basic_string<Ch> Str;

        // Root ptree cannot have data
        if (depth == 0 && !pt.template get_value<Str>().empty())
            return false;
        
        // Ptree cannot have both children and data
        if (!pt.template get_value<Str>().empty() && !pt.empty())
            return false;

        // Check children
        typename Ptree::const_iterator it = pt.begin();
        for (; it != pt.end(); ++it)
            if (!verify_json(it->second, depth + 1))
                return false;

        // Success
        return true;

    }

To me this is a random limitation. I removed it locally and tried to generate an instance with attributes and children:

{
    "Object":{
        "Name": "Object Name",
        "Child": {
            "Name": "Child Name"
        }
    }
}

It works well. Why is this policy built into the json writer? Why is there no way to deactivate this policy?

Attachments (2)

test_property_tree.cpp (1.5 KB ) - added by anonymous 11 years ago.
Shop.xml (403 bytes ) - added by anonymous 11 years ago.

Download all attachments as: .zip

Change History (5)

comment:1 by Sebastian Redl, 12 years ago

Resolution: invalid
Status: newclosed

A property tree node can have both data and children, i.e. a tree can look like this:

root:                           <- has only children
  child1: "child 1 data"        <- has both data and children
    child1.1: "child 1.1 data"  <- has only data
    child1.2: "child 1.2 data"  <- has only data
  child2: "child 2 data"        <- has only data

There is no way to represent such a tree as JSON. A JSON node can be an object (it has named children), an array (it has unnamed children), or a basic data item (string, number, boolean, null). There is no JSON node that can have both direct data and children.

Therefore, the validation function rejects such trees when generating JSON.

by anonymous, 11 years ago

Attachment: test_property_tree.cpp added

by anonymous, 11 years ago

Attachment: Shop.xml added

comment:2 by Christian Ceelen <christian.ceelen@…>, 11 years ago

Resolution: invalid
Status: closedreopened

Thanks. I see this check is mandatory. I was poking into this function only for the sole reason that the error indicated it while i was playing a simple XML to JSON conversion. For instance:

<object>
        <type>Shop</type>
        <POC>
                <name>Private Investigation</name>
                <street>221B Baker Street</street>
                <town>London</town>
                <zip>NW1 6XE</zip>
                <country>United Kingdom</country>
        </POC>
        <workingDays>
            <Monday/>
            <Saturday/>
            <Sunday/>
        </workingDays>
</object>

This simple XML file is read in without any trouble. But if i walk through the resulting ptree and print all key and value contents, then it looks like this:

object: "



"
    type: "Private Investigation"
    POC: "





        "
        name: "Cabbies United"
        street: "221B Baker Street"
        town: "London"
        zip: "NW1 6XE"
        country: "United Kingdom"
    workingDays: "



        "
        Monday: ""
        Saturday: ""
        Sunday: ""

The attempt to serialize in JSON this ptree fails, because of the empty strings stored in the value fields of the keys object, POC, and workingDays. As the original XML has no attributes for these tags i am quite puzzled where these are coming from.

See the attached files for the small test i wrote. This fails for version 1.46.0 and 1.46.1.

comment:3 by Sebastian Redl, 11 years ago

Resolution: invalid
Status: reopenedclosed

Your XML contains whitespace between the various elements, and it is preserved. Therefore, the nodes contain both data and children, which, as mentioned, is not allowed for the JSON writer.

Pass trim_whitespace to read_xml, and it should collapse and remove the whitespace.

Note: See TracTickets for help on using tickets.