|
| 1 | +#include "boost_json_serializer.h" |
| 2 | + |
| 3 | +#include "../value_visitors.h" |
| 4 | + |
| 5 | +// #include <boost::json/prettywriter.h> |
| 6 | + |
| 7 | +namespace jinja2 |
| 8 | +{ |
| 9 | +namespace boost_json_serializer |
| 10 | +{ |
| 11 | +namespace |
| 12 | +{ |
| 13 | +struct JsonInserter : visitors::BaseVisitor<boost::json::value> |
| 14 | +{ |
| 15 | + using BaseVisitor::operator(); |
| 16 | + |
| 17 | + explicit JsonInserter() {} |
| 18 | + |
| 19 | + boost::json::value operator()(const ListAdapter& list) const |
| 20 | + { |
| 21 | + boost::json::array listValue; //(boost::json::kind::array); |
| 22 | + |
| 23 | + for (auto& v : list) |
| 24 | + { |
| 25 | + listValue.push_back(Apply<JsonInserter>(v)); |
| 26 | + } |
| 27 | + return listValue; |
| 28 | + } |
| 29 | + |
| 30 | + boost::json::value operator()(const MapAdapter& map) const |
| 31 | + { |
| 32 | + boost::json::object mapNode; //(boost::json::kind::object); |
| 33 | + |
| 34 | + const auto& keys = map.GetKeys(); |
| 35 | + for (auto& k : keys) |
| 36 | + { |
| 37 | + mapNode.emplace(k.c_str(), Apply<JsonInserter>(map.GetValueByName(k))); |
| 38 | + } |
| 39 | + |
| 40 | + return mapNode; |
| 41 | + } |
| 42 | + |
| 43 | + boost::json::value operator()(const KeyValuePair& kwPair) const |
| 44 | + { |
| 45 | + boost::json::object pairNode; //(boost::json::kind::object); |
| 46 | + pairNode.emplace(kwPair.key.c_str(), Apply<JsonInserter>(kwPair.value)); |
| 47 | + |
| 48 | + return pairNode; |
| 49 | + } |
| 50 | + |
| 51 | + boost::json::value operator()(const std::string& str) const { return boost::json::value(str.c_str()); } |
| 52 | + |
| 53 | + boost::json::value operator()(const nonstd::string_view& str) const |
| 54 | + { |
| 55 | + return boost::json::value(boost::json::string(str)); |
| 56 | + // str.data(), static_cast<std::size_t>(str.size())); |
| 57 | + } |
| 58 | + |
| 59 | + boost::json::value operator()(const std::wstring& str) const |
| 60 | + { |
| 61 | + auto s = ConvertString<std::string>(str); |
| 62 | + return boost::json::value(s.c_str()); |
| 63 | + } |
| 64 | + |
| 65 | + boost::json::value operator()(const nonstd::wstring_view& str) const |
| 66 | + { |
| 67 | + auto s = ConvertString<std::string>(str); |
| 68 | + return boost::json::value(s.c_str()); |
| 69 | + } |
| 70 | + |
| 71 | + boost::json::value operator()(bool val) const { return boost::json::value(val); } |
| 72 | + |
| 73 | + boost::json::value operator()(EmptyValue) const { return boost::json::value(); } |
| 74 | + |
| 75 | + boost::json::value operator()(const Callable&) const { return boost::json::value("<callable>"); } |
| 76 | + |
| 77 | + boost::json::value operator()(double val) const { return boost::json::value(val); } |
| 78 | + |
| 79 | + boost::json::value operator()(int64_t val) const { return boost::json::value(val); } |
| 80 | + |
| 81 | + // boost::json::Document::AllocatorType& m_allocator; |
| 82 | +}; |
| 83 | +} // namespace |
| 84 | + |
| 85 | +DocumentWrapper::DocumentWrapper() |
| 86 | +// : m_document(std::make_shared<boost::json::Document>()) |
| 87 | +{ |
| 88 | +} |
| 89 | + |
| 90 | +ValueWrapper DocumentWrapper::CreateValue(const InternalValue& value) const |
| 91 | +{ |
| 92 | + auto v = Apply<JsonInserter>(value); |
| 93 | + return ValueWrapper(std::move(v)); |
| 94 | +} |
| 95 | + |
| 96 | +ValueWrapper::ValueWrapper(boost::json::value&& value) |
| 97 | + : m_value(std::move(value)) |
| 98 | +{ |
| 99 | +} |
| 100 | + |
| 101 | +void PrettyPrint(std::ostream& os, const boost::json::value& jv, uint8_t indent = 4, std::string* indentString = nullptr) |
| 102 | +{ |
| 103 | + std::string indentString_; |
| 104 | + if (!indentString) |
| 105 | + indentString = &indentString_; |
| 106 | + switch (jv.kind()) |
| 107 | + { |
| 108 | + case boost::json::kind::object: |
| 109 | + { |
| 110 | + os << "{\n"; |
| 111 | + indentString->append(indent, ' '); |
| 112 | + auto const& obj = jv.get_object(); |
| 113 | + if (!obj.empty()) |
| 114 | + { |
| 115 | + auto it = obj.begin(); |
| 116 | + for (;;) |
| 117 | + { |
| 118 | + os << *indentString << boost::json::serialize(it->key()) << " : "; |
| 119 | + PrettyPrint(os, it->value(), indent, indentString); |
| 120 | + if (++it == obj.end()) |
| 121 | + break; |
| 122 | + os << ",\n"; |
| 123 | + } |
| 124 | + } |
| 125 | + os << "\n"; |
| 126 | + indentString->resize(indentString->size() - indent); |
| 127 | + os << *indentString << "}"; |
| 128 | + break; |
| 129 | + } |
| 130 | + |
| 131 | + case boost::json::kind::array: |
| 132 | + { |
| 133 | + //os << "[\n"; |
| 134 | + os << "["; |
| 135 | + indentString->append(1, ' '); |
| 136 | + auto const& arr = jv.get_array(); |
| 137 | + if (!arr.empty()) |
| 138 | + { |
| 139 | + auto it = arr.begin(); |
| 140 | + for (;;) |
| 141 | + { |
| 142 | + os << ((it == arr.begin()) ? "" : *indentString); |
| 143 | + PrettyPrint(os, *it, indent, indentString); |
| 144 | + if (++it == arr.end()) |
| 145 | + break; |
| 146 | + //os << ",\n"; |
| 147 | + os << ","; |
| 148 | + } |
| 149 | + } |
| 150 | + //os << "\n"; |
| 151 | + indentString->resize(indentString->size() - indent); |
| 152 | + os << *indentString << "]"; |
| 153 | + break; |
| 154 | + } |
| 155 | + |
| 156 | + case boost::json::kind::string: |
| 157 | + { |
| 158 | + os << boost::json::serialize(jv.get_string()); |
| 159 | + break; |
| 160 | + } |
| 161 | + |
| 162 | + case boost::json::kind::uint64: |
| 163 | + case boost::json::kind::int64: |
| 164 | + case boost::json::kind::double_: |
| 165 | + os << jv; |
| 166 | + break; |
| 167 | + |
| 168 | + case boost::json::kind::bool_: |
| 169 | + if (jv.get_bool()) |
| 170 | + os << "true"; |
| 171 | + else |
| 172 | + os << "false"; |
| 173 | + break; |
| 174 | + |
| 175 | + case boost::json::kind::null: |
| 176 | + os << "null"; |
| 177 | + break; |
| 178 | + } |
| 179 | + |
| 180 | + //if (indentString->empty()) |
| 181 | + // os << "\n"; |
| 182 | +} |
| 183 | + |
| 184 | +std::string ValueWrapper::AsString(const uint8_t indent) const |
| 185 | +{ |
| 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(); */ |
| 206 | +} |
| 207 | + |
| 208 | +} // namespace boost_json_serializer |
| 209 | +} // namespace jinja2 |
0 commit comments