writer.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #pragma once
  2. #include "consumer.h"
  3. #include "output.h"
  4. #include "scalar.h"
  5. #include "stream_type.h"
  6. #include <memory>
  7. namespace NYsonPull {
  8. //! \brief YSON writer facade class
  9. //!
  10. //! Owns a YSON consumer and a corresponding output stream.
  11. //! Methods invoke corresponding \p NYsonPull::IConsumer methods and can be chained.
  12. class TWriter {
  13. THolder<NOutput::IStream> Stream_;
  14. THolder<IConsumer> Impl_;
  15. public:
  16. TWriter(
  17. THolder<NOutput::IStream> stream,
  18. THolder<IConsumer> impl)
  19. : Stream_{std::move(stream)}
  20. , Impl_{std::move(impl)} {
  21. }
  22. //! \brief Get a reference to underlying consumer.
  23. //!
  24. //! Useful with \p NYsonPull::bridge
  25. IConsumer& GetConsumer() {
  26. return *Impl_;
  27. }
  28. TWriter& BeginStream() {
  29. Impl_->OnBeginStream();
  30. return *this;
  31. }
  32. TWriter& EndStream() {
  33. Impl_->OnEndStream();
  34. return *this;
  35. }
  36. TWriter& BeginList() {
  37. Impl_->OnBeginList();
  38. return *this;
  39. }
  40. TWriter& EndList() {
  41. Impl_->OnEndList();
  42. return *this;
  43. }
  44. TWriter& BeginMap() {
  45. Impl_->OnBeginMap();
  46. return *this;
  47. }
  48. TWriter& EndMap() {
  49. Impl_->OnEndMap();
  50. return *this;
  51. }
  52. TWriter& BeginAttributes() {
  53. Impl_->OnBeginAttributes();
  54. return *this;
  55. }
  56. TWriter& EndAttributes() {
  57. Impl_->OnEndAttributes();
  58. return *this;
  59. }
  60. TWriter& Key(TStringBuf name) {
  61. Impl_->OnKey(name);
  62. return *this;
  63. }
  64. TWriter& Entity() {
  65. Impl_->OnEntity();
  66. return *this;
  67. }
  68. TWriter& Boolean(bool value) {
  69. Impl_->OnScalarBoolean(value);
  70. return *this;
  71. }
  72. TWriter& Int64(i64 value) {
  73. Impl_->OnScalarInt64(value);
  74. return *this;
  75. }
  76. TWriter& UInt64(ui64 value) {
  77. Impl_->OnScalarUInt64(value);
  78. return *this;
  79. }
  80. TWriter& Float64(double value) {
  81. Impl_->OnScalarFloat64(value);
  82. return *this;
  83. }
  84. TWriter& String(TStringBuf value) {
  85. Impl_->OnScalarString(value);
  86. return *this;
  87. }
  88. TWriter& Scalar(const TScalar& value) {
  89. Impl_->OnScalar(value);
  90. return *this;
  91. }
  92. TWriter& Event(const TEvent& value) {
  93. Impl_->OnEvent(value);
  94. return *this;
  95. }
  96. };
  97. //! \brief Construct a writer for binary YSON format.
  98. TWriter MakeBinaryWriter(
  99. THolder<NOutput::IStream> stream,
  100. EStreamType mode);
  101. //! \brief Construct a writer for text YSON format.
  102. TWriter MakeTextWriter(
  103. THolder<NOutput::IStream> stream,
  104. EStreamType mode);
  105. //! \brief Construct a writer for pretty text YSON format.
  106. TWriter MakePrettyTextWriter(
  107. THolder<NOutput::IStream> stream,
  108. EStreamType mode,
  109. size_t indent_size = 4);
  110. }