PageRenderTime 31ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/mordor/protobuf.h

http://github.com/mozy/mordor
C Header | 109 lines | 27 code | 15 blank | 67 comment | 0 complexity | 6611e618051e07c4e22989b78df8fb8f MD5 | raw file
Possible License(s): BSD-3-Clause
  1. #ifndef __MORDOR_PROTOBUF_H__
  2. #define __MORDOR_PROTOBUF_H__
  3. // Copyright (c) 2010 - Mozy, Inc.
  4. #include "mordor/json.h"
  5. namespace google {
  6. namespace protobuf {
  7. class Message;
  8. }}
  9. namespace Mordor {
  10. struct Buffer;
  11. void serializeToBuffer(const google::protobuf::Message &proto,
  12. Mordor::Buffer &buffer);
  13. void parseFromBuffer(google::protobuf::Message &proto,
  14. const Mordor::Buffer &buffer);
  15. // reflection utils
  16. void serializeToJsonObject(const google::protobuf::Message &proto,
  17. Mordor::JSON::Object &object, bool validate=false, bool includeNull=false);
  18. void parseFromJsonObject(google::protobuf::Message *proto,
  19. const Mordor::JSON::Object &object, bool validate=false);
  20. google::protobuf::Message* forName(const std::string& typeName);
  21. std::string toJson(const google::protobuf::Message &proto, bool validate=false, bool includeNull=false);
  22. google::protobuf::Message * fromJson(const Mordor::Buffer &buffer,
  23. bool validate=false);
  24. void setField(google::protobuf::Message *proto, const std::string &fieldName,
  25. const Mordor::JSON::Value &value);
  26. Mordor::JSON::Value getField(google::protobuf::Message *proto,
  27. const std::string &fieldName);
  28. }
  29. /* sample code
  30. // test reflection
  31. Message *msg = forName("restpb.PartialObject");
  32. setField(msg, "store_dat.len", 100);
  33. Value val = getField(msg, "store_dat.len");
  34. std::cout << "set/getField store_dat.len = " << val << std::endl;
  35. try {
  36. Buffer json =
  37. "{\n"
  38. " \"restpb.PartialObject\" : {\n"
  39. " \"container_id\": 12345,\n"
  40. " \"filename\": \"/tmp/ahaha.txt\",\n"
  41. " \"store_dat\": {\n"
  42. " \"len\": 100,\n"
  43. " \"m_factor\": 100,\n"
  44. " \"frag\": [\n"
  45. " {\n"
  46. " \"frag_id\": 1001,\n"
  47. " \"tdn_id\": 45,\n"
  48. " \"tmp_file\": \"/frag-tmp/ajfajfaf.dat\"\n"
  49. " },\n"
  50. " {\n"
  51. " \"frag_id\": 1001,\n"
  52. " \"tmp_file\": \"/frag-tmp/ajfafafafjfaf.dat\"\n"
  53. " }\n"
  54. " ]\n"
  55. " }\n"
  56. " }\n"
  57. "}\n";
  58. Message * msg = fromJson(json);
  59. std::cout << "from Json\n" << json.toString() << std::endl << "debug string: " << msg->DebugString() << std::endl;
  60. // set indexed field
  61. setField(msg, "store_dat.frag[1].tdn_id", 46);
  62. std::string output = toJson(*msg);
  63. std::cout << "back to Json\n" << output << std::endl;
  64. std::cout << "store_dat.frag[0].tmp_file = " << getField(msg, "store_dat.frag[0].tmp_file") << std::endl;
  65. } catch (std::runtime_error &e) {
  66. std::cerr << "ERROR: " << e.what() << std::endl;
  67. }
  68. // validating prototype
  69. msg = forName("restpb.StoreDat");
  70. Object obj;
  71. try {
  72. parseFromJsonObject(msg, obj, true);
  73. } catch (std::exception &e) {
  74. std::cerr << "validating error : " << e.what() << std::endl;
  75. obj["m_factor"] = 10;
  76. obj["len"] = 100;
  77. try {
  78. parseFromJsonObject(msg, obj, true);
  79. } catch (std::exception &e) {
  80. std::cerr << "validating error : " << e.what() << std::endl;
  81. }
  82. }
  83. }
  84. */
  85. #endif