json_value_output.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #pragma once
  2. #include "json_output.h"
  3. #include <library/cpp/json/writer/json_value.h>
  4. #include <util/generic/stack.h>
  5. namespace NProtobufJson {
  6. class TJsonValueOutput: public IJsonOutput {
  7. public:
  8. TJsonValueOutput(NJson::TJsonValue& value)
  9. : Root(value)
  10. {
  11. Context.emplace(TContext::JSON_AFTER_KEY, Root);
  12. }
  13. void DoWrite(const TStringBuf& s) override;
  14. void DoWrite(const TString& s) override;
  15. void DoWrite(int i) override;
  16. void DoWrite(unsigned int i) override;
  17. void DoWrite(long long i) override;
  18. void DoWrite(unsigned long long i) override;
  19. void DoWrite(float f) override;
  20. void DoWrite(double f) override;
  21. void DoWrite(bool b) override;
  22. void DoWriteNull() override;
  23. void DoBeginList() override;
  24. void DoEndList() override;
  25. void DoBeginObject() override;
  26. void DoWriteKey(const TStringBuf& key) override;
  27. void DoEndObject() override;
  28. void DoWriteRawJson(const TStringBuf& str) override;
  29. private:
  30. template <typename T>
  31. void WriteImpl(const T& t);
  32. struct TContext {
  33. enum EType {
  34. JSON_MAP,
  35. JSON_ARRAY,
  36. JSON_AFTER_KEY,
  37. };
  38. TContext(EType type, NJson::TJsonValue& value)
  39. : Type(type)
  40. , Value(value)
  41. {
  42. }
  43. EType Type;
  44. NJson::TJsonValue& Value;
  45. };
  46. NJson::TJsonValue& Root;
  47. TStack<TContext, TVector<TContext>> Context;
  48. };
  49. }