Simplify and clean up
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
#include "json_commit_request_parser.hpp"
|
||||
#include "json_token_enum.hpp"
|
||||
#include <cassert>
|
||||
#include <charconv>
|
||||
#include <cstring>
|
||||
#include <simdutf.h>
|
||||
@@ -467,13 +468,15 @@ bool JsonCommitRequestParser::begin_streaming_parse(CommitRequest &request) {
|
||||
if (!parser_context_) {
|
||||
parser_context_ = std::make_unique<ParserContext>(&request.arena());
|
||||
} else {
|
||||
parser_context_->reset_arena_memory(&request.arena());
|
||||
parser_context_->attach_arena(&request.arena());
|
||||
parser_context_->parse_error = nullptr;
|
||||
parser_context_->parse_complete = false;
|
||||
parser_context_->has_read_version_been_set = false;
|
||||
}
|
||||
|
||||
if (json_parser_) {
|
||||
if (!json_parser_) {
|
||||
json_parser_ = WeaselJsonParser_create(64, &json_callbacks, this, 0);
|
||||
} else {
|
||||
WeaselJsonParser_reset(json_parser_);
|
||||
}
|
||||
|
||||
@@ -482,6 +485,7 @@ bool JsonCommitRequestParser::begin_streaming_parse(CommitRequest &request) {
|
||||
|
||||
JsonCommitRequestParser::ParseStatus
|
||||
JsonCommitRequestParser::parse_chunk(char *data, size_t len) {
|
||||
assert(len != 0);
|
||||
if (!json_parser_ || !parser_context_) {
|
||||
return ParseStatus::Error;
|
||||
}
|
||||
@@ -503,12 +507,15 @@ JsonCommitRequestParser::parse_chunk(char *data, size_t len) {
|
||||
case WeaselJson_AGAIN:
|
||||
return ParseStatus::Incomplete;
|
||||
case WeaselJson_REJECT:
|
||||
parser_context_->parse_error = "JSON parsing failed - Invalid JSON";
|
||||
return ParseStatus::Error;
|
||||
case WeaselJson_OVERFLOW:
|
||||
default:
|
||||
parser_context_->parse_error =
|
||||
"JSON parsing failed - invalid or oversized JSON";
|
||||
parser_context_->parse_error = "JSON parsing failed - JSON nested too deep";
|
||||
return ParseStatus::Error;
|
||||
}
|
||||
parser_context_->parse_error =
|
||||
"JSON parsing failed - unknown weasel json status";
|
||||
return ParseStatus::Error;
|
||||
}
|
||||
|
||||
JsonCommitRequestParser::ParseStatus
|
||||
@@ -526,8 +533,6 @@ JsonCommitRequestParser::finish_streaming_parse() {
|
||||
|
||||
if (status == WeaselJson_OK && parser_context_->parse_complete &&
|
||||
!parser_context_->parse_error) {
|
||||
// Clear the memory used only during parsing
|
||||
parser_context_->reset_arena_memory(¤t_request_->arena());
|
||||
return ParseStatus::Complete;
|
||||
} else {
|
||||
parser_context_->parse_error =
|
||||
@@ -536,14 +541,10 @@ JsonCommitRequestParser::finish_streaming_parse() {
|
||||
}
|
||||
}
|
||||
|
||||
bool JsonCommitRequestParser::has_parse_error() const {
|
||||
return parser_context_ && parser_context_->parse_error != nullptr;
|
||||
}
|
||||
|
||||
const char *JsonCommitRequestParser::get_parse_error() const {
|
||||
return parser_context_ ? parser_context_->parse_error : nullptr;
|
||||
}
|
||||
|
||||
bool JsonCommitRequestParser::has_read_version_been_set() const {
|
||||
return parser_context_ && parser_context_->has_read_version_been_set;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,7 +77,7 @@ private:
|
||||
has_read_version_been_set = false;
|
||||
}
|
||||
|
||||
void reset_arena_memory(ArenaAllocator *arena) {
|
||||
void attach_arena(ArenaAllocator *arena) {
|
||||
current_key = ArenaString{ArenaStlAllocator<char>(arena)};
|
||||
current_string = ArenaString{ArenaStlAllocator<char>(arena)};
|
||||
current_number = ArenaString{ArenaStlAllocator<char>(arena)};
|
||||
@@ -116,7 +116,6 @@ public:
|
||||
bool begin_streaming_parse(CommitRequest &request) override;
|
||||
ParseStatus parse_chunk(char *data, size_t len) override;
|
||||
ParseStatus finish_streaming_parse() override;
|
||||
bool has_parse_error() const override;
|
||||
const char *get_parse_error() const override;
|
||||
|
||||
/**
|
||||
|
||||
@@ -38,7 +38,7 @@ public:
|
||||
/**
|
||||
* @brief Parse additional data incrementally.
|
||||
* @param data Pointer to the data buffer
|
||||
* @param len Length of the data
|
||||
* @param len Length of the data. Must be non-zero.
|
||||
* @return ParseStatus indicating current parse state
|
||||
*/
|
||||
virtual ParseStatus parse_chunk(char *data, size_t len) = 0;
|
||||
@@ -53,11 +53,11 @@ public:
|
||||
* @brief Check if there was a parse error.
|
||||
* @return true if there was a parse error
|
||||
*/
|
||||
virtual bool has_parse_error() const = 0;
|
||||
bool has_parse_error() const { return get_parse_error(); };
|
||||
|
||||
/**
|
||||
* @brief Get the parse error message if there was an error.
|
||||
* @return Error message string, or nullptr if no error
|
||||
*/
|
||||
virtual const char *get_parse_error() const = 0;
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user