json_writer_output.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #pragma once
  2. #include "json_output.h"
  3. #include "config.h"
  4. #include <library/cpp/json/json_writer.h>
  5. #include <util/string/builder.h>
  6. #include <util/generic/store_policy.h>
  7. namespace NProtobufJson {
  8. class TBaseJsonWriterOutput: public IJsonOutput {
  9. public:
  10. TBaseJsonWriterOutput(NJson::TJsonWriter& writer)
  11. : Writer(writer)
  12. {
  13. }
  14. private:
  15. void DoWrite(int i) override {
  16. Writer.Write(i);
  17. }
  18. void DoWrite(unsigned int i) override {
  19. Writer.Write(i);
  20. }
  21. void DoWrite(long long i) override {
  22. Writer.Write(i);
  23. }
  24. void DoWrite(unsigned long long i) override {
  25. Writer.Write(i);
  26. }
  27. void DoWrite(float f) override {
  28. Writer.Write(f);
  29. }
  30. void DoWrite(double f) override {
  31. Writer.Write(f);
  32. }
  33. void DoWrite(bool b) override {
  34. Writer.Write(b);
  35. }
  36. void DoWriteNull() override {
  37. Writer.WriteNull();
  38. }
  39. void DoWrite(const TStringBuf& s) override {
  40. Writer.Write(s);
  41. }
  42. void DoWrite(const TString& s) override {
  43. Writer.Write(s);
  44. }
  45. void DoBeginList() override {
  46. Writer.OpenArray();
  47. }
  48. void DoEndList() override {
  49. Writer.CloseArray();
  50. }
  51. void DoBeginObject() override {
  52. Writer.OpenMap();
  53. }
  54. void DoWriteKey(const TStringBuf& key) override {
  55. Writer.Write(key);
  56. }
  57. void DoEndObject() override {
  58. Writer.CloseMap();
  59. }
  60. void DoWriteRawJson(const TStringBuf& str) override {
  61. Writer.UnsafeWrite(str);
  62. }
  63. NJson::TJsonWriter& Writer;
  64. };
  65. class TJsonWriterOutput: public TEmbedPolicy<NJson::TJsonWriter>, public TBaseJsonWriterOutput {
  66. public:
  67. TJsonWriterOutput(IOutputStream* outputStream, const NJson::TJsonWriterConfig& cfg)
  68. : TEmbedPolicy<NJson::TJsonWriter>(outputStream, cfg)
  69. , TBaseJsonWriterOutput(*Ptr())
  70. {
  71. }
  72. TJsonWriterOutput(IOutputStream* outputStream, const TProto2JsonConfig& cfg)
  73. : TEmbedPolicy<NJson::TJsonWriter>(outputStream, CreateJsonWriterConfig(cfg))
  74. , TBaseJsonWriterOutput(*Ptr())
  75. {
  76. }
  77. protected:
  78. static NJson::TJsonWriterConfig CreateJsonWriterConfig(const TProto2JsonConfig& cfg);
  79. };
  80. class TJsonStringWriterOutput: public TEmbedPolicy<TStringOutput>, public TJsonWriterOutput {
  81. public:
  82. template <typename TConfig>
  83. TJsonStringWriterOutput(TString* str, const TConfig& cfg)
  84. : TEmbedPolicy<TStringOutput>(*str)
  85. // If Unbuffered = false, TJsonWriter uses its own string and then flushes it into TStringOutput.
  86. , TJsonWriterOutput(TEmbedPolicy<TStringOutput>::Ptr(), CreateJsonWriterConfig(cfg).SetUnbuffered(true))
  87. {
  88. }
  89. };
  90. }