yson2json_adapter.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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(::NYson::TYsonConsumerBase* impl, bool throwException = false);
  19. bool OnNull() override;
  20. bool OnBoolean(bool val) override;
  21. bool OnInteger(long long val) override;
  22. bool OnUInteger(unsigned long long val) override;
  23. bool OnString(const TStringBuf& val) override;
  24. bool OnDouble(double val) override;
  25. bool OnOpenArray() override;
  26. bool OnCloseArray() override;
  27. bool OnOpenMap() override;
  28. bool OnCloseMap() override;
  29. bool OnMapKey(const TStringBuf& val) override;
  30. TState State() const {
  31. return State_;
  32. }
  33. void Reset(const TState& state) {
  34. State_ = state;
  35. }
  36. private:
  37. void WrapIfListItem();
  38. private:
  39. ::NYson::TYsonConsumerBase* Impl_;
  40. TState State_;
  41. };
  42. }