Skip to content

Instantly share code, notes, and snippets.

@jupp0r
Created July 19, 2016 09:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jupp0r/20115ee7ae59368c9d2c7515d7800def to your computer and use it in GitHub Desktop.
Save jupp0r/20115ee7ae59368c9d2c7515d7800def to your computer and use it in GitHub Desktop.
#include <google/protobuf/util/json_util.h>
#include <iostream>
#include "test.pb.h"
int main(int argc, char** argv) {
auto t = test::Simple{};
t.set_testint(4);
t.set_teststring("foobar");
std::string result;
google::protobuf::util::MessageToJsonString(t, &result, google::protobuf::util::JsonPrintOptions());
std::cout << result << std::endl;
return 0;
}
// Generated by the protocol buffer compiler. DO NOT EDIT!
// source: test.proto
#define INTERNAL_SUPPRESS_PROTOBUF_FIELD_DEPRECATION
#include "test.pb.h"
#include <algorithm>
#include <google/protobuf/stubs/common.h>
#include <google/protobuf/stubs/port.h>
#include <google/protobuf/stubs/once.h>
#include <google/protobuf/io/coded_stream.h>
#include <google/protobuf/wire_format_lite_inl.h>
#include <google/protobuf/descriptor.h>
#include <google/protobuf/generated_message_reflection.h>
#include <google/protobuf/reflection_ops.h>
#include <google/protobuf/wire_format.h>
// @@protoc_insertion_point(includes)
namespace test {
namespace {
const ::google::protobuf::Descriptor* Simple_descriptor_ = NULL;
const ::google::protobuf::internal::GeneratedMessageReflection*
Simple_reflection_ = NULL;
} // namespace
void protobuf_AssignDesc_test_2eproto() {
protobuf_AddDesc_test_2eproto();
const ::google::protobuf::FileDescriptor* file =
::google::protobuf::DescriptorPool::generated_pool()->FindFileByName(
"test.proto");
GOOGLE_CHECK(file != NULL);
Simple_descriptor_ = file->message_type(0);
static const int Simple_offsets_[2] = {
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(Simple, testint_),
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(Simple, teststring_),
};
Simple_reflection_ =
::google::protobuf::internal::GeneratedMessageReflection::NewGeneratedMessageReflection(
Simple_descriptor_,
Simple::default_instance_,
Simple_offsets_,
-1,
-1,
-1,
sizeof(Simple),
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(Simple, _internal_metadata_),
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(Simple, _is_default_instance_));
}
namespace {
GOOGLE_PROTOBUF_DECLARE_ONCE(protobuf_AssignDescriptors_once_);
inline void protobuf_AssignDescriptorsOnce() {
::google::protobuf::GoogleOnceInit(&protobuf_AssignDescriptors_once_,
&protobuf_AssignDesc_test_2eproto);
}
void protobuf_RegisterTypes(const ::std::string&) {
protobuf_AssignDescriptorsOnce();
::google::protobuf::MessageFactory::InternalRegisterGeneratedMessage(
Simple_descriptor_, &Simple::default_instance());
}
} // namespace
void protobuf_ShutdownFile_test_2eproto() {
delete Simple::default_instance_;
delete Simple_reflection_;
}
void protobuf_AddDesc_test_2eproto() {
static bool already_here = false;
if (already_here) return;
already_here = true;
GOOGLE_PROTOBUF_VERIFY_VERSION;
::google::protobuf::DescriptorPool::InternalAddGeneratedFile(
"\n\ntest.proto\022\004test\"-\n\006Simple\022\017\n\007testInt\030"
"\001 \001(\003\022\022\n\ntestString\030\002 \001(\tb\006proto3", 73);
::google::protobuf::MessageFactory::InternalRegisterGeneratedFile(
"test.proto", &protobuf_RegisterTypes);
Simple::default_instance_ = new Simple();
Simple::default_instance_->InitAsDefaultInstance();
::google::protobuf::internal::OnShutdown(&protobuf_ShutdownFile_test_2eproto);
}
// Force AddDescriptors() to be called at static initialization time.
struct StaticDescriptorInitializer_test_2eproto {
StaticDescriptorInitializer_test_2eproto() {
protobuf_AddDesc_test_2eproto();
}
} static_descriptor_initializer_test_2eproto_;
namespace {
static void MergeFromFail(int line) GOOGLE_ATTRIBUTE_COLD;
static void MergeFromFail(int line) {
GOOGLE_CHECK(false) << __FILE__ << ":" << line;
}
} // namespace
// ===================================================================
#if !defined(_MSC_VER) || _MSC_VER >= 1900
const int Simple::kTestIntFieldNumber;
const int Simple::kTestStringFieldNumber;
#endif // !defined(_MSC_VER) || _MSC_VER >= 1900
Simple::Simple()
: ::google::protobuf::Message(), _internal_metadata_(NULL) {
SharedCtor();
// @@protoc_insertion_point(constructor:test.Simple)
}
void Simple::InitAsDefaultInstance() {
_is_default_instance_ = true;
}
Simple::Simple(const Simple& from)
: ::google::protobuf::Message(),
_internal_metadata_(NULL) {
SharedCtor();
MergeFrom(from);
// @@protoc_insertion_point(copy_constructor:test.Simple)
}
void Simple::SharedCtor() {
_is_default_instance_ = false;
::google::protobuf::internal::GetEmptyString();
_cached_size_ = 0;
testint_ = GOOGLE_LONGLONG(0);
teststring_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
Simple::~Simple() {
// @@protoc_insertion_point(destructor:test.Simple)
SharedDtor();
}
void Simple::SharedDtor() {
teststring_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
if (this != default_instance_) {
}
}
void Simple::SetCachedSize(int size) const {
GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN();
_cached_size_ = size;
GOOGLE_SAFE_CONCURRENT_WRITES_END();
}
const ::google::protobuf::Descriptor* Simple::descriptor() {
protobuf_AssignDescriptorsOnce();
return Simple_descriptor_;
}
const Simple& Simple::default_instance() {
if (default_instance_ == NULL) protobuf_AddDesc_test_2eproto();
return *default_instance_;
}
Simple* Simple::default_instance_ = NULL;
Simple* Simple::New(::google::protobuf::Arena* arena) const {
Simple* n = new Simple;
if (arena != NULL) {
arena->Own(n);
}
return n;
}
void Simple::Clear() {
// @@protoc_insertion_point(message_clear_start:test.Simple)
testint_ = GOOGLE_LONGLONG(0);
teststring_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
bool Simple::MergePartialFromCodedStream(
::google::protobuf::io::CodedInputStream* input) {
#define DO_(EXPRESSION) if (!GOOGLE_PREDICT_TRUE(EXPRESSION)) goto failure
::google::protobuf::uint32 tag;
// @@protoc_insertion_point(parse_start:test.Simple)
for (;;) {
::std::pair< ::google::protobuf::uint32, bool> p = input->ReadTagWithCutoff(127);
tag = p.first;
if (!p.second) goto handle_unusual;
switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) {
// optional int64 testInt = 1;
case 1: {
if (tag == 8) {
DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive<
::google::protobuf::int64, ::google::protobuf::internal::WireFormatLite::TYPE_INT64>(
input, &testint_)));
} else {
goto handle_unusual;
}
if (input->ExpectTag(18)) goto parse_testString;
break;
}
// optional string testString = 2;
case 2: {
if (tag == 18) {
parse_testString:
DO_(::google::protobuf::internal::WireFormatLite::ReadString(
input, this->mutable_teststring()));
DO_(::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
this->teststring().data(), this->teststring().length(),
::google::protobuf::internal::WireFormatLite::PARSE,
"test.Simple.testString"));
} else {
goto handle_unusual;
}
if (input->ExpectAtEnd()) goto success;
break;
}
default: {
handle_unusual:
if (tag == 0 ||
::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) ==
::google::protobuf::internal::WireFormatLite::WIRETYPE_END_GROUP) {
goto success;
}
DO_(::google::protobuf::internal::WireFormatLite::SkipField(input, tag));
break;
}
}
}
success:
// @@protoc_insertion_point(parse_success:test.Simple)
return true;
failure:
// @@protoc_insertion_point(parse_failure:test.Simple)
return false;
#undef DO_
}
void Simple::SerializeWithCachedSizes(
::google::protobuf::io::CodedOutputStream* output) const {
// @@protoc_insertion_point(serialize_start:test.Simple)
// optional int64 testInt = 1;
if (this->testint() != 0) {
::google::protobuf::internal::WireFormatLite::WriteInt64(1, this->testint(), output);
}
// optional string testString = 2;
if (this->teststring().size() > 0) {
::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
this->teststring().data(), this->teststring().length(),
::google::protobuf::internal::WireFormatLite::SERIALIZE,
"test.Simple.testString");
::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(
2, this->teststring(), output);
}
// @@protoc_insertion_point(serialize_end:test.Simple)
}
::google::protobuf::uint8* Simple::SerializeWithCachedSizesToArray(
::google::protobuf::uint8* target) const {
// @@protoc_insertion_point(serialize_to_array_start:test.Simple)
// optional int64 testInt = 1;
if (this->testint() != 0) {
target = ::google::protobuf::internal::WireFormatLite::WriteInt64ToArray(1, this->testint(), target);
}
// optional string testString = 2;
if (this->teststring().size() > 0) {
::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
this->teststring().data(), this->teststring().length(),
::google::protobuf::internal::WireFormatLite::SERIALIZE,
"test.Simple.testString");
target =
::google::protobuf::internal::WireFormatLite::WriteStringToArray(
2, this->teststring(), target);
}
// @@protoc_insertion_point(serialize_to_array_end:test.Simple)
return target;
}
int Simple::ByteSize() const {
// @@protoc_insertion_point(message_byte_size_start:test.Simple)
int total_size = 0;
// optional int64 testInt = 1;
if (this->testint() != 0) {
total_size += 1 +
::google::protobuf::internal::WireFormatLite::Int64Size(
this->testint());
}
// optional string testString = 2;
if (this->teststring().size() > 0) {
total_size += 1 +
::google::protobuf::internal::WireFormatLite::StringSize(
this->teststring());
}
GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN();
_cached_size_ = total_size;
GOOGLE_SAFE_CONCURRENT_WRITES_END();
return total_size;
}
void Simple::MergeFrom(const ::google::protobuf::Message& from) {
// @@protoc_insertion_point(generalized_merge_from_start:test.Simple)
if (GOOGLE_PREDICT_FALSE(&from == this)) MergeFromFail(__LINE__);
const Simple* source =
::google::protobuf::internal::DynamicCastToGenerated<const Simple>(
&from);
if (source == NULL) {
// @@protoc_insertion_point(generalized_merge_from_cast_fail:test.Simple)
::google::protobuf::internal::ReflectionOps::Merge(from, this);
} else {
// @@protoc_insertion_point(generalized_merge_from_cast_success:test.Simple)
MergeFrom(*source);
}
}
void Simple::MergeFrom(const Simple& from) {
// @@protoc_insertion_point(class_specific_merge_from_start:test.Simple)
if (GOOGLE_PREDICT_FALSE(&from == this)) MergeFromFail(__LINE__);
if (from.testint() != 0) {
set_testint(from.testint());
}
if (from.teststring().size() > 0) {
teststring_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.teststring_);
}
}
void Simple::CopyFrom(const ::google::protobuf::Message& from) {
// @@protoc_insertion_point(generalized_copy_from_start:test.Simple)
if (&from == this) return;
Clear();
MergeFrom(from);
}
void Simple::CopyFrom(const Simple& from) {
// @@protoc_insertion_point(class_specific_copy_from_start:test.Simple)
if (&from == this) return;
Clear();
MergeFrom(from);
}
bool Simple::IsInitialized() const {
return true;
}
void Simple::Swap(Simple* other) {
if (other == this) return;
InternalSwap(other);
}
void Simple::InternalSwap(Simple* other) {
std::swap(testint_, other->testint_);
teststring_.Swap(&other->teststring_);
_internal_metadata_.Swap(&other->_internal_metadata_);
std::swap(_cached_size_, other->_cached_size_);
}
::google::protobuf::Metadata Simple::GetMetadata() const {
protobuf_AssignDescriptorsOnce();
::google::protobuf::Metadata metadata;
metadata.descriptor = Simple_descriptor_;
metadata.reflection = Simple_reflection_;
return metadata;
}
#if PROTOBUF_INLINE_NOT_IN_HEADERS
// Simple
// optional int64 testInt = 1;
void Simple::clear_testint() {
testint_ = GOOGLE_LONGLONG(0);
}
::google::protobuf::int64 Simple::testint() const {
// @@protoc_insertion_point(field_get:test.Simple.testInt)
return testint_;
}
void Simple::set_testint(::google::protobuf::int64 value) {
testint_ = value;
// @@protoc_insertion_point(field_set:test.Simple.testInt)
}
// optional string testString = 2;
void Simple::clear_teststring() {
teststring_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
const ::std::string& Simple::teststring() const {
// @@protoc_insertion_point(field_get:test.Simple.testString)
return teststring_.GetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
void Simple::set_teststring(const ::std::string& value) {
teststring_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value);
// @@protoc_insertion_point(field_set:test.Simple.testString)
}
void Simple::set_teststring(const char* value) {
teststring_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value));
// @@protoc_insertion_point(field_set_char:test.Simple.testString)
}
void Simple::set_teststring(const char* value, size_t size) {
teststring_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(),
::std::string(reinterpret_cast<const char*>(value), size));
// @@protoc_insertion_point(field_set_pointer:test.Simple.testString)
}
::std::string* Simple::mutable_teststring() {
// @@protoc_insertion_point(field_mutable:test.Simple.testString)
return teststring_.MutableNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
::std::string* Simple::release_teststring() {
// @@protoc_insertion_point(field_release:test.Simple.testString)
return teststring_.ReleaseNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
void Simple::set_allocated_teststring(::std::string* teststring) {
if (teststring != NULL) {
} else {
}
teststring_.SetAllocatedNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), teststring);
// @@protoc_insertion_point(field_set_allocated:test.Simple.testString)
}
#endif // PROTOBUF_INLINE_NOT_IN_HEADERS
// @@protoc_insertion_point(namespace_scope)
} // namespace test
// @@protoc_insertion_point(global_scope)
// Generated by the protocol buffer compiler. DO NOT EDIT!
// source: test.proto
#ifndef PROTOBUF_test_2eproto__INCLUDED
#define PROTOBUF_test_2eproto__INCLUDED
#include <string>
#include <google/protobuf/stubs/common.h>
#if GOOGLE_PROTOBUF_VERSION < 3000000
#error This file was generated by a newer version of protoc which is
#error incompatible with your Protocol Buffer headers. Please update
#error your headers.
#endif
#if 3000000 < GOOGLE_PROTOBUF_MIN_PROTOC_VERSION
#error This file was generated by an older version of protoc which is
#error incompatible with your Protocol Buffer headers. Please
#error regenerate this file with a newer version of protoc.
#endif
#include <google/protobuf/arena.h>
#include <google/protobuf/arenastring.h>
#include <google/protobuf/generated_message_util.h>
#include <google/protobuf/metadata.h>
#include <google/protobuf/message.h>
#include <google/protobuf/repeated_field.h>
#include <google/protobuf/extension_set.h>
#include <google/protobuf/unknown_field_set.h>
// @@protoc_insertion_point(includes)
namespace test {
// Internal implementation detail -- do not call these.
void protobuf_AddDesc_test_2eproto();
void protobuf_AssignDesc_test_2eproto();
void protobuf_ShutdownFile_test_2eproto();
class Simple;
// ===================================================================
class Simple : public ::google::protobuf::Message {
public:
Simple();
virtual ~Simple();
Simple(const Simple& from);
inline Simple& operator=(const Simple& from) {
CopyFrom(from);
return *this;
}
static const ::google::protobuf::Descriptor* descriptor();
static const Simple& default_instance();
void Swap(Simple* other);
// implements Message ----------------------------------------------
inline Simple* New() const { return New(NULL); }
Simple* New(::google::protobuf::Arena* arena) const;
void CopyFrom(const ::google::protobuf::Message& from);
void MergeFrom(const ::google::protobuf::Message& from);
void CopyFrom(const Simple& from);
void MergeFrom(const Simple& from);
void Clear();
bool IsInitialized() const;
int ByteSize() const;
bool MergePartialFromCodedStream(
::google::protobuf::io::CodedInputStream* input);
void SerializeWithCachedSizes(
::google::protobuf::io::CodedOutputStream* output) const;
::google::protobuf::uint8* SerializeWithCachedSizesToArray(::google::protobuf::uint8* output) const;
int GetCachedSize() const { return _cached_size_; }
private:
void SharedCtor();
void SharedDtor();
void SetCachedSize(int size) const;
void InternalSwap(Simple* other);
private:
inline ::google::protobuf::Arena* GetArenaNoVirtual() const {
return _internal_metadata_.arena();
}
inline void* MaybeArenaPtr() const {
return _internal_metadata_.raw_arena_ptr();
}
public:
::google::protobuf::Metadata GetMetadata() const;
// nested types ----------------------------------------------------
// accessors -------------------------------------------------------
// optional int64 testInt = 1;
void clear_testint();
static const int kTestIntFieldNumber = 1;
::google::protobuf::int64 testint() const;
void set_testint(::google::protobuf::int64 value);
// optional string testString = 2;
void clear_teststring();
static const int kTestStringFieldNumber = 2;
const ::std::string& teststring() const;
void set_teststring(const ::std::string& value);
void set_teststring(const char* value);
void set_teststring(const char* value, size_t size);
::std::string* mutable_teststring();
::std::string* release_teststring();
void set_allocated_teststring(::std::string* teststring);
// @@protoc_insertion_point(class_scope:test.Simple)
private:
::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_;
bool _is_default_instance_;
::google::protobuf::int64 testint_;
::google::protobuf::internal::ArenaStringPtr teststring_;
mutable int _cached_size_;
friend void protobuf_AddDesc_test_2eproto();
friend void protobuf_AssignDesc_test_2eproto();
friend void protobuf_ShutdownFile_test_2eproto();
void InitAsDefaultInstance();
static Simple* default_instance_;
};
// ===================================================================
// ===================================================================
#if !PROTOBUF_INLINE_NOT_IN_HEADERS
// Simple
// optional int64 testInt = 1;
inline void Simple::clear_testint() {
testint_ = GOOGLE_LONGLONG(0);
}
inline ::google::protobuf::int64 Simple::testint() const {
// @@protoc_insertion_point(field_get:test.Simple.testInt)
return testint_;
}
inline void Simple::set_testint(::google::protobuf::int64 value) {
testint_ = value;
// @@protoc_insertion_point(field_set:test.Simple.testInt)
}
// optional string testString = 2;
inline void Simple::clear_teststring() {
teststring_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline const ::std::string& Simple::teststring() const {
// @@protoc_insertion_point(field_get:test.Simple.testString)
return teststring_.GetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline void Simple::set_teststring(const ::std::string& value) {
teststring_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value);
// @@protoc_insertion_point(field_set:test.Simple.testString)
}
inline void Simple::set_teststring(const char* value) {
teststring_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value));
// @@protoc_insertion_point(field_set_char:test.Simple.testString)
}
inline void Simple::set_teststring(const char* value, size_t size) {
teststring_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(),
::std::string(reinterpret_cast<const char*>(value), size));
// @@protoc_insertion_point(field_set_pointer:test.Simple.testString)
}
inline ::std::string* Simple::mutable_teststring() {
// @@protoc_insertion_point(field_mutable:test.Simple.testString)
return teststring_.MutableNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline ::std::string* Simple::release_teststring() {
// @@protoc_insertion_point(field_release:test.Simple.testString)
return teststring_.ReleaseNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline void Simple::set_allocated_teststring(::std::string* teststring) {
if (teststring != NULL) {
} else {
}
teststring_.SetAllocatedNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), teststring);
// @@protoc_insertion_point(field_set_allocated:test.Simple.testString)
}
#endif // !PROTOBUF_INLINE_NOT_IN_HEADERS
// @@protoc_insertion_point(namespace_scope)
} // namespace test
// @@protoc_insertion_point(global_scope)
#endif // PROTOBUF_test_2eproto__INCLUDED
syntax = "proto3";
package test;
message Simple {
int64 testInt = 1;
string testString = 2;
Inner inner = 3;
}
message Inner {
int64
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment