node_io.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #include "node_io.h"
  2. #include "node_builder.h"
  3. #include "node_visitor.h"
  4. #include <library/cpp/yson/json/json_writer.h>
  5. #include <library/cpp/yson/parser.h>
  6. #include <library/cpp/yson/writer.h>
  7. #include <library/cpp/yson/json/yson2json_adapter.h>
  8. #include <library/cpp/json/json_reader.h>
  9. #include <library/cpp/json/json_value.h>
  10. #include <util/generic/size_literals.h>
  11. #include <util/stream/input.h>
  12. #include <util/stream/output.h>
  13. #include <util/stream/str.h>
  14. #include <util/stream/mem.h>
  15. namespace NYT {
  16. static void WalkJsonTree(const NJson::TJsonValue& jsonValue, NJson::TJsonCallbacks* callbacks)
  17. {
  18. using namespace NJson;
  19. switch (jsonValue.GetType()) {
  20. case JSON_NULL:
  21. callbacks->OnNull();
  22. return;
  23. case JSON_BOOLEAN:
  24. callbacks->OnBoolean(jsonValue.GetBoolean());
  25. return;
  26. case JSON_INTEGER:
  27. callbacks->OnInteger(jsonValue.GetInteger());
  28. return;
  29. case JSON_UINTEGER:
  30. callbacks->OnUInteger(jsonValue.GetUInteger());
  31. return;
  32. case JSON_DOUBLE:
  33. callbacks->OnDouble(jsonValue.GetDouble());
  34. return;
  35. case JSON_STRING:
  36. callbacks->OnString(jsonValue.GetString());
  37. return;
  38. case JSON_MAP:
  39. {
  40. callbacks->OnOpenMap();
  41. for (const auto& item : jsonValue.GetMap()) {
  42. callbacks->OnMapKey(item.first);
  43. WalkJsonTree(item.second, callbacks);
  44. }
  45. callbacks->OnCloseMap();
  46. }
  47. return;
  48. case JSON_ARRAY:
  49. {
  50. callbacks->OnOpenArray();
  51. for (const auto& item : jsonValue.GetArray()) {
  52. WalkJsonTree(item, callbacks);
  53. }
  54. callbacks->OnCloseArray();
  55. }
  56. return;
  57. case JSON_UNDEFINED:
  58. ythrow yexception() << "cannot consume undefined json value";
  59. return;
  60. }
  61. Y_UNREACHABLE();
  62. }
  63. static TNode CreateEmptyNodeByType(::NYson::EYsonType type)
  64. {
  65. TNode result;
  66. switch (type) {
  67. case ::NYson::EYsonType::ListFragment:
  68. result = TNode::CreateList();
  69. break;
  70. case ::NYson::EYsonType::MapFragment:
  71. result = TNode::CreateMap();
  72. break;
  73. default:
  74. break;
  75. }
  76. return result;
  77. }
  78. static TNode NodeFromYsonStream(IInputStream* input, ::NYson::EYsonType type, bool consumeUntilEof)
  79. {
  80. TNode result = CreateEmptyNodeByType(type);
  81. ui64 bufferSizeLimit = 64_KB;
  82. if (!consumeUntilEof) {
  83. // Other values might be in the stream, so reading one symbol at a time.
  84. bufferSizeLimit = 1;
  85. }
  86. TNodeBuilder builder(&result);
  87. ::NYson::TYsonParser parser(
  88. &builder,
  89. input,
  90. type,
  91. /*enableLinePositionInfo*/ false,
  92. bufferSizeLimit,
  93. consumeUntilEof);
  94. parser.Parse();
  95. return result;
  96. }
  97. TNode NodeFromYsonString(const TStringBuf input, ::NYson::EYsonType type)
  98. {
  99. TMemoryInput stream(input);
  100. return NodeFromYsonStream(&stream, type);
  101. }
  102. TString NodeToYsonString(const TNode& node, NYson::EYsonFormat format)
  103. {
  104. TStringStream stream;
  105. NodeToYsonStream(node, &stream, format);
  106. return stream.Str();
  107. }
  108. TString NodeToCanonicalYsonString(const TNode& node, NYson::EYsonFormat format)
  109. {
  110. TStringStream stream;
  111. NodeToCanonicalYsonStream(node, &stream, format);
  112. return stream.Str();
  113. }
  114. TNode NodeFromYsonStream(IInputStream* input, ::NYson::EYsonType type)
  115. {
  116. return NodeFromYsonStream(input, type, /*consumeUntilEof*/ true);
  117. }
  118. TNode NodeFromYsonStreamNonGreedy(IInputStream* input, ::NYson::EYsonType type)
  119. {
  120. return NodeFromYsonStream(input, type, /*consumeUntilEof*/ false);
  121. }
  122. void NodeToYsonStream(const TNode& node, IOutputStream* output, NYson::EYsonFormat format)
  123. {
  124. ::NYson::TYsonWriter writer(output, format);
  125. TNodeVisitor visitor(&writer);
  126. visitor.Visit(node);
  127. }
  128. void NodeToCanonicalYsonStream(const TNode& node, IOutputStream* output, NYson::EYsonFormat format)
  129. {
  130. ::NYson::TYsonWriter writer(output, format);
  131. TNodeVisitor visitor(&writer, /*sortMapKeys*/ true);
  132. visitor.Visit(node);
  133. }
  134. bool TryNodeFromJsonString(const TStringBuf input, TNode& dst)
  135. {
  136. TMemoryInput stream(input);
  137. TNodeBuilder builder(&dst);
  138. TYson2JsonCallbacksAdapter callbacks(&builder, /*throwException*/ false);
  139. NJson::TJsonReaderConfig config;
  140. config.DontValidateUtf8 = true;
  141. NJson::ReadJson(&stream, &config, &callbacks);
  142. return !callbacks.GetHaveErrors();
  143. }
  144. TNode NodeFromJsonString(const TStringBuf input)
  145. {
  146. TMemoryInput stream(input);
  147. TNode result;
  148. TNodeBuilder builder(&result);
  149. TYson2JsonCallbacksAdapter callbacks(&builder, /*throwException*/ true);
  150. NJson::TJsonReaderConfig config;
  151. config.DontValidateUtf8 = true;
  152. NJson::ReadJson(&stream, &config, &callbacks);
  153. return result;
  154. }
  155. TNode NodeFromJsonStringIterative(const TStringBuf input, ui64 maxDepth)
  156. {
  157. TMemoryInput stream(input);
  158. TNode result;
  159. TNodeBuilder builder(&result);
  160. TYson2JsonCallbacksAdapter callbacks(&builder, /*throwException*/ true, maxDepth);
  161. NJson::TJsonReaderConfig config;
  162. config.DontValidateUtf8 = true;
  163. config.UseIterativeParser = true;
  164. config.MaxDepth = maxDepth;
  165. NJson::ReadJson(&stream, &config, &callbacks);
  166. return result;
  167. }
  168. TNode NodeFromJsonValue(const NJson::TJsonValue& input)
  169. {
  170. TNode result;
  171. TNodeBuilder builder(&result);
  172. TYson2JsonCallbacksAdapter callbacks(&builder, /*throwException*/ true);
  173. WalkJsonTree(input, &callbacks);
  174. return result;
  175. }
  176. ////////////////////////////////////////////////////////////////////////////////
  177. } // namespace NYT