aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yson/node/node_io.h
blob: 2db1318db44b3e0a8009f2857878c295cccdb185 (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
#pragma once

#include "node.h"
#include <library/cpp/yson/public.h>

namespace NJson {
    class TJsonValue;
} // namespace NJson

namespace NYT {

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

// Parse TNode from string in YSON format
TNode NodeFromYsonString(const TStringBuf input, ::NYson::EYsonType type = ::NYson::EYsonType::Node);

// Serialize TNode to string in one of YSON formats with random order of maps' keys (don't use in tests)
TString NodeToYsonString(const TNode& node, ::NYson::EYsonFormat format = ::NYson::EYsonFormat::Text);

// Same as the latter, but maps' keys are sorted lexicographically (to be used in tests)
TString NodeToCanonicalYsonString(const TNode& node, ::NYson::EYsonFormat format = ::NYson::EYsonFormat::Text);

// Parse TNode from stream in YSON format
TNode NodeFromYsonStream(IInputStream* input, ::NYson::EYsonType type = ::NYson::EYsonType::Node);

// Parse TNode from stream in YSON format.
// NB: This is substantially slower (1.5-2x using the benchmark from `./benchmark/saveload.cpp`) than using
//     the original `NodeFromYsonStream`.
// Stops reading from `input` as soon as some valid YSON was read, leaving the remainder of the stream unread.
// Used in TNode::Load to support cases of saveloading multiple values after the TNode from/to the same stream.
TNode NodeFromYsonStreamNonGreedy(IInputStream* input, ::NYson::EYsonType type = ::NYson::EYsonType::Node);

// Serialize TNode to stream in one of YSON formats with random order of maps' keys (don't use in tests)
void NodeToYsonStream(const TNode& node, IOutputStream* output, ::NYson::EYsonFormat format = ::NYson::EYsonFormat::Text);

// Same as the latter, but maps' keys are sorted lexicographically (to be used in tests)
void NodeToCanonicalYsonStream(const TNode& node, IOutputStream* output, ::NYson::EYsonFormat format = ::NYson::EYsonFormat::Text);

// Parse TNode from string in JSON format
TNode NodeFromJsonString(const TStringBuf input);
bool TryNodeFromJsonString(const TStringBuf input, TNode& dst);

// Parse TNode from string in JSON format using an iterative JSON parser.
// Iterative JSON parsers still use the stack, but allocate it on the heap (instead of using the system call stack).
// Needed to mitigate stack overflow with short stacks on deeply nested JSON strings
//  (e.g. 256kb of stack when parsing "[[[[[[...]]]]]]" crashes the whole binary).
TNode NodeFromJsonStringIterative(const TStringBuf input, ui64 maxDepth = 1024);

// Convert TJsonValue to TNode
TNode NodeFromJsonValue(const ::NJson::TJsonValue& input);

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

} // namespace NYT