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
|
#include "node_io.h"
#include "node_builder.h"
#include "node_visitor.h"
#include <library/cpp/yson/json/json_writer.h>
#include <library/cpp/yson/parser.h>
#include <library/cpp/yson/writer.h>
#include <library/cpp/yson/json/yson2json_adapter.h>
#include <library/cpp/json/json_reader.h>
#include <library/cpp/json/json_value.h>
#include <util/stream/input.h>
#include <util/stream/output.h>
#include <util/stream/str.h>
#include <util/stream/mem.h>
namespace NYT {
static void WalkJsonTree(const NJson::TJsonValue& jsonValue, NJson::TJsonCallbacks* callbacks)
{
using namespace NJson;
switch (jsonValue.GetType()) {
case JSON_NULL:
callbacks->OnNull();
return;
case JSON_BOOLEAN:
callbacks->OnBoolean(jsonValue.GetBoolean());
return;
case JSON_INTEGER:
callbacks->OnInteger(jsonValue.GetInteger());
return;
case JSON_UINTEGER:
callbacks->OnUInteger(jsonValue.GetUInteger());
return;
case JSON_DOUBLE:
callbacks->OnDouble(jsonValue.GetDouble());
return;
case JSON_STRING:
callbacks->OnString(jsonValue.GetString());
return;
case JSON_MAP:
{
callbacks->OnOpenMap();
for (const auto& item : jsonValue.GetMap()) {
callbacks->OnMapKey(item.first);
WalkJsonTree(item.second, callbacks);
}
callbacks->OnCloseMap();
}
return;
case JSON_ARRAY:
{
callbacks->OnOpenArray();
for (const auto& item : jsonValue.GetArray()) {
WalkJsonTree(item, callbacks);
}
callbacks->OnCloseArray();
}
return;
case JSON_UNDEFINED:
ythrow yexception() << "cannot consume undefined json value";
return;
}
Y_UNREACHABLE();
}
static TNode CreateEmptyNodeByType(::NYson::EYsonType type)
{
TNode result;
switch (type) {
case ::NYson::EYsonType::ListFragment:
result = TNode::CreateList();
break;
case ::NYson::EYsonType::MapFragment:
result = TNode::CreateMap();
break;
default:
break;
}
return result;
}
TNode NodeFromYsonString(const TStringBuf input, ::NYson::EYsonType type)
{
TMemoryInput stream(input);
return NodeFromYsonStream(&stream, type);
}
TString NodeToYsonString(const TNode& node, NYson::EYsonFormat format)
{
TStringStream stream;
NodeToYsonStream(node, &stream, format);
return stream.Str();
}
TString NodeToCanonicalYsonString(const TNode& node, NYson::EYsonFormat format)
{
TStringStream stream;
NodeToCanonicalYsonStream(node, &stream, format);
return stream.Str();
}
TNode NodeFromYsonStream(IInputStream* input, ::NYson::EYsonType type)
{
TNode result = CreateEmptyNodeByType(type);
TNodeBuilder builder(&result);
::NYson::TYsonParser parser(&builder, input, type);
parser.Parse();
return result;
}
void NodeToYsonStream(const TNode& node, IOutputStream* output, NYson::EYsonFormat format)
{
::NYson::TYsonWriter writer(output, format);
TNodeVisitor visitor(&writer);
visitor.Visit(node);
}
void NodeToCanonicalYsonStream(const TNode& node, IOutputStream* output, NYson::EYsonFormat format)
{
::NYson::TYsonWriter writer(output, format);
TNodeVisitor visitor(&writer, /*sortMapKeys*/ true);
visitor.Visit(node);
}
bool TryNodeFromJsonString(const TStringBuf input, TNode& dst)
{
TMemoryInput stream(input);
TNodeBuilder builder(&dst);
TYson2JsonCallbacksAdapter callbacks(&builder, /*throwException*/ false);
NJson::TJsonReaderConfig config;
config.DontValidateUtf8 = true;
NJson::ReadJson(&stream, &config, &callbacks);
return !callbacks.GetHaveErrors();
}
TNode NodeFromJsonString(const TStringBuf input)
{
TMemoryInput stream(input);
TNode result;
TNodeBuilder builder(&result);
TYson2JsonCallbacksAdapter callbacks(&builder, /*throwException*/ true);
NJson::TJsonReaderConfig config;
config.DontValidateUtf8 = true;
NJson::ReadJson(&stream, &config, &callbacks);
return result;
}
TNode NodeFromJsonValue(const NJson::TJsonValue& input)
{
TNode result;
TNodeBuilder builder(&result);
TYson2JsonCallbacksAdapter callbacks(&builder, /*throwException*/ true);
WalkJsonTree(input, &callbacks);
return result;
}
////////////////////////////////////////////////////////////////////////////////
} // namespace NYT
|