yson2json_adapter.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #pragma once
  2. #include <library/cpp/yson/consumer.h>
  3. #include <library/cpp/json/json_reader.h>
  4. #include <util/generic/stack.h>
  5. namespace NYT {
  6. class TYson2JsonCallbacksAdapter
  7. : public NJson::TJsonCallbacks {
  8. public:
  9. class TState {
  10. private:
  11. // Stores current context stack
  12. // If true - we are in a list
  13. // If false - we are in a map
  14. TStack<bool> ContextStack;
  15. friend class TYson2JsonCallbacksAdapter;
  16. };
  17. public:
  18. TYson2JsonCallbacksAdapter(
  19. ::NYson::TYsonConsumerBase* impl,
  20. bool throwException = false,
  21. ui64 maxDepth = std::numeric_limits<ui64>::max());
  22. bool OnNull() override;
  23. bool OnBoolean(bool val) override;
  24. bool OnInteger(long long val) override;
  25. bool OnUInteger(unsigned long long val) override;
  26. bool OnString(const TStringBuf& val) override;
  27. bool OnDouble(double val) override;
  28. bool OnOpenArray() override;
  29. bool OnCloseArray() override;
  30. bool OnOpenMap() override;
  31. bool OnCloseMap() override;
  32. bool OnMapKey(const TStringBuf& val) override;
  33. TState State() const {
  34. return State_;
  35. }
  36. void Reset(const TState& state) {
  37. State_ = state;
  38. }
  39. private:
  40. void WrapIfListItem();
  41. private:
  42. ::NYson::TYsonConsumerBase* Impl_;
  43. TState State_;
  44. ui64 MaxDepth_;
  45. };
  46. }