summaryrefslogtreecommitdiff
path: root/lib/json_parser.cpp
blob: d66297f8dbd8373f9976383da0586ebca8ae46bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Copyright (C) by Josh Blum. See LICENSE.txt for licensing information.

//--------- begin bullshit --------------//
#include <boost/version.hpp>
//why the fuck does no OS ever patch boost when there is a bug
//https://svn.boost.org/trac/boost/ticket/6785
#if BOOST_VERSION == 104900
#include "boost_fail/json_parser_read_104900.hpp"
#endif
//--------- end bullshit --------------//

#include "gras_impl/debug.hpp"
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/regex.hpp>
#include <sstream>
#include <string>

boost::property_tree::ptree json_to_ptree(const std::string &s)
{
    std::stringstream ss(s);
    boost::property_tree::ptree pt;
    boost::property_tree::json_parser::read_json(ss, pt);
    return pt;
}

//http://stackoverflow.com/questions/13464383/boost-property-write-json-incorrect-behaviour
std::string ptree_to_json(const boost::property_tree::ptree &p)
{
    boost::regex exp("\"(null|true|false|[0-9]+(\\.[0-9]+)?)\"");
    std::stringstream ss;
    boost::property_tree::json_parser::write_json(ss, p);
    std::string rv = boost::regex_replace(ss.str(), exp, "$1");

    return rv;
}