Coverage Report

Created: 2024-10-29 13:13

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