Chris@16: // ---------------------------------------------------------------------------- Chris@16: // Copyright (C) 2002-2006 Marcin Kalicinski Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. Chris@16: // (See accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: // For more information, see www.boost.org Chris@16: // ---------------------------------------------------------------------------- Chris@16: #ifndef BOOST_PROPERTY_TREE_JSON_PARSER_HPP_INCLUDED Chris@16: #define BOOST_PROPERTY_TREE_JSON_PARSER_HPP_INCLUDED Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost { namespace property_tree { namespace json_parser Chris@16: { Chris@16: Chris@16: /** Chris@16: * Read JSON from a the given stream and translate it to a property tree. Chris@16: * @note Clears existing contents of property tree. In case of error the Chris@16: * property tree unmodified. Chris@16: * @note Items of JSON arrays are translated into ptree keys with empty Chris@16: * names. Members of objects are translated into named keys. Chris@16: * @note JSON data can be a string, a numeric value, or one of literals Chris@16: * "null", "true" and "false". During parse, any of the above is Chris@16: * copied verbatim into ptree data string. Chris@16: * @throw json_parser_error In case of error deserializing the property Chris@16: * tree. Chris@16: * @param stream Stream from which to read in the property tree. Chris@16: * @param[out] pt The property tree to populate. Chris@16: */ Chris@16: template Chris@16: void read_json(std::basic_istream< Chris@16: typename Ptree::key_type::value_type Chris@16: > &stream, Chris@16: Ptree &pt) Chris@16: { Chris@16: read_json_internal(stream, pt, std::string()); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Read JSON from a the given file and translate it to a property tree. Chris@16: * @note Clears existing contents of property tree. In case of error the Chris@16: * property tree unmodified. Chris@16: * @note Items of JSON arrays are translated into ptree keys with empty Chris@16: * names. Members of objects are translated into named keys. Chris@16: * @note JSON data can be a string, a numeric value, or one of literals Chris@16: * "null", "true" and "false". During parse, any of the above is Chris@16: * copied verbatim into ptree data string. Chris@16: * @throw json_parser_error In case of error deserializing the property Chris@16: * tree. Chris@16: * @param filename Name of file from which to read in the property tree. Chris@16: * @param[out] pt The property tree to populate. Chris@16: * @param loc The locale to use when reading in the file contents. Chris@16: */ Chris@16: template Chris@16: void read_json(const std::string &filename, Chris@16: Ptree &pt, Chris@16: const std::locale &loc = std::locale()) Chris@16: { Chris@16: std::basic_ifstream Chris@16: stream(filename.c_str()); Chris@16: if (!stream) Chris@16: BOOST_PROPERTY_TREE_THROW(json_parser_error( Chris@16: "cannot open file", filename, 0)); Chris@16: stream.imbue(loc); Chris@16: read_json_internal(stream, pt, filename); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Translates the property tree to JSON and writes it the given output Chris@16: * stream. Chris@16: * @note Any property tree key containing only unnamed subkeys will be Chris@16: * rendered as JSON arrays. Chris@16: * @pre @e pt cannot contain keys that have both subkeys and non-empty data. Chris@16: * @throw json_parser_error In case of error translating the property tree Chris@16: * to JSON or writing to the output stream. Chris@16: * @param stream The stream to which to write the JSON representation of the Chris@16: * property tree. Chris@16: * @param pt The property tree to tranlsate to JSON and output. Chris@16: * @param pretty Whether to pretty-print. Defaults to true for backward Chris@16: * compatibility. Chris@16: */ Chris@16: template Chris@16: void write_json(std::basic_ostream< Chris@16: typename Ptree::key_type::value_type Chris@16: > &stream, Chris@16: const Ptree &pt, Chris@16: bool pretty = true) Chris@16: { Chris@16: write_json_internal(stream, pt, std::string(), pretty); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Translates the property tree to JSON and writes it the given file. Chris@16: * @note Any property tree key containing only unnamed subkeys will be Chris@16: * rendered as JSON arrays. Chris@16: * @pre @e pt cannot contain keys that have both subkeys and non-empty data. Chris@16: * @throw json_parser_error In case of error translating the property tree Chris@16: * to JSON or writing to the file. Chris@16: * @param filename The name of the file to which to write the JSON Chris@16: * representation of the property tree. Chris@16: * @param pt The property tree to translate to JSON and output. Chris@16: * @param loc The locale to use when writing out to the output file. Chris@16: * @param pretty Whether to pretty-print. Defaults to true and last place Chris@16: * for backward compatibility. Chris@16: */ Chris@16: template Chris@16: void write_json(const std::string &filename, Chris@16: const Ptree &pt, Chris@16: const std::locale &loc = std::locale(), Chris@16: bool pretty = true) Chris@16: { Chris@16: std::basic_ofstream Chris@16: stream(filename.c_str()); Chris@16: if (!stream) Chris@16: BOOST_PROPERTY_TREE_THROW(json_parser_error( Chris@16: "cannot open file", filename, 0)); Chris@16: stream.imbue(loc); Chris@16: write_json_internal(stream, pt, filename, pretty); Chris@16: } Chris@16: Chris@16: } } } Chris@16: Chris@16: namespace boost { namespace property_tree Chris@16: { Chris@16: using json_parser::read_json; Chris@16: using json_parser::write_json; Chris@16: using json_parser::json_parser_error; Chris@16: } } Chris@16: Chris@16: #endif