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_JSON_TO_PB_H |
21 | #define BRPC_JSON2PB_JSON_TO_PB_H |
22 | |
23 | #include <google/protobuf/message.h> |
24 | #include <google/protobuf/io/zero_copy_stream.h> // ZeroCopyInputStream |
25 | |
26 | namespace json2pb { |
27 | |
28 | struct Json2PbOptions { |
29 | Json2PbOptions(); |
30 | |
31 | // Decode string in json using base64 decoding if the type of |
32 | // corresponding field is bytes when this option is turned on. |
33 | // Default: false for baidu-interal, true otherwise. |
34 | bool base64_to_bytes; |
35 | }; |
36 | |
37 | // Convert `json' to protobuf `message'. |
38 | // Returns true on success. `error' (if not NULL) will be set with error |
39 | // message on failure. |
40 | bool JsonToProtoMessage(const std::string& json, |
41 | google::protobuf::Message* message, |
42 | const Json2PbOptions& options, |
43 | std::string* error = NULL); |
44 | |
45 | // send output to ZeroCopyOutputStream instead of std::string. |
46 | bool JsonToProtoMessage(google::protobuf::io::ZeroCopyInputStream *json, |
47 | google::protobuf::Message* message, |
48 | const Json2PbOptions& options, |
49 | std::string* error = NULL); |
50 | |
51 | // Using default Json2PbOptions. |
52 | bool JsonToProtoMessage(const std::string& json, |
53 | google::protobuf::Message* message, |
54 | std::string* error = NULL); |
55 | |
56 | bool JsonToProtoMessage(google::protobuf::io::ZeroCopyInputStream* stream, |
57 | google::protobuf::Message* message, |
58 | std::string* error = NULL); |
59 | } // namespace json2pb |
60 | |
61 | #endif // BRPC_JSON2PB_JSON_TO_PB_H |
62 | |