json_reader.h 5.0 KB

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