json_reader.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. // Allow to read Nan and Inf as integer values
  18. bool AllowReadNanInf = false;
  19. ui64 MaxDepth = 0;
  20. void SetBufferSize(size_t bufferSize);
  21. size_t GetBufferSize() const;
  22. private:
  23. size_t BufferSize;
  24. };
  25. bool ReadJsonTree(TStringBuf in, TJsonValue* out, bool throwOnError = false);
  26. bool ReadJsonTree(TStringBuf in, bool allowComments, TJsonValue* out, bool throwOnError = false);
  27. bool ReadJsonTree(TStringBuf in, const TJsonReaderConfig* config, TJsonValue* out, bool throwOnError = false);
  28. bool ReadJsonTree(IInputStream* in, TJsonValue* out, bool throwOnError = false);
  29. bool ReadJsonTree(IInputStream* in, bool allowComments, TJsonValue* out, bool throwOnError = false);
  30. bool ReadJsonTree(IInputStream* in, const TJsonReaderConfig* config, TJsonValue* out, bool throwOnError = false);
  31. TJsonValue ReadJsonTree(IInputStream* in, bool throwOnError = false);
  32. TJsonValue ReadJsonTree(IInputStream* in, bool allowComments, bool throwOnError);
  33. TJsonValue ReadJsonTree(IInputStream* in, const TJsonReaderConfig* config, bool throwOnError = false);
  34. bool ReadJson(IInputStream* in, TJsonCallbacks* callbacks);
  35. bool ReadJson(IInputStream* in, bool allowComments, TJsonCallbacks* callbacks);
  36. bool ReadJson(IInputStream* in, bool allowComments, bool allowEscapedApostrophe, TJsonCallbacks* callbacks);
  37. bool ReadJson(IInputStream* in, const TJsonReaderConfig* config, TJsonCallbacks* callbacks);
  38. enum ReaderConfigFlags {
  39. NANINF = 0b10000,
  40. ITERATIVE = 0b1000,
  41. COMMENTS = 0b0100,
  42. VALIDATE = 0b0010,
  43. ESCAPE = 0b0001,
  44. };
  45. inline bool ValidateJson(IInputStream* in, const TJsonReaderConfig* config, bool throwOnError = false) {
  46. TJsonCallbacks c(throwOnError);
  47. return ReadJson(in, config, &c);
  48. }
  49. inline bool ValidateJson(TStringBuf in, const TJsonReaderConfig& config = TJsonReaderConfig(), bool throwOnError = false) {
  50. TMemoryInput min(in.data(), in.size());
  51. return ValidateJson(&min, &config, throwOnError);
  52. }
  53. inline bool ValidateJsonThrow(IInputStream* in, const TJsonReaderConfig* config) {
  54. return ValidateJson(in, config, true);
  55. }
  56. inline bool ValidateJsonThrow(TStringBuf in, const TJsonReaderConfig& config = TJsonReaderConfig()) {
  57. return ValidateJson(in, config, true);
  58. }
  59. class TParserCallbacks: public TJsonCallbacks {
  60. public:
  61. TParserCallbacks(TJsonValue& value, bool throwOnError = false, bool notClosedBracketIsError = false);
  62. bool OnNull() override;
  63. bool OnBoolean(bool val) override;
  64. bool OnInteger(long long val) override;
  65. bool OnUInteger(unsigned long long val) override;
  66. bool OnString(const TStringBuf& val) override;
  67. bool OnDouble(double val) override;
  68. bool OnOpenArray() override;
  69. bool OnCloseArray() override;
  70. bool OnOpenMap() override;
  71. bool OnCloseMap() override;
  72. bool OnMapKey(const TStringBuf& val) override;
  73. bool OnEnd() override;
  74. protected:
  75. TJsonValue& Value;
  76. TString Key;
  77. TVector<TJsonValue*> ValuesStack;
  78. bool NotClosedBracketIsError;
  79. enum {
  80. START,
  81. AFTER_MAP_KEY,
  82. IN_MAP,
  83. IN_ARRAY,
  84. FINISH
  85. } CurrentState;
  86. template <class T>
  87. bool SetValue(const T& value) {
  88. switch (CurrentState) {
  89. case START:
  90. Value.SetValue(value);
  91. break;
  92. case AFTER_MAP_KEY:
  93. ValuesStack.back()->InsertValue(Key, value);
  94. CurrentState = IN_MAP;
  95. break;
  96. case IN_ARRAY:
  97. ValuesStack.back()->AppendValue(value);
  98. break;
  99. case IN_MAP:
  100. case FINISH:
  101. return false;
  102. default:
  103. ythrow yexception() << "TParserCallbacks::SetValue invalid enum";
  104. }
  105. return true;
  106. }
  107. bool OpenComplexValue(EJsonValueType type);
  108. bool CloseComplexValue();
  109. };
  110. //// relaxed json, used in library/cpp/scheme
  111. bool ReadJsonFastTree(TStringBuf in, TJsonValue* out, bool throwOnError = false, bool notClosedBracketIsError = false);
  112. TJsonValue ReadJsonFastTree(TStringBuf in, bool notClosedBracketIsError = false);
  113. }