Line | Count | Source |
1 | | #ifndef ZSERIO_JSON_ENCODER_H_INC |
2 | | #define ZSERIO_JSON_ENCODER_H_INC |
3 | | |
4 | | #include <type_traits> |
5 | | |
6 | | #include "zserio/CppRuntimeException.h" |
7 | | #include "zserio/String.h" |
8 | | #include "zserio/StringConvertUtil.h" |
9 | | #include "zserio/StringView.h" |
10 | | #include "zserio/Types.h" |
11 | | #include "zserio/Vector.h" |
12 | | |
13 | | namespace zserio |
14 | | { |
15 | | |
16 | | /** |
17 | | * Converts zserio values to Json string representation. |
18 | | */ |
19 | | class JsonEncoder |
20 | | { |
21 | | public: |
22 | | /** |
23 | | * Encodes JSON null value to the given stream. |
24 | | * |
25 | | * \param stream Stream to use. |
26 | | */ |
27 | | static void encodeNull(std::ostream& stream); |
28 | | |
29 | | /** |
30 | | * Encodes JSON boolean value to the given stream. |
31 | | * |
32 | | * \param stream Stream to use. |
33 | | * \param value Value to encode. |
34 | | */ |
35 | | static void encodeBool(std::ostream& stream, bool value); |
36 | | |
37 | | /** |
38 | | * Encodes JSON integral value to the given stream. |
39 | | * |
40 | | * \param stream Stream to use. |
41 | | * \param value Value to encode. |
42 | | */ |
43 | | template <typename T> |
44 | | static void encodeIntegral(std::ostream& stream, T value); |
45 | | |
46 | | /** |
47 | | * Encodes JSON floating-point value to the given stream. |
48 | | * |
49 | | * \param stream Stream to use. |
50 | | * \param value Value to encode. |
51 | | */ |
52 | | static void encodeFloatingPoint(std::ostream& stream, double value); |
53 | | |
54 | | /** |
55 | | * Encodes JSON string value to the given stream. |
56 | | * |
57 | | * Note that this method performs escaping necessary to get a proper JSON string. |
58 | | * |
59 | | * \param stream Stream to use. |
60 | | * \param value Value to encode. |
61 | | */ |
62 | | static void encodeString(std::ostream& stream, StringView value); |
63 | | }; |
64 | | |
65 | | template <typename T> |
66 | | void JsonEncoder::encodeIntegral(std::ostream& stream, T value) |
67 | 52 | { |
68 | 52 | using U = typename std::conditional<std::is_signed<T>::value, int64_t, uint64_t>::type; |
69 | 52 | stream << static_cast<U>(value); |
70 | 52 | } |
71 | | |
72 | | } // namespace zserio |
73 | | |
74 | | #endif // ZSERIO_JSON_ENCODER_H_INC |