aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/protobuf/json/string_transform.h
blob: e4b296bc011c2099caf63cfbbd8d9614d04d6037 (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
#pragma once

#include <library/cpp/string_utils/relaxed_escaper/relaxed_escaper.h>
#include <util/generic/ptr.h>
#include <util/generic/refcount.h>

namespace NProtobufJson {
    class IStringTransform: public TSimpleRefCount<IStringTransform> {
    public:
        virtual ~IStringTransform() {
        }

        /// Some transforms have special meaning.
        /// For example, escape transforms cause generic JSON escaping to be turned off.
        enum Type {
            EscapeTransform = 0x1,
        };

        virtual int GetType() const = 0;

        /// This method is called for each string field in proto
        virtual void Transform(TString& str) const = 0;

        /// This method is called for each bytes field in proto
        virtual void TransformBytes(TString& str) const {
            // Default behaviour is to apply string transform
            return Transform(str);
        }
    };

    using TStringTransformPtr = TIntrusivePtr<IStringTransform>;

    template <bool quote, bool tounicode>
    class TEscapeJTransform: public IStringTransform {
    public:
        int GetType() const override {
            return EscapeTransform;
        }

        void Transform(TString& str) const override {
            TString newStr;
            NEscJ::EscapeJ<quote, tounicode>(str, newStr);
            str = newStr;
        }
    };

    class TCEscapeTransform: public IStringTransform {
    public:
        int GetType() const override {
            return EscapeTransform;
        }

        void Transform(TString& str) const override;
    };

    class TSafeUtf8CEscapeTransform: public IStringTransform {
    public:
        int GetType() const override {
            return EscapeTransform;
        }

        void Transform(TString& str) const override;
    };

    class TDoubleEscapeTransform: public IStringTransform {
    public:
        int GetType() const override {
            return EscapeTransform;
        }

        void Transform(TString& str) const override;
    };

    class TDoubleUnescapeTransform: public NProtobufJson::IStringTransform {
    public:
        int GetType() const override {
            return NProtobufJson::IStringTransform::EscapeTransform;
        }

        void Transform(TString& str) const override;

    private:
        TString Unescape(const TString& str) const;
    };

    class TBase64EncodeBytesTransform: public NProtobufJson::IStringTransform {
    public:
        int GetType() const override {
            return 0;
        }

        void Transform(TString&) const override {
            // Do not transform strings
        }

        void TransformBytes(TString &str) const override;
    };

    class TBase64DecodeBytesTransform: public NProtobufJson::IStringTransform {
    public:
        int GetType() const override {
            return 0;
        }

        void Transform(TString&) const override {
            // Do not transform strings
        }

        void TransformBytes(TString &str) const override;
    };
}