proto2json.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #pragma once
  2. #include "config.h"
  3. #include "json_output.h"
  4. #include <google/protobuf/descriptor.h>
  5. #include <google/protobuf/descriptor.pb.h>
  6. #include <google/protobuf/message.h>
  7. #include <util/generic/fwd.h>
  8. #include <util/generic/vector.h>
  9. #include <util/generic/yexception.h>
  10. #include <util/stream/str.h>
  11. #include <functional>
  12. namespace NJson {
  13. class TJsonValue;
  14. class TJsonWriter;
  15. }
  16. class IOutputStream;
  17. class TStringStream;
  18. namespace NProtobufJson {
  19. void Proto2Json(const NProtoBuf::Message& proto, IJsonOutput& jsonOutput,
  20. const TProto2JsonConfig& config = TProto2JsonConfig(), bool closeMap = true);
  21. void Proto2Json(const NProtoBuf::Message& proto, NJson::TJsonWriter& writer,
  22. const TProto2JsonConfig& config = TProto2JsonConfig());
  23. /// @throw yexception
  24. void Proto2Json(const NProtoBuf::Message& proto, NJson::TJsonValue& json,
  25. const TProto2JsonConfig& config = TProto2JsonConfig());
  26. /// @throw yexception
  27. void Proto2Json(const NProtoBuf::Message& proto, IOutputStream& out,
  28. const TProto2JsonConfig& config);
  29. // Generated code shortcut
  30. template <class T>
  31. inline void Proto2Json(const T& proto, IOutputStream& out) {
  32. out << proto.AsJSON();
  33. }
  34. // TStringStream deserves a special overload as its operator TString() would cause ambiguity
  35. /// @throw yexception
  36. void Proto2Json(const NProtoBuf::Message& proto, TStringStream& out,
  37. const TProto2JsonConfig& config);
  38. // Generated code shortcut
  39. template <class T>
  40. inline void Proto2Json(const T& proto, TStringStream& out) {
  41. out << proto.AsJSON();
  42. }
  43. /// @throw yexception
  44. void Proto2Json(const NProtoBuf::Message& proto, TString& str,
  45. const TProto2JsonConfig& config);
  46. // Generated code shortcut
  47. template <class T>
  48. inline void Proto2Json(const T& proto, TString& str) {
  49. str.clear();
  50. TStringOutput out(str);
  51. out << proto.AsJSON();
  52. }
  53. /// @throw yexception
  54. TString Proto2Json(const NProtoBuf::Message& proto,
  55. const TProto2JsonConfig& config);
  56. // Returns incorrect result if proto contains another NProtoBuf::Message
  57. // Generated code shortcut
  58. template <class T>
  59. inline TString Proto2Json(const T& proto) {
  60. TString result;
  61. Proto2Json(proto, result);
  62. return result;
  63. }
  64. }