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

#include "config.h"
#include "json_output.h"

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

#include <util/generic/fwd.h> 
#include <util/generic/vector.h>
#include <util/generic/yexception.h>
#include <util/stream/str.h>

#include <functional>

namespace NJson {
    class TJsonValue;
    class TJsonWriter;
}

class IOutputStream;
class TStringStream;

namespace NProtobufJson {
    void Proto2Json(const NProtoBuf::Message& proto, IJsonOutput& jsonOutput,
                    const TProto2JsonConfig& config = TProto2JsonConfig(), bool closeMap = true);

    void Proto2Json(const NProtoBuf::Message& proto, NJson::TJsonWriter& writer,
                    const TProto2JsonConfig& config = TProto2JsonConfig());

    /// @throw yexception
    void Proto2Json(const NProtoBuf::Message& proto, NJson::TJsonValue& json,
                    const TProto2JsonConfig& config = TProto2JsonConfig());

    /// @throw yexception
    void Proto2Json(const NProtoBuf::Message& proto, IOutputStream& out,
                    const TProto2JsonConfig& config);
    // Generated code shortcut
    template <class T>
    inline void Proto2Json(const T& proto, IOutputStream& out) {
        out << proto.AsJSON();
    }

    // TStringStream deserves a special overload as its operator TString() would cause ambiguity
    /// @throw yexception
    void Proto2Json(const NProtoBuf::Message& proto, TStringStream& out,
                    const TProto2JsonConfig& config);
    // Generated code shortcut
    template <class T>
    inline void Proto2Json(const T& proto, TStringStream& out) {
        out << proto.AsJSON();
    }

    /// @throw yexception
    void Proto2Json(const NProtoBuf::Message& proto, TString& str,
                    const TProto2JsonConfig& config);
    // Generated code shortcut
    template <class T>
    inline void Proto2Json(const T& proto, TString& str) {
        str.clear();
        TStringOutput out(str);
        out << proto.AsJSON();
    }

    /// @throw yexception
    TString Proto2Json(const NProtoBuf::Message& proto,
                       const TProto2JsonConfig& config);
    // Returns incorrect result if proto contains another NProtoBuf::Message
    // Generated code shortcut
    template <class T>
    inline TString Proto2Json(const T& proto) {
        TString result;
        Proto2Json(proto, result);
        return result;
    }

}