|
| 1 | +#pragma once |
| 2 | + |
| 3 | +// (C) Copyright Takayama Fumihiko 2024. |
| 4 | +// Distributed under the Boost Software License, Version 1.0. |
| 5 | +// (See https://www.boost.org/LICENSE_1_0.txt) |
| 6 | + |
| 7 | +#include <nlohmann/json.hpp> |
| 8 | +#include <sstream> |
| 9 | +#include <unordered_set> |
| 10 | + |
| 11 | +namespace pqrs { |
| 12 | +namespace json { |
| 13 | +namespace pqrs_formatter { |
| 14 | + |
| 15 | +// The custom JSON formatter follows these rules: |
| 16 | +// |
| 17 | +// - Arrays are kept on a single line. However, they are spread across multiple lines in the following cases: |
| 18 | +// - If the size is 2 or more and they contain arrays or objects as values. |
| 19 | +// - If they contain multi-line values. |
| 20 | +// - Objects with only one key and a single-line value are kept on a single line. |
| 21 | +// |
| 22 | +// ```json |
| 23 | +// { |
| 24 | +// "bool": true, |
| 25 | +// "double": 123.456, |
| 26 | +// "int": 42, |
| 27 | +// "force_multi_line_array": [ |
| 28 | +// "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.", |
| 29 | +// "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.", |
| 30 | +// "Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.", |
| 31 | +// "Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." |
| 32 | +// ], |
| 33 | +// "multi_line_array1": [ |
| 34 | +// [1, 2, 3, 4], |
| 35 | +// [5, 6, 7, 8] |
| 36 | +// ], |
| 37 | +// "multi_line_array2": [ |
| 38 | +// { "key": 1 }, |
| 39 | +// { "key": 2 } |
| 40 | +// ], |
| 41 | +// "multi_line_object1": { |
| 42 | +// "key1": 1, |
| 43 | +// "key2": 2 |
| 44 | +// }, |
| 45 | +// "multi_line_object2": { |
| 46 | +// "key": { |
| 47 | +// "key1": 1, |
| 48 | +// "key2": 2 |
| 49 | +// } |
| 50 | +// }, |
| 51 | +// "null": null, |
| 52 | +// "single_line_array1": [], |
| 53 | +// "single_line_array2": [1, 2, 3, 4], |
| 54 | +// "single_line_array3": [[1, 2, 3, 4]], |
| 55 | +// "single_line_array4": [{ "key": "value" }], |
| 56 | +// "single_line_object1": {}, |
| 57 | +// "single_line_object2": { "key": "value" }, |
| 58 | +// "single_line_object3": { "key": [1, 2, 3, 4] }, |
| 59 | +// "single_line_object4": { "key1": { "key2": { "key3": "value3" } } } |
| 60 | +// } |
| 61 | +// ``` |
| 62 | + |
| 63 | +struct options { |
| 64 | + int indent_size; |
| 65 | + std::unordered_set<std::string> force_multi_line_array_object_keys; |
| 66 | +}; |
| 67 | + |
| 68 | +namespace impl { |
| 69 | + |
| 70 | +template <typename T> |
| 71 | +bool multi_line(const T& json, |
| 72 | + const options& options, |
| 73 | + std::optional<std::string> parent_object_key) { |
| 74 | + if (json.is_object()) { |
| 75 | + if (json.size() == 0) { |
| 76 | + return false; |
| 77 | + } |
| 78 | + |
| 79 | + if (json.size() == 1) { |
| 80 | + return multi_line(json.begin().value(), |
| 81 | + options, |
| 82 | + json.begin().key()); |
| 83 | + } |
| 84 | + |
| 85 | + if (json.size() > 1) { |
| 86 | + return true; |
| 87 | + } |
| 88 | + |
| 89 | + } else if (json.is_array()) { |
| 90 | + if (auto key = parent_object_key) { |
| 91 | + if (options.force_multi_line_array_object_keys.contains(*key)) { |
| 92 | + return true; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + if (json.size() == 0) { |
| 97 | + return false; |
| 98 | + } |
| 99 | + |
| 100 | + if (json.size() == 1) { |
| 101 | + return multi_line(json[0], |
| 102 | + options, |
| 103 | + std::nullopt); |
| 104 | + } |
| 105 | + |
| 106 | + for (const auto& j : json) { |
| 107 | + if (j.is_object() || j.is_array()) { |
| 108 | + return true; |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + return false; |
| 114 | +} |
| 115 | + |
| 116 | +void indent(std::ostringstream& ss, |
| 117 | + const options& options, |
| 118 | + int indent_level) { |
| 119 | + for (int i = 0; i < options.indent_size * indent_level; ++i) { |
| 120 | + ss << ' '; |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +template <typename T> |
| 125 | +void format(std::ostringstream& ss, |
| 126 | + const T& json, |
| 127 | + const options& options, |
| 128 | + std::optional<std::string> parent_object_key, |
| 129 | + int indent_level) { |
| 130 | + if (json.is_object()) { |
| 131 | + if (!multi_line(json, options, parent_object_key)) { |
| 132 | + // |
| 133 | + // Single-line object |
| 134 | + // |
| 135 | + |
| 136 | + if (json.empty()) { |
| 137 | + ss << "{}"; |
| 138 | + |
| 139 | + } else { |
| 140 | + |
| 141 | + ss << "{ "; |
| 142 | + |
| 143 | + ss << std::quoted(json.begin().key()) << ": "; |
| 144 | + |
| 145 | + format(ss, |
| 146 | + json.begin().value(), |
| 147 | + options, |
| 148 | + json.begin().key(), |
| 149 | + indent_level + 1); |
| 150 | + |
| 151 | + ss << " }"; |
| 152 | + } |
| 153 | + |
| 154 | + } else { |
| 155 | + // |
| 156 | + // Multi-line object |
| 157 | + // |
| 158 | + |
| 159 | + ss << "{\n"; |
| 160 | + |
| 161 | + bool first = true; |
| 162 | + for (const auto& [k, v] : json.items()) { |
| 163 | + if (!first) { |
| 164 | + ss << ",\n"; |
| 165 | + } |
| 166 | + first = false; |
| 167 | + |
| 168 | + indent(ss, |
| 169 | + options, |
| 170 | + indent_level + 1); |
| 171 | + |
| 172 | + ss << std::quoted(k) << ": "; |
| 173 | + format(ss, |
| 174 | + v, |
| 175 | + options, |
| 176 | + k, |
| 177 | + indent_level + 1); |
| 178 | + } |
| 179 | + |
| 180 | + ss << '\n'; |
| 181 | + |
| 182 | + indent(ss, |
| 183 | + options, |
| 184 | + indent_level); |
| 185 | + |
| 186 | + ss << '}'; |
| 187 | + } |
| 188 | + } else if (json.is_array()) { |
| 189 | + if (!multi_line(json, options, parent_object_key)) { |
| 190 | + // |
| 191 | + // Single-line array |
| 192 | + // |
| 193 | + |
| 194 | + ss << '['; |
| 195 | + |
| 196 | + bool first = true; |
| 197 | + for (const auto& v : json) { |
| 198 | + if (!first) { |
| 199 | + ss << ", "; |
| 200 | + } |
| 201 | + first = false; |
| 202 | + |
| 203 | + format(ss, |
| 204 | + v, |
| 205 | + options, |
| 206 | + std::nullopt, |
| 207 | + indent_level + 1); |
| 208 | + } |
| 209 | + |
| 210 | + ss << ']'; |
| 211 | + |
| 212 | + } else { |
| 213 | + // |
| 214 | + // Multi-line array |
| 215 | + // |
| 216 | + |
| 217 | + ss << "[\n"; |
| 218 | + |
| 219 | + bool first = true; |
| 220 | + for (const auto& v : json) { |
| 221 | + if (!first) { |
| 222 | + ss << ",\n"; |
| 223 | + } |
| 224 | + first = false; |
| 225 | + |
| 226 | + indent(ss, |
| 227 | + options, |
| 228 | + indent_level + 1); |
| 229 | + |
| 230 | + format(ss, |
| 231 | + v, |
| 232 | + options, |
| 233 | + std::nullopt, |
| 234 | + indent_level + 1); |
| 235 | + } |
| 236 | + |
| 237 | + ss << '\n'; |
| 238 | + |
| 239 | + indent(ss, |
| 240 | + options, |
| 241 | + indent_level); |
| 242 | + |
| 243 | + ss << ']'; |
| 244 | + } |
| 245 | + } else { |
| 246 | + ss << json; |
| 247 | + } |
| 248 | +} |
| 249 | + |
| 250 | +} // namespace impl |
| 251 | + |
| 252 | +template <typename T> |
| 253 | +std::string format(const T& json, |
| 254 | + const options& options) { |
| 255 | + std::ostringstream ss; |
| 256 | + impl::format(ss, json, options, std::nullopt, 0); |
| 257 | + return ss.str(); |
| 258 | +} |
| 259 | + |
| 260 | +} // namespace pqrs_formatter |
| 261 | +} // namespace json |
| 262 | +} // namespace pqrs |
0 commit comments