node_io.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #pragma once
  2. #include "node.h"
  3. #include <library/cpp/yson/public.h>
  4. namespace NJson {
  5. class TJsonValue;
  6. } // namespace NJson
  7. namespace NYT {
  8. ////////////////////////////////////////////////////////////////////////////////
  9. // Parse TNode from string in YSON format
  10. TNode NodeFromYsonString(const TStringBuf input, ::NYson::EYsonType type = ::NYson::EYsonType::Node);
  11. // Serialize TNode to string in one of YSON formats with random order of maps' keys (don't use in tests)
  12. TString NodeToYsonString(const TNode& node, ::NYson::EYsonFormat format = ::NYson::EYsonFormat::Text);
  13. // Same as the latter, but maps' keys are sorted lexicographically (to be used in tests)
  14. TString NodeToCanonicalYsonString(const TNode& node, ::NYson::EYsonFormat format = ::NYson::EYsonFormat::Text);
  15. // Parse TNode from stream in YSON format
  16. TNode NodeFromYsonStream(IInputStream* input, ::NYson::EYsonType type = ::NYson::EYsonType::Node);
  17. // Parse TNode from stream in YSON format.
  18. // NB: This is substantially slower (1.5-2x using the benchmark from `./benchmark/saveload.cpp`) than using
  19. // the original `NodeFromYsonStream`.
  20. // Stops reading from `input` as soon as some valid YSON was read, leaving the remainder of the stream unread.
  21. // Used in TNode::Load to support cases of saveloading multiple values after the TNode from/to the same stream.
  22. TNode NodeFromYsonStreamNonGreedy(IInputStream* input, ::NYson::EYsonType type = ::NYson::EYsonType::Node);
  23. // Serialize TNode to stream in one of YSON formats with random order of maps' keys (don't use in tests)
  24. void NodeToYsonStream(const TNode& node, IOutputStream* output, ::NYson::EYsonFormat format = ::NYson::EYsonFormat::Text);
  25. // Same as the latter, but maps' keys are sorted lexicographically (to be used in tests)
  26. void NodeToCanonicalYsonStream(const TNode& node, IOutputStream* output, ::NYson::EYsonFormat format = ::NYson::EYsonFormat::Text);
  27. // Parse TNode from string in JSON format
  28. TNode NodeFromJsonString(const TStringBuf input);
  29. bool TryNodeFromJsonString(const TStringBuf input, TNode& dst);
  30. // Parse TNode from string in JSON format using an iterative JSON parser.
  31. // Iterative JSON parsers still use the stack, but allocate it on the heap (instead of using the system call stack).
  32. // Needed to mitigate stack overflow with short stacks on deeply nested JSON strings
  33. // (e.g. 256kb of stack when parsing "[[[[[[...]]]]]]" crashes the whole binary).
  34. TNode NodeFromJsonStringIterative(const TStringBuf input, ui64 maxDepth = 1024);
  35. // Convert TJsonValue to TNode
  36. TNode NodeFromJsonValue(const ::NJson::TJsonValue& input);
  37. ////////////////////////////////////////////////////////////////////////////////
  38. } // namespace NYT