[Boost-users] [property tree][json]how to create empty array?

1,296 views
Skip to first unread message

Kulti

unread,
Mar 12, 2011, 12:26:57 PM3/12/11
to boost...@lists.boost.org
Hi, all!

It's possible to get following output from
boost::property_tree::json_parser::write_json ?

{
  "id": "id",
  "method": "test_method",
  "params": []
}

I know how to create array with some items, but when I try to create
empty array, I've get "params": ""
_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Sebastian Redl

unread,
Mar 12, 2011, 12:39:59 PM3/12/11
to boost...@lists.boost.org

On 12.03.2011, at 18:26, Kulti wrote:

> Hi, all!
>
> It's possible to get following output from
> boost::property_tree::json_parser::write_json ?
>
> {
> "id": "id",
> "method": "test_method",
> "params": []
> }
>
> I know how to create array with some items, but when I try to create
> empty array, I've get "params": ""

Sorry, that's not possible. The fundamental problem is that PTree is only a rough match to JSON. PTree can have more complicated nodes, but JSON is typed. So when serializing a PTree as JSON, the serializer is guessing at the correct serialization form for each node: nodes with only named children become objects, those with only unnamed children become arrays, all others become strings.

A PTree with a different data type than string could be a better match, but the current JSON functionality is not usable with such a tree.

Sebastian

Julian Gonggrijp

unread,
Mar 12, 2011, 1:02:51 PM3/12/11
to boost...@lists.boost.org
Sebastian Redl wrote:

> On 12.03.2011, at 18:26, Kulti wrote:
>
>> It's possible to get following output from
>> boost::property_tree::json_parser::write_json ?
>>
>> {
>> "id": "id",
>> "method": "test_method",
>> "params": []
>> }
>>
>> I know how to create array with some items, but when I try to create
>> empty array, I've get "params": ""
>
> Sorry, that's not possible.

If you just need JSON, you might want to have a look at JsonCpp:
http://jsoncpp.sourceforge.net/

-Julian

Kulti

unread,
Mar 12, 2011, 1:34:47 PM3/12/11
to boost...@lists.boost.org

Thanks to all. I know about alternatives, but I'm use other parts of
boost and don't wanna have more dependencies. So I did some hack in my
case.

Reply all
Reply to author
Forward
0 new messages