Remove on_{begin,end}_{string,number}

And add `done` arg to data callback
This commit is contained in:
2025-05-25 21:01:37 -04:00
parent f92b33eec3
commit f6cd807da3
6 changed files with 95 additions and 154 deletions

View File

@@ -11,18 +11,14 @@ inline WeaselJsonCallbacks printCallbacks() {
WeaselJsonCallbacks result;
result.on_begin_object = +[](void *) { puts("on_begin_object"); };
result.on_end_object = +[](void *) { puts("on_end_object"); };
result.on_begin_string = +[](void *) { puts("on_begin_string"); };
result.on_string_data = +[](void *, const char *buf, int len) {
result.on_string_data = +[](void *, const char *buf, int len, int /*done*/) {
printf("on_string_data `%.*s`\n", len, buf);
};
result.on_end_string = +[](void *) { puts("on_end_string"); };
result.on_begin_array = +[](void *) { puts("on_begin_array"); };
result.on_end_array = +[](void *) { puts("on_end_array"); };
result.on_begin_number = +[](void *) { puts("on_begin_number"); };
result.on_number_data = +[](void *, const char *buf, int len) {
result.on_number_data = +[](void *, const char *buf, int len, int /*done*/) {
printf("on_number_data `%.*s`\n", len, buf);
};
result.on_end_number = +[](void *) { puts("on_end_number"); };
result.on_true_literal = +[](void *) { puts("on_true_literal"); };
result.on_false_literal = +[](void *) { puts("on_false_literal"); };
result.on_null_literal = +[](void *) { puts("on_null_literal"); };
@@ -33,14 +29,10 @@ inline WeaselJsonCallbacks noopCallbacks() {
WeaselJsonCallbacks result;
result.on_begin_object = +[](void *) {};
result.on_end_object = +[](void *) {};
result.on_begin_string = +[](void *) {};
result.on_string_data = +[](void *, const char *, int) {};
result.on_end_string = +[](void *) {};
result.on_string_data = +[](void *, const char *, int, int) {};
result.on_begin_array = +[](void *) {};
result.on_end_array = +[](void *) {};
result.on_begin_number = +[](void *) {};
result.on_number_data = +[](void *, const char *, int) {};
result.on_end_number = +[](void *) {};
result.on_number_data = +[](void *, const char *, int, int) {};
result.on_true_literal = +[](void *) {};
result.on_false_literal = +[](void *) {};
result.on_null_literal = +[](void *) {};
@@ -70,6 +62,7 @@ struct SerializeState {
}
}
std::vector<Cursor> stack;
bool startedData = false;
};
inline WeaselJsonCallbacks serializeCallbacks() {
@@ -85,18 +78,18 @@ inline WeaselJsonCallbacks serializeCallbacks() {
state->stack.pop_back();
state->result.append("}");
};
result.on_begin_string = +[](void *p) {
auto *state = (SerializeState *)p;
state->on_begin_value();
state->result.append("<");
};
result.on_string_data = +[](void *p, const char *buf, int len) {
result.on_string_data = +[](void *p, const char *buf, int len, int done) {
auto *state = (SerializeState *)p;
if (!state->startedData) {
state->startedData = true;
state->on_begin_value();
state->result.append("<");
}
state->result.append(std::string(buf, len));
};
result.on_end_string = +[](void *p) {
auto *state = (SerializeState *)p;
state->result.append(">");
if (done) {
state->startedData = false;
state->result.append(">");
}
};
result.on_begin_array = +[](void *p) {
auto *state = (SerializeState *)p;
@@ -109,18 +102,18 @@ inline WeaselJsonCallbacks serializeCallbacks() {
state->stack.pop_back();
state->result.append("]");
};
result.on_begin_number = +[](void *p) {
auto *state = (SerializeState *)p;
state->on_begin_value();
state->result.append("(");
};
result.on_number_data = +[](void *p, const char *buf, int len) {
result.on_number_data = +[](void *p, const char *buf, int len, int done) {
auto *state = (SerializeState *)p;
if (!state->startedData) {
state->startedData = true;
state->on_begin_value();
state->result.append("(");
}
state->result.append(std::string(buf, len));
};
result.on_end_number = +[](void *p) {
auto *state = (SerializeState *)p;
state->result.append(")");
if (done) {
state->startedData = false;
state->result.append(")");
}
};
result.on_true_literal = +[](void *p) {
auto *state = (SerializeState *)p;