aboutsummaryrefslogtreecommitdiffstats
path: root/yt/yt/core/yson/pull_parser_deserialize.cpp
blob: 0ccc9cdc424ef905d057ca0d4b19189668ad0ebc (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include "pull_parser_deserialize.h"

#include <library/cpp/yt/misc/cast.h>

namespace NYT::NYson {

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

// integers
template <typename T>
void DeserializeInteger(T& value, TYsonPullParserCursor* cursor, TStringBuf typeName)
{
    switch ((*cursor)->GetType()) {
        case EYsonItemType::Int64Value:
            value = CheckedIntegralCast<T>((*cursor)->UncheckedAsInt64());
            cursor->Next();
            break;
        case EYsonItemType::Uint64Value:
            value = CheckedIntegralCast<T>((*cursor)->UncheckedAsUint64());
            cursor->Next();
            break;
        case EYsonItemType::BeginAttributes:
            SkipAttributes(cursor);
            DeserializeInteger(value, cursor, typeName);
            break;
        default:
            ThrowUnexpectedYsonTokenException(
                typeName,
                *cursor,
                {EYsonItemType::Int64Value, EYsonItemType::Uint64Value});
    }
}

#define DESERIALIZE(type) \
    void Deserialize(type& value, TYsonPullParserCursor* cursor) \
    { \
        DeserializeInteger(value, cursor, TStringBuf(#type)); \
    }

DESERIALIZE(signed char)
DESERIALIZE(short)
DESERIALIZE(int)
DESERIALIZE(long)
DESERIALIZE(long long)
DESERIALIZE(unsigned char)
DESERIALIZE(unsigned short)
DESERIALIZE(unsigned)
DESERIALIZE(unsigned long)
DESERIALIZE(unsigned long long)

#undef DESERIALIZE

// double
void Deserialize(double& value, TYsonPullParserCursor* cursor)
{
    switch ((*cursor)->GetType()) {
        case EYsonItemType::Int64Value:
            // Allow integers to be deserialized into doubles.
            value = (*cursor)->UncheckedAsInt64();
            cursor->Next();
            break;
        case EYsonItemType::Uint64Value:
            value = (*cursor)->UncheckedAsUint64();
            cursor->Next();
            break;
        case EYsonItemType::DoubleValue:
            value = (*cursor)->UncheckedAsDouble();
            cursor->Next();
            break;
        case EYsonItemType::BeginAttributes:
            SkipAttributes(cursor);
            Deserialize(value, cursor);
            break;
        default:
            ThrowUnexpectedYsonTokenException(
                "double",
                *cursor,
                {EYsonItemType::Int64Value, EYsonItemType::Uint64Value, EYsonItemType::DoubleValue});
    }
}

// TString.
void Deserialize(TString& value, TYsonPullParserCursor* cursor)
{
    MaybeSkipAttributes(cursor);
    EnsureYsonToken("string", *cursor, EYsonItemType::StringValue);
    value = (*cursor)->UncheckedAsString();
    cursor->Next();
}

// bool
void Deserialize(bool& value, TYsonPullParserCursor* cursor)
{
    switch ((*cursor)->GetType()) {
        case EYsonItemType::BooleanValue:
            value = (*cursor)->UncheckedAsBoolean();
            cursor->Next();
            break;
        case EYsonItemType::StringValue:
            value = ParseBool(TString((*cursor)->UncheckedAsString()));
            cursor->Next();
            break;
        case EYsonItemType::Int64Value: {
            auto intValue = (*cursor)->UncheckedAsInt64();
            if (intValue != 0 && intValue != 1) {
                THROW_ERROR_EXCEPTION("Expected 0 or 1 but found %v", intValue);
            }
            value = static_cast<bool>(intValue);
            cursor->Next();
            break;
        }
        case EYsonItemType::Uint64Value: {
            auto uintValue = (*cursor)->UncheckedAsUint64();
            if (uintValue != 0 && uintValue != 1) {
                THROW_ERROR_EXCEPTION("Expected 0 or 1 but found %v", uintValue);
            }
            value = static_cast<bool>(uintValue);
            cursor->Next();
            break;
        }
        case EYsonItemType::BeginAttributes:
            SkipAttributes(cursor);
            Deserialize(value, cursor);
            break;
        default:
            ThrowUnexpectedYsonTokenException(
                "bool",
                *cursor,
                {EYsonItemType::BooleanValue, EYsonItemType::StringValue});
    }
}

// char
void Deserialize(char& value, TYsonPullParserCursor* cursor)
{
    MaybeSkipAttributes(cursor);
    EnsureYsonToken("char", *cursor, EYsonItemType::StringValue);
    auto stringValue = (*cursor)->UncheckedAsString();
    if (stringValue.size() != 1) {
        THROW_ERROR_EXCEPTION("Expected string of length 1 but found of length %v", stringValue.size());
    }
    value = stringValue[0];
    cursor->Next();
}

// TDuration
void Deserialize(TDuration& value, TYsonPullParserCursor* cursor)
{
    switch ((*cursor)->GetType()) {
        case EYsonItemType::Int64Value:
            value = TDuration::MilliSeconds((*cursor)->UncheckedAsInt64());
            cursor->Next();
            break;

        case EYsonItemType::Uint64Value:
            value = TDuration::MilliSeconds((*cursor)->UncheckedAsUint64());
            cursor->Next();
            break;

        case EYsonItemType::StringValue:
            value = TDuration::Parse((*cursor)->UncheckedAsString());
            cursor->Next();
            break;

        case EYsonItemType::DoubleValue: {
            auto ms = (*cursor)->UncheckedAsDouble();
            if (ms < 0) {
                THROW_ERROR_EXCEPTION("Duration cannot be negative");
            }
            value = TDuration::MicroSeconds(static_cast<ui64>(ms * 1000.0));
            cursor->Next();
            break;
        }

        case EYsonItemType::BeginAttributes:
            SkipAttributes(cursor);
            Deserialize(value, cursor);
            break;


        default:
            ThrowUnexpectedYsonTokenException(
                "TDuration",
                *cursor,
                {EYsonItemType::Int64Value, EYsonItemType::Uint64Value});
    }
}

// TInstant.
void Deserialize(TInstant& value, TYsonPullParserCursor* cursor)
{
    switch ((*cursor)->GetType()) {
        case EYsonItemType::Int64Value:
            value = TInstant::MilliSeconds((*cursor)->UncheckedAsInt64());
            cursor->Next();
            break;

        case EYsonItemType::Uint64Value:
            value = TInstant::MilliSeconds((*cursor)->UncheckedAsUint64());
            cursor->Next();
            break;

        case EYsonItemType::StringValue:
            value = TInstant::ParseIso8601((*cursor)->UncheckedAsString());
            cursor->Next();
            break;

        case EYsonItemType::DoubleValue: {
            auto ms = (*cursor)->UncheckedAsDouble();
            if (ms < 0) {
                THROW_ERROR_EXCEPTION("Duration cannot be negative");
            }
            value = TInstant::MicroSeconds(static_cast<ui64>(ms * 1000.0));
            cursor->Next();
            break;
        }

        case EYsonItemType::BeginAttributes:
            SkipAttributes(cursor);
            Deserialize(value, cursor);
            break;

        default:
            ThrowUnexpectedYsonTokenException(
                "TInstant",
                *cursor,
                {EYsonItemType::Int64Value, EYsonItemType::Uint64Value, EYsonItemType::StringValue});
    }
}

// TGuid.
void Deserialize(TGuid& value, TYsonPullParserCursor* cursor)
{
    MaybeSkipAttributes(cursor);
    EnsureYsonToken("GUID", *cursor, EYsonItemType::StringValue);
    value = TGuid::FromString((*cursor)->UncheckedAsString());
    cursor->Next();
}

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

} // namespace NYT::NYson