yson2json_adapter.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #include "yson2json_adapter.h"
  2. namespace NYT {
  3. TYson2JsonCallbacksAdapter::TYson2JsonCallbacksAdapter(
  4. ::NYson::TYsonConsumerBase* impl,
  5. bool throwException,
  6. ui64 maxDepth)
  7. : NJson::TJsonCallbacks(throwException)
  8. , Impl_(impl)
  9. , MaxDepth_(maxDepth)
  10. {
  11. }
  12. bool TYson2JsonCallbacksAdapter::OnNull() {
  13. WrapIfListItem();
  14. Impl_->OnEntity();
  15. return true;
  16. }
  17. bool TYson2JsonCallbacksAdapter::OnBoolean(bool val) {
  18. WrapIfListItem();
  19. Impl_->OnBooleanScalar(val);
  20. return true;
  21. }
  22. bool TYson2JsonCallbacksAdapter::OnInteger(long long val) {
  23. WrapIfListItem();
  24. Impl_->OnInt64Scalar(val);
  25. return true;
  26. }
  27. bool TYson2JsonCallbacksAdapter::OnUInteger(unsigned long long val) {
  28. WrapIfListItem();
  29. Impl_->OnUint64Scalar(val);
  30. return true;
  31. }
  32. bool TYson2JsonCallbacksAdapter::OnString(const TStringBuf& val) {
  33. WrapIfListItem();
  34. Impl_->OnStringScalar(val);
  35. return true;
  36. }
  37. bool TYson2JsonCallbacksAdapter::OnDouble(double val) {
  38. WrapIfListItem();
  39. Impl_->OnDoubleScalar(val);
  40. return true;
  41. }
  42. bool TYson2JsonCallbacksAdapter::OnOpenArray() {
  43. WrapIfListItem();
  44. State_.ContextStack.push(true);
  45. if (State_.ContextStack.size() > MaxDepth_) {
  46. return false;
  47. }
  48. Impl_->OnBeginList();
  49. return true;
  50. }
  51. bool TYson2JsonCallbacksAdapter::OnCloseArray() {
  52. State_.ContextStack.pop();
  53. Impl_->OnEndList();
  54. return true;
  55. }
  56. bool TYson2JsonCallbacksAdapter::OnOpenMap() {
  57. WrapIfListItem();
  58. State_.ContextStack.push(false);
  59. if (State_.ContextStack.size() > MaxDepth_) {
  60. return false;
  61. }
  62. Impl_->OnBeginMap();
  63. return true;
  64. }
  65. bool TYson2JsonCallbacksAdapter::OnCloseMap() {
  66. State_.ContextStack.pop();
  67. Impl_->OnEndMap();
  68. return true;
  69. }
  70. bool TYson2JsonCallbacksAdapter::OnMapKey(const TStringBuf& val) {
  71. Impl_->OnKeyedItem(val);
  72. return true;
  73. }
  74. void TYson2JsonCallbacksAdapter::WrapIfListItem() {
  75. if (!State_.ContextStack.empty() && State_.ContextStack.top()) {
  76. Impl_->OnListItem();
  77. }
  78. }
  79. }