json2yson.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #pragma once
  2. #include <library/cpp/json/json_reader.h>
  3. #include <library/cpp/json/json_value.h>
  4. #include <library/cpp/yson/writer.h>
  5. namespace NJson2Yson {
  6. class TJsonBuilderImpl: public NYson::TYsonConsumerBase {
  7. public:
  8. TJsonBuilderImpl(NJson::TJsonCallbacks* parserCallbacks)
  9. : ParserCallbacks_(parserCallbacks)
  10. {
  11. }
  12. void OnStringScalar(TStringBuf value) override {
  13. ParserCallbacks_->OnString(value);
  14. }
  15. void OnInt64Scalar(i64 value) override {
  16. ParserCallbacks_->OnInteger(value);
  17. }
  18. void OnUint64Scalar(ui64 value) override {
  19. ParserCallbacks_->OnUInteger(value);
  20. }
  21. void OnDoubleScalar(double value) override {
  22. ParserCallbacks_->OnDouble(value);
  23. }
  24. void OnBooleanScalar(bool value) override {
  25. ParserCallbacks_->OnBoolean(value);
  26. }
  27. void OnEntity() override {
  28. ParserCallbacks_->OnNull();
  29. }
  30. void OnBeginList() override {
  31. ParserCallbacks_->OnOpenArray();
  32. }
  33. void OnListItem() override {
  34. }
  35. void OnEndList() override {
  36. ParserCallbacks_->OnCloseArray();
  37. }
  38. void OnBeginMap() override {
  39. ParserCallbacks_->OnOpenMap();
  40. }
  41. void OnKeyedItem(TStringBuf key) override {
  42. ParserCallbacks_->OnMapKey(key);
  43. }
  44. void OnEndMap() override {
  45. ParserCallbacks_->OnCloseMap();
  46. }
  47. void OnBeginAttributes() override {
  48. }
  49. void OnEndAttributes() override {
  50. }
  51. private:
  52. NJson::TJsonCallbacks* ParserCallbacks_;
  53. };
  54. template <typename TBase>
  55. class TSkipAttributesProxy: public TBase {
  56. public:
  57. template <typename... TArgs>
  58. TSkipAttributesProxy(TArgs&&... args)
  59. : TBase(std::forward<TArgs>(args)...)
  60. {
  61. }
  62. void OnStringScalar(TStringBuf value) override {
  63. if (AttributesDepth == 0) {
  64. TBase::OnStringScalar(value);
  65. }
  66. }
  67. void OnInt64Scalar(i64 value) override {
  68. if (AttributesDepth == 0) {
  69. TBase::OnInt64Scalar(value);
  70. }
  71. }
  72. void OnUint64Scalar(ui64 value) override {
  73. if (AttributesDepth == 0) {
  74. TBase::OnUint64Scalar(value);
  75. }
  76. }
  77. void OnDoubleScalar(double value) override {
  78. if (AttributesDepth == 0) {
  79. TBase::OnDoubleScalar(value);
  80. }
  81. }
  82. void OnBooleanScalar(bool value) override {
  83. if (AttributesDepth == 0) {
  84. TBase::OnBooleanScalar(value);
  85. }
  86. }
  87. void OnEntity() override {
  88. if (AttributesDepth == 0) {
  89. TBase::OnEntity();
  90. }
  91. }
  92. void OnBeginList() override {
  93. if (AttributesDepth == 0) {
  94. TBase::OnBeginList();
  95. }
  96. }
  97. void OnListItem() override {
  98. if (AttributesDepth == 0) {
  99. TBase::OnListItem();
  100. }
  101. }
  102. void OnEndList() override {
  103. if (AttributesDepth == 0) {
  104. TBase::OnEndList();
  105. }
  106. }
  107. void OnBeginMap() override {
  108. if (AttributesDepth == 0) {
  109. TBase::OnBeginMap();
  110. }
  111. }
  112. void OnKeyedItem(TStringBuf key) override {
  113. if (AttributesDepth == 0) {
  114. TBase::OnKeyedItem(key);
  115. }
  116. }
  117. void OnEndMap() override {
  118. if (AttributesDepth == 0) {
  119. TBase::OnEndMap();
  120. }
  121. }
  122. void OnBeginAttributes() override {
  123. ++AttributesDepth;
  124. }
  125. void OnEndAttributes() override {
  126. --AttributesDepth;
  127. Y_ASSERT(AttributesDepth >= 0);
  128. }
  129. private:
  130. int AttributesDepth = 0;
  131. };
  132. using TJsonBuilder = TSkipAttributesProxy<TJsonBuilderImpl>;
  133. void ConvertYson2Json(IInputStream* inputStream, IOutputStream* outputStream);
  134. void ConvertYson2Json(TStringBuf yson, IOutputStream* outputStream);
  135. TString ConvertYson2Json(TStringBuf yson);
  136. bool DeserializeYsonAsJsonValue(IInputStream* inputStream, NJson::TJsonValue* outputValue, bool throwOnError = false);
  137. bool DeserializeYsonAsJsonValue(TStringBuf str, NJson::TJsonValue* outputValue, bool throwOnError = false);
  138. void SerializeJsonValueAsYson(const NJson::TJsonValue& inputValue, NYson::TYsonWriter* ysonWriter);
  139. void SerializeJsonValueAsYson(const NJson::TJsonValue& inputValue, IOutputStream* outputStream);
  140. void SerializeJsonValueAsYson(const NJson::TJsonValue& inputValue, TString& result);
  141. TString SerializeJsonValueAsYson(const NJson::TJsonValue& inputValue);
  142. }