blob: f23150ebe21ce919a8f48f1411c62562d9cf0be1 [file] [log] [blame]
Ed Tanous57fce802019-05-21 13:00:34 -07001#include <nlohmann/json.hpp>
2
3#include <algorithm>
4
5namespace json_html_util
6{
7
8static constexpr uint8_t utf8Accept = 0;
9static constexpr uint8_t utf8Reject = 1;
10
11inline uint8_t decode(uint8_t& state, uint32_t& codePoint,
12 const uint8_t byte) noexcept
13{
14 // clang-format off
15 static const std::array<std::uint8_t, 400> utf8d =
16 {
17 {
18 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 00..1F
19 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 20..3F
20 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 40..5F
21 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 60..7F
22 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, // 80..9F
23 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // A0..BF
24 8, 8, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // C0..DF
25 0xA, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x4, 0x3, 0x3, // E0..EF
26 0xB, 0x6, 0x6, 0x6, 0x5, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, // F0..FF
27 0x0, 0x1, 0x2, 0x3, 0x5, 0x8, 0x7, 0x1, 0x1, 0x1, 0x4, 0x6, 0x1, 0x1, 0x1, 0x1, // s0..s0
28 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, // s1..s2
29 1, 2, 1, 1, 1, 1, 1, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, // s3..s4
30 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 3, 1, 1, 1, 1, 1, 1, // s5..s6
31 1, 3, 1, 1, 1, 1, 1, 3, 1, 3, 1, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 // s7..s8
32 }
33 };
34 // clang-format on
35
36 if (state > 0x8)
37 {
38 return state;
39 }
40
41 const uint8_t type = utf8d[byte];
42
43 codePoint = (state != utf8Accept)
Ed Tanousdbb59d42022-01-25 11:09:55 -080044 ? (byte & 0x3fU) | (codePoint << 6)
Ed Tanous57fce802019-05-21 13:00:34 -070045 : static_cast<uint32_t>(0xff >> type) & (byte);
46
Ed Tanousdbb59d42022-01-25 11:09:55 -080047 state = utf8d[256U + state * 16U + type];
Ed Tanous57fce802019-05-21 13:00:34 -070048 return state;
49}
50
51inline void dumpEscaped(std::string& out, const std::string& str)
52{
53 std::array<char, 512> stringBuffer{{}};
54 uint32_t codePoint = 0;
55 uint8_t state = utf8Accept;
56 std::size_t bytes = 0; // number of bytes written to string_buffer
57
58 // number of bytes written at the point of the last valid byte
Ed Tanous2c70f802020-09-28 14:29:23 -070059 std::size_t bytesAfterLastAccept = 0;
Ed Tanous57fce802019-05-21 13:00:34 -070060 std::size_t undumpedChars = 0;
61
62 for (std::size_t i = 0; i < str.size(); ++i)
63 {
64 const uint8_t byte = static_cast<uint8_t>(str[i]);
65
66 switch (decode(state, codePoint, byte))
67 {
68 case utf8Accept: // decode found a new code point
69 {
70 switch (codePoint)
71 {
72 case 0x08: // backspace
73 {
74 stringBuffer[bytes++] = '\\';
75 stringBuffer[bytes++] = 'b';
76 break;
77 }
78
79 case 0x09: // horizontal tab
80 {
81 stringBuffer[bytes++] = '\\';
82 stringBuffer[bytes++] = 't';
83 break;
84 }
85
86 case 0x0A: // newline
87 {
88 stringBuffer[bytes++] = '\\';
89 stringBuffer[bytes++] = 'n';
90 break;
91 }
92
93 case 0x0C: // formfeed
94 {
95 stringBuffer[bytes++] = '\\';
96 stringBuffer[bytes++] = 'f';
97 break;
98 }
99
100 case 0x0D: // carriage return
101 {
102 stringBuffer[bytes++] = '\\';
103 stringBuffer[bytes++] = 'r';
104 break;
105 }
106
107 case 0x22: // quotation mark
108 {
109 stringBuffer[bytes++] = '&';
110 stringBuffer[bytes++] = 'q';
111 stringBuffer[bytes++] = 'u';
112 stringBuffer[bytes++] = 'o';
113 stringBuffer[bytes++] = 't';
114 stringBuffer[bytes++] = ';';
115 break;
116 }
117
118 case 0x27: // apostrophe
119 {
120 stringBuffer[bytes++] = '&';
121 stringBuffer[bytes++] = 'a';
122 stringBuffer[bytes++] = 'p';
123 stringBuffer[bytes++] = 'o';
124 stringBuffer[bytes++] = 's';
125 stringBuffer[bytes++] = ';';
126 break;
127 }
128
129 case 0x26: // ampersand
130 {
131 stringBuffer[bytes++] = '&';
132 stringBuffer[bytes++] = 'a';
133 stringBuffer[bytes++] = 'm';
134 stringBuffer[bytes++] = 'p';
135 stringBuffer[bytes++] = ';';
136 break;
137 }
138
139 case 0x3C: // less than
140 {
141 stringBuffer[bytes++] = '\\';
142 stringBuffer[bytes++] = 'l';
143 stringBuffer[bytes++] = 't';
144 stringBuffer[bytes++] = ';';
145 break;
146 }
147
148 case 0x3E: // greater than
149 {
150 stringBuffer[bytes++] = '\\';
151 stringBuffer[bytes++] = 'g';
152 stringBuffer[bytes++] = 't';
153 stringBuffer[bytes++] = ';';
154 break;
155 }
156
157 default:
158 {
159 // escape control characters (0x00..0x1F)
160 if ((codePoint <= 0x1F) or (codePoint >= 0x7F))
161 {
162 if (codePoint <= 0xFFFF)
163 {
Ed Tanous49d1eea2022-01-07 09:39:33 -0800164 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
Patrick Williams84ea4b12023-05-10 17:07:06 -0500165 std::snprintf(&stringBuffer[bytes], 7,
Ed Tanous49d1eea2022-01-07 09:39:33 -0800166 "\\u%04x",
167 static_cast<uint16_t>(codePoint));
Ed Tanous57fce802019-05-21 13:00:34 -0700168 bytes += 6;
169 }
170 else
171 {
Ed Tanous49d1eea2022-01-07 09:39:33 -0800172 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
173 std::snprintf(
Patrick Williams84ea4b12023-05-10 17:07:06 -0500174 &stringBuffer[bytes], 13, "\\u%04x\\u%04x",
Ed Tanous57fce802019-05-21 13:00:34 -0700175 static_cast<uint16_t>(0xD7C0 +
176 (codePoint >> 10)),
177 static_cast<uint16_t>(0xDC00 +
178 (codePoint & 0x3FF)));
179 bytes += 12;
180 }
181 }
182 else
183 {
184 // copy byte to buffer (all previous bytes
185 // been copied have in default case above)
186 stringBuffer[bytes++] = str[i];
187 }
188 break;
189 }
190 }
191
192 // write buffer and reset index; there must be 13 bytes
193 // left, as this is the maximal number of bytes to be
194 // written ("\uxxxx\uxxxx\0") for one code point
195 if (stringBuffer.size() - bytes < 13)
196 {
197 out.append(stringBuffer.data(), bytes);
198 bytes = 0;
199 }
200
201 // remember the byte position of this accept
Ed Tanous2c70f802020-09-28 14:29:23 -0700202 bytesAfterLastAccept = bytes;
Ed Tanous57fce802019-05-21 13:00:34 -0700203 undumpedChars = 0;
204 break;
205 }
206
207 case utf8Reject: // decode found invalid UTF-8 byte
208 {
209 // in case we saw this character the first time, we
210 // would like to read it again, because the byte
211 // may be OK for itself, but just not OK for the
212 // previous sequence
213 if (undumpedChars > 0)
214 {
215 --i;
216 }
217
218 // reset length buffer to the last accepted index;
219 // thus removing/ignoring the invalid characters
Ed Tanous2c70f802020-09-28 14:29:23 -0700220 bytes = bytesAfterLastAccept;
Ed Tanous57fce802019-05-21 13:00:34 -0700221
222 stringBuffer[bytes++] = '\\';
223 stringBuffer[bytes++] = 'u';
224 stringBuffer[bytes++] = 'f';
225 stringBuffer[bytes++] = 'f';
226 stringBuffer[bytes++] = 'f';
227 stringBuffer[bytes++] = 'd';
228
Ed Tanous2c70f802020-09-28 14:29:23 -0700229 bytesAfterLastAccept = bytes;
Ed Tanous57fce802019-05-21 13:00:34 -0700230
231 undumpedChars = 0;
232
233 // continue processing the string
234 state = utf8Accept;
235 break;
236
237 break;
238 }
239
240 default: // decode found yet incomplete multi-byte code point
241 {
242 ++undumpedChars;
243 break;
244 }
245 }
246 }
247
248 // we finished processing the string
249 if (state == utf8Accept)
250 {
251 // write buffer
252 if (bytes > 0)
253 {
254 out.append(stringBuffer.data(), bytes);
255 }
256 }
257 else
258 {
259 // write all accepted bytes
Ed Tanous2c70f802020-09-28 14:29:23 -0700260 out.append(stringBuffer.data(), bytesAfterLastAccept);
Ed Tanous57fce802019-05-21 13:00:34 -0700261 out += "\\ufffd";
262 }
263}
264
265inline unsigned int countDigits(uint64_t number) noexcept
266{
Ed Tanous2c70f802020-09-28 14:29:23 -0700267 unsigned int nDigits = 1;
Ed Tanous57fce802019-05-21 13:00:34 -0700268 for (;;)
269 {
270 if (number < 10)
271 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700272 return nDigits;
Ed Tanous57fce802019-05-21 13:00:34 -0700273 }
274 if (number < 100)
275 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700276 return nDigits + 1;
Ed Tanous57fce802019-05-21 13:00:34 -0700277 }
278 if (number < 1000)
279 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700280 return nDigits + 2;
Ed Tanous57fce802019-05-21 13:00:34 -0700281 }
282 if (number < 10000)
283 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700284 return nDigits + 3;
Ed Tanous57fce802019-05-21 13:00:34 -0700285 }
Ed Tanousdbb59d42022-01-25 11:09:55 -0800286 number = number / 10000U;
Ed Tanous2c70f802020-09-28 14:29:23 -0700287 nDigits += 4;
Ed Tanous57fce802019-05-21 13:00:34 -0700288 }
289}
290
291template <typename NumberType,
292 std::enable_if_t<std::is_same<NumberType, uint64_t>::value or
293 std::is_same<NumberType, int64_t>::value,
294 int> = 0>
295void dumpInteger(std::string& out, NumberType number)
296{
297 std::array<char, 64> numberbuffer{{}};
298
Ed Tanous2c70f802020-09-28 14:29:23 -0700299 static constexpr std::array<std::array<char, 2>, 100> digitsTo99{{
Ed Tanous57fce802019-05-21 13:00:34 -0700300 {'0', '0'}, {'0', '1'}, {'0', '2'}, {'0', '3'}, {'0', '4'}, {'0', '5'},
301 {'0', '6'}, {'0', '7'}, {'0', '8'}, {'0', '9'}, {'1', '0'}, {'1', '1'},
302 {'1', '2'}, {'1', '3'}, {'1', '4'}, {'1', '5'}, {'1', '6'}, {'1', '7'},
303 {'1', '8'}, {'1', '9'}, {'2', '0'}, {'2', '1'}, {'2', '2'}, {'2', '3'},
304 {'2', '4'}, {'2', '5'}, {'2', '6'}, {'2', '7'}, {'2', '8'}, {'2', '9'},
305 {'3', '0'}, {'3', '1'}, {'3', '2'}, {'3', '3'}, {'3', '4'}, {'3', '5'},
306 {'3', '6'}, {'3', '7'}, {'3', '8'}, {'3', '9'}, {'4', '0'}, {'4', '1'},
307 {'4', '2'}, {'4', '3'}, {'4', '4'}, {'4', '5'}, {'4', '6'}, {'4', '7'},
308 {'4', '8'}, {'4', '9'}, {'5', '0'}, {'5', '1'}, {'5', '2'}, {'5', '3'},
309 {'5', '4'}, {'5', '5'}, {'5', '6'}, {'5', '7'}, {'5', '8'}, {'5', '9'},
310 {'6', '0'}, {'6', '1'}, {'6', '2'}, {'6', '3'}, {'6', '4'}, {'6', '5'},
311 {'6', '6'}, {'6', '7'}, {'6', '8'}, {'6', '9'}, {'7', '0'}, {'7', '1'},
312 {'7', '2'}, {'7', '3'}, {'7', '4'}, {'7', '5'}, {'7', '6'}, {'7', '7'},
313 {'7', '8'}, {'7', '9'}, {'8', '0'}, {'8', '1'}, {'8', '2'}, {'8', '3'},
314 {'8', '4'}, {'8', '5'}, {'8', '6'}, {'8', '7'}, {'8', '8'}, {'8', '9'},
315 {'9', '0'}, {'9', '1'}, {'9', '2'}, {'9', '3'}, {'9', '4'}, {'9', '5'},
316 {'9', '6'}, {'9', '7'}, {'9', '8'}, {'9', '9'},
317 }};
318
319 // special case for "0"
320 if (number == 0)
321 {
322 out += '0';
323 return;
324 }
325
326 // use a pointer to fill the buffer
Patrick Williams84ea4b12023-05-10 17:07:06 -0500327 auto bufferPtr = numberbuffer.begin();
Ed Tanous57fce802019-05-21 13:00:34 -0700328
329 const bool isNegative = std::is_same<NumberType, int64_t>::value &&
330 !(number >= 0); // see issue #755
Ed Tanous543f4402022-01-06 13:12:53 -0800331 uint64_t absValue = 0;
Ed Tanous57fce802019-05-21 13:00:34 -0700332
Ed Tanous543f4402022-01-06 13:12:53 -0800333 unsigned int nChars = 0;
Ed Tanous57fce802019-05-21 13:00:34 -0700334
335 if (isNegative)
336 {
337 *bufferPtr = '-';
338 absValue = static_cast<uint64_t>(0 - number);
339
340 // account one more byte for the minus sign
341 nChars = 1 + countDigits(absValue);
342 }
343 else
344 {
345 absValue = static_cast<uint64_t>(number);
346 nChars = countDigits(absValue);
347 }
348
349 // spare 1 byte for '\0'
350 if (nChars >= numberbuffer.size() - 1)
351 {
352 return;
353 }
354
355 // jump to the end to generate the string from backward
356 // so we later avoid reversing the result
Patrick Williams84ea4b12023-05-10 17:07:06 -0500357 std::advance(bufferPtr, nChars);
Ed Tanous57fce802019-05-21 13:00:34 -0700358
359 // Fast int2ascii implementation inspired by "Fastware" talk by Andrei
360 // Alexandrescu See: https://www.youtube.com/watch?v=o4-CwDo2zpg
361 while (absValue >= 100)
362 {
363 const auto digitsIndex = static_cast<unsigned>((absValue % 100));
364 absValue /= 100;
Patrick Williams84ea4b12023-05-10 17:07:06 -0500365 *bufferPtr = digitsTo99[digitsIndex][1];
366 bufferPtr = std::prev(bufferPtr);
367 *bufferPtr = digitsTo99[digitsIndex][0];
368 bufferPtr = std::prev(bufferPtr);
Ed Tanous57fce802019-05-21 13:00:34 -0700369 }
370
371 if (absValue >= 10)
372 {
373 const auto digitsIndex = static_cast<unsigned>(absValue);
Patrick Williams84ea4b12023-05-10 17:07:06 -0500374 *bufferPtr = digitsTo99[digitsIndex][1];
375 bufferPtr = std::prev(bufferPtr);
376 *bufferPtr = digitsTo99[digitsIndex][0];
377 // assignment never used: bufferPtr = std::prev(bufferPtr);
Ed Tanous57fce802019-05-21 13:00:34 -0700378 }
379 else
380 {
Patrick Williams84ea4b12023-05-10 17:07:06 -0500381 *bufferPtr = static_cast<char>('0' + absValue);
382 // assignment never used: bufferPtr = std::prev(bufferPtr);
Ed Tanous57fce802019-05-21 13:00:34 -0700383 }
384
385 out.append(numberbuffer.data(), nChars);
386}
387
388inline void dumpfloat(std::string& out, double number,
389 std::true_type /*isIeeeSingleOrDouble*/)
390{
391 std::array<char, 64> numberbuffer{{}};
Ed Tanousca45aa32022-01-07 09:28:45 -0800392
Patrick Williams84ea4b12023-05-10 17:07:06 -0500393 ::nlohmann::detail::to_chars(numberbuffer.begin(), numberbuffer.end(),
394 number);
Ed Tanous57fce802019-05-21 13:00:34 -0700395
Patrick Williams84ea4b12023-05-10 17:07:06 -0500396 out += numberbuffer.data();
Ed Tanous57fce802019-05-21 13:00:34 -0700397}
398
399inline void dumpfloat(std::string& out, double number,
400 std::false_type /*isIeeeSingleOrDouble*/)
401{
402 std::array<char, 64> numberbuffer{{}};
403 // get number of digits for a float -> text -> float round-trip
404 static constexpr auto d = std::numeric_limits<double>::max_digits10;
405
406 // the actual conversion
Ed Tanous49d1eea2022-01-07 09:39:33 -0800407 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
Ed Tanous3c2a8d02021-09-22 10:07:25 -0700408 std::ptrdiff_t len = std::snprintf(numberbuffer.data(), numberbuffer.size(),
409 "%.*g", d, number);
Ed Tanous57fce802019-05-21 13:00:34 -0700410
411 // negative value indicates an error
412 if (len <= 0)
413 {
414 return;
415 }
416
417 // check if buffer was large enough
418 if (numberbuffer.size() < static_cast<std::size_t>(len))
419 {
420 return;
421 }
422
Patrick Williams84ea4b12023-05-10 17:07:06 -0500423 auto end = numberbuffer.begin();
424 std::advance(end, len);
425 end = std::remove(numberbuffer.begin(), end, ',');
Ed Tanous57fce802019-05-21 13:00:34 -0700426 std::fill(end, numberbuffer.end(), '\0');
427
Patrick Williams84ea4b12023-05-10 17:07:06 -0500428 if (std::distance(numberbuffer.begin(), end) > len)
Ed Tanous57fce802019-05-21 13:00:34 -0700429 {
430 return;
431 }
Patrick Williams84ea4b12023-05-10 17:07:06 -0500432 len = std::distance(numberbuffer.begin(), end);
Ed Tanous57fce802019-05-21 13:00:34 -0700433
434 out.append(numberbuffer.data(), static_cast<std::size_t>(len));
435
436 // determine if need to append ".0"
Patrick Williams84ea4b12023-05-10 17:07:06 -0500437 auto newEnd = numberbuffer.begin();
438 std::advance(newEnd, len + 1);
439
Ed Tanous57fce802019-05-21 13:00:34 -0700440 const bool valueIsIntLike =
Patrick Williams84ea4b12023-05-10 17:07:06 -0500441 std::none_of(numberbuffer.begin(), newEnd,
Ed Tanous57fce802019-05-21 13:00:34 -0700442 [](char c) { return (c == '.' or c == 'e'); });
443
444 if (valueIsIntLike)
445 {
446 out += ".0";
447 }
448}
449
450inline void dumpfloat(std::string& out, double number)
451{
452 // NaN / inf
453 if (!std::isfinite(number))
454 {
455 out += "null";
456 return;
457 }
458
459 // If float is an IEEE-754 single or double precision number,
460 // use the Grisu2 algorithm to produce short numbers which are
461 // guaranteed to round-trip, using strtof and strtod, resp.
462 //
463 // NB: The test below works if <long double> == <double>.
464 static constexpr bool isIeeeSingleOrDouble =
465 (std::numeric_limits<double>::is_iec559 and
466 std::numeric_limits<double>::digits == 24 and
467 std::numeric_limits<double>::max_exponent == 128) or
468 (std::numeric_limits<double>::is_iec559 and
469 std::numeric_limits<double>::digits == 53 and
470 std::numeric_limits<double>::max_exponent == 1024);
471
472 dumpfloat(out, number,
473 std::integral_constant<bool, isIeeeSingleOrDouble>());
474}
475
476inline void dump(std::string& out, const nlohmann::json& val)
477{
478 switch (val.type())
479 {
480 case nlohmann::json::value_t::object:
481 {
482 if (val.empty())
483 {
484 out += "{}";
485 return;
486 }
487
488 out += "{";
489
490 out += "<div class=tab>";
491 for (auto i = val.begin(); i != val.end();)
492 {
493 out += "&quot";
494 dumpEscaped(out, i.key());
495 out += "&quot: ";
496
497 bool inATag = false;
498 if (i.key() == "@odata.id" || i.key() == "@odata.context" ||
499 i.key() == "Members@odata.nextLink" || i.key() == "Uri")
500 {
501 inATag = true;
502 out += "<a href=\"";
503 dumpEscaped(out, i.value());
504 out += "\">";
505 }
506 dump(out, i.value());
507 if (inATag)
508 {
509 out += "</a>";
510 }
511 i++;
512 if (i != val.end())
513 {
514 out += ",";
515 }
516 out += "<br>";
517 }
518 out += "</div>";
519 out += '}';
520
521 return;
522 }
523
524 case nlohmann::json::value_t::array:
525 {
526 if (val.empty())
527 {
528 out += "[]";
529 return;
530 }
531
532 out += "[";
533
534 out += "<div class=tab>";
535
536 // first n-1 elements
537 for (auto i = val.cbegin(); i != val.cend() - 1; ++i)
538 {
539 dump(out, *i);
540 out += ",<br>";
541 }
542
543 // last element
544 dump(out, val.back());
545
546 out += "</div>";
547 out += ']';
548
549 return;
550 }
551
552 case nlohmann::json::value_t::string:
553 {
554 out += '\"';
555 const std::string* ptr = val.get_ptr<const std::string*>();
556 dumpEscaped(out, *ptr);
557 out += '\"';
558 return;
559 }
560
561 case nlohmann::json::value_t::boolean:
562 {
563 if (*(val.get_ptr<const bool*>()))
564 {
565 out += "true";
566 }
567 else
568 {
569 out += "false";
570 }
571 return;
572 }
573
574 case nlohmann::json::value_t::number_integer:
575 {
576 dumpInteger(out, *(val.get_ptr<const int64_t*>()));
577 return;
578 }
579
580 case nlohmann::json::value_t::number_unsigned:
581 {
582 dumpInteger(out, *(val.get_ptr<const uint64_t*>()));
583 return;
584 }
585
586 case nlohmann::json::value_t::number_float:
587 {
588 dumpfloat(out, *(val.get_ptr<const double*>()));
589 return;
590 }
591
592 case nlohmann::json::value_t::discarded:
593 {
594 out += "<discarded>";
595 return;
596 }
597
598 case nlohmann::json::value_t::null:
599 {
600 out += "null";
601 return;
602 }
Ed Tanous3e4c7792020-09-25 08:12:41 -0700603 case nlohmann::json::value_t::binary:
604 {
605 // Do nothing; Should never happen.
606 return;
607 }
Ed Tanous57fce802019-05-21 13:00:34 -0700608 }
609}
610
611inline void dumpHtml(std::string& out, const nlohmann::json& json)
612{
613 out += "<html>\n"
614 "<head>\n"
615 "<title>Redfish API</title>\n"
616 "<link href=\"/redfish.css\" rel=\"stylesheet\">\n"
617 "</head>\n"
618 "<body>\n"
619 "<div class=\"container\">\n"
620 "<img src=\"/DMTF_Redfish_logo_2017.svg\" alt=\"redfish\" "
621 "height=\"406px\" "
622 "width=\"576px\">\n"
623 "<div class=\"content\">\n";
624 dump(out, json);
625 out += "</div>\n"
626 "</div>\n"
627 "</body>\n"
628 "</html>\n";
629}
630
631} // namespace json_html_util