json_writer.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #pragma once
  2. #include <library/cpp/yson/public.h>
  3. #include <library/cpp/yson/consumer.h>
  4. #include <library/cpp/json/json_writer.h>
  5. #include <util/generic/vector.h>
  6. namespace NYT {
  7. ////////////////////////////////////////////////////////////////////////////////
  8. enum EJsonFormat {
  9. JF_TEXT,
  10. JF_PRETTY
  11. };
  12. enum EJsonAttributesMode {
  13. JAM_NEVER,
  14. JAM_ON_DEMAND,
  15. JAM_ALWAYS
  16. };
  17. enum ESerializedBoolFormat {
  18. SBF_BOOLEAN,
  19. SBF_STRING
  20. };
  21. class TJsonWriter
  22. : public ::NYson::TYsonConsumerBase {
  23. public:
  24. TJsonWriter(
  25. IOutputStream* output,
  26. ::NYson::EYsonType type = ::NYson::EYsonType::Node,
  27. EJsonFormat format = JF_TEXT,
  28. EJsonAttributesMode attributesMode = JAM_ON_DEMAND,
  29. ESerializedBoolFormat booleanFormat = SBF_STRING);
  30. TJsonWriter(
  31. IOutputStream* output,
  32. NJson::TJsonWriterConfig config,
  33. ::NYson::EYsonType type = ::NYson::EYsonType::Node,
  34. EJsonAttributesMode attributesMode = JAM_ON_DEMAND,
  35. ESerializedBoolFormat booleanFormat = SBF_STRING);
  36. void Flush();
  37. void OnStringScalar(TStringBuf value) override;
  38. void OnInt64Scalar(i64 value) override;
  39. void OnUint64Scalar(ui64 value) override;
  40. void OnDoubleScalar(double value) override;
  41. void OnBooleanScalar(bool value) override;
  42. void OnEntity() override;
  43. void OnBeginList() override;
  44. void OnListItem() override;
  45. void OnEndList() override;
  46. void OnBeginMap() override;
  47. void OnKeyedItem(TStringBuf key) override;
  48. void OnEndMap() override;
  49. void OnBeginAttributes() override;
  50. void OnEndAttributes() override;
  51. private:
  52. THolder<NJson::TJsonWriter> UnderlyingJsonWriter;
  53. NJson::TJsonWriter* JsonWriter;
  54. IOutputStream* Output;
  55. ::NYson::EYsonType Type;
  56. EJsonAttributesMode AttributesMode;
  57. ESerializedBoolFormat BooleanFormat;
  58. void WriteStringScalar(const TStringBuf& value);
  59. void EnterNode();
  60. void LeaveNode();
  61. bool IsWriteAllowed();
  62. TVector<bool> HasUnfoldedStructureStack;
  63. int InAttributesBalance;
  64. bool HasAttributes;
  65. int Depth;
  66. };
  67. ////////////////////////////////////////////////////////////////////////////////
  68. }