to_string is in its own header

This commit is contained in:
Vinnie Falco 2020-08-27 13:18:52 -07:00
parent 2d98a58a68
commit 2cebc31222
14 changed files with 192 additions and 93 deletions

View File

@ -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]
[/-----------------------------------------------------------------------------]

View File

@ -24,6 +24,7 @@
#include <boost/json/storage_ptr.hpp>
#include <boost/json/string.hpp>
#include <boost/json/system_error.hpp>
#include <boost/json/to_string.hpp>
#include <boost/json/value.hpp>
#include <boost/json/value_from.hpp>
#include <boost/json/value_to.hpp>

View File

@ -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

View File

@ -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 <boost/json/to_string.hpp>
#include <boost/json/serializer.hpp>
#include <ostream>
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

View File

@ -15,7 +15,6 @@
#include <boost/json/detail/format.hpp>
#include <boost/json/detail/stack.hpp>
#include <boost/json/detail/stream.hpp>
#include <iosfwd>
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

View File

@ -36,6 +36,7 @@ in a translation unit of the program.
#include <boost/json/impl/parser.ipp>
#include <boost/json/impl/serializer.ipp>
#include <boost/json/impl/string.ipp>
#include <boost/json/impl/to_string.ipp>
#include <boost/json/impl/value.ipp>
#include <boost/json/impl/value_builder.ipp>
#include <boost/json/impl/value_ref.ipp>

View File

@ -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 <boost/json/detail/config.hpp>
#include <boost/json/value.hpp>
#include <iosfwd>
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 <boost/json/impl/to_string.ipp>
#endif
#endif

View File

@ -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

View File

@ -13,7 +13,7 @@
#include <boost/json/monotonic_resource.hpp>
#include <boost/json/parse.hpp>
#include <boost/json/serializer.hpp>
#include <boost/json/to_string.hpp>
#include <sstream>
#include <iostream>

View File

@ -11,6 +11,7 @@
#include <boost/json/serializer.hpp>
#include <boost/json/parse.hpp>
#include <boost/json/to_string.hpp>
#include <iostream>
#include "parse-vectors.hpp"

47
test/to_string.cpp Normal file
View File

@ -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 <boost/json/to_string.hpp>
#include <boost/json/parse.hpp>
#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

View File

@ -11,7 +11,7 @@
#include <boost/json/value_builder.hpp>
#include <boost/json/monotonic_resource.hpp>
#include <boost/json/serializer.hpp>
#include <boost/json/to_string.hpp>
#include "test_suite.hpp"

View File

@ -11,8 +11,8 @@
// Test that header file is self-contained.
#include <boost/json/value_from.hpp>
#include <boost/json/serializer.hpp>
#include <boost/json/value.hpp> // prevent intellisense bugs
#include <boost/json/to_string.hpp>
#include "test_suite.hpp"

View File

@ -11,7 +11,7 @@
#include <boost/json/value_ref.hpp>
#include <boost/json/value.hpp>
#include <boost/json/serializer.hpp>
#include <boost/json/to_string.hpp>
#include "test_suite.hpp"