aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOnur Gündüz <ogunduz@gmail.com>2026-06-25 14:00:54 +0200
committerOnur Gündüz <ogunduz@gmail.com>2026-06-25 14:01:11 +0200
commit13dd2132310fb4dc5aa3dd217ed62f2e8301b981 (patch)
tree0e6f5dfdc686365af06fb59d32957e47a3ca5a79
parentb673eb9a72138b89f1efecf52578020cb43857ff (diff)
downloadffwd-13dd2132310fb4dc5aa3dd217ed62f2e8301b981.tar.gz
ffwd-13dd2132310fb4dc5aa3dd217ed62f2e8301b981.zip
all: migrate to yyjson
-rw-r--r--.github/workflows/release.yml4
-rw-r--r--README.md7
-rw-r--r--bench/Makefile11
-rw-r--r--build.mk22
-rwxr-xr-xdevtools/container-build-test.sh17
-rw-r--r--libffwd/Makefile5
-rw-r--r--libffwd/bpe.c2
-rw-r--r--libffwd/config.c2
-rw-r--r--tests/Makefile19
-rw-r--r--tools/cli/Makefile2
-rw-r--r--tools/server/Makefile4
-rw-r--r--tools/server/encode.c3
-rw-r--r--tools/server/handlers.c253
-rw-r--r--tools/server/json.c121
-rw-r--r--tools/server/models.c28
-rw-r--r--tools/server/schedule.c16
-rw-r--r--tools/server/server.c2
-rw-r--r--tools/server/server_internal.h40
-rw-r--r--tools/server/test_json.c119
19 files changed, 354 insertions, 323 deletions
diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml
index 60d848c..8ebe776 100644
--- a/.github/workflows/release.yml
+++ b/.github/workflows/release.yml
@@ -42,7 +42,7 @@ jobs:
clang \
clang-tidy \
clang-tools \
- libcjson-dev \
+ libyyjson-dev \
libopenblas-dev \
pkg-config \
valgrind
@@ -50,7 +50,7 @@ jobs:
cc --version | sed -n '1,2p'
clang --version | sed -n '1,2p'
clang-tidy --version | sed -n '1,2p'
- pkg-config --modversion libcjson 2>/dev/null || true
+ pkg-config --modversion libyyjson 2>/dev/null || true
- name: Cache ccache
uses: actions/cache@v4
diff --git a/README.md b/README.md
index fbc4dc3..a6a4fbc 100644
--- a/README.md
+++ b/README.md
@@ -30,11 +30,8 @@ architectures include:
Before building, install the dependencies for your platform:
-- On Linux, install `libopenblas-dev`.
-- On macOS, install `mlx` and `mlx-c` through Homebrew.
-
-The server also needs `libcjson-dev`/`cjson`. You can skip it if you only want
-`libffwd` and the command-line tool.
+- On Linux, install `libopenblas-dev` and `libyyjson-dev`.
+- On macOS, install `mlx`, `mlx-c` and `yyjson` through Homebrew.
Once the dependencies are in place, run `make` to build the libraries and tools.
diff --git a/bench/Makefile b/bench/Makefile
index 9eeaa95..948dab3 100644
--- a/bench/Makefile
+++ b/bench/Makefile
@@ -4,6 +4,7 @@ include ../build.mk
CFLAGS += -std=c11
BLAS_LIBS := $(HOST_BLAS_LDFLAGS)
+JSON_LIBS := $(YYJSON_LDFLAGS)
# Benchmarks link the blas libffwd archive from the per-config build tree and
# include its headers.
@@ -30,24 +31,24 @@ all: $(BENCHES)
libffwd-blas:
$(MAKE) -C $(LIBFFWD_DIR) BACKEND=blas
-$(eval $(call c_program_deps,bench_kernels,bench_kernels.c,bench.h libffwd-blas,$(LIBFFWD_A),-pthread -I$(LIBFFWD_DIR),$(BLAS_LIBS) -lm))
+$(eval $(call c_program_deps,bench_kernels,bench_kernels.c,bench.h libffwd-blas,$(LIBFFWD_A),-pthread -I$(LIBFFWD_DIR),$(BLAS_LIBS) $(JSON_LIBS) -lm))
-$(eval $(call c_program_deps,bench_tokenizer,bench_tokenizer.c,libffwd-blas,$(LIBFFWD_A),-I$(LIBFFWD_DIR),-lm))
+$(eval $(call c_program_deps,bench_tokenizer,bench_tokenizer.c,libffwd-blas,$(LIBFFWD_A),-I$(LIBFFWD_DIR),$(JSON_LIBS) -lm))
# The server utilities (base64, sbuf, util) depend on no library, so the bench
# compiles those sources directly here and keeps the build self-contained.
SERVER_UTIL_SRCS := $(SERVER_DIR)/base64.c $(SERVER_DIR)/sbuf.c $(SERVER_DIR)/util.c
$(eval $(call c_program_deps,bench_server_utils,bench_server_utils.c $(SERVER_UTIL_SRCS),bench.h,,-I$(SERVER_DIR),-lm))
-$(eval $(call c_program_deps,bench_model,bench_model.c,bench.h libffwd-blas,$(LIBFFWD_A),-pthread -I$(LIBFFWD_DIR),$(BLAS_LIBS) -lm))
+$(eval $(call c_program_deps,bench_model,bench_model.c,bench.h libffwd-blas,$(LIBFFWD_A),-pthread -I$(LIBFFWD_DIR),$(BLAS_LIBS) $(JSON_LIBS) -lm))
-$(eval $(call c_program_deps,bench_late_rerank,bench_late_rerank.c,libffwd-blas,$(LIBFFWD_A),-pthread -I$(LIBFFWD_DIR),$(BLAS_LIBS) -lm))
+$(eval $(call c_program_deps,bench_late_rerank,bench_late_rerank.c,libffwd-blas,$(LIBFFWD_A),-pthread -I$(LIBFFWD_DIR),$(BLAS_LIBS) $(JSON_LIBS) -lm))
# MLX rerank benchmark: the blas engine archive for the CPU reference paths plus
# mlx.c for the device API. The archive calls cblas, so the bench links
# Accelerate/OpenBLAS itself ($(HOST_BLAS_LDFLAGS)) on top of Metal/MLX. macOS only.
MLX_DRIVER_CFLAGS := -pthread -DCHECK_MLX -I$(MLXC_PREFIX)/include -I$(LIBFFWD_DIR) $(HOST_BLAS_CFLAGS)
-MLX_DRIVER_LDLIBS := $(MLX_BACKEND_LDFLAGS) $(HOST_BLAS_LDFLAGS) -lm
+MLX_DRIVER_LDLIBS := $(MLX_BACKEND_LDFLAGS) $(HOST_BLAS_LDFLAGS) $(JSON_LIBS) -lm
$(eval $(call c_program_deps,bench_late_rerank_mlx,bench_late_rerank.c $(LIBFFWD_DIR)/mlx.c,libffwd-blas,$(LIBFFWD_A),$(MLX_DRIVER_CFLAGS),$(MLX_DRIVER_LDLIBS)))
bench: bench_kernels
diff --git a/build.mk b/build.mk
index 109f2fb..667cf03 100644
--- a/build.mk
+++ b/build.mk
@@ -34,18 +34,18 @@ ifeq ($(strip $(OPENBLAS_LDFLAGS)),)
OPENBLAS_LDFLAGS = -lopenblas
endif
-CJSON_CFLAGS ?= $(shell sh -c 'pkg-config --cflags libcjson 2>/dev/null')
-CJSON_LDFLAGS ?= $(shell sh -c 'pkg-config --libs libcjson 2>/dev/null')
+YYJSON_CFLAGS ?= $(shell sh -c 'pkg-config --cflags yyjson 2>/dev/null')
+YYJSON_LDFLAGS ?= $(shell sh -c 'pkg-config --libs yyjson 2>/dev/null')
ifeq ($(OS),Darwin)
-ifeq ($(strip $(CJSON_CFLAGS)),)
- CJSON_CFLAGS = -I/opt/homebrew/include -I/usr/local/include
+ifeq ($(strip $(YYJSON_CFLAGS)),)
+ YYJSON_CFLAGS = -I/opt/homebrew/include -I/usr/local/include
endif
-ifeq ($(strip $(CJSON_LDFLAGS)),)
- CJSON_LDFLAGS = -L/opt/homebrew/lib -L/usr/local/lib -lcjson
+ifeq ($(strip $(YYJSON_LDFLAGS)),)
+ YYJSON_LDFLAGS = -L/opt/homebrew/lib -L/usr/local/lib -lyyjson
endif
endif
-ifeq ($(strip $(CJSON_LDFLAGS)),)
- CJSON_LDFLAGS = -lcjson
+ifeq ($(strip $(YYJSON_LDFLAGS)),)
+ YYJSON_LDFLAGS = -lyyjson
endif
MLX_PREFIX := $(shell brew --prefix mlx 2>/dev/null)
@@ -154,9 +154,7 @@ else
CFLAGS = $(OPT)
endif
CPPFLAGS ?=
-# Vendored yyjson (deps/yyjson) is the one JSON parser/writer for the whole
-# tree; every component includes its header from here.
-CPPFLAGS += -I$(ROOT)/deps/yyjson
+CPPFLAGS += $(YYJSON_CFLAGS)
LDLIBS ?=
SHARED_LDLIBS ?=
@@ -233,7 +231,7 @@ $(1): $(2) $(3)
endef
export BACKEND MODE ARCH_FLAGS CFLAGS_BASE OPT LDFLAGS_OPT CUDA_HOME NVCC CUDAFLAGS
-export OPENBLAS_CFLAGS OPENBLAS_LDFLAGS CJSON_CFLAGS CJSON_LDFLAGS
+export OPENBLAS_CFLAGS OPENBLAS_LDFLAGS YYJSON_CFLAGS YYJSON_LDFLAGS
export MLX_PREFIX MLXC_PREFIX
export HOST_BLAS_CFLAGS HOST_BLAS_LDFLAGS
export BLAS_BACKEND_CFLAGS BLAS_BACKEND_LDFLAGS
diff --git a/devtools/container-build-test.sh b/devtools/container-build-test.sh
index d2fecc8..3437868 100755
--- a/devtools/container-build-test.sh
+++ b/devtools/container-build-test.sh
@@ -1,11 +1,10 @@
#!/usr/bin/env bash
-# Build, test and bench the OpenBLAS backend in a throwaway Ubuntu container to
-# catch Linux portability breakage a macOS/Accelerate build hides. Needs Apple's
-# `container` CLI running. Run from anywhere in the repo: it finds the repo root
-# via git, bind-mounts it at /work, installs the Runpod toolchain, re-execs
-# itself inside with --in-container, and runs make cpu/test/bench/bench-server-
-# utils/debug, then `make clean` (skip with KEEP=1). Override the image with
-# IMAGE=ubuntu:22.04. Exits non-zero at the first failing step.
+# Build, test and bench the OpenBLAS backend in an Ubuntu container to catch
+# Linux portability breakage a macOS/Accelerate build hides.
+# It finds the repo root via git, bind-mounts it at /work, installs the Runpod
+# toolchain, re-execs itself inside with --in-container, and runs
+# make cpu/test/bench/bench-server-utils/debug, then `make clean`.
+# Skip clean with KEEP=1. Override the image with IMAGE=ubuntu:22.04.
set -euo pipefail
IMAGE="${IMAGE:-ubuntu:24.04}"
@@ -20,13 +19,13 @@ run_in_container() {
# export map (ffwd.map) is generated at build time by devtools/gen_exports.py
# (see libffwd/Makefile), so `make cpu` fails without it.
apt-get install -y --no-install-recommends \
- build-essential ca-certificates libcjson-dev libopenblas-dev \
+ build-essential ca-certificates libyyjson-dev libopenblas-dev \
pkg-config git python3 >/dev/null
echo "==================== system / toolchain ===================="
uname -a
cc --version | sed -n '1p'
- echo "libcjson $(pkg-config --modversion libcjson 2>/dev/null || echo MISSING) :: $(pkg-config --cflags --libs libcjson 2>/dev/null)"
+ echo "libyyjson $(pkg-config --modversion libyyjson 2>/dev/null || echo MISSING) :: $(pkg-config --cflags --libs libyyjson 2>/dev/null)"
echo "openblas $(pkg-config --modversion openblas 2>/dev/null || echo MISSING) :: $(pkg-config --cflags --libs openblas 2>/dev/null)"
cd /work
diff --git a/libffwd/Makefile b/libffwd/Makefile
index b9349c3..70ec9ad 100644
--- a/libffwd/Makefile
+++ b/libffwd/Makefile
@@ -14,9 +14,6 @@ SHARED_LDFLAGS += $(EXPORT_FLAGS)
# Tokenizer, config, safetensors reader, and L2/cosine helpers: every backend.
COMMON_SRCS := config.c vec.c tokenize.c bpe.c sentencepiece.c wordpiece.c safetensors.c
-# Vendored JSON library, compiled into libffwd so config/tokenizer loading and
-# every consumer (CLI, server) share one parser instead of hand-rolled scanners.
-COMMON_SRCS += ../deps/yyjson/yyjson.c
# CPU math kernels and SIMD dispatch. generic.c is always built; avx.c and
# neon.c each guard their entire body by arch (__AVX2__ / __ARM_NEON), so the
@@ -47,7 +44,7 @@ DEPS := $(call deps,$(OBJS))
all: $(STATIC) $(SHARED)
$(eval $(call static_archive,$(STATIC),$(OBJS)))
-$(eval $(call shared_library,$(SHARED),$(OBJS),,$(BACKEND_LDFLAGS) -lm))
+$(eval $(call shared_library,$(SHARED),$(OBJS),,$(BACKEND_LDFLAGS) $(YYJSON_LDFLAGS) -lm))
# The export map is generated from the public ABI (the FFWD_API declarations in
# ffwd.h) instead of being committed, so it cannot drift from the header. It is
# regenerated when the header or the generator changes, and the shared library
diff --git a/libffwd/bpe.c b/libffwd/bpe.c
index 9b8b471..b3c8a8c 100644
--- a/libffwd/bpe.c
+++ b/libffwd/bpe.c
@@ -1,6 +1,6 @@
#include "bpe.h"
-#include "yyjson.h"
+#include <yyjson.h>
#include <limits.h>
#include <stdint.h>
diff --git a/libffwd/config.c b/libffwd/config.c
index ac106bf..6531658 100644
--- a/libffwd/config.c
+++ b/libffwd/config.c
@@ -1,6 +1,6 @@
#include "config.h"
-#include "yyjson.h"
+#include <yyjson.h>
#include <errno.h>
#include <limits.h>
diff --git a/tests/Makefile b/tests/Makefile
index 92540c3..ded6fed 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -4,6 +4,7 @@ include ../build.mk
CFLAGS += -std=c11
BLAS_LIBS := $(HOST_BLAS_LDFLAGS)
+JSON_LIBS := $(YYJSON_LDFLAGS)
# Every test links the blas libffwd archive (built into the per-config build
# tree by the libffwd-blas phony) and includes its headers; only the fixture
@@ -57,33 +58,33 @@ late-check-driver: late_check
late-check-driver-mlx: late_check_mlx
$(foreach t,$(TOK_TESTS),\
- $(eval $(call c_program_deps,$(t),$(t).c,libffwd-blas,$(LIBFFWD_A),-I$(LIBFFWD_DIR),-lm)))
+ $(eval $(call c_program_deps,$(t),$(t).c,libffwd-blas,$(LIBFFWD_A),-I$(LIBFFWD_DIR),$(JSON_LIBS) -lm)))
$(foreach t,$(ST_TESTS),\
- $(eval $(call c_program_deps,$(t),$(t).c,libffwd-blas,$(LIBFFWD_A),-I$(LIBFFWD_DIR),)))
+ $(eval $(call c_program_deps,$(t),$(t).c,libffwd-blas,$(LIBFFWD_A),-I$(LIBFFWD_DIR),$(JSON_LIBS))))
$(foreach t,$(KERNEL_TESTS),\
- $(eval $(call c_program_deps,$(t),$(t).c,libffwd-blas,$(LIBFFWD_A),-pthread -I$(LIBFFWD_DIR),$(BLAS_LIBS) -lm)))
+ $(eval $(call c_program_deps,$(t),$(t).c,libffwd-blas,$(LIBFFWD_A),-pthread -I$(LIBFFWD_DIR),$(BLAS_LIBS) $(JSON_LIBS) -lm)))
# Generic (no USE_BLAS) kernel build: compile the kernel sources directly so the
# scalar/SIMD paths are linked instead of cblas; no Accelerate/OpenBLAS needed.
$(eval $(call c_program_deps,test_kernels_generic,test_kernels.c $(KERNEL_SRCS),,,-pthread -I$(LIBFFWD_DIR),-lm))
$(foreach t,$(FFWD_TESTS),\
- $(eval $(call c_program_deps,$(t),$(t).c,libffwd-blas,$(LIBFFWD_A),-pthread -I$(LIBFFWD_DIR),$(BLAS_LIBS) -lm)))
+ $(eval $(call c_program_deps,$(t),$(t).c,libffwd-blas,$(LIBFFWD_A),-pthread -I$(LIBFFWD_DIR),$(BLAS_LIBS) $(JSON_LIBS) -lm)))
$(foreach t,$(PARITY_TOK_DRIVERS),\
- $(eval $(call c_program_deps,$(t),$(t).c,libffwd-blas,$(LIBFFWD_A),-I$(LIBFFWD_DIR),-lm)))
+ $(eval $(call c_program_deps,$(t),$(t).c,libffwd-blas,$(LIBFFWD_A),-I$(LIBFFWD_DIR),$(JSON_LIBS) -lm)))
$(foreach t,$(FFWD_DRIVERS),\
- $(eval $(call c_program_deps,$(t),$(t).c,libffwd-blas,$(LIBFFWD_A),-pthread -I$(LIBFFWD_DIR),$(BLAS_LIBS) -lm)))
+ $(eval $(call c_program_deps,$(t),$(t).c,libffwd-blas,$(LIBFFWD_A),-pthread -I$(LIBFFWD_DIR),$(BLAS_LIBS) $(JSON_LIBS) -lm)))
# The MLX driver variants reuse the BLAS engine archive for the CPU reference
# paths (e.g. ffwd_late_maxsim) and add mlx.c for the device API. The archive
# calls cblas, so the driver links Accelerate/OpenBLAS itself ($(HOST_BLAS_LDFLAGS))
# on top of the Metal/MLX libraries. macOS only, built on demand.
MLX_DRIVER_CFLAGS := -pthread -DCHECK_MLX -I$(MLXC_PREFIX)/include -I$(LIBFFWD_DIR) $(HOST_BLAS_CFLAGS)
-MLX_DRIVER_LDLIBS := $(MLX_BACKEND_LDFLAGS) $(HOST_BLAS_LDFLAGS) -lm
+MLX_DRIVER_LDLIBS := $(MLX_BACKEND_LDFLAGS) $(HOST_BLAS_LDFLAGS) $(JSON_LIBS) -lm
$(eval $(call c_program_deps,contextual_batch_mlx,contextual_batch.c $(LIBFFWD_DIR)/mlx.c,libffwd-blas,$(LIBFFWD_A),$(MLX_DRIVER_CFLAGS),$(MLX_DRIVER_LDLIBS)))
@@ -102,8 +103,8 @@ COV_ROOT := ..
COV_DIR := coverage
COV_OUT := tests/$(COV_DIR)
COV_FLAGS := -fprofile-instr-generate -fcoverage-mapping -O0
-COV_CFLAGS := -Wall -Wextra -Ilibffwd -Itests -pthread $(HOST_BLAS_CFLAGS) $(COV_FLAGS)
-COV_LDLIBS := $(BLAS_LIBS) -lm
+COV_CFLAGS := -Wall -Wextra -Ilibffwd -Itests -pthread $(HOST_BLAS_CFLAGS) $(YYJSON_CFLAGS) $(COV_FLAGS)
+COV_LDLIBS := $(BLAS_LIBS) $(JSON_LIBS) -lm
# Report library sources only: drop the test harnesses (test_*), the fixture
# headers, and system headers.
COV_IGNORE := -ignore-filename-regex='test_|_fixture|tiny_model|/opt/|/usr/'
diff --git a/tools/cli/Makefile b/tools/cli/Makefile
index cc076f1..47f697a 100644
--- a/tools/cli/Makefile
+++ b/tools/cli/Makefile
@@ -9,7 +9,7 @@ LIBFFWD := $(ROOT)/libffwd
BIN := $(OUTDIR)/ffwd-cli
SRCS := main.c
LIBS := $(OUTDIR)/libffwd.a
-LDLIBS := $(BACKEND_LDFLAGS) -lm
+LDLIBS := $(BACKEND_LDFLAGS) $(YYJSON_LDFLAGS) -lm
CFLAGS += -std=gnu11 -pthread
CPPFLAGS += -I. -I$(LIBFFWD)
diff --git a/tools/server/Makefile b/tools/server/Makefile
index 4a11b0d..34c8608 100644
--- a/tools/server/Makefile
+++ b/tools/server/Makefile
@@ -15,10 +15,10 @@ TEST_BINS := test_base64 test_sbuf test_encode test_json test_schedule test_mode
TEST_OBJS := $(filter-out $(OBJDIR)/main.o,$(OBJS))
LIBS := $(OUTDIR)/libffwd.a $(OUTDIR)/libae.a
-LDLIBS := $(CJSON_LDFLAGS) $(BACKEND_LDFLAGS) -lm
+LDLIBS := $(YYJSON_LDFLAGS) $(BACKEND_LDFLAGS) -lm
CFLAGS += -std=gnu11 -pthread
-CPPFLAGS += -I. -I$(LIBFFWD) -I$(AE) $(CJSON_CFLAGS)
+CPPFLAGS += -I. -I$(LIBFFWD) -I$(AE)
$(OBJDIR)/main.o: CFLAGS += $(VERSION_CFLAGS)
diff --git a/tools/server/encode.c b/tools/server/encode.c
index 61ff4ea..1674774 100644
--- a/tools/server/encode.c
+++ b/tools/server/encode.c
@@ -68,6 +68,9 @@ static void append_embedding_object(sbuf *b, int index, const char *embedding) {
void append_embedding_value(
sbuf *b, int index, const float *emb, int dims, const char *encoding, embedding_api_t api) {
if (!strcmp(encoding, "float")) {
+ /* Floats are hand-formatted with %.9g (the shortest form that
+ * round-trips float32) and streamed into the sbuf, never serialized
+ * through yyjson's writer. */
sbuf_printf(b, "{\"object\":\"embedding\",\"index\":%d,\"embedding\":[", index);
for (int i = 0; i < dims; i++) {
if (i)
diff --git a/tools/server/handlers.c b/tools/server/handlers.c
index fed4671..2c98077 100644
--- a/tools/server/handlers.c
+++ b/tools/server/handlers.c
@@ -249,22 +249,21 @@ void execute_contextual_request_list(contextual_request **reqs, int n_reqs) {
free(items);
}
-static int validate_common(cJSON *root,
- cJSON *detail,
+static int validate_common(yyjson_val *root,
+ yyjson_mut_doc *detail,
model_kind expected_kind,
loaded_model **out_model,
int *out_dims,
const char **out_encoding,
http_server *s) {
- cJSON *model_item = cJSON_GetObjectItemCaseSensitive(root, "model");
+ yyjson_val *model_item = yyjson_obj_get(root, "model");
const char *model_id = NULL;
loaded_model *m = NULL;
if (!model_item) {
ve_add(detail, "[\"body\",\"model\"]", "field required", "missing");
- } else if (!cJSON_IsString(model_item) || !model_item->valuestring) {
+ } else if (!(model_id = yyjson_get_str(model_item))) {
ve_add(detail, "[\"body\",\"model\"]", "model must be a string", "type_error.string");
} else {
- model_id = model_item->valuestring;
m = loaded_model_for_label(s, model_id);
if (!m) {
ve_add(detail, "[\"body\",\"model\"]",
@@ -284,12 +283,13 @@ static int validate_common(cJSON *root,
return 0;
}
-void prepare_embedding_request(job *j, cJSON *root, http_server *s, embedding_request *out) {
+void prepare_embedding_request(job *j, yyjson_doc *root_doc, http_server *s, embedding_request *out) {
memset(out, 0, sizeof(*out));
out->j = j;
- out->root = root;
+ out->root = root_doc;
+ yyjson_val *root = yyjson_doc_get_root(root_doc);
- cJSON *detail = cJSON_CreateArray();
+ yyjson_mut_doc *detail = ve_new();
loaded_model *m = NULL;
int dims = 0;
const char *encoding = NULL;
@@ -303,47 +303,49 @@ void prepare_embedding_request(job *j, cJSON *root, http_server *s, embedding_re
? m->info->query_instruct
: NULL;
- cJSON *input = cJSON_GetObjectItemCaseSensitive(root, "input");
+ yyjson_val *input = yyjson_obj_get(root, "input");
int n_inputs = 0;
if (!input) {
ve_add(detail, "[\"body\",\"input\"]", "field required", "missing");
- } else if (cJSON_IsString(input)) {
- if (!input->valuestring || input->valuestring[0] == '\0')
+ } else if (yyjson_is_str(input)) {
+ const char *text = yyjson_get_str(input);
+ if (!text || text[0] == '\0')
ve_add(detail, "[\"body\",\"input\"]", "input must not be empty", "value_error.empty");
n_inputs = 1;
- } else if (cJSON_IsArray(input)) {
- n_inputs = cJSON_GetArraySize(input);
+ } else if (yyjson_is_arr(input)) {
+ n_inputs = (int)yyjson_arr_size(input);
if (n_inputs < 1)
ve_add(detail, "[\"body\",\"input\"]", "input must contain at least 1 item",
"value_error.list.min_items");
else if (n_inputs > FFWD_API_MAX_STANDARD_INPUTS)
ve_add(detail, "[\"body\",\"input\"]", "input must contain at most 512 items",
"value_error.list.max_items");
- cJSON *item;
- int i = 0;
- cJSON_ArrayForEach(item, input) {
+ size_t idx, max;
+ yyjson_val *item;
+ yyjson_arr_foreach(input, idx, max, item) {
char loc[64];
- snprintf(loc, sizeof(loc), "[\"body\",\"input\",%d]", i);
- if (!cJSON_IsString(item)) {
+ snprintf(loc, sizeof(loc), "[\"body\",\"input\",%d]", (int)idx);
+ if (!yyjson_is_str(item)) {
ve_add(detail, loc, "input item must be a string", "type_error.string");
- } else if (!item->valuestring || item->valuestring[0] == '\0') {
- ve_add(detail, loc, "input item must not be empty", "value_error.empty");
+ } else {
+ const char *text = yyjson_get_str(item);
+ if (!text || text[0] == '\0')
+ ve_add(detail, loc, "input item must not be empty", "value_error.empty");
}
- i++;
}
} else {
ve_add(detail, "[\"body\",\"input\"]", "input must be a string or an array of strings",
"type_error");
}
- if (cJSON_GetArraySize(detail) == 0 && m && input) {
+ if (ve_count(detail) == 0 && m && input) {
out->n_inputs = n_inputs;
out->tokens = xcalloc((size_t)n_inputs, sizeof(*out->tokens));
out->inputs = xmalloc((size_t)n_inputs * sizeof(*out->inputs));
- int idx = 0;
- if (input && cJSON_IsString(input) && input->valuestring) {
- if (tokenize_input(m, j, input->valuestring, query_instruct, &out->tokens[0]) != 0) {
+ const char *single = yyjson_get_str(input);
+ if (single) {
+ if (tokenize_input(m, j, single, query_instruct, &out->tokens[0]) != 0) {
ve_add(detail, "[\"body\",\"input\"]", "tokenization failed",
"value_error.tokenization");
} else {
@@ -355,16 +357,17 @@ void prepare_embedding_request(job *j, cJSON *root, http_server *s, embedding_re
"value_error.context_length");
}
} else {
- cJSON *item;
- cJSON_ArrayForEach(item, input) {
+ size_t idx, max;
+ yyjson_val *item;
+ yyjson_arr_foreach(input, idx, max, item) {
char loc[64];
- snprintf(loc, sizeof(loc), "[\"body\",\"input\",%d]", idx);
- if (!cJSON_IsString(item) || !item->valuestring) {
+ snprintf(loc, sizeof(loc), "[\"body\",\"input\",%d]", (int)idx);
+ const char *text = yyjson_get_str(item);
+ if (!text) {
ve_add(detail, loc, "input item must be a string", "type_error.string");
- idx++;
continue;
}
- if (tokenize_input(m, j, item->valuestring, query_instruct, &out->tokens[idx]) != 0) {
+ if (tokenize_input(m, j, text, query_instruct, &out->tokens[idx]) != 0) {
ve_add(detail, loc, "tokenization failed", "value_error.tokenization");
} else {
out->inputs[idx].ids = out->tokens[idx].ids;
@@ -374,7 +377,6 @@ void prepare_embedding_request(job *j, cJSON *root, http_server *s, embedding_re
ve_add(detail, loc, "input exceeds 32768 token limit",
"value_error.context_length");
}
- idx++;
}
}
@@ -383,22 +385,26 @@ void prepare_embedding_request(job *j, cJSON *root, http_server *s, embedding_re
"value_error.context_length");
}
- if (cJSON_GetArraySize(detail) > 0) {
+ if (ve_count(detail) > 0) {
job_set_422(j, detail);
embedding_request_free(out);
out->j = j;
} else {
out->ready = 1;
}
- cJSON_Delete(detail);
+ yyjson_mut_doc_free(detail);
}
-void prepare_contextual_request(job *j, cJSON *root, http_server *s, contextual_request *out) {
+void prepare_contextual_request(job *j,
+ yyjson_doc *root_doc,
+ http_server *s,
+ contextual_request *out) {
memset(out, 0, sizeof(*out));
out->j = j;
- out->root = root;
+ out->root = root_doc;
+ yyjson_val *root = yyjson_doc_get_root(root_doc);
- cJSON *detail = cJSON_CreateArray();
+ yyjson_mut_doc *detail = ve_new();
loaded_model *m = NULL;
int dims = 0;
const char *encoding = NULL;
@@ -407,94 +413,92 @@ void prepare_contextual_request(job *j, cJSON *root, http_server *s, contextual_
out->dims = dims;
out->encoding = encoding;
- cJSON *input = cJSON_GetObjectItemCaseSensitive(root, "input");
+ yyjson_val *input = yyjson_obj_get(root, "input");
int n_docs = 0;
int total_chunks = 0;
if (!input) {
ve_add(detail, "[\"body\",\"input\"]", "field required", "missing");
- } else if (!cJSON_IsArray(input)) {
+ } else if (!yyjson_is_arr(input)) {
ve_add(detail, "[\"body\",\"input\"]", "input must be an array of document chunk arrays",
"type_error.array");
} else {
- n_docs = cJSON_GetArraySize(input);
+ n_docs = (int)yyjson_arr_size(input);
if (n_docs < 1)
ve_add(detail, "[\"body\",\"input\"]", "input must contain at least 1 document",
"value_error.list.min_items");
else if (n_docs > FFWD_API_MAX_CONTEXT_DOCS)
ve_add(detail, "[\"body\",\"input\"]", "input must contain at most 512 documents",
"value_error.list.max_items");
- cJSON *doc_arr;
- int di = 0;
- cJSON_ArrayForEach(doc_arr, input) {
- if (!cJSON_IsArray(doc_arr)) {
+ size_t di, dn;
+ yyjson_val *doc_arr;
+ yyjson_arr_foreach(input, di, dn, doc_arr) {
+ if (!yyjson_is_arr(doc_arr)) {
char loc[64];
- snprintf(loc, sizeof(loc), "[\"body\",\"input\",%d]", di);
+ snprintf(loc, sizeof(loc), "[\"body\",\"input\",%d]", (int)di);
ve_add(detail, loc, "document must be an array of strings", "type_error.array");
- di++;
continue;
}
- int doc_chunks = cJSON_GetArraySize(doc_arr);
+ int doc_chunks = (int)yyjson_arr_size(doc_arr);
if (doc_chunks > FFWD_API_MAX_CONTEXT_CHUNKS - total_chunks)
total_chunks = FFWD_API_MAX_CONTEXT_CHUNKS + 1;
else
total_chunks += doc_chunks;
if (doc_chunks < 1) {
char loc[64];
- snprintf(loc, sizeof(loc), "[\"body\",\"input\",%d]", di);
+ snprintf(loc, sizeof(loc), "[\"body\",\"input\",%d]", (int)di);
ve_add(detail, loc, "document must contain at least 1 chunk",
"value_error.list.min_items");
}
- cJSON *chunk;
- int ci = 0;
- cJSON_ArrayForEach(chunk, doc_arr) {
+ size_t ci, cn;
+ yyjson_val *chunk;
+ yyjson_arr_foreach(doc_arr, ci, cn, chunk) {
char loc[80];
- snprintf(loc, sizeof(loc), "[\"body\",\"input\",%d,%d]", di, ci);
- if (!cJSON_IsString(chunk)) {
+ snprintf(loc, sizeof(loc), "[\"body\",\"input\",%d,%d]", (int)di, (int)ci);
+ if (!yyjson_is_str(chunk)) {
ve_add(detail, loc, "chunk must be a string", "type_error.string");
- } else if (!chunk->valuestring || chunk->valuestring[0] == '\0') {
- ve_add(detail, loc, "chunk must not be empty", "value_error.empty");
+ } else {
+ const char *text = yyjson_get_str(chunk);
+ if (!text || text[0] == '\0')
+ ve_add(detail, loc, "chunk must not be empty", "value_error.empty");
}
- ci++;
}
- di++;
}
if (total_chunks > FFWD_API_MAX_CONTEXT_CHUNKS)
ve_add(detail, "[\"body\",\"input\"]", "input must contain at most 16000 chunks",
"value_error.list.max_items");
}
- if (cJSON_GetArraySize(detail) == 0 && m) {
+ if (ve_count(detail) == 0 && m) {
out->n_docs = n_docs;
out->docs = xcalloc((size_t)n_docs, sizeof(*out->docs));
int64_t request_tokens = 0;
- cJSON *doc_arr;
- int di = 0;
- cJSON_ArrayForEach(doc_arr, input) {
+ size_t di, dn;
+ yyjson_val *doc_arr;
+ yyjson_arr_foreach(input, di, dn, doc_arr) {
contextual_doc *doc = &out->docs[di];
- int n_chunks = cJSON_GetArraySize(doc_arr);
+ int n_chunks = (int)yyjson_arr_size(doc_arr);
token_buf *chunk_tokens = xcalloc((size_t)n_chunks, sizeof(*chunk_tokens));
doc->spans = xcalloc((size_t)n_chunks, sizeof(*doc->spans));
doc->n_spans = n_chunks;
int64_t doc_tokens = n_chunks > 1 ? n_chunks - 1 : 0;
int doc_valid = 1;
- cJSON *chunk;
- int ci = 0;
- cJSON_ArrayForEach(chunk, doc_arr) {
+ size_t ci, cn;
+ yyjson_val *chunk;
+ yyjson_arr_foreach(doc_arr, ci, cn, chunk) {
char loc[80];
- snprintf(loc, sizeof(loc), "[\"body\",\"input\",%d,%d]", di, ci);
- if (tokenize_one(m, j, chunk->valuestring, &chunk_tokens[ci]) != 0) {
+ snprintf(loc, sizeof(loc), "[\"body\",\"input\",%d,%d]", (int)di, (int)ci);
+ if (tokenize_one(m, j, yyjson_get_str(chunk), &chunk_tokens[ci]) != 0) {
ve_add(detail, loc, "tokenization failed", "value_error.tokenization");
doc_valid = 0;
} else {
doc_tokens += chunk_tokens[ci].n_tokens;
}
- ci++;
}
if (doc_tokens > FFWD_API_MAX_ITEM_TOKENS) {
char loc[64];
- snprintf(loc, sizeof(loc), "[\"body\",\"input\",%d]", di);
+ snprintf(loc, sizeof(loc), "[\"body\",\"input\",%d]", (int)di);
ve_add(detail, loc, "document exceeds 32768 token limit",
"value_error.context_length");
doc_valid = 0;
@@ -506,24 +510,23 @@ void prepare_contextual_request(job *j, cJSON *root, http_server *s, contextual_
doc->ids = xmalloc((size_t)doc->n_tokens * sizeof(*doc->ids));
int sep_id = ffwd_tok_context_separator_id(m->tok);
int pos = 0;
- for (ci = 0; ci < n_chunks; ci++) {
- doc->spans[ci].start = pos;
- doc->spans[ci].n_tokens = chunk_tokens[ci].n_tokens;
+ for (int k = 0; k < n_chunks; k++) {
+ doc->spans[k].start = pos;
+ doc->spans[k].n_tokens = chunk_tokens[k].n_tokens;
/* n_tokens > 0 implies ids != NULL (tokenize_one sets both
* together), and the guard avoids a memcpy(dst, NULL, 0) for
* an empty chunk that the static analyzer reports as UB. */
- if (chunk_tokens[ci].n_tokens > 0)
- memcpy(doc->ids + pos, chunk_tokens[ci].ids,
- (size_t)chunk_tokens[ci].n_tokens * sizeof(*doc->ids));
- pos += chunk_tokens[ci].n_tokens;
- if (ci + 1 < n_chunks) {
+ if (chunk_tokens[k].n_tokens > 0)
+ memcpy(doc->ids + pos, chunk_tokens[k].ids,
+ (size_t)chunk_tokens[k].n_tokens * sizeof(*doc->ids));
+ pos += chunk_tokens[k].n_tokens;
+ if (k + 1 < n_chunks) {
doc->ids[pos++] = sep_id;
}
}
}
free_token_bufs(chunk_tokens, n_chunks);
free(chunk_tokens);
- di++;
}
if (request_tokens > FFWD_API_MAX_TOTAL_TOKENS) {
ve_add(detail, "[\"body\",\"input\"]", "request exceeds 120000 token limit",
@@ -534,28 +537,31 @@ void prepare_contextual_request(job *j, cJSON *root, http_server *s, contextual_
}
}
- if (cJSON_GetArraySize(detail) > 0) {
+ if (ve_count(detail) > 0) {
job_set_422(j, detail);
contextual_request_free(out);
out->j = j;
} else {
out->ready = 1;
}
- cJSON_Delete(detail);
+ yyjson_mut_doc_free(detail);
}
-static int
-validate_rerank_model(cJSON *root, cJSON *detail, http_server *s, loaded_model **out_model) {
- cJSON *item = cJSON_GetObjectItemCaseSensitive(root, "model");
+static int validate_rerank_model(yyjson_val *root,
+ yyjson_mut_doc *detail,
+ http_server *s,
+ loaded_model **out_model) {
+ yyjson_val *item = yyjson_obj_get(root, "model");
if (!item) {
ve_add(detail, "[\"body\",\"model\"]", "field required", "missing");
return 0;
}
- if (!cJSON_IsString(item) || !item->valuestring) {
+ const char *model_id = yyjson_get_str(item);
+ if (!model_id) {
ve_add(detail, "[\"body\",\"model\"]", "model must be a string", "type_error.string");
return 0;
}
- loaded_model *m = loaded_model_for_label(s, item->valuestring);
+ loaded_model *m = loaded_model_for_label(s, model_id);
if (!m) {
ve_add(detail, "[\"body\",\"model\"]", "value is not a valid enum member for this endpoint",
"enum");
@@ -569,85 +575,89 @@ validate_rerank_model(cJSON *root, cJSON *detail, http_server *s, loaded_model *
return 0;
}
-void prepare_rerank_request(job *j, cJSON *root, http_server *s, rerank_request *out) {
+void prepare_rerank_request(job *j, yyjson_doc *root_doc, http_server *s, rerank_request *out) {
memset(out, 0, sizeof(*out));
out->j = j;
- out->root = root;
+ out->root = root_doc;
+ yyjson_val *root = yyjson_doc_get_root(root_doc);
- cJSON *detail = cJSON_CreateArray();
+ yyjson_mut_doc *detail = ve_new();
validate_rerank_model(root, detail, s, &out->model);
- cJSON *query = cJSON_GetObjectItemCaseSensitive(root, "query");
- cJSON *documents = cJSON_GetObjectItemCaseSensitive(root, "documents");
+ yyjson_val *query = yyjson_obj_get(root, "query");
+ yyjson_val *documents = yyjson_obj_get(root, "documents");
/* query_str is set only once query passes every check; the tokenize step
* below runs only when detail is empty, so it is non-NULL there. Hoisting it
* also keeps the validated value explicit for the static analyzer. */
const char *query_str = NULL;
if (!query) {
ve_add(detail, "[\"body\",\"query\"]", "field required", "missing");
- } else if (!cJSON_IsString(query)) {
+ } else if (!yyjson_is_str(query)) {
ve_add(detail, "[\"body\",\"query\"]", "query must be a string", "type_error.string");
- } else if (!query->valuestring || !query->valuestring[0]) {
+ } else if (!yyjson_get_str(query)[0]) {
ve_add(detail, "[\"body\",\"query\"]", "query must not be empty", "value_error.empty");
} else {
- query_str = query->valuestring;
+ query_str = yyjson_get_str(query);
}
int n_documents = 0;
if (!documents) {
ve_add(detail, "[\"body\",\"documents\"]", "field required", "missing");
- } else if (!cJSON_IsArray(documents)) {
+ } else if (!yyjson_is_arr(documents)) {
ve_add(detail, "[\"body\",\"documents\"]", "documents must be an array of strings",
"type_error.array");
} else {
- n_documents = cJSON_GetArraySize(documents);
+ n_documents = (int)yyjson_arr_size(documents);
if (n_documents < 1)
ve_add(detail, "[\"body\",\"documents\"]", "documents must contain at least 1 item",
"value_error.list.min_items");
else if (n_documents > FFWD_API_MAX_RERANK_DOCUMENTS)
ve_add(detail, "[\"body\",\"documents\"]", "documents must contain at most 1000 items",
"value_error.list.max_items");
- cJSON *document;
- int i = 0;
- cJSON_ArrayForEach(document, documents) {
+ size_t i, dmax;
+ yyjson_val *document;
+ yyjson_arr_foreach(documents, i, dmax, document) {
char loc[72];
- snprintf(loc, sizeof(loc), "[\"body\",\"documents\",%d]", i++);
- if (!cJSON_IsString(document))
+ snprintf(loc, sizeof(loc), "[\"body\",\"documents\",%d]", (int)i);
+ if (!yyjson_is_str(document)) {
ve_add(detail, loc, "document must be a string", "type_error.string");
- else if (!document->valuestring || !document->valuestring[0])
- ve_add(detail, loc, "document must not be empty", "value_error.empty");
+ } else {
+ const char *text = yyjson_get_str(document);
+ if (!text || !text[0])
+ ve_add(detail, loc, "document must not be empty", "value_error.empty");
+ }
}
}
- cJSON *top_n = cJSON_GetObjectItemCaseSensitive(root, "top_n");
- cJSON *top_k = cJSON_GetObjectItemCaseSensitive(root, "top_k");
+ yyjson_val *top_n = yyjson_obj_get(root, "top_n");
+ yyjson_val *top_k = yyjson_obj_get(root, "top_k");
if (top_n && top_k) {
ve_add(detail, "[\"body\",\"top_n\"]", "top_n and top_k are aliases; provide only one",
"value_error.conflict");
}
- cJSON *top = top_n ? top_n : top_k;
+ yyjson_val *top = top_n ? top_n : top_k;
out->top_n = n_documents;
if (top) {
- if (!cjson_is_integer(top)) {
+ if (!json_is_integer(top)) {
ve_add(detail, top_n ? "[\"body\",\"top_n\"]" : "[\"body\",\"top_k\"]",
"top_n must be an integer", "type_error.integer");
} else {
- out->top_n = top->valueint;
+ out->top_n = (int)yyjson_get_num(top);
if (out->top_n < 1 || out->top_n > n_documents)
ve_add(detail, top_n ? "[\"body\",\"top_n\"]" : "[\"body\",\"top_k\"]",
"top_n must be between 1 and the number of documents", "value_error.range");
}
}
- cJSON *return_documents = cJSON_GetObjectItemCaseSensitive(root, "return_documents");
+ yyjson_val *return_documents = yyjson_obj_get(root, "return_documents");
if (return_documents) {
- if (!cJSON_IsBool(return_documents))
+ if (!yyjson_is_bool(return_documents))
ve_add(detail, "[\"body\",\"return_documents\"]", "return_documents must be a boolean",
"type_error.bool");
else
- out->return_documents = cJSON_IsTrue(return_documents);
+ out->return_documents = yyjson_get_bool(return_documents);
}
- if (cJSON_GetArraySize(detail) == 0 && out->model) {
+ if (ve_count(detail) == 0 && out->model) {
out->n_documents = n_documents;
out->documents = xcalloc((size_t)n_documents, sizeof(*out->documents));
if (tokenize_late_text(out->model, j, query_str, 1, &out->query) != 0) {
@@ -659,12 +669,12 @@ void prepare_rerank_request(job *j, cJSON *root, http_server *s, rerank_request
"value_error.context_length");
}
- cJSON *document;
- int i = 0;
- cJSON_ArrayForEach(document, documents) {
+ size_t i, dmax;
+ yyjson_val *document;
+ yyjson_arr_foreach(documents, i, dmax, document) {
char loc[72];
- snprintf(loc, sizeof(loc), "[\"body\",\"documents\",%d]", i);
- if (tokenize_late_text(out->model, j, document->valuestring, 0, &out->documents[i]) !=
+ snprintf(loc, sizeof(loc), "[\"body\",\"documents\",%d]", (int)i);
+ if (tokenize_late_text(out->model, j, yyjson_get_str(document), 0, &out->documents[i]) !=
0) {
ve_add(detail, loc, "tokenization failed", "value_error.tokenization");
} else {
@@ -677,7 +687,6 @@ void prepare_rerank_request(job *j, cJSON *root, http_server *s, rerank_request
else
out->document_tokens = INT_MAX;
}
- i++;
}
if (out->query_tokens > FFWD_API_MAX_TOTAL_TOKENS - out->document_tokens) {
ve_add(detail, "[\"body\",\"documents\"]", "request exceeds 120000 token limit",
@@ -685,14 +694,14 @@ void prepare_rerank_request(job *j, cJSON *root, http_server *s, rerank_request
}
}
- if (cJSON_GetArraySize(detail) > 0) {
+ if (ve_count(detail) > 0) {
job_set_422(j, detail);
rerank_request_free(out);
out->j = j;
} else {
out->ready = 1;
}
- cJSON_Delete(detail);
+ yyjson_mut_doc_free(detail);
}
typedef struct {
@@ -764,16 +773,16 @@ void execute_rerank_request(rerank_request *r) {
t0 = nstime();
sbuf b = {0};
sbuf_printf(&b, "{\"object\":\"list\",\"model\":\"%s\",\"results\":[", r->model->info->id);
- cJSON *documents = cJSON_GetObjectItemCaseSensitive(r->root, "documents");
+ yyjson_val *documents = yyjson_obj_get(yyjson_doc_get_root(r->root), "documents");
for (int i = 0; i < r->top_n; i++) {
if (i)
sbuf_putc(&b, ',');
sbuf_printf(&b, "{\"index\":%d,\"relevance_score\":%.9g", ranked[i].index,
(double)ranked[i].score);
if (r->return_documents) {
- cJSON *document = cJSON_GetArrayItem(documents, ranked[i].index);
+ yyjson_val *document = yyjson_arr_get(documents, (size_t)ranked[i].index);
sbuf_puts(&b, ",\"document\":");
- append_json_string(&b, document->valuestring);
+ append_json_string(&b, yyjson_get_str(document));
}
sbuf_putc(&b, '}');
}
diff --git a/tools/server/json.c b/tools/server/json.c
index 4812164..8f9aec2 100644
--- a/tools/server/json.c
+++ b/tools/server/json.c
@@ -1,8 +1,6 @@
#include "server_internal.h"
#include "util.h"
-#include <cjson/cJSON.h>
-#include <ctype.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
@@ -75,26 +73,47 @@ void job_set_error(job *j, int status, const char *message, const char *type) {
j->response = json_error_body(message, type, &j->response_len);
}
-bool ve_add(cJSON *detail, const char *loc_json, const char *msg, const char *type) {
- cJSON *obj = cJSON_CreateObject();
- cJSON *loc = cJSON_Parse(loc_json);
- if (!obj || !loc) {
- cJSON_Delete(obj);
- cJSON_Delete(loc);
+yyjson_mut_doc *ve_new(void) {
+ yyjson_mut_doc *detail = yyjson_mut_doc_new(NULL);
+ yyjson_mut_doc_set_root(detail, yyjson_mut_arr(detail));
+ return detail;
+}
+
+bool ve_add(yyjson_mut_doc *detail, const char *loc_json, const char *msg, const char *type) {
+ yyjson_mut_val *arr = yyjson_mut_doc_get_root(detail);
+ yyjson_mut_val *obj = yyjson_mut_obj(detail);
+ /* loc is a JSON array literal (mix of strings and integer indices) built by
+ * the caller; parse it and deep-copy it into the detail document so it owns
+ * the values once the temporary parse is freed. */
+ yyjson_doc *loc_doc = yyjson_read(loc_json, strlen(loc_json), 0);
+ yyjson_mut_val *loc = loc_doc ? yyjson_val_mut_copy(detail, yyjson_doc_get_root(loc_doc)) : NULL;
+ yyjson_doc_free(loc_doc);
+ if (!arr || !obj || !loc)
return false;
- }
- cJSON_AddItemToObject(obj, "loc", loc);
- cJSON_AddStringToObject(obj, "msg", msg ? msg : "");
- cJSON_AddStringToObject(obj, "type", type ? type : "");
- cJSON_AddItemToArray(detail, obj);
+ yyjson_mut_obj_add_val(detail, obj, "loc", loc);
+ /* msg may point at a caller stack buffer, so copy the value strings. */
+ yyjson_mut_obj_add_strcpy(detail, obj, "msg", msg ? msg : "");
+ yyjson_mut_obj_add_strcpy(detail, obj, "type", type ? type : "");
+ yyjson_mut_arr_add_val(arr, obj);
return true;
}
-void job_set_422(job *j, cJSON *detail) {
- cJSON *root = cJSON_CreateObject();
- cJSON_AddItemToObject(root, "detail", cJSON_Duplicate(detail, 1));
- char *s = cJSON_PrintUnformatted(root);
- cJSON_Delete(root);
+size_t ve_count(yyjson_mut_doc *detail) {
+ return yyjson_mut_arr_size(yyjson_mut_doc_get_root(detail));
+}
+
+void job_set_422(job *j, yyjson_mut_doc *detail) {
+ /* Reparent the detail array under a fresh {"detail":[...]} root in the same
+ * document, then serialize. The caller frees the document right after, so
+ * mutating its root here is safe and avoids a deep copy. */
+ yyjson_mut_val *arr = yyjson_mut_doc_get_root(detail);
+ yyjson_mut_val *root = yyjson_mut_obj(detail);
+ size_t len = 0;
+ char *s = NULL;
+ if (root && arr && yyjson_mut_obj_add_val(detail, root, "detail", arr)) {
+ yyjson_mut_doc_set_root(detail, root);
+ s = yyjson_mut_write(detail, 0, &len);
+ }
if (!s) {
job_set_error(j, 500, "failed to render validation error", "server_error");
return;
@@ -102,57 +121,52 @@ void job_set_422(job *j, cJSON *detail) {
free(j->response);
j->status = 422;
j->content_type = "application/json";
- j->response = xstrdup(s);
- j->response_len = strlen(j->response);
- cJSON_free(s);
+ j->response = s;
+ j->response_len = len;
}
-cJSON *parse_json_body(job *j, cJSON *detail) {
+yyjson_doc *parse_json_body(job *j, yyjson_mut_doc *detail) {
if (!j->body || j->body_len == 0) {
ve_add(detail, "[\"body\"]", "body required", "missing");
return NULL;
}
- const char *end = NULL;
- cJSON *root = cJSON_ParseWithLengthOpts(j->body, j->body_len, &end, false);
- if (!root) {
+ /* yyjson_read with default flags requires the whole buffer to be one JSON
+ * value (trailing whitespace is allowed, trailing content is an error), so
+ * it already enforces "the entire body must be consumed". */
+ yyjson_doc *doc = yyjson_read(j->body, j->body_len, 0);
+ if (!doc) {
ve_add(detail, "[\"body\"]", "invalid JSON", "json_invalid");
return NULL;
}
- const char *body_end = j->body + j->body_len;
- while (end && end < body_end && isspace((unsigned char)*end))
- end++;
- if (!end || end != body_end) {
- ve_add(detail, "[\"body\"]", "invalid JSON", "json_invalid");
- cJSON_Delete(root);
- return NULL;
- }
- if (!cJSON_IsObject(root)) {
+ if (!yyjson_is_obj(yyjson_doc_get_root(doc))) {
ve_add(detail, "[\"body\"]", "body must be a JSON object", "type_error.object");
- cJSON_Delete(root);
+ yyjson_doc_free(doc);
return NULL;
}
- return root;
+ return doc;
}
-bool cjson_is_integer(cJSON *item) {
- if (!cJSON_IsNumber(item))
+bool json_is_integer(yyjson_val *item) {
+ if (!yyjson_is_num(item))
+ return false;
+ double d = yyjson_get_num(item);
+ if (!(d >= (double)INT_MIN && d <= (double)INT_MAX))
return false;
- double d = item->valuedouble;
- int i = item->valueint;
- return d >= (double)INT_MIN && d <= (double)INT_MAX && d == (double)i;
+ /* A real such as 5.0 counts as an integer; 5.5 does not. */
+ return d == (double)(int)d;
}
-const char *encoding_from_root(cJSON *root, cJSON *detail, embedding_api_t api) {
+const char *encoding_from_root(yyjson_val *root, yyjson_mut_doc *detail, embedding_api_t api) {
const char *dflt = api == FFWD_API_PERPLEXITY ? "base64_int8" : "float";
- cJSON *encoding = cJSON_GetObjectItemCaseSensitive(root, "encoding_format");
+ yyjson_val *encoding = yyjson_obj_get(root, "encoding_format");
if (!encoding)
return dflt;
- if (!cJSON_IsString(encoding) || !encoding->valuestring) {
+ const char *v = yyjson_get_str(encoding);
+ if (!v) {
ve_add(detail, "[\"body\",\"encoding_format\"]", "encoding_format must be a string",
"type_error.string");
return dflt;
}
- const char *v = encoding->valuestring;
int ok = api == FFWD_API_PERPLEXITY
? (!strcmp(v, "base64_int8") || !strcmp(v, "base64_binary") || !strcmp(v, "float"))
: (!strcmp(v, "float") || !strcmp(v, "base64"));
@@ -171,8 +185,8 @@ const char *encoding_from_root(cJSON *root, cJSON *detail, embedding_api_t api)
/* Parse the `text_type` hint. Returns 1 when the model's query instruction
* should be prepended, 0 otherwise (document / absent). Models without a
* published query instruction reject the field. */
-int text_type_is_query(cJSON *root, cJSON *detail, const char *query_instruct) {
- cJSON *tt = cJSON_GetObjectItemCaseSensitive(root, "text_type");
+int text_type_is_query(yyjson_val *root, yyjson_mut_doc *detail, const char *query_instruct) {
+ yyjson_val *tt = yyjson_obj_get(root, "text_type");
if (!tt)
return 0;
if (!query_instruct) {
@@ -180,11 +194,11 @@ int text_type_is_query(cJSON *root, cJSON *detail, const char *query_instruct) {
"text_type is only supported for instruction embedding models", "extra_fields");
return 0;
}
- if (!cJSON_IsString(tt) || !tt->valuestring) {
+ const char *v = yyjson_get_str(tt);
+ if (!v) {
ve_add(detail, "[\"body\",\"text_type\"]", "text_type must be a string", "type_error.string");
return 0;
}
- const char *v = tt->valuestring;
if (!strcmp(v, "query"))
return 1;
if (!strcmp(v, "document"))
@@ -194,15 +208,16 @@ int text_type_is_query(cJSON *root, cJSON *detail, const char *query_instruct) {
return 0;
}
-int dimensions_from_root(cJSON *root, cJSON *detail, int min_dim, int max_dim, const char *encoding) {
+int dimensions_from_root(
+ yyjson_val *root, yyjson_mut_doc *detail, int min_dim, int max_dim, const char *encoding) {
int dims = max_dim;
- cJSON *dimensions = cJSON_GetObjectItemCaseSensitive(root, "dimensions");
+ yyjson_val *dimensions = yyjson_obj_get(root, "dimensions");
if (dimensions) {
- if (!cjson_is_integer(dimensions)) {
+ if (!json_is_integer(dimensions)) {
ve_add(detail, "[\"body\",\"dimensions\"]", "dimensions must be an integer",
"type_error.integer");
} else {
- dims = dimensions->valueint;
+ dims = (int)yyjson_get_num(dimensions);
if (dims < min_dim || dims > max_dim) {
char msg[96];
snprintf(msg, sizeof(msg), "dimensions must be between %d and %d", min_dim, max_dim);
diff --git a/tools/server/models.c b/tools/server/models.c
index 8fdc590..479ff97 100644
--- a/tools/server/models.c
+++ b/tools/server/models.c
@@ -89,25 +89,27 @@ static int read_query_instruct_from_files(const char *model_dir, char **out) {
int rc = read_optional_text_file(model_dir, "config_sentence_transformers.json", &buf);
if (rc <= 0)
return rc;
- cJSON *root = cJSON_Parse(buf);
+ yyjson_doc *doc = yyjson_read(buf, strlen(buf), 0);
free(buf);
- if (!root) {
+ if (!doc) {
server_log("ffwd-server: invalid config_sentence_transformers.json: %s", model_dir);
return -1;
}
- cJSON *prompts = cJSON_GetObjectItemCaseSensitive(root, "prompts");
- cJSON *query = prompts ? cJSON_GetObjectItemCaseSensitive(prompts, "query") : NULL;
- if (query && !cJSON_IsString(query)) {
- cJSON_Delete(root);
+ yyjson_val *root = yyjson_doc_get_root(doc);
+ yyjson_val *prompts = yyjson_obj_get(root, "prompts");
+ yyjson_val *query = prompts ? yyjson_obj_get(prompts, "query") : NULL;
+ if (query && !yyjson_is_str(query)) {
+ yyjson_doc_free(doc);
server_log("ffwd-server: prompts.query must be a string: %s", model_dir);
return -1;
}
- if (query && query->valuestring && query->valuestring[0]) {
- *out = xstrdup(query->valuestring);
- cJSON_Delete(root);
+ const char *qs = yyjson_get_str(query);
+ if (qs && qs[0]) {
+ *out = xstrdup(qs);
+ yyjson_doc_free(doc);
return *out ? 1 : -1;
}
- cJSON_Delete(root);
+ yyjson_doc_free(doc);
return 0;
}
@@ -156,7 +158,7 @@ void embedding_request_free(embedding_request *r) {
free(r->tokens);
free(r->inputs);
if (r->root)
- cJSON_Delete(r->root);
+ yyjson_doc_free(r->root);
memset(r, 0, sizeof(*r));
}
@@ -169,7 +171,7 @@ void contextual_request_free(contextual_request *r) {
}
free(r->docs);
if (r->root)
- cJSON_Delete(r->root);
+ yyjson_doc_free(r->root);
memset(r, 0, sizeof(*r));
}
@@ -189,7 +191,7 @@ void rerank_request_free(rerank_request *r) {
late_tokens_free(&r->documents[i]);
free(r->documents);
if (r->root)
- cJSON_Delete(r->root);
+ yyjson_doc_free(r->root);
memset(r, 0, sizeof(*r));
}
diff --git a/tools/server/schedule.c b/tools/server/schedule.c
index 9fb7db6..76da240 100644
--- a/tools/server/schedule.c
+++ b/tools/server/schedule.c
@@ -298,22 +298,22 @@ static int has_unfinished_jobs(const int *done, int n_jobs) {
return 0;
}
-static int parse_job_root(job *j, cJSON **out_root) {
- cJSON *detail = cJSON_CreateArray();
- cJSON *root = parse_json_body(j, detail);
+static int parse_job_root(job *j, yyjson_doc **out_root) {
+ yyjson_mut_doc *detail = ve_new();
+ yyjson_doc *root = parse_json_body(j, detail);
if (!root) {
job_set_422(j, detail);
- cJSON_Delete(detail);
+ yyjson_mut_doc_free(detail);
*out_root = NULL;
return -1;
}
- cJSON_Delete(detail);
+ yyjson_mut_doc_free(detail);
*out_root = root;
return 0;
}
static void process_unknown_job(job *j) {
- cJSON *root = NULL;
+ yyjson_doc *root = NULL;
if (!j->started_ns)
j->started_ns = nstime();
uint64_t t0 = nstime();
@@ -322,7 +322,7 @@ static void process_unknown_job(job *j) {
if (rc != 0)
return;
job_set_error(j, 404, "unknown endpoint", "invalid_request_error");
- cJSON_Delete(root);
+ yyjson_doc_free(root);
}
/* Parse and tokenize one job into a heap request attached to j->prep, which the
@@ -344,7 +344,7 @@ void tokenize_job(http_server *s, job *j) {
else
return;
- cJSON *root = NULL;
+ yyjson_doc *root = NULL;
uint64_t t0 = nstime();
int parse_rc = parse_job_root(j, &root);
j->parse_ns += nstime() - t0;
diff --git a/tools/server/server.c b/tools/server/server.c
index f18d264..40b8e18 100644
--- a/tools/server/server.c
+++ b/tools/server/server.c
@@ -35,8 +35,6 @@
# include <sys/sysctl.h>
#endif
-#include <cjson/cJSON.h>
-
static volatile sig_atomic_t g_stop_requested = 0;
static volatile sig_atomic_t g_signal_wfd = -1;
diff --git a/tools/server/server_internal.h b/tools/server/server_internal.h
index 315acdd..462af53 100644
--- a/tools/server/server_internal.h
+++ b/tools/server/server_internal.h
@@ -13,7 +13,7 @@
#include "ae.h"
-#include <cjson/cJSON.h>
+#include <yyjson.h>
#include <pthread.h>
#include <stdbool.h>
#include <stddef.h>
@@ -197,7 +197,7 @@ struct job {
};
/* Parsed request state, shared by the prepare (handlers), execute (schedule),
- * and render (encode) stages. Each carries its source cJSON root (freed when
+ * and render (encode) stages. Each carries its source JSON document (freed when
* the request is freed), the resolved model, and the tokenized inputs. */
typedef struct {
int *ids;
@@ -206,7 +206,7 @@ typedef struct {
typedef struct {
job *j;
- cJSON *root;
+ yyjson_doc *root;
loaded_model *model;
int dims;
const char *encoding;
@@ -226,7 +226,7 @@ typedef struct {
typedef struct {
job *j;
- cJSON *root;
+ yyjson_doc *root;
loaded_model *model;
int dims;
const char *encoding;
@@ -242,7 +242,7 @@ typedef ffwd_late_tokens_t late_tokens;
typedef struct {
job *j;
- cJSON *root;
+ yyjson_doc *root;
loaded_model *model;
late_tokens query;
late_tokens *documents;
@@ -258,13 +258,20 @@ typedef struct {
void append_json_string(sbuf *b, const char *s);
char *json_error_body(const char *message, const char *type, size_t *len);
void job_set_error(job *j, int status, const char *message, const char *type);
-bool ve_add(cJSON *detail, const char *loc_json, const char *msg, const char *type);
-void job_set_422(job *j, cJSON *detail);
-cJSON *parse_json_body(job *j, cJSON *detail);
-bool cjson_is_integer(cJSON *item);
-const char *encoding_from_root(cJSON *root, cJSON *detail, embedding_api_t api);
-int text_type_is_query(cJSON *root, cJSON *detail, const char *query_instruct);
-int dimensions_from_root(cJSON *root, cJSON *detail, int min_dim, int max_dim, const char *encoding);
+/* Validation errors accumulate in a mutable yyjson document whose root is the
+ * {"detail":[...]} array: ve_new creates it, ve_add appends one entry, ve_count
+ * reports how many were added, and job_set_422 serializes it. Release the
+ * document with yyjson_mut_doc_free. */
+yyjson_mut_doc *ve_new(void);
+bool ve_add(yyjson_mut_doc *detail, const char *loc_json, const char *msg, const char *type);
+size_t ve_count(yyjson_mut_doc *detail);
+void job_set_422(job *j, yyjson_mut_doc *detail);
+yyjson_doc *parse_json_body(job *j, yyjson_mut_doc *detail);
+bool json_is_integer(yyjson_val *item);
+const char *encoding_from_root(yyjson_val *root, yyjson_mut_doc *detail, embedding_api_t api);
+int text_type_is_query(yyjson_val *root, yyjson_mut_doc *detail, const char *query_instruct);
+int dimensions_from_root(
+ yyjson_val *root, yyjson_mut_doc *detail, int min_dim, int max_dim, const char *encoding);
/* ---- server_http.c: connection lifecycle, header parse, response framing ---- */
int set_nonblock(int fd);
@@ -323,13 +330,16 @@ int load_one_model(http_server *s, const ffwd_server_model_spec_t *spec);
void free_models(http_server *s);
/* ---- server_handlers.c: per-endpoint prepare / batch-group / execute ---- */
-void prepare_embedding_request(job *j, cJSON *root, http_server *s, embedding_request *out);
+void prepare_embedding_request(job *j, yyjson_doc *root_doc, http_server *s, embedding_request *out);
int embedding_request_compatible(const embedding_request *a, const embedding_request *b);
void execute_embedding_request_list(embedding_request **reqs, int n_reqs);
-void prepare_contextual_request(job *j, cJSON *root, http_server *s, contextual_request *out);
+void prepare_contextual_request(job *j,
+ yyjson_doc *root_doc,
+ http_server *s,
+ contextual_request *out);
int contextual_request_compatible(const contextual_request *a, const contextual_request *b);
void execute_contextual_request_list(contextual_request **reqs, int n_reqs);
-void prepare_rerank_request(job *j, cJSON *root, http_server *s, rerank_request *out);
+void prepare_rerank_request(job *j, yyjson_doc *root_doc, http_server *s, rerank_request *out);
void execute_rerank_request(rerank_request *r);
/* ---- server_schedule.c: job queue, micro-batching, completion ---- */
diff --git a/tools/server/test_json.c b/tools/server/test_json.c
index 76a0f45..22b95c8 100644
--- a/tools/server/test_json.c
+++ b/tools/server/test_json.c
@@ -11,6 +11,10 @@
static const char *TEST_QWEN3_QUERY_INSTRUCT =
"Instruct: Given a web search query, retrieve relevant passages that answer the query\nQuery:";
+/* Parse a JSON literal into an immutable document so its root can be passed to
+ * the read-only validators (encoding_from_root, text_type_is_query). */
+static yyjson_doc *parse_obj(const char *json) { return yyjson_read(json, strlen(json), 0); }
+
static void test_json_error_body_escaping(void) {
size_t len = 0;
char *body = json_error_body("line\nquote\" tab\t ctrl\001 slash\\", "bad\ntype", &len);
@@ -20,24 +24,26 @@ static void test_json_error_body_escaping(void) {
TEST_ASSERT(len == strlen(body));
TEST_ASSERT(strchr(body, '\n') == NULL);
TEST_ASSERT(strchr(body, '\t') == NULL);
- cJSON *root = cJSON_Parse(body);
+ yyjson_doc *doc = yyjson_read(body, strlen(body), 0);
+ yyjson_val *root = yyjson_doc_get_root(doc);
TEST_ASSERT(root != NULL);
if (!root) {
+ yyjson_doc_free(doc);
free(body);
return;
}
- cJSON *err = cJSON_GetObjectItemCaseSensitive(root, "error");
- cJSON *msg = err ? cJSON_GetObjectItemCaseSensitive(err, "message") : NULL;
- cJSON *type = err ? cJSON_GetObjectItemCaseSensitive(err, "type") : NULL;
- if (!msg || !type || !cJSON_IsString(msg) || !cJSON_IsString(type)) {
+ yyjson_val *err = yyjson_obj_get(root, "error");
+ yyjson_val *msg = err ? yyjson_obj_get(err, "message") : NULL;
+ yyjson_val *type = err ? yyjson_obj_get(err, "type") : NULL;
+ if (!msg || !type || !yyjson_is_str(msg) || !yyjson_is_str(type)) {
TEST_ASSERT(0 && "error body fields must be strings");
- cJSON_Delete(root);
+ yyjson_doc_free(doc);
free(body);
return;
}
- TEST_ASSERT(strcmp(msg->valuestring, "line\nquote\" tab\t ctrl\001 slash\\") == 0);
- TEST_ASSERT(strcmp(type->valuestring, "bad\ntype") == 0);
- cJSON_Delete(root);
+ TEST_ASSERT(strcmp(yyjson_get_str(msg), "line\nquote\" tab\t ctrl\001 slash\\") == 0);
+ TEST_ASSERT(strcmp(yyjson_get_str(type), "bad\ntype") == 0);
+ yyjson_doc_free(doc);
free(body);
}
@@ -48,7 +54,7 @@ static void test_job_set_422_replaces_response(void) {
j.response_len = strlen(j.response);
j.status = 500;
- cJSON *detail = cJSON_CreateArray();
+ yyjson_mut_doc *detail = ve_new();
ve_add(detail, "[\"body\",\"field\"]", "bad value", "value_error");
job_set_422(&j, detail);
TEST_ASSERT(j.status == 422);
@@ -58,80 +64,75 @@ static void test_job_set_422_replaces_response(void) {
TEST_ASSERT(strstr(j.response, "bad value") != NULL);
}
- cJSON_Delete(detail);
+ yyjson_mut_doc_free(detail);
free(j.response);
}
static void test_encoding_from_root_family(void) {
- cJSON *detail = cJSON_CreateArray();
+ yyjson_mut_doc *detail = ve_new();
/* Default encoding follows the explicit serving API. */
- cJSON *empty = cJSON_CreateObject();
- TEST_ASSERT(strcmp(encoding_from_root(empty, detail, FFWD_API_OPENAI), "float") == 0);
- TEST_ASSERT(strcmp(encoding_from_root(empty, detail, FFWD_API_PERPLEXITY), "base64_int8") ==
+ yyjson_doc *empty = parse_obj("{}");
+ yyjson_val *empty_root = yyjson_doc_get_root(empty);
+ TEST_ASSERT(strcmp(encoding_from_root(empty_root, detail, FFWD_API_OPENAI), "float") == 0);
+ TEST_ASSERT(strcmp(encoding_from_root(empty_root, detail, FFWD_API_PERPLEXITY), "base64_int8") ==
0);
- cJSON_Delete(empty);
- TEST_ASSERT(cJSON_GetArraySize(detail) == 0);
+ yyjson_doc_free(empty);
+ TEST_ASSERT(ve_count(detail) == 0);
/* OpenAI rejects int8 formats; Perplexity rejects the OpenAI base64. */
- cJSON *o = cJSON_CreateObject();
- cJSON_AddStringToObject(o, "encoding_format", "base64_int8");
- encoding_from_root(o, detail, FFWD_API_OPENAI);
- TEST_ASSERT(cJSON_GetArraySize(detail) == 1);
- cJSON_Delete(o);
+ yyjson_doc *o = parse_obj("{\"encoding_format\":\"base64_int8\"}");
+ encoding_from_root(yyjson_doc_get_root(o), detail, FFWD_API_OPENAI);
+ TEST_ASSERT(ve_count(detail) == 1);
+ yyjson_doc_free(o);
- cJSON *p = cJSON_CreateObject();
- cJSON_AddStringToObject(p, "encoding_format", "base64");
- encoding_from_root(p, detail, FFWD_API_PERPLEXITY);
- TEST_ASSERT(cJSON_GetArraySize(detail) == 2);
- cJSON_Delete(p);
+ yyjson_doc *p = parse_obj("{\"encoding_format\":\"base64\"}");
+ encoding_from_root(yyjson_doc_get_root(p), detail, FFWD_API_PERPLEXITY);
+ TEST_ASSERT(ve_count(detail) == 2);
+ yyjson_doc_free(p);
/* An unknown format is rejected. */
- cJSON *bad = cJSON_CreateObject();
- cJSON_AddStringToObject(bad, "encoding_format", "base64_fp16");
- encoding_from_root(bad, detail, FFWD_API_OPENAI);
- TEST_ASSERT(cJSON_GetArraySize(detail) == 3);
- cJSON_Delete(bad);
+ yyjson_doc *bad = parse_obj("{\"encoding_format\":\"base64_fp16\"}");
+ encoding_from_root(yyjson_doc_get_root(bad), detail, FFWD_API_OPENAI);
+ TEST_ASSERT(ve_count(detail) == 3);
+ yyjson_doc_free(bad);
- cJSON_Delete(detail);
+ yyjson_mut_doc_free(detail);
}
static void test_text_type(void) {
- cJSON *detail = cJSON_CreateArray();
+ yyjson_mut_doc *detail = ve_new();
/* Absent: no-op for both model types, no error. */
- cJSON *empty = cJSON_CreateObject();
- TEST_ASSERT(text_type_is_query(empty, detail, TEST_QWEN3_QUERY_INSTRUCT) == 0);
- TEST_ASSERT(text_type_is_query(empty, detail, NULL) == 0);
- cJSON_Delete(empty);
- TEST_ASSERT(cJSON_GetArraySize(detail) == 0);
+ yyjson_doc *empty = parse_obj("{}");
+ yyjson_val *empty_root = yyjson_doc_get_root(empty);
+ TEST_ASSERT(text_type_is_query(empty_root, detail, TEST_QWEN3_QUERY_INSTRUCT) == 0);
+ TEST_ASSERT(text_type_is_query(empty_root, detail, NULL) == 0);
+ yyjson_doc_free(empty);
+ TEST_ASSERT(ve_count(detail) == 0);
/* Qwen3: query -> 1, document -> 0, both accepted. */
- cJSON *q = cJSON_CreateObject();
- cJSON_AddStringToObject(q, "text_type", "query");
- TEST_ASSERT(text_type_is_query(q, detail, TEST_QWEN3_QUERY_INSTRUCT) == 1);
- cJSON_Delete(q);
- cJSON *d = cJSON_CreateObject();
- cJSON_AddStringToObject(d, "text_type", "document");
- TEST_ASSERT(text_type_is_query(d, detail, TEST_QWEN3_QUERY_INSTRUCT) == 0);
- cJSON_Delete(d);
- TEST_ASSERT(cJSON_GetArraySize(detail) == 0);
+ yyjson_doc *q = parse_obj("{\"text_type\":\"query\"}");
+ TEST_ASSERT(text_type_is_query(yyjson_doc_get_root(q), detail, TEST_QWEN3_QUERY_INSTRUCT) == 1);
+ yyjson_doc_free(q);
+ yyjson_doc *d = parse_obj("{\"text_type\":\"document\"}");
+ TEST_ASSERT(text_type_is_query(yyjson_doc_get_root(d), detail, TEST_QWEN3_QUERY_INSTRUCT) == 0);
+ yyjson_doc_free(d);
+ TEST_ASSERT(ve_count(detail) == 0);
/* Invalid enum value on Qwen3 is reported. */
- cJSON *bad = cJSON_CreateObject();
- cJSON_AddStringToObject(bad, "text_type", "passage");
- TEST_ASSERT(text_type_is_query(bad, detail, TEST_QWEN3_QUERY_INSTRUCT) == 0);
- TEST_ASSERT(cJSON_GetArraySize(detail) == 1);
- cJSON_Delete(bad);
+ yyjson_doc *bad = parse_obj("{\"text_type\":\"passage\"}");
+ TEST_ASSERT(text_type_is_query(yyjson_doc_get_root(bad), detail, TEST_QWEN3_QUERY_INSTRUCT) == 0);
+ TEST_ASSERT(ve_count(detail) == 1);
+ yyjson_doc_free(bad);
/* text_type on a model with no query instruction is rejected. */
- cJSON *p = cJSON_CreateObject();
- cJSON_AddStringToObject(p, "text_type", "query");
- TEST_ASSERT(text_type_is_query(p, detail, NULL) == 0);
- TEST_ASSERT(cJSON_GetArraySize(detail) == 2);
- cJSON_Delete(p);
+ yyjson_doc *p = parse_obj("{\"text_type\":\"query\"}");
+ TEST_ASSERT(text_type_is_query(yyjson_doc_get_root(p), detail, NULL) == 0);
+ TEST_ASSERT(ve_count(detail) == 2);
+ yyjson_doc_free(p);
- cJSON_Delete(detail);
+ yyjson_mut_doc_free(detail);
}
int main(void) {