Start working on c api

This commit is contained in:
2025-05-22 10:55:15 -04:00
parent 96ef50d52f
commit 6e602d8fd5
6 changed files with 150 additions and 121 deletions

View File

@@ -1,48 +1,49 @@
#include "callbacks.h"
#include "json_value.h"
#include "parser3.h"
#include "weaseljson.h"
#include <simdjson.h>
std::pair<std::string, parser3::Status> runStreaming(std::string copy,
int stride) {
std::pair<std::string, WeaselJsonStatus> runStreaming(std::string copy,
int stride) {
SerializeState state;
auto c = serializeCallbacks();
parser3::Parser3 parser(&c, &state);
if (stride == 0) {
auto s = parser.parse(copy.data(), copy.size());
if (s != parser3::S_AGAIN) {
if (s != WeaselJson_AGAIN) {
return {state.result, s};
}
} else {
for (int i = 0; i < copy.size(); i += stride) {
auto s =
parser.parse(copy.data() + i, std::min<int>(stride, copy.size() - i));
if (s != parser3::S_AGAIN) {
if (s != WeaselJson_AGAIN) {
return {state.result, s};
}
}
}
auto s = parser.parse(nullptr, 0);
if (s != parser3::S_OK) {
if (s != WeaselJson_OK) {
return {state.result, s};
}
return {state.result, parser3::S_OK};
return {state.result, WeaselJson_OK};
}
std::pair<std::string, parser3::Status> runBatch(std::string copy) {
std::pair<std::string, WeaselJsonStatus> runBatch(std::string copy) {
SerializeState state;
auto c = serializeCallbacks();
parser3::Parser3 parser(&c, &state);
auto s = parser.parse(copy.data(), copy.size());
if (s != parser3::S_AGAIN) {
if (s != WeaselJson_AGAIN) {
return {state.result, s};
}
s = parser.parse(nullptr, 0);
if (s != parser3::S_OK) {
if (s != WeaselJson_OK) {
return {state.result, s};
}
return {state.result, parser3::S_OK};
return {state.result, WeaselJson_OK};
}
void testStreaming(std::string const &json) {
@@ -51,15 +52,15 @@ void testStreaming(std::string const &json) {
auto streaming = runStreaming(json, stride);
if (streaming != batch) {
if (streaming.second == batch.second &&
streaming.second != parser3::S_OK) {
streaming.second != WeaselJson_OK) {
// It's ok if the processed data doesn't match if parsing failed
return;
}
printf("streaming: %s, %s\n",
streaming.second == parser3::S_OK ? "accept" : "reject",
streaming.second == WeaselJson_OK ? "accept" : "reject",
streaming.first.c_str());
printf("batch: %s, %s\n",
streaming.second == parser3::S_OK ? "accept" : "reject",
streaming.second == WeaselJson_OK ? "accept" : "reject",
batch.first.c_str());
abort();
}
@@ -67,13 +68,13 @@ void testStreaming(std::string const &json) {
}
void compareWithSimdjson(std::string const &json) {
parser3::Status ours;
WeaselJsonStatus ours;
{
auto copy = json;
auto c = noopCallbacks();
parser3::Parser3 parser3(&c, nullptr);
ours = parser3.parse(copy.data(), copy.size());
if (ours == parser3::S_AGAIN) {
if (ours == WeaselJson_AGAIN) {
ours = parser3.parse(nullptr, 0);
}
}
@@ -83,10 +84,10 @@ void compareWithSimdjson(std::string const &json) {
simdjson::dom::parser parser;
auto doc = parser.parse(my_padded_data);
auto theirs = doc.error();
if (ours == parser3::S_OVERFLOW || theirs == simdjson::DEPTH_ERROR) {
if (ours == WeaselJson_OVERFLOW || theirs == simdjson::DEPTH_ERROR) {
return;
}
if ((ours == parser3::S_OK) != (theirs == simdjson::SUCCESS)) {
if ((ours == WeaselJson_OK) != (theirs == simdjson::SUCCESS)) {
if (json.starts_with("\xef\xbb\xbf")) {
// What to do with byte order mark?
return;