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