11#include " boost_json_serializer.h"
22
33#include " ../value_visitors.h"
4+ #include < iterator>
5+ #include < fmt/ostream.h>
46
5- // #include <boost::json/prettywriter.h>
7+ template <> struct fmt ::formatter <boost::json::value> : ostream_formatter {};
68
79namespace jinja2
810{
@@ -52,8 +54,7 @@ struct JsonInserter : visitors::BaseVisitor<boost::json::value>
5254
5355 boost::json::value operator ()(const nonstd::string_view& str) const
5456 {
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 ()));
5758 }
5859
5960 boost::json::value operator ()(const std::wstring& str) const
@@ -78,12 +79,10 @@ struct JsonInserter : visitors::BaseVisitor<boost::json::value>
7879
7980 boost::json::value operator ()(int64_t val) const { return boost::json::value (val); }
8081
81- // boost::json::Document::AllocatorType& m_allocator;
8282};
8383} // namespace
8484
8585DocumentWrapper::DocumentWrapper ()
86- // : m_document(std::make_shared<boost::json::Document>())
8786{
8887}
8988
@@ -98,111 +97,97 @@ ValueWrapper::ValueWrapper(boost::json::value&& value)
9897{
9998}
10099
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 )
102101{
103- std::string indentString_;
104- if (!indentString)
105- indentString = &indentString_;
106102 switch (jv.kind ())
107103 {
108104 case boost::json::kind::object:
109105 {
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 ();
113112 if (!obj.empty ())
114113 {
115114 auto it = obj.begin ();
116115 for (;;)
117116 {
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 );
120127 if (++it == obj.end ())
121128 break ;
122- os << " ,\n " ;
129+ fmt::format_to ( std::back_inserter (os), " {: <{}} " , " ," , (indent == 0 ) ? 0 : 2 ) ;
123130 }
124131 }
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 ;
129138 }
130139
131140 case boost::json::kind::array:
132141 {
133- // os << "[\n";
134- os << " [" ;
135- indentString->append (1 , ' ' );
142+ fmt::format_to (std::back_inserter (os), " [" );
136143 auto const & arr = jv.get_array ();
137144 if (!arr.empty ())
138145 {
139146 auto it = arr.begin ();
140147 for (;;)
141148 {
142- os << ((it == arr.begin ()) ? " " : *indentString);
143- PrettyPrint (os, *it, indent, indentString);
149+ PrettyPrint (os, *it, indent, level + 1 );
144150 if (++it == arr.end ())
145151 break ;
146- // os << ",\n";
147- os << " ," ;
152+ fmt::format_to (std::back_inserter (os), " {: <{}}" , " ," , (indent == 0 ) ? 0 : 2 );
148153 }
149154 }
150- // os << "\n";
151- indentString->resize (indentString->size () - indent);
152- os << *indentString << " ]" ;
155+ fmt::format_to (std::back_inserter (os), " ]" );
153156 break ;
154157 }
155158
156159 case boost::json::kind::string:
157160 {
158- os << boost::json::serialize (jv.get_string ());
161+ fmt::format_to ( std::back_inserter (os), " {} " , boost::json::serialize (jv.get_string () ));
159162 break ;
160163 }
161164
162165 case boost::json::kind::uint64:
163166 case boost::json::kind::int64:
164167 case boost::json::kind::double_:
165- os << jv;
166- break ;
167-
168+ {
169+ fmt::format_to (std::back_inserter (os), " {}" , jv);
170+ break ;
171+ }
168172 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 ());
173175 break ;
176+ }
174177
175178 case boost::json::kind::null:
176- os << " null" ;
179+ {
180+ fmt::format_to (std::back_inserter (os), " null" );
177181 break ;
178182 }
179-
180- // if (indentString->empty())
181- // os << "\n";
183+ }
182184}
183185
184186std::string ValueWrapper::AsString (const uint8_t indent) const
185187{
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);
206191}
207192
208193} // namespace boost_json_serializer
0 commit comments