2
0
mirror of https://github.com/boostorg/json.git synced 2026-01-19 16:22:20 +00:00
Files
json/test/serialize.cpp
Vinnie Falco 591e2dc5a5 serializer refactor (API Change):
fix #275

* serialize() replaces to_string()
* serialize.hpp replaces to_string.hpp
* serializer works on all types
* serialize, operator<< work on all types
* reset() takes pointer arguments
* resetting constructor removed
2020-09-07 10:36:26 -07:00

91 lines
2.2 KiB
C++

//
// 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/serialize.hpp>
#include <boost/json/parse.hpp>
#include <boost/json/static_resource.hpp>
#include <sstream>
#include "test_suite.hpp"
BOOST_JSON_NS_BEGIN
class serialize_test
{
public:
template<class T>
static
std::string
print(T const& t)
{
std::stringstream ss;
ss << t;
return ss.str();
}
void
testSerialize()
{
{
value const jv = { 1, 2, 3 };
BOOST_TEST(serialize(jv) == "[1,2,3]");
BOOST_TEST(print(jv) == "[1,2,3]");
unsigned char buf[100];
static_resource mr(buf);
BOOST_TEST(serialize(
jv, &mr).storage().get() == &mr);
}
{
array const arr = { 1, 2 ,3 };
BOOST_TEST(serialize(arr) == "[1,2,3]");
BOOST_TEST(print(arr) == "[1,2,3]");
unsigned char buf[100];
static_resource mr(buf);
BOOST_TEST(serialize(
arr, &mr).storage().get() == &mr);
}
{
object const obj = { {"k1",1}, {"k2",2} };
BOOST_TEST(serialize(obj) == "{\"k1\":1,\"k2\":2}");
BOOST_TEST(print(obj) == "{\"k1\":1,\"k2\":2}");
unsigned char buf[100];
static_resource mr(buf);
BOOST_TEST(serialize(
obj, &mr).storage().get() == &mr);
}
{
string const str = "123";
BOOST_TEST(serialize(str) == "\"123\"");
BOOST_TEST(print(str) == "\"123\"");
unsigned char buf[100];
static_resource mr(buf);
BOOST_TEST(serialize(
str, &mr).storage().get() == &mr);
}
}
void
testOstream()
{
}
void
run()
{
testSerialize();
testOstream();
}
};
TEST_SUITE(serialize_test, "boost.json.serialize");
BOOST_JSON_NS_END