syntax = "proto3";
import "google/protobuf/any.proto";import "google/protobuf/timestamp.proto";
package movie.pbuf;
// This message is a one-to-one mapping to the Movie Collection in our MongoDBmessage Movie { google.protobuf.Timestamp start_time = 1; string movie_name = 2; // TODO action flag // TODO add more here}
message CommandControlMsg { string message = 1; google.protobuf.Any command = 2;}
/** * Serialize a protobuf message to JSON and parse back again. */TEST_F(BasicProtoGtest, SerializeToJsonAndBackAgain) { // Movie PBUF message Movie startMovie;
struct timeval tv; gettimeofday(&tv, NULL);
::google::protobuf::Timestamp* timestamp = new ::google::protobuf::Timestamp(); timestamp->set_seconds(tv.tv_sec); timestamp->set_nanos(tv.tv_usec * 1000); startMovie.set_allocated_start_time(timestamp); startMovie.set_movie_name("my happy movie"); std::string serialized = startMovie.SerializeAsString(); std::string json_string; // Create a TypeResolver used to resolve protobuf message types google::protobuf::util::JsonOptions options; options.always_print_primitive_fields = true; std::unique_ptr<google::protobuf::util::TypeResolver> resolver(google::protobuf::util::NewTypeResolverForDescriptorPool( "type.googleapis.com", google::protobuf::DescriptorPool::generated_pool())); // Assert we can find the Movie type in the resolver Type type; ASSERT_TRUE(resolver->ResolveMessageType( "type.googleapis.com/movie.pbuf.Movie", &type).ok()); auto status = google::protobuf::util::BinaryToJsonString(resolver.get(), "type.googleapis.com/movie.pbuf.Movie", serialized, &json_string, options);// std::cout << "~~~~~~~ bin str\n" << serialized << std::endl; std::cout << "*******\n" << json_string << std::endl; // Turn JSON into serialized protobuf message std::string movieBin; google::protobuf::util::JsonToBinaryString(resolver.get(), "type.googleapis.com/movie.pbuf.Movie", json_string, &movieBin); Movie startMovie2; startMovie2.ParseFromString(movieBin); EXPECT_EQ(startMovie.movie_name(), startMovie2.movie_name()); EXPECT_EQ(startMovie.start_time(), startMovie2.start_time()); }
cd project_dir
mkdir build
cd build
cmake ..
make
ctest
--
You received this message because you are subscribed to a topic in the Google Groups "Protocol Buffers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/protobuf/ecxdP_mN8OI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to protobuf+u...@googlegroups.com.
To post to this group, send email to prot...@googlegroups.com.
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.
protobuf-3.0.0-beta-3/src/google/protobuf/util