Skip to content

Commit 17fe19b

Browse files
committed
* fix c++14 build
* change default json binding to boost
1 parent 4eda81a commit 17fe19b

File tree

3 files changed

+63
-67
lines changed

3 files changed

+63
-67
lines changed

CMakeLists.txt

+7-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ set(JINJA2CPP_SUPPORTED_REGEX std boost)
1515
set(JINJA2CPP_USE_REGEX boost CACHE STRING "Use regex parser in lexer, boost works faster on most platforms")
1616
set_property(CACHE JINJA2CPP_USE_REGEX PROPERTY STRINGS ${JINJA2CPP_SUPPORTED_REGEX})
1717
set(JINJA2CPP_WITH_JSON_BINDINGS boost nlohmann rapid all none)
18-
set(JINJA2CPP_WITH_JSON_BINDINGS all CACHE STRING "Build with json support")
18+
set(JINJA2CPP_WITH_JSON_BINDINGS boost CACHE STRING "Build with json support(boost|rapid)")
1919
set_property(CACHE JINJA2CPP_WITH_JSON_BINDINGS PROPERTY STRINGS ${JINJA2CPP_WITH_JSON_BINDINGS})
2020
set (JINJA2CPP_DEPS_MODE "internal" CACHE STRING "Jinja2Cpp dependency management mode (internal | external | external-boost | conan-build). See documentation for details. 'interal' is default.")
2121
option(JINJA2CPP_BUILD_TESTS "Build Jinja2Cpp unit tests" ${JINJA2CPP_IS_MAIN_PROJECT})
@@ -212,11 +212,17 @@ endif ()
212212
if ("${JINJA2CPP_USE_REGEX}" STREQUAL "boost")
213213
set(_regex_define "-DJINJA2CPP_USE_REGEX_BOOST")
214214
endif()
215+
if ("${JINJA2CPP_WITH_JSON_BINDINGS}" STREQUAL "boost")
216+
set(_bindings_define "-DJINJA2CPP_WITH_JSON_BINDINGS_BOOST")
217+
elseif("${JINJA2CPP_WITH_JSON_BINDINGS}" STREQUAL "rapid")
218+
set(_bindings_define "-DJINJA2CPP_WITH_JSON_BINDINGS_RAPID")
219+
endif()
215220
target_compile_definitions(${LIB_TARGET_NAME}
216221
PUBLIC
217222
-DBOOST_SYSTEM_NO_DEPRECATED
218223
-DBOOST_ERROR_CODE_HEADER_ONLY
219224
${_regex_define}
225+
${_bindings_define}
220226
)
221227

222228
if (JINJA2CPP_BUILD_SHARED)

src/binding/boost_json_serializer.cpp

+46-61
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
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

79
namespace 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

8585
DocumentWrapper::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

184186
std::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

src/serialize_filters.cpp

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#include "binding/rapid_json_serializer.h"
2-
#include "binding/boost_json_serializer.h"
31
#include "filters.h"
42
#include "generic_adapters.h"
53
#include "out_stream.h"
@@ -15,6 +13,15 @@
1513
#include <sstream>
1614
#include <string>
1715

16+
#ifdef JINJA2CPP_WITH_JSON_BINDINGS_BOOST
17+
#include "binding/boost_json_serializer.h"
18+
using DocumentWrapper = jinja2::boost_json_serializer::DocumentWrapper;
19+
#else
20+
#include "binding/rapid_json_serializer.h"
21+
using DocumentWrapper = jinja2::rapidjson_serializer::DocumentWrapper;
22+
#endif
23+
24+
1825
using namespace std::string_literals;
1926

2027
namespace jinja2
@@ -142,9 +149,7 @@ InternalValue Serialize::Filter(const InternalValue& value, RenderContext& conte
142149
if (m_mode == JsonMode)
143150
{
144151
const auto indent = ConvertToInt(this->GetArgumentValue("indent", context));
145-
146-
jinja2::rapidjson_serializer::DocumentWrapper jsonDoc;
147-
//jinja2::boost_json_serializer::DocumentWrapper jsonDoc;
152+
DocumentWrapper jsonDoc;
148153
const auto jsonValue = jsonDoc.CreateValue(value);
149154
const auto jsonString = jsonValue.AsString(static_cast<uint8_t>(indent));
150155
const auto result = std::accumulate(jsonString.begin(), jsonString.end(), ""s, [](const auto &str, const auto &c)

0 commit comments

Comments
 (0)