From 2cebc31222ae93df25e54c7d04e6cefe7b971d6b Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Thu, 27 Aug 2020 13:18:52 -0700 Subject: [PATCH] to_string is in its own header --- doc/qbk/main.qbk | 2 +- include/boost/json.hpp | 1 + include/boost/json/impl/serializer.ipp | 46 ----------------- include/boost/json/impl/to_string.ipp | 67 +++++++++++++++++++++++++ include/boost/json/serializer.hpp | 42 ---------------- include/boost/json/src.hpp | 1 + include/boost/json/to_string.hpp | 69 ++++++++++++++++++++++++++ test/Jamfile | 1 + test/parser.cpp | 2 +- test/serializer.cpp | 1 + test/to_string.cpp | 47 ++++++++++++++++++ test/value_builder.cpp | 2 +- test/value_from.cpp | 2 +- test/value_ref.cpp | 2 +- 14 files changed, 192 insertions(+), 93 deletions(-) create mode 100644 include/boost/json/impl/to_string.ipp create mode 100644 include/boost/json/to_string.hpp create mode 100644 test/to_string.cpp diff --git a/doc/qbk/main.qbk b/doc/qbk/main.qbk index 65df22ae..f3fa44ff 100644 --- a/doc/qbk/main.qbk +++ b/doc/qbk/main.qbk @@ -89,7 +89,7 @@ [import ../../example/pretty.cpp] [import ../../example/validate.cpp] -[import ../../include/boost/json/impl/serializer.ipp] +[import ../../include/boost/json/impl/to_string.ipp] [import ../../test/snippets.cpp] [/-----------------------------------------------------------------------------] diff --git a/include/boost/json.hpp b/include/boost/json.hpp index 55c09b1f..6885c1fe 100644 --- a/include/boost/json.hpp +++ b/include/boost/json.hpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include diff --git a/include/boost/json/impl/serializer.ipp b/include/boost/json/impl/serializer.ipp index b6cb2d05..d24af8a2 100644 --- a/include/boost/json/impl/serializer.ipp +++ b/include/boost/json/impl/serializer.ipp @@ -753,52 +753,6 @@ read(char* dest, std::size_t size) return write_some(dest, size); } -//---------------------------------------------------------- - -string -to_string( - json::value const& jv) -{ - string s; - serializer sr(jv); - while(! sr.is_done()) - { - if(s.size() >= s.capacity()) - s.reserve(s.capacity() + 1); - s.grow(static_cast< - string::size_type>( - sr.read(s.data() + s.size(), - s.capacity() - s.size()))); - } - return s; -} - -//[example_operator_lt__lt_ -// Serialize a value into an output stream - -std::ostream& -operator<<( std::ostream& os, value const& jv ) -{ - // Create a serializer that is set to output our value. - serializer sr( jv ); - - // Loop until all output is produced. - while( ! sr.is_done() ) - { - // Use a local 4KB buffer. - char buf[4096]; - - // Try to fill up the local buffer. - auto const n = sr.read(buf, sizeof(buf)); - - // Write the valid portion of the buffer to the output stream. - os.write(buf, n); - } - - return os; -} -//] - } // json } // boost diff --git a/include/boost/json/impl/to_string.ipp b/include/boost/json/impl/to_string.ipp new file mode 100644 index 00000000..d43689cc --- /dev/null +++ b/include/boost/json/impl/to_string.ipp @@ -0,0 +1,67 @@ +// +// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com) +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// Official repository: https://github.com/cppalliance/json +// + +#ifndef BOOST_JSON_IMPL_TO_STRING_IPP +#define BOOST_JSON_IMPL_TO_STRING_IPP + +#include +#include +#include + +namespace boost { +namespace json { + +string +to_string( + json::value const& jv) +{ + string s; + serializer sr(jv); + while(! sr.is_done()) + { + if(s.size() >= s.capacity()) + s.reserve(s.capacity() + 1); + s.grow(static_cast< + string::size_type>( + sr.read(s.data() + s.size(), + s.capacity() - s.size()))); + } + return s; +} + +//[example_operator_lt__lt_ +// Serialize a value into an output stream + +std::ostream& +operator<<( std::ostream& os, value const& jv ) +{ + // Create a serializer that is set to output our value. + serializer sr( jv ); + + // Loop until all output is produced. + while( ! sr.is_done() ) + { + // Use a local 4KB buffer. + char buf[4096]; + + // Try to fill up the local buffer. + auto const n = sr.read( buf, sizeof(buf) ); + + // Write the valid portion of the buffer to the output stream. + os.write(buf, n); + } + + return os; +} +//] + +} // json +} // boost + +#endif diff --git a/include/boost/json/serializer.hpp b/include/boost/json/serializer.hpp index d7ef40b4..c14de003 100644 --- a/include/boost/json/serializer.hpp +++ b/include/boost/json/serializer.hpp @@ -15,7 +15,6 @@ #include #include #include -#include namespace boost { namespace json { @@ -160,47 +159,6 @@ public: read(char* dest, std::size_t size); }; -//---------------------------------------------------------- - -/** Return a string representing a serialized @ref value. - - This function serializes the specified value - and returns it as a @ref string. - - @par Exception Safety - - Strong guarantee. - Calls to `memory_resource::allocate` may throw. - - @param jv The value to serialize. -*/ -BOOST_JSON_DECL -string -to_string( - value const& jv); - -/** Serialize a @ref value to an output stream. - - This function serializes the specified value - into the output stream. - - @par Exception Safety - - Strong guarantee. - Calls to `memory_resource::allocate` may throw. - - @param os The output stream to serialize to. - - @param jv The value to serialize. - - @return `os`. -*/ -BOOST_JSON_DECL -std::ostream& -operator<<( - std::ostream& os, - value const& jv); - } // json } // boost diff --git a/include/boost/json/src.hpp b/include/boost/json/src.hpp index 29167ef7..c2bda5e9 100644 --- a/include/boost/json/src.hpp +++ b/include/boost/json/src.hpp @@ -36,6 +36,7 @@ in a translation unit of the program. #include #include #include +#include #include #include #include diff --git a/include/boost/json/to_string.hpp b/include/boost/json/to_string.hpp new file mode 100644 index 00000000..e3acf63d --- /dev/null +++ b/include/boost/json/to_string.hpp @@ -0,0 +1,69 @@ +// +// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com) +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// Official repository: https://github.com/cppalliance/json +// + +#ifndef BOOST_JSON_TO_STRING_HPP +#define BOOST_JSON_TO_STRING_HPP + +#include +#include +#include + +namespace boost { +namespace json { + +/** Return a string representing a serialized @ref value. + + This function serializes the specified value + and returns it as a @ref string using the + default memory resource. + + @par Exception Safety + + Strong guarantee. + Calls to `memory_resource::allocate` may throw. + + @return The serialized string + + @param jv The value to serialize +*/ +BOOST_JSON_DECL +string +to_string( + value const& jv); + +/** Serialize a @ref value to an output stream. + + This function serializes the specified value + into the output stream. + + @par Exception Safety + + Strong guarantee. + Calls to `memory_resource::allocate` may throw. + + @return `os`. + + @param os The output stream to serialize to. + + @param jv The value to serialize. +*/ +BOOST_JSON_DECL +std::ostream& +operator<<( + std::ostream& os, + value const& jv); + +} // json +} // boost + +#ifdef BOOST_JSON_HEADER_ONLY +#include +#endif + +#endif diff --git a/test/Jamfile b/test/Jamfile index 28b54bf6..4e97878d 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -44,6 +44,7 @@ local SOURCES = string.cpp string_view.cpp system_error.cpp + to_string.cpp value.cpp value_builder.cpp value_from.cpp diff --git a/test/parser.cpp b/test/parser.cpp index 1d71a673..87649e65 100644 --- a/test/parser.cpp +++ b/test/parser.cpp @@ -13,7 +13,7 @@ #include #include -#include +#include #include #include diff --git a/test/serializer.cpp b/test/serializer.cpp index 8618ea1e..e0820d12 100644 --- a/test/serializer.cpp +++ b/test/serializer.cpp @@ -11,6 +11,7 @@ #include #include +#include #include #include "parse-vectors.hpp" diff --git a/test/to_string.cpp b/test/to_string.cpp new file mode 100644 index 00000000..cd0d7887 --- /dev/null +++ b/test/to_string.cpp @@ -0,0 +1,47 @@ +// +// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com) +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// Official repository: https://github.com/cppalliance/json +// + +// Test that header file is self-contained. +#include + +#include + +#include "test_suite.hpp" + +namespace boost { +namespace json { + +class to_string_test +{ +public: + void + testToString() + { + BOOST_TEST( + to_string(parse("[1,2,3]")) == "[1,2,3]"); + } + + void + testOstream() + { + } + + void + run() + { + testToString(); + testOstream(); + } +}; + +TEST_SUITE(to_string_test, "boost.json.to_string"); + +} // json +} // boost + diff --git a/test/value_builder.cpp b/test/value_builder.cpp index 79c9bd33..12832d6c 100644 --- a/test/value_builder.cpp +++ b/test/value_builder.cpp @@ -11,7 +11,7 @@ #include #include -#include +#include #include "test_suite.hpp" diff --git a/test/value_from.cpp b/test/value_from.cpp index 1e5ac2d2..d31f0020 100644 --- a/test/value_from.cpp +++ b/test/value_from.cpp @@ -11,8 +11,8 @@ // Test that header file is self-contained. #include -#include #include // prevent intellisense bugs +#include #include "test_suite.hpp" diff --git a/test/value_ref.cpp b/test/value_ref.cpp index 79edb713..f5f2b090 100644 --- a/test/value_ref.cpp +++ b/test/value_ref.cpp @@ -11,7 +11,7 @@ #include #include -#include +#include #include "test_suite.hpp"