node_io.cpp 5.0 KB

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