1 |
|
|
#ifndef ZSERIO_JSON_ENCODER_H_INC |
2 |
|
|
#define ZSERIO_JSON_ENCODER_H_INC |
3 |
|
|
|
4 |
|
|
#include <type_traits> |
5 |
|
|
|
6 |
|
|
#include "zserio/String.h" |
7 |
|
|
#include "zserio/Vector.h" |
8 |
|
|
#include "zserio/StringConvertUtil.h" |
9 |
|
|
#include "zserio/CppRuntimeException.h" |
10 |
|
|
#include "zserio/StringView.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 os Stream to use. |
25 |
|
|
*/ |
26 |
|
|
static void encodeNull(std::ostream& os); |
27 |
|
|
|
28 |
|
|
/** |
29 |
|
|
* Encodes JSON boolean value to the given stream. |
30 |
|
|
* |
31 |
|
|
* \param os Stream to use. |
32 |
|
|
* \param value Value to encode. |
33 |
|
|
*/ |
34 |
|
|
static void encodeBool(std::ostream& os, bool value); |
35 |
|
|
|
36 |
|
|
/** |
37 |
|
|
* Encodes JSON integral value to the given stream. |
38 |
|
|
* |
39 |
|
|
* \param os Stream to use. |
40 |
|
|
* \param value Value to encode. |
41 |
|
|
*/ |
42 |
|
|
template <typename T> |
43 |
|
|
static void encodeIntegral(std::ostream& os, T value); |
44 |
|
|
|
45 |
|
|
/** |
46 |
|
|
* Encodes JSON floating-point value to the given stream. |
47 |
|
|
* |
48 |
|
|
* \param os Stream to use. |
49 |
|
|
* \param value Value to encode. |
50 |
|
|
*/ |
51 |
|
|
static void encodeFloatingPoint(std::ostream& os, 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 os Stream to use. |
59 |
|
|
* \param value Value to encode. |
60 |
|
|
*/ |
61 |
|
|
static void encodeString(std::ostream& os, StringView value); |
62 |
|
|
}; |
63 |
|
|
|
64 |
|
|
template <typename T> |
65 |
|
52 |
void JsonEncoder::encodeIntegral(std::ostream& os, T value) |
66 |
|
|
{ |
67 |
|
|
using U = typename std::conditional<std::is_signed<T>::value, int64_t, uint64_t>::type; |
68 |
|
52 |
os << static_cast<U>(value); |
69 |
|
52 |
} |
70 |
|
|
|
71 |
|
|
} // namespace zserio |
72 |
|
|
|
73 |
|
|
#endif // ZSERIO_JSON_ENCODER_H_INC |