aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yson/node/serialize.cpp
blob: c59fc7b624db5b6b6bc1f900e2509d4030de0cdb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include "serialize.h"

#include "node_visitor.h"

#include <library/cpp/yson/consumer.h>

namespace NYT {

////////////////////////////////////////////////////////////////////////////////

void Serialize(const TString& value, NYson::IYsonConsumer* consumer)
{
    consumer->OnStringScalar(value);
}

void Serialize(const TStringBuf& value, NYson::IYsonConsumer* consumer)
{
    consumer->OnStringScalar(value);
}

void Serialize(const char* value, NYson::IYsonConsumer* consumer)
{
    consumer->OnStringScalar(value);
}

void Deserialize(TString& value, const TNode& node)
{
    value = node.AsString();
}

#define SERIALIZE_SIGNED(type) \
void Serialize(type value, NYson::IYsonConsumer* consumer) \
{ \
    consumer->OnInt64Scalar(static_cast<i64>(value)); \
}

#define SERIALIZE_UNSIGNED(type) \
void Serialize(type value, NYson::IYsonConsumer* consumer) \
{ \
    consumer->OnUint64Scalar(static_cast<ui64>(value)); \
}

SERIALIZE_SIGNED(signed char)
SERIALIZE_SIGNED(short)
SERIALIZE_SIGNED(int)
SERIALIZE_SIGNED(long)
SERIALIZE_SIGNED(long long)

SERIALIZE_UNSIGNED(unsigned char)
SERIALIZE_UNSIGNED(unsigned short)
SERIALIZE_UNSIGNED(unsigned int)
SERIALIZE_UNSIGNED(unsigned long)
SERIALIZE_UNSIGNED(unsigned long long)

#undef SERIALIZE_SIGNED
#undef SERIALIZE_UNSIGNED

void Deserialize(i64& value, const TNode& node)
{
    value = node.AsInt64();
}

void Deserialize(ui64& value, const TNode& node)
{
    value = node.AsUint64();
}

void Serialize(double value, NYson::IYsonConsumer* consumer)
{
    consumer->OnDoubleScalar(value);
}

void Deserialize(double& value, const TNode& node)
{
    value = node.AsDouble();
}

void Serialize(bool value, NYson::IYsonConsumer* consumer)
{
    consumer->OnBooleanScalar(value);
}

void Deserialize(bool& value, const TNode& node)
{
    value = node.AsBool();
}

void Serialize(const TNode& node, NYson::IYsonConsumer* consumer)
{
    TNodeVisitor visitor(consumer);
    visitor.Visit(node);
}

void Deserialize(TNode& value, const TNode& node)
{
    value = node;
}

////////////////////////////////////////////////////////////////////////////////

} // namespace NYT