|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef GOOGLE_PROTOBUF_UTIL_JSON_UTIL_H__
|
|
|
#define GOOGLE_PROTOBUF_UTIL_JSON_UTIL_H__
|
|
|
|
|
|
#include <google/protobuf/message.h>
|
|
|
#include <google/protobuf/util/type_resolver.h>
|
|
|
#include <google/protobuf/stubs/bytestream.h>
|
|
|
|
|
|
namespace google {
|
|
|
namespace protobuf {
|
|
|
namespace io {
|
|
|
class ZeroCopyInputStream;
|
|
|
class ZeroCopyOutputStream;
|
|
|
}
|
|
|
namespace util {
|
|
|
|
|
|
struct JsonParseOptions {
|
|
|
|
|
|
bool ignore_unknown_fields;
|
|
|
|
|
|
JsonParseOptions() : ignore_unknown_fields(false) {}
|
|
|
};
|
|
|
|
|
|
struct JsonPrintOptions {
|
|
|
|
|
|
|
|
|
bool add_whitespace;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool always_print_primitive_fields;
|
|
|
|
|
|
|
|
|
bool always_print_enums_as_ints;
|
|
|
|
|
|
bool preserve_proto_field_names;
|
|
|
|
|
|
JsonPrintOptions()
|
|
|
: add_whitespace(false),
|
|
|
always_print_primitive_fields(false),
|
|
|
always_print_enums_as_ints(false),
|
|
|
preserve_proto_field_names(false) {}
|
|
|
};
|
|
|
|
|
|
|
|
|
typedef JsonPrintOptions JsonOptions;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LIBPROTOBUF_EXPORT util::Status MessageToJsonString(const Message& message,
|
|
|
string* output,
|
|
|
const JsonOptions& options);
|
|
|
|
|
|
inline util::Status MessageToJsonString(const Message& message,
|
|
|
string* output) {
|
|
|
return MessageToJsonString(message, output, JsonOptions());
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LIBPROTOBUF_EXPORT util::Status JsonStringToMessage(const string& input,
|
|
|
Message* message,
|
|
|
const JsonParseOptions& options);
|
|
|
|
|
|
inline util::Status JsonStringToMessage(const string& input,
|
|
|
Message* message) {
|
|
|
return JsonStringToMessage(input, message, JsonParseOptions());
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LIBPROTOBUF_EXPORT util::Status BinaryToJsonStream(
|
|
|
TypeResolver* resolver,
|
|
|
const string& type_url,
|
|
|
io::ZeroCopyInputStream* binary_input,
|
|
|
io::ZeroCopyOutputStream* json_output,
|
|
|
const JsonPrintOptions& options);
|
|
|
|
|
|
inline util::Status BinaryToJsonStream(
|
|
|
TypeResolver* resolver, const string& type_url,
|
|
|
io::ZeroCopyInputStream* binary_input,
|
|
|
io::ZeroCopyOutputStream* json_output) {
|
|
|
return BinaryToJsonStream(resolver, type_url, binary_input, json_output,
|
|
|
JsonPrintOptions());
|
|
|
}
|
|
|
|
|
|
LIBPROTOBUF_EXPORT util::Status BinaryToJsonString(
|
|
|
TypeResolver* resolver,
|
|
|
const string& type_url,
|
|
|
const string& binary_input,
|
|
|
string* json_output,
|
|
|
const JsonPrintOptions& options);
|
|
|
|
|
|
inline util::Status BinaryToJsonString(TypeResolver* resolver,
|
|
|
const string& type_url,
|
|
|
const string& binary_input,
|
|
|
string* json_output) {
|
|
|
return BinaryToJsonString(resolver, type_url, binary_input, json_output,
|
|
|
JsonPrintOptions());
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LIBPROTOBUF_EXPORT util::Status JsonToBinaryStream(
|
|
|
TypeResolver* resolver,
|
|
|
const string& type_url,
|
|
|
io::ZeroCopyInputStream* json_input,
|
|
|
io::ZeroCopyOutputStream* binary_output,
|
|
|
const JsonParseOptions& options);
|
|
|
|
|
|
inline util::Status JsonToBinaryStream(
|
|
|
TypeResolver* resolver,
|
|
|
const string& type_url,
|
|
|
io::ZeroCopyInputStream* json_input,
|
|
|
io::ZeroCopyOutputStream* binary_output) {
|
|
|
return JsonToBinaryStream(resolver, type_url, json_input, binary_output,
|
|
|
JsonParseOptions());
|
|
|
}
|
|
|
|
|
|
LIBPROTOBUF_EXPORT util::Status JsonToBinaryString(
|
|
|
TypeResolver* resolver,
|
|
|
const string& type_url,
|
|
|
const string& json_input,
|
|
|
string* binary_output,
|
|
|
const JsonParseOptions& options);
|
|
|
|
|
|
inline util::Status JsonToBinaryString(
|
|
|
TypeResolver* resolver,
|
|
|
const string& type_url,
|
|
|
const string& json_input,
|
|
|
string* binary_output) {
|
|
|
return JsonToBinaryString(resolver, type_url, json_input, binary_output,
|
|
|
JsonParseOptions());
|
|
|
}
|
|
|
|
|
|
namespace internal {
|
|
|
|
|
|
class LIBPROTOBUF_EXPORT ZeroCopyStreamByteSink : public strings::ByteSink {
|
|
|
public:
|
|
|
explicit ZeroCopyStreamByteSink(io::ZeroCopyOutputStream* stream)
|
|
|
: stream_(stream), buffer_(NULL), buffer_size_(0) {}
|
|
|
~ZeroCopyStreamByteSink();
|
|
|
|
|
|
virtual void Append(const char* bytes, size_t len);
|
|
|
|
|
|
private:
|
|
|
io::ZeroCopyOutputStream* stream_;
|
|
|
void* buffer_;
|
|
|
int buffer_size_;
|
|
|
|
|
|
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ZeroCopyStreamByteSink);
|
|
|
};
|
|
|
}
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
}
|
|
|
#endif
|
|
|
|