src/zserio/JsonEncoder.cpp
Line | Count | Source |
1 | | #include <array> |
2 | | #include <cmath> |
3 | | #include <iomanip> |
4 | | #include <string> |
5 | | |
6 | | #include "zserio/JsonEncoder.h" |
7 | | |
8 | | namespace zserio |
9 | | { |
10 | | |
11 | | void JsonEncoder::encodeNull(std::ostream& stream) |
12 | 2 | { |
13 | 2 | stream << "null"; |
14 | 2 | } |
15 | | |
16 | | void JsonEncoder::encodeBool(std::ostream& stream, bool value) |
17 | 3 | { |
18 | 3 | stream << std::boolalpha << value << std::noboolalpha; |
19 | 3 | } |
20 | | |
21 | | void JsonEncoder::encodeFloatingPoint(std::ostream& stream, double value) |
22 | 13 | { |
23 | 13 | if (std::isnan(value)) |
24 | 1 | { |
25 | 1 | stream << "NaN"; |
26 | 1 | } |
27 | 12 | else if (std::isinf(value)) |
28 | 2 | { |
29 | 2 | if (value < 0.0) |
30 | 1 | { |
31 | 1 | stream << "-"; |
32 | 1 | } |
33 | 2 | stream << "Infinity"; |
34 | 2 | } |
35 | 10 | else |
36 | 10 | { |
37 | 10 | double intPart = 1e16; |
38 | 10 | const double fractPart = std::modf(value, &intPart); |
39 | | // trying to get closer to behavior of Python |
40 | 10 | if (fractPart == 0.0 && intPart > -1e165 && intPart < 1e164 ) |
41 | 3 | { |
42 | 3 | stream << std::fixed << std::setprecision(1) << value << std::defaultfloat; |
43 | 3 | } |
44 | 7 | else |
45 | 7 | { |
46 | 7 | stream << std::setprecision(15) << value << std::defaultfloat; |
47 | 7 | } |
48 | 10 | } |
49 | 13 | } |
50 | | |
51 | | void JsonEncoder::encodeString(std::ostream& stream, StringView value) |
52 | 159 | { |
53 | 159 | static const std::array<char, 17> HEX = {"0123456789abcdef"}; |
54 | | |
55 | 159 | (void)stream.put('"'); |
56 | 159 | for (char character : value) |
57 | 1.07k | { |
58 | 1.07k | if (character == '\\' || character == '"'1.06k ) |
59 | 4 | { |
60 | 4 | (void)stream.put('\\'); |
61 | 4 | (void)stream.put(character); |
62 | 4 | } |
63 | 1.06k | else if (character == '\b') |
64 | 1 | { |
65 | 1 | (void)stream.put('\\'); |
66 | 1 | (void)stream.put('b'); |
67 | 1 | } |
68 | 1.06k | else if (character == '\f') |
69 | 1 | { |
70 | 1 | (void)stream.put('\\'); |
71 | 1 | (void)stream.put('f'); |
72 | 1 | } |
73 | 1.06k | else if (character == '\n') |
74 | 2 | { |
75 | 2 | (void)stream.put('\\'); |
76 | 2 | (void)stream.put('n'); |
77 | 2 | } |
78 | 1.06k | else if (character == '\r') |
79 | 1 | { |
80 | 1 | (void)stream.put('\\'); |
81 | 1 | (void)stream.put('r'); |
82 | 1 | } |
83 | 1.06k | else if (character == '\t') |
84 | 2 | { |
85 | 2 | (void)stream.put('\\'); |
86 | 2 | (void)stream.put('t'); |
87 | 2 | } |
88 | 1.06k | else |
89 | 1.06k | { |
90 | 1.06k | const unsigned int characterInt = |
91 | 1.06k | static_cast<unsigned int>(std::char_traits<char>::to_int_type(character)); |
92 | 1.06k | if (characterInt <= 0x1F) |
93 | 1 | { |
94 | 1 | (void)stream.put('\\'); |
95 | 1 | (void)stream.put('u'); |
96 | 1 | (void)stream.put('0'); |
97 | 1 | (void)stream.put('0'); |
98 | 1 | (void)stream.put(HEX[(characterInt >> 4U) & 0xFU]); |
99 | 1 | (void)stream.put(HEX[characterInt & 0xFU]); |
100 | 1 | } |
101 | 1.05k | else |
102 | 1.05k | { |
103 | 1.05k | (void)stream.put(character); |
104 | 1.05k | } |
105 | 1.06k | } |
106 | 1.07k | } |
107 | 159 | (void)stream.put('"'); |
108 | 159 | } |
109 | | |
110 | | } // namespace zserio |