json_reader.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #pragma once
  2. #include "json_value.h"
  3. #include <library/cpp/json/common/defs.h>
  4. #include <library/cpp/json/fast_sax/parser.h>
  5. #include <util/generic/yexception.h>
  6. #include <util/stream/input.h>
  7. #include <util/stream/str.h>
  8. #include <util/stream/mem.h>
  9. namespace NJson {
  10. struct TJsonReaderConfig {
  11. TJsonReaderConfig();
  12. bool UseIterativeParser = false;
  13. // js-style comments (both // and /**/)
  14. bool AllowComments = false;
  15. bool DontValidateUtf8 = false;
  16. bool AllowEscapedApostrophe = false;
  17. ui64 MaxDepth = 0;
  18. void SetBufferSize(size_t bufferSize);
  19. size_t GetBufferSize() const;
  20. private:
  21. size_t BufferSize;
  22. };
  23. bool ReadJsonTree(TStringBuf in, TJsonValue* out, bool throwOnError = false);
  24. bool ReadJsonTree(TStringBuf in, bool allowComments, TJsonValue* out, bool throwOnError = false);
  25. bool ReadJsonTree(TStringBuf in, const TJsonReaderConfig* config, TJsonValue* out, bool throwOnError = false);
  26. bool ReadJsonTree(IInputStream* in, TJsonValue* out, bool throwOnError = false);
  27. bool ReadJsonTree(IInputStream* in, bool allowComments, TJsonValue* out, bool throwOnError = false);
  28. bool ReadJsonTree(IInputStream* in, const TJsonReaderConfig* config, TJsonValue* out, bool throwOnError = false);
  29. TJsonValue ReadJsonTree(IInputStream* in, bool throwOnError = false);
  30. TJsonValue ReadJsonTree(IInputStream* in, bool allowComments, bool throwOnError);
  31. TJsonValue ReadJsonTree(IInputStream* in, const TJsonReaderConfig* config, bool throwOnError = false);
  32. bool ReadJson(IInputStream* in, TJsonCallbacks* callbacks);
  33. bool ReadJson(IInputStream* in, bool allowComments, TJsonCallbacks* callbacks);
  34. bool ReadJson(IInputStream* in, bool allowComments, bool allowEscapedApostrophe, TJsonCallbacks* callbacks);
  35. bool ReadJson(IInputStream* in, const TJsonReaderConfig* config, TJsonCallbacks* callbacks);
  36. enum ReaderConfigFlags {
  37. ITERATIVE = 0b1000,
  38. COMMENTS = 0b0100,
  39. VALIDATE = 0b0010,
  40. ESCAPE = 0b0001,
  41. };
  42. inline bool ValidateJson(IInputStream* in, const TJsonReaderConfig* config, bool throwOnError = false) {
  43. TJsonCallbacks c(throwOnError);
  44. return ReadJson(in, config, &c);
  45. }
  46. inline bool ValidateJson(TStringBuf in, const TJsonReaderConfig& config = TJsonReaderConfig(), bool throwOnError = false) {
  47. TMemoryInput min(in.data(), in.size());
  48. return ValidateJson(&min, &config, throwOnError);
  49. }
  50. inline bool ValidateJsonThrow(IInputStream* in, const TJsonReaderConfig* config) {
  51. return ValidateJson(in, config, true);
  52. }
  53. inline bool ValidateJsonThrow(TStringBuf in, const TJsonReaderConfig& config = TJsonReaderConfig()) {
  54. return ValidateJson(in, config, true);
  55. }
  56. class TParserCallbacks: public TJsonCallbacks {
  57. public:
  58. TParserCallbacks(TJsonValue& value, bool throwOnError = false, bool notClosedBracketIsError = false);
  59. bool OnNull() override;
  60. bool OnBoolean(bool val) override;
  61. bool OnInteger(long long val) override;
  62. bool OnUInteger(unsigned long long val) override;
  63. bool OnString(const TStringBuf& val) override;
  64. bool OnDouble(double val) override;
  65. bool OnOpenArray() override;
  66. bool OnCloseArray() override;
  67. bool OnOpenMap() override;
  68. bool OnCloseMap() override;
  69. bool OnMapKey(const TStringBuf& val) override;
  70. bool OnEnd() override;
  71. protected:
  72. TJsonValue& Value;
  73. TString Key;
  74. TVector<TJsonValue*> ValuesStack;
  75. bool NotClosedBracketIsError;
  76. enum {
  77. START,
  78. AFTER_MAP_KEY,
  79. IN_MAP,
  80. IN_ARRAY,
  81. FINISH
  82. } CurrentState;
  83. template <class T>
  84. bool SetValue(const T& value) {
  85. switch (CurrentState) {
  86. case START:
  87. Value.SetValue(value);
  88. break;
  89. case AFTER_MAP_KEY:
  90. ValuesStack.back()->InsertValue(Key, value);
  91. CurrentState = IN_MAP;
  92. break;
  93. case IN_ARRAY:
  94. ValuesStack.back()->AppendValue(value);
  95. break;
  96. case IN_MAP:
  97. case FINISH:
  98. return false;
  99. default:
  100. ythrow yexception() << "TParserCallbacks::SetValue invalid enum";
  101. }
  102. return true;
  103. }
  104. bool OpenComplexValue(EJsonValueType type);
  105. bool CloseComplexValue();
  106. };
  107. //// relaxed json, used in library/cpp/scheme
  108. bool ReadJsonFastTree(TStringBuf in, TJsonValue* out, bool throwOnError = false, bool notClosedBracketIsError = false);
  109. TJsonValue ReadJsonFastTree(TStringBuf in, bool notClosedBracketIsError = false);
  110. }