1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements. See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership. The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License. You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied. See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18// protobuf-json: Conversions between protobuf and json.
19
20#ifndef BRPC_JSON2PB_PB_TO_JSON_H
21#define BRPC_JSON2PB_PB_TO_JSON_H
22
23#include <string>
24#include <google/protobuf/message.h>
25#include <google/protobuf/io/zero_copy_stream.h> // ZeroCopyOutputStream
26
27namespace json2pb {
28
29enum EnumOption {
30 OUTPUT_ENUM_BY_NAME = 0, // Output enum by its name
31 OUTPUT_ENUM_BY_NUMBER = 1, // Output enum by its value
32};
33
34struct Pb2JsonOptions {
35 Pb2JsonOptions();
36
37 // Control how enum fields are output
38 // Default: OUTPUT_ENUM_BY_NAME
39 EnumOption enum_option;
40
41 // Use rapidjson::PrettyWriter to generate the json when this option is on.
42 // NOTE: currently PrettyWriter is not optimized yet thus the conversion
43 // functions may be slower when this option is turned on.
44 // Default: false
45 bool pretty_json;
46
47 // Convert "repeated { required string key = 1; required string value = 2; }"
48 // to a map object of json and vice versa when this option is turned on.
49 // Default: true
50 bool enable_protobuf_map;
51
52 // Encode the field of type bytes to string in json using base64
53 // encoding when this option is turned on.
54 // Default: false for baidu-internal, true otherwise.
55 bool bytes_to_base64;
56
57 // Convert the repeated field that has no entry
58 // to a empty array of json when this option is turned on.
59 // Default: false
60 bool jsonify_empty_array;
61
62 // Whether to always print primitive fields. By default proto3 primitive
63 // fields with default values will be omitted in JSON output. For example, an
64 // int32 field set to 0 will be omitted. Set this flag to true will override
65 // the default behavior and print primitive fields regardless of their values.
66 bool always_print_primitive_fields;
67};
68
69// Convert protobuf `messge' to `json' according to `options'.
70// Returns true on success. `error' (if not NULL) will be set with error
71// message on failure.
72bool ProtoMessageToJson(const google::protobuf::Message& message,
73 std::string* json,
74 const Pb2JsonOptions& options,
75 std::string* error = NULL);
76// send output to ZeroCopyOutputStream instead of std::string.
77bool ProtoMessageToJson(const google::protobuf::Message& message,
78 google::protobuf::io::ZeroCopyOutputStream *json,
79 const Pb2JsonOptions& options,
80 std::string* error = NULL);
81
82// Using default Pb2JsonOptions.
83bool ProtoMessageToJson(const google::protobuf::Message& message,
84 std::string* json,
85 std::string* error = NULL);
86bool ProtoMessageToJson(const google::protobuf::Message& message,
87 google::protobuf::io::ZeroCopyOutputStream* json,
88 std::string* error = NULL);
89} // namespace json2pb
90
91#endif // BRPC_JSON2PB_PB_TO_JSON_H
92