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
// protobuf-json: Conversions between protobuf and json.
// Copyright (c) 2014 Baidu, Inc.
// Date: 2014-10-29 18:30:33
#ifndef BRPC_JSON2PB_PB_TO_JSON_H
#define BRPC_JSON2PB_PB_TO_JSON_H
#include <string>
#include <google/protobuf/message.h>
#include <google/protobuf/io/zero_copy_stream.h> // ZeroCopyOutputStream
namespace json2pb {
enum EnumOption {
OUTPUT_ENUM_BY_NAME = 0, // Output enum by its name
OUTPUT_ENUM_BY_NUMBER = 1, // Output enum by its value
};
struct Pb2JsonOptions {
Pb2JsonOptions();
// Control how enum fields are output
// Default: OUTPUT_ENUM_BY_NAME
EnumOption enum_option;
// Use rapidjson::PrettyWriter to generate the json when this option is on.
// NOTE: currently PrettyWriter is not optimized yet thus the conversion
// functions may be slower when this option is turned on.
// Default: false
bool pretty_json;
// Convert "repeated { required string key = 1; required string value = 2; }"
// to a map object of json and vice versa when this option is turned on.
// Default: true
bool enable_protobuf_map;
// Encode the field of type bytes to string in json using base64
// encoding when this option is turned on.
// Default: false for baidu-internal, true otherwise.
bool bytes_to_base64;
// Convert the repeated field that has no entry
// to a empty array of json when this option is turned on.
// Default: false
bool jsonify_empty_array;
};
// Convert protobuf `messge' to `json' according to `options'.
// Returns true on success. `error' (if not NULL) will be set with error
// message on failure.
bool ProtoMessageToJson(const google::protobuf::Message& message,
std::string* json,
const Pb2JsonOptions& options,
std::string* error = NULL);
// send output to ZeroCopyOutputStream instead of std::string.
bool ProtoMessageToJson(const google::protobuf::Message& message,
google::protobuf::io::ZeroCopyOutputStream *json,
const Pb2JsonOptions& options,
std::string* error = NULL);
// Using default Pb2JsonOptions.
bool ProtoMessageToJson(const google::protobuf::Message& message,
std::string* json,
std::string* error = NULL);
bool ProtoMessageToJson(const google::protobuf::Message& message,
google::protobuf::io::ZeroCopyOutputStream* json,
std::string* error = NULL);
} // namespace json2pb
#endif // BRPC_JSON2PB_PB_TO_JSON_H