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

#include "json_output.h"

#include <google/protobuf/descriptor.h>
#include <google/protobuf/descriptor.pb.h>
#include <google/protobuf/message.h>

#include <util/generic/strbuf.h>
#include <util/generic/string.h>

namespace NProtobufJson {
    struct TProto2JsonConfig;

    class TProto2JsonPrinter {
    public:
        TProto2JsonPrinter(const TProto2JsonConfig& config);
        virtual ~TProto2JsonPrinter();

        virtual void Print(const NProtoBuf::Message& proto, IJsonOutput& json, bool closeMap = true);

        virtual const TProto2JsonConfig& GetConfig() const {
            return Config;
        }

    protected:
        virtual TStringBuf MakeKey(const NProtoBuf::FieldDescriptor& field);

        virtual void PrintField(const NProtoBuf::Message& proto,
                                const NProtoBuf::FieldDescriptor& field,
                                IJsonOutput& json,
                                TStringBuf key = {});

        void PrintRepeatedField(const NProtoBuf::Message& proto,
                                const NProtoBuf::FieldDescriptor& field,
                                IJsonOutput& json,
                                TStringBuf key = {});

        void PrintSingleField(const NProtoBuf::Message& proto,
                              const NProtoBuf::FieldDescriptor& field,
                              IJsonOutput& json,
                              TStringBuf key = {});

        void PrintKeyValue(const NProtoBuf::Message& proto,
                           IJsonOutput& json);

        TString MakeKey(const NProtoBuf::Message& proto,
                        const NProtoBuf::FieldDescriptor& field);

        template <bool InMapContext>
        void PrintEnumValue(const TStringBuf& key,
                            const NProtoBuf::EnumValueDescriptor* value,
                            IJsonOutput& json);

        template <bool InMapContext>
        void PrintStringValue(const NProtoBuf::FieldDescriptor& field,
                              const TStringBuf& key, const TString& value,
                              IJsonOutput& json);

        template <class T>
        bool NeedStringifyNumber(T value) const;

    protected:
        const TProto2JsonConfig& Config;
        TString TmpBuf;
    };

}