aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yson/writer.h
blob: 40f5d7d5014dbd391bebe36490c6b9e8304a9af0 (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
#pragma once

#include "public.h"
#include "token.h"
#include "consumer.h"

#include <util/generic/noncopyable.h>

class IOutputStream;
class IZeroCopyInput;

namespace NYson {
    ////////////////////////////////////////////////////////////////////////////////

    class TYsonWriter
       : public TYsonConsumerBase,
          private TNonCopyable {
    public:
        class TState {
        private:
            int Depth;
            bool BeforeFirstItem;

            friend class TYsonWriter;
        };

    public:
        TYsonWriter(
            IOutputStream* stream,
            EYsonFormat format = EYsonFormat::Binary,
            EYsonType type = ::NYson::EYsonType::Node,
            bool enableRaw = false);

        void OnStringScalar(TStringBuf value) override;
        void OnInt64Scalar(i64 value) override;
        void OnUint64Scalar(ui64 value) override;
        void OnDoubleScalar(double value) override;
        void OnBooleanScalar(bool value) override;
        void OnEntity() override;

        void OnBeginList() override;
        void OnListItem() override;
        void OnEndList() override;

        void OnBeginMap() override;
        void OnKeyedItem(TStringBuf key) override;
        void OnEndMap() override;

        void OnBeginAttributes() override;
        void OnEndAttributes() override;

        void OnRaw(TStringBuf yson, EYsonType type = ::NYson::EYsonType::Node) override;

        TState State() const;
        void Reset(const TState& state);

    protected:
        IOutputStream* Stream;
        EYsonFormat Format;
        EYsonType Type;
        bool EnableRaw;

        int Depth;
        bool BeforeFirstItem;

        static const int IndentSize = 4;

        void WriteIndent();
        void WriteStringScalar(const TStringBuf& value);

        void BeginCollection(ETokenType beginToken);
        void CollectionItem(ETokenType separatorToken);
        void EndCollection(ETokenType endToken);

        bool IsTopLevelFragmentContext() const;
        void EndNode();
    };

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

    void ReformatYsonStream(
        IInputStream* input,
        IOutputStream* output,
        EYsonFormat format = EYsonFormat::Binary,
        EYsonType type = ::NYson::EYsonType::Node);

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

} // namespace NYson