1
1
#include " boost_json_serializer.h"
2
2
3
3
#include " ../value_visitors.h"
4
+ #include < iterator>
5
+ #include < fmt/ostream.h>
4
6
5
- // #include <boost::json/prettywriter.h>
7
+ template <> struct fmt ::formatter <boost::json::value> : ostream_formatter {};
6
8
7
9
namespace jinja2
8
10
{
@@ -52,8 +54,7 @@ struct JsonInserter : visitors::BaseVisitor<boost::json::value>
52
54
53
55
boost::json::value operator ()(const nonstd::string_view& str) const
54
56
{
55
- return boost::json::value (boost::json::string (str));
56
- // str.data(), static_cast<std::size_t>(str.size()));
57
+ return boost::json::value (boost::json::string (str.data (), str.size ()));
57
58
}
58
59
59
60
boost::json::value operator ()(const std::wstring& str) const
@@ -78,12 +79,10 @@ struct JsonInserter : visitors::BaseVisitor<boost::json::value>
78
79
79
80
boost::json::value operator ()(int64_t val) const { return boost::json::value (val); }
80
81
81
- // boost::json::Document::AllocatorType& m_allocator;
82
82
};
83
83
} // namespace
84
84
85
85
DocumentWrapper::DocumentWrapper ()
86
- // : m_document(std::make_shared<boost::json::Document>())
87
86
{
88
87
}
89
88
@@ -98,111 +97,97 @@ ValueWrapper::ValueWrapper(boost::json::value&& value)
98
97
{
99
98
}
100
99
101
- void PrettyPrint (std::ostream & os, const boost::json::value& jv, uint8_t indent = 4 , std::string* indentString = nullptr )
100
+ void PrettyPrint (fmt::basic_memory_buffer< char > & os, const boost::json::value& jv, uint8_t indent = 4 , int level = 0 )
102
101
{
103
- std::string indentString_;
104
- if (!indentString)
105
- indentString = &indentString_;
106
102
switch (jv.kind ())
107
103
{
108
104
case boost::json::kind::object:
109
105
{
110
- os << " {\n " ;
111
- indentString->append (indent, ' ' );
112
- auto const & obj = jv.get_object ();
106
+ fmt::format_to (std::back_inserter (os), " {}" , ' {' );
107
+ if (indent != 0 )
108
+ {
109
+ fmt::format_to (std::back_inserter (os), " {}" , " \n " );
110
+ }
111
+ const auto & obj = jv.get_object ();
113
112
if (!obj.empty ())
114
113
{
115
114
auto it = obj.begin ();
116
115
for (;;)
117
116
{
118
- os << *indentString << boost::json::serialize (it->key ()) << " : " ;
119
- PrettyPrint (os, it->value (), indent, indentString);
117
+ auto key = boost::json::serialize (it->key ());
118
+ fmt::format_to (
119
+ std::back_inserter (os),
120
+ " {: >{}}{: <{}}" ,
121
+ key,
122
+ key.size () + indent * (level + 1 ),
123
+ " :" ,
124
+ (indent == 0 ) ? 0 : 2
125
+ );
126
+ PrettyPrint (os, it->value (), indent, level + 1 );
120
127
if (++it == obj.end ())
121
128
break ;
122
- os << " ,\n " ;
129
+ fmt::format_to ( std::back_inserter (os), " {: <{}} " , " ," , (indent == 0 ) ? 0 : 2 ) ;
123
130
}
124
131
}
125
- os << " \n " ;
126
- indentString->resize (indentString->size () - indent);
127
- os << *indentString << " }" ;
128
- break ;
132
+ if (indent != 0 )
133
+ {
134
+ fmt::format_to (std::back_inserter (os), " {}" , " \n " );
135
+ }
136
+ fmt::format_to (std::back_inserter (os), " {: >{}}" , " }" , (indent * level) + 1 );
137
+ break ;
129
138
}
130
139
131
140
case boost::json::kind::array:
132
141
{
133
- // os << "[\n";
134
- os << " [" ;
135
- indentString->append (1 , ' ' );
142
+ fmt::format_to (std::back_inserter (os), " [" );
136
143
auto const & arr = jv.get_array ();
137
144
if (!arr.empty ())
138
145
{
139
146
auto it = arr.begin ();
140
147
for (;;)
141
148
{
142
- os << ((it == arr.begin ()) ? " " : *indentString);
143
- PrettyPrint (os, *it, indent, indentString);
149
+ PrettyPrint (os, *it, indent, level + 1 );
144
150
if (++it == arr.end ())
145
151
break ;
146
- // os << ",\n";
147
- os << " ," ;
152
+ fmt::format_to (std::back_inserter (os), " {: <{}}" , " ," , (indent == 0 ) ? 0 : 2 );
148
153
}
149
154
}
150
- // os << "\n";
151
- indentString->resize (indentString->size () - indent);
152
- os << *indentString << " ]" ;
155
+ fmt::format_to (std::back_inserter (os), " ]" );
153
156
break ;
154
157
}
155
158
156
159
case boost::json::kind::string:
157
160
{
158
- os << boost::json::serialize (jv.get_string ());
161
+ fmt::format_to ( std::back_inserter (os), " {} " , boost::json::serialize (jv.get_string () ));
159
162
break ;
160
163
}
161
164
162
165
case boost::json::kind::uint64:
163
166
case boost::json::kind::int64:
164
167
case boost::json::kind::double_:
165
- os << jv;
166
- break ;
167
-
168
+ {
169
+ fmt::format_to (std::back_inserter (os), " {}" , jv);
170
+ break ;
171
+ }
168
172
case boost::json::kind::bool_:
169
- if (jv.get_bool ())
170
- os << " true" ;
171
- else
172
- os << " false" ;
173
+ {
174
+ fmt::format_to (std::back_inserter (os), " {}" , jv.get_bool ());
173
175
break ;
176
+ }
174
177
175
178
case boost::json::kind::null:
176
- os << " null" ;
179
+ {
180
+ fmt::format_to (std::back_inserter (os), " null" );
177
181
break ;
178
182
}
179
-
180
- // if (indentString->empty())
181
- // os << "\n";
183
+ }
182
184
}
183
185
184
186
std::string ValueWrapper::AsString (const uint8_t indent) const
185
187
{
186
- // using Writer = boost::json::Writer<boost::json::StringBuffer, boost::json::Document::EncodingType, boost::json::UTF8<>>;
187
- // using PrettyWriter = boost::json::PrettyWriter<boost::json::StringBuffer, boost::json::Document::EncodingType, boost::json::UTF8<>>;
188
- std::stringstream ss;
189
- PrettyPrint (ss, m_value, indent);
190
- return ss.str ();
191
- /* boost::json::StringBuffer buffer; */
192
- /* if (indent == 0) */
193
- /* { */
194
- /* Writer writer(buffer); */
195
- /* m_value.Accept(writer); */
196
- /* } */
197
- /* else */
198
- /* { */
199
- /* PrettyWriter writer(buffer); */
200
- /* writer.SetIndent(' ', indent); */
201
- /* writer.SetFormatOptions(boost::json::kind::FormatSingleLineArray); */
202
- /* m_value.Accept(writer); */
203
- /* } */
204
-
205
- /* return buffer.GetString(); */
188
+ fmt::memory_buffer out;
189
+ PrettyPrint (out, m_value, indent);
190
+ return fmt::to_string (out);
206
191
}
207
192
208
193
} // namespace boost_json_serializer
0 commit comments