aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yson/json/json_writer.h
blob: 8dca12fe0ef31bf42ee83e9c55d6a115cd74073f (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 <library/cpp/yson/public.h>
#include <library/cpp/yson/consumer.h>

#include <library/cpp/json/json_writer.h>

#include <util/generic/vector.h>

namespace NYT {
    //////////////////////////////////////////////////////////////////////////////// 

    enum EJsonFormat { 
        JF_TEXT, 
        JF_PRETTY 
    }; 

    enum EJsonAttributesMode { 
        JAM_NEVER, 
        JAM_ON_DEMAND, 
        JAM_ALWAYS 
    }; 

    enum ESerializedBoolFormat { 
        SBF_BOOLEAN, 
        SBF_STRING 
    }; 

    class TJsonWriter 
       : public ::NYson::TYsonConsumerBase {
    public: 
        TJsonWriter( 
            IOutputStream* output, 
            ::NYson::EYsonType type = ::NYson::EYsonType::Node,
            EJsonFormat format = JF_TEXT, 
            EJsonAttributesMode attributesMode = JAM_ON_DEMAND, 
            ESerializedBoolFormat booleanFormat = SBF_STRING); 

        TJsonWriter(
            IOutputStream* output,
            NJson::TJsonWriterConfig config,
            ::NYson::EYsonType type = ::NYson::EYsonType::Node,
            EJsonAttributesMode attributesMode = JAM_ON_DEMAND,
            ESerializedBoolFormat booleanFormat = SBF_STRING);

        void Flush(); 

        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; 

    private: 
        THolder<NJson::TJsonWriter> UnderlyingJsonWriter; 
        NJson::TJsonWriter* JsonWriter; 
        IOutputStream* Output; 
        ::NYson::EYsonType Type;
        EJsonAttributesMode AttributesMode; 
        ESerializedBoolFormat BooleanFormat; 

        void WriteStringScalar(const TStringBuf& value); 

        void EnterNode(); 
        void LeaveNode(); 
        bool IsWriteAllowed(); 

        TVector<bool> HasUnfoldedStructureStack; 
        int InAttributesBalance; 
        bool HasAttributes; 
        int Depth; 
    }; 

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

}