1//
2// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
3//
4// Distributed under the Boost Software License, Version 1.0. (See accompanying
5// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6//
7// Official repository: https://github.com/boostorg/json
8//
9
10// Test that header file is self-contained.
11#include <boost/json/serialize.hpp>
12
13#include <boost/json/parse.hpp>
14#include <boost/json/static_resource.hpp>
15#include <limits>
16#include <sstream>
17
18#include "test_suite.hpp"
19
20namespace boost {
21namespace json {
22
23class serialize_test
24{
25public:
26 template<class T>
27 static
28 std::string
29 print(T const& t)
30 {
31 std::stringstream ss;
32 ss << t;
33 return ss.str();
34 }
35
36 void
37 testSerialize()
38 {
39 {
40 value const jv = { 1, 2, 3 };
41 BOOST_TEST(serialize(jv) == "[1,2,3]");
42 BOOST_TEST(print(jv) == "[1,2,3]");
43 }
44 {
45 array const arr = { 1, 2 ,3 };
46 BOOST_TEST(serialize(arr) == "[1,2,3]");
47 BOOST_TEST(print(arr) == "[1,2,3]");
48 }
49 {
50 object const obj = { {"k1",1}, {"k2",2} };
51 BOOST_TEST(serialize(obj) == "{\"k1\":1,\"k2\":2}");
52 BOOST_TEST(print(obj) == "{\"k1\":1,\"k2\":2}");
53 }
54 {
55 string const str = "123";
56 BOOST_TEST(serialize(str) == "\"123\"");
57 BOOST_TEST(print(str) == "\"123\"");
58 }
59 }
60
61 void
62 testSpecialNumbers()
63 {
64 using Lims = std::numeric_limits<double>;
65 value const jv = {
66 Lims::quiet_NaN(), Lims::infinity(), -Lims::infinity() };
67 BOOST_TEST( serialize(jv) == "[null,1e99999,-1e99999]" );
68 BOOST_TEST( print(jv) == "[null,1e99999,-1e99999]" );
69
70 serialize_options opts;
71 opts.allow_infinity_and_nan = true;
72 BOOST_TEST( serialize(jv, opts) == "[NaN,Infinity,-Infinity]" );
73
74 std::stringstream ss;
75 ss << opts << jv;
76 BOOST_TEST( ss.str() == "[NaN,Infinity,-Infinity]" );
77
78 opts.allow_infinity_and_nan = false;
79 ss.str(s: "");
80 ss << opts << jv;
81 BOOST_TEST( ss.str() == "[null,1e99999,-1e99999]" );
82 }
83
84 void
85 run()
86 {
87 testSerialize();
88 testSpecialNumbers();
89 }
90};
91
92TEST_SUITE(serialize_test, "boost.json.serialize");
93
94} // namespace json
95} // namespace boost
96

source code of boost/libs/json/test/serialize.cpp