Ticket #9496: boost_property_tree_json.patch

File boost_property_tree_json.patch, 2.2 KB (added by Egbert van der Wal <ewal@…>, 9 years ago)

Patch on SVN Trunk to improve the JSON output of the boost::property_tree::write_json function

  • boost/property_tree/detail/json_parser_write.hpp

     
    7575        if (indent > 0 && pt.empty())
    7676        {
    7777            // Write value
    78             Str data = create_escapes(pt.template get_value<Str>());
    79             stream << Ch('"') << data << Ch('"');
    80 
     78            Str str_data = create_escapes(pt.template get_value<Str>());
     79            bool written = false;
     80            if (!written)
     81            {
     82                if (str_data == "true" || str_data == "false")
     83                {
     84                    stream << str_data;
     85                    written = true;
     86                }
     87            }
     88            if (!written)
     89            {
     90                try
     91                {
     92                    int data = pt.template get_value<int>();
     93                    stream << data;
     94                    written = true;
     95                }
     96                catch (...)
     97                {}
     98            }
     99            if (!written)
     100            {
     101                try
     102                {
     103                    double data = pt.template get_value<double>();
     104                    stream << data;
     105                    written = true;
     106                }
     107                catch (...)
     108                {}
     109            }
     110            if (!written)
     111            {
     112                if (str_data == "null")
     113                    stream << "null";
     114                else
     115                    stream << Ch('"') << str_data << Ch('"');
     116            }
    81117        }
    82118        else if (indent > 0 && pt.count(Str()) == pt.size())
    83119        {
     
    159195    {
    160196        if (!verify_json(pt, 0))
    161197            BOOST_PROPERTY_TREE_THROW(json_parser_error("ptree contains data that cannot be represented in JSON format", filename, 0));
     198
     199        stream.setf(std::ios::fixed, std::ios::floatfield);
     200        stream.setf(std::ios::showpoint);
     201        stream.precision(std::numeric_limits<double>::digits10 + 1);
     202
    162203        write_json_helper(stream, pt, 0, pretty);
    163204        stream << std::endl;
    164205        if (!stream.good())