3D Repo Bouncer  1.4
json_parser.h
1 // ----------------------------------------------------------------------------
2 // Copyright (C) 2002-2006 Marcin Kalicinski
3 //
4 // Distributed under the Boost Software License, Version 1.0.
5 // (See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //
8 // For more information, see www.boost.org
9 // ----------------------------------------------------------------------------
10 #ifndef BOOST_PROPERTY_TREE_JSON_PARSER_HPP_INCLUDED
11 #define BOOST_PROPERTY_TREE_JSON_PARSER_HPP_INCLUDED
12 
13 #include <boost/property_tree/ptree.hpp>
14 #if BOOST_VERSION < 105900
15 #include <boost/property_tree/detail/json_parser_read.hpp>
16 #include <boost/property_tree/detail/json_parser_error.hpp>
17 #elif BOOST_VERSION > 106000
18 #include <boost/property_tree/json_parser/detail/read.hpp>
19 #include <boost/property_tree/json_parser/error.hpp>
20 #else
21 #include <boost/property_tree/detail/json_parser/read.hpp>
22 #include <boost/property_tree/detail/json_parser_error.hpp>
23 #endif
24 #include "json_parser_write.h"
25 
26 #include <fstream>
27 #include <string>
28 #include <locale>
29 
30 namespace boost {
31  namespace property_tree {
32  namespace json_parser
33  {
48  template<class Ptree>
49  void read_json(std::basic_istream <
50  typename Ptree::key_type::value_type
51  > &stream,
52  Ptree &pt)
53  {
54  read_json_internal(stream, pt, std::string());
55  }
56 
72  template<class Ptree>
73  void read_json(const std::string &filename,
74  Ptree &pt,
75  const std::locale &loc = std::locale())
76  {
77  std::basic_ifstream<typename Ptree::key_type::value_type>
78  stream(filename.c_str());
79  if (!stream)
80  BOOST_PROPERTY_TREE_THROW(json_parser_error(
81  "cannot open file", filename, 0));
82  stream.imbue(loc);
83  read_json_internal(stream, pt, filename);
84  }
85 
100  template<class Ptree>
101  void write_json(std::basic_ostream <
102  typename Ptree::key_type::value_type
103  > &stream,
104  const Ptree &pt,
105  bool pretty = true)
106  {
107  write_json_internal(stream, pt, std::string(), pretty);
108  }
109 
124  template<class Ptree>
125  void write_json(const std::string &filename,
126  const Ptree &pt,
127  const std::locale &loc = std::locale(),
128  bool pretty = true)
129  {
130  std::basic_ofstream<typename Ptree::key_type::value_type>
131  stream(filename.c_str());
132  if (!stream)
133  BOOST_PROPERTY_TREE_THROW(json_parser_error(
134  "cannot open file", filename, 0));
135  stream.imbue(loc);
136  write_json_internal(stream, pt, filename, pretty);
137  }
138  }
139  }
140 }
141 
142 namespace boost {
143  namespace property_tree
144  {
145  using json_parser::read_json;
146  using json_parser::write_json;
147  using json_parser::json_parser_error;
148  }
149 }
150 
151 #endif
Definition: json_parser.h:30