writer.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #pragma once
  2. #include "public.h"
  3. #include "token.h"
  4. #include "consumer.h"
  5. #include <util/generic/noncopyable.h>
  6. class IOutputStream;
  7. class IZeroCopyInput;
  8. namespace NYson {
  9. ////////////////////////////////////////////////////////////////////////////////
  10. class TYsonWriter
  11. : public TYsonConsumerBase,
  12. private TNonCopyable {
  13. public:
  14. class TState {
  15. private:
  16. int Depth;
  17. bool BeforeFirstItem;
  18. friend class TYsonWriter;
  19. };
  20. public:
  21. TYsonWriter(
  22. IOutputStream* stream,
  23. EYsonFormat format = EYsonFormat::Binary,
  24. EYsonType type = ::NYson::EYsonType::Node,
  25. bool enableRaw = false);
  26. void OnStringScalar(TStringBuf value) override;
  27. void OnInt64Scalar(i64 value) override;
  28. void OnUint64Scalar(ui64 value) override;
  29. void OnDoubleScalar(double value) override;
  30. void OnBooleanScalar(bool value) override;
  31. void OnEntity() override;
  32. void OnBeginList() override;
  33. void OnListItem() override;
  34. void OnEndList() override;
  35. void OnBeginMap() override;
  36. void OnKeyedItem(TStringBuf key) override;
  37. void OnEndMap() override;
  38. void OnBeginAttributes() override;
  39. void OnEndAttributes() override;
  40. void OnRaw(TStringBuf yson, EYsonType type = ::NYson::EYsonType::Node) override;
  41. TState State() const;
  42. void Reset(const TState& state);
  43. protected:
  44. IOutputStream* Stream;
  45. EYsonFormat Format;
  46. EYsonType Type;
  47. bool EnableRaw;
  48. int Depth;
  49. bool BeforeFirstItem;
  50. static const int IndentSize = 4;
  51. void WriteIndent();
  52. void WriteStringScalar(const TStringBuf& value);
  53. void BeginCollection(ETokenType beginToken);
  54. void CollectionItem(ETokenType separatorToken);
  55. void EndCollection(ETokenType endToken);
  56. bool IsTopLevelFragmentContext() const;
  57. void EndNode();
  58. };
  59. ////////////////////////////////////////////////////////////////////////////////
  60. void ReformatYsonStream(
  61. IInputStream* input,
  62. IOutputStream* output,
  63. EYsonFormat format = EYsonFormat::Binary,
  64. EYsonType type = ::NYson::EYsonType::Node);
  65. ////////////////////////////////////////////////////////////////////////////////
  66. } // namespace NYson