json_to_pb.h 1.65 KB
Newer Older
gejun's avatar
gejun committed
1
// protobuf-json: Conversions between protobuf and json.
gejun's avatar
gejun committed
2
// Copyright (c) 2014 Baidu, Inc.
gejun's avatar
gejun committed
3 4 5 6 7 8 9 10 11
// Date:  2014-10-29 18:30:33

#ifndef BRPC_JSON2PB_JSON_TO_PB_H
#define BRPC_JSON2PB_JSON_TO_PB_H

#include <google/protobuf/message.h>
#include <google/protobuf/io/zero_copy_stream.h>    // ZeroCopyInputStream

namespace json2pb {
zhujiashun's avatar
zhujiashun committed
12 13 14 15 16 17 18 19 20 21

struct Json2PbOptions {
    Json2PbOptions();

    // Decode string in json using base64 decoding if the type of
    // corresponding field is bytes when this option is turned on.
    // Default: false for baidu-interal, true otherwise.
    bool base64_to_bytes;
};

gejun's avatar
gejun committed
22 23 24 25 26
// Convert `json' to protobuf `message'.
// Returns true on success. `error' (if not NULL) will be set with error
// message on failure.
bool JsonToProtoMessage(const std::string& json,
                        google::protobuf::Message* message,
zhujiashun's avatar
zhujiashun committed
27
                        const Json2PbOptions& options,
gejun's avatar
gejun committed
28 29 30 31
                        std::string* error = NULL);

// send output to ZeroCopyOutputStream instead of std::string.
bool JsonToProtoMessage(google::protobuf::io::ZeroCopyInputStream *json,
zhujiashun's avatar
zhujiashun committed
32 33 34 35 36 37 38 39 40 41
                        google::protobuf::Message* message,
                        const Json2PbOptions& options,
                        std::string* error = NULL);

// Using default Json2PbOptions.
bool JsonToProtoMessage(const std::string& json,
                        google::protobuf::Message* message,
                        std::string* error = NULL);

bool JsonToProtoMessage(google::protobuf::io::ZeroCopyInputStream* stream,
gejun's avatar
gejun committed
42 43
                        google::protobuf::Message* message,
                        std::string* error = NULL);
zhujiashun's avatar
zhujiashun committed
44
} // namespace json2pb
gejun's avatar
gejun committed
45 46

#endif // BRPC_JSON2PB_JSON_TO_PB_H