serialize.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include "serialize.h"
  2. #include "node_visitor.h"
  3. #include <library/cpp/yson/consumer.h>
  4. namespace NYT {
  5. ////////////////////////////////////////////////////////////////////////////////
  6. void Serialize(const TString& value, NYson::IYsonConsumer* consumer)
  7. {
  8. consumer->OnStringScalar(value);
  9. }
  10. void Serialize(const TStringBuf& value, NYson::IYsonConsumer* consumer)
  11. {
  12. consumer->OnStringScalar(value);
  13. }
  14. void Serialize(const char* value, NYson::IYsonConsumer* consumer)
  15. {
  16. consumer->OnStringScalar(value);
  17. }
  18. void Deserialize(TString& value, const TNode& node)
  19. {
  20. value = node.AsString();
  21. }
  22. #define SERIALIZE_SIGNED(type) \
  23. void Serialize(type value, NYson::IYsonConsumer* consumer) \
  24. { \
  25. consumer->OnInt64Scalar(static_cast<i64>(value)); \
  26. }
  27. #define SERIALIZE_UNSIGNED(type) \
  28. void Serialize(type value, NYson::IYsonConsumer* consumer) \
  29. { \
  30. consumer->OnUint64Scalar(static_cast<ui64>(value)); \
  31. }
  32. SERIALIZE_SIGNED(signed char)
  33. SERIALIZE_SIGNED(short)
  34. SERIALIZE_SIGNED(int)
  35. SERIALIZE_SIGNED(long)
  36. SERIALIZE_SIGNED(long long)
  37. SERIALIZE_UNSIGNED(unsigned char)
  38. SERIALIZE_UNSIGNED(unsigned short)
  39. SERIALIZE_UNSIGNED(unsigned int)
  40. SERIALIZE_UNSIGNED(unsigned long)
  41. SERIALIZE_UNSIGNED(unsigned long long)
  42. #undef SERIALIZE_SIGNED
  43. #undef SERIALIZE_UNSIGNED
  44. void Deserialize(i64& value, const TNode& node)
  45. {
  46. value = node.AsInt64();
  47. }
  48. void Deserialize(ui64& value, const TNode& node)
  49. {
  50. value = node.AsUint64();
  51. }
  52. void Serialize(double value, NYson::IYsonConsumer* consumer)
  53. {
  54. consumer->OnDoubleScalar(value);
  55. }
  56. void Deserialize(double& value, const TNode& node)
  57. {
  58. value = node.AsDouble();
  59. }
  60. void Serialize(bool value, NYson::IYsonConsumer* consumer)
  61. {
  62. consumer->OnBooleanScalar(value);
  63. }
  64. void Deserialize(bool& value, const TNode& node)
  65. {
  66. value = node.AsBool();
  67. }
  68. void Serialize(const TNode& node, NYson::IYsonConsumer* consumer)
  69. {
  70. TNodeVisitor visitor(consumer);
  71. visitor.Visit(node);
  72. }
  73. void Deserialize(TNode& value, const TNode& node)
  74. {
  75. value = node;
  76. }
  77. ////////////////////////////////////////////////////////////////////////////////
  78. } // namespace NYT