json_output.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #pragma once
  2. #include <util/generic/ptr.h>
  3. #include <util/generic/strbuf.h>
  4. namespace NProtobufJson {
  5. class IJsonOutput {
  6. public:
  7. template <typename T>
  8. IJsonOutput& Write(const T& t) {
  9. DoWrite(t);
  10. return *this;
  11. }
  12. IJsonOutput& WriteNull() {
  13. DoWriteNull();
  14. return *this;
  15. }
  16. IJsonOutput& BeginList() {
  17. DoBeginList();
  18. return *this;
  19. }
  20. IJsonOutput& EndList() {
  21. DoEndList();
  22. return *this;
  23. }
  24. IJsonOutput& BeginObject() {
  25. DoBeginObject();
  26. return *this;
  27. }
  28. IJsonOutput& WriteKey(const TStringBuf& key) {
  29. DoWriteKey(key);
  30. return *this;
  31. }
  32. IJsonOutput& EndObject() {
  33. DoEndObject();
  34. return *this;
  35. }
  36. IJsonOutput& WriteRawJson(const TStringBuf& str) {
  37. DoWriteRawJson(str);
  38. return *this;
  39. }
  40. virtual ~IJsonOutput() {
  41. }
  42. protected:
  43. virtual void DoWrite(const TStringBuf& s) = 0;
  44. virtual void DoWrite(const TString& s) = 0;
  45. virtual void DoWrite(int i) = 0;
  46. void DoWrite(long i) {
  47. DoWrite(static_cast<long long>(i));
  48. }
  49. virtual void DoWrite(long long i) = 0;
  50. virtual void DoWrite(unsigned int i) = 0;
  51. void DoWrite(unsigned long i) {
  52. DoWrite(static_cast<unsigned long long>(i));
  53. }
  54. virtual void DoWrite(unsigned long long i) = 0;
  55. virtual void DoWrite(float f) = 0;
  56. virtual void DoWrite(double f) = 0;
  57. virtual void DoWrite(bool b) = 0;
  58. virtual void DoWriteNull() = 0;
  59. virtual void DoBeginList() = 0;
  60. virtual void DoEndList() = 0;
  61. virtual void DoBeginObject() = 0;
  62. virtual void DoWriteKey(const TStringBuf& key) = 0;
  63. virtual void DoEndObject() = 0;
  64. virtual void DoWriteRawJson(const TStringBuf& str) = 0;
  65. };
  66. using TJsonMapOutputPtr = THolder<IJsonOutput>;
  67. }