chai2010 / protorpc.cxx

This package provides C++ Protobuf-RPC support.
BSD 3-Clause "New" or "Revised" License
10 stars 4 forks source link

XML API usage question #3

Open mhaberler opened 9 years ago

mhaberler commented 9 years ago

How do I just convert an existing google::protobuf::Message to and from XML? I am not going all messages to XmlMessage just to get XML I/O, and I want to keep the existing semantics for SerializeToString and ParseFromString, not override those

here is an example of something similar what we're using for JSON I/O: https://github.com/machinekit/machinekit/blob/master/src/machinetalk/lib/json2pb.cc

here is a usage example: https://github.com/machinekit/machinekit/blob/master/src/machinetalk/support/encdec.cc#L64-L69 the other direction: https://github.com/machinekit/machinekit/blob/master/src/machinetalk/support/encdec.cc#L61

that would seem less intrusive to me, what do you think?

chai2010 commented 9 years ago

The core code is XmlMessage, it is simple:

class XmlMessage {
 public:
  XmlMessage(Message& pbMessage);
  ~XmlMessage();

  bool LoadFile(const std::string& filename);
  bool SaveFile(const std::string& filename)const;

  std::string SerializeToString()const;
  bool ParseFromString(const std::string& source);

  std::string GetErrorText()const;
};

This a examlpe:

MyPbMessage pbMsg;
XmlMessage xmlMsg(pbMsg);

auto out = xmlMsg.SerializeToString();

MyPbMessage pbMsg2;
XmlMessage xmlMsg2(pbMsg2);

auto ok = xmlMsg2.ParseFromString(out);
if(!ok) {
  panic(xmlMsg2.GetErrorText());
}
mhaberler commented 9 years ago

overlooked the obvious - works fine!

thanks, - Michael