JW_OBJ
- Macro
- August 22, 2025
Table of Contents
JW_OBJ
JW_OBJ
Description
Begin a JSON object and write key-value entries using JW_*_KV macros. This macro must be used as a wrapper for other JW_*_KV
macros to generate a JSON object. Tracks whether commas are needed between entries using an internal flag.
Parameters
Name | Direction | Description |
---|---|---|
writer | in | A block of code containing JW_*_KV calls for populating the object. |
Usage example (from documentation)
JW_OBJ(json, {
JW_STR_KV(json, "name", obj.name);
JW_INT_KV(json, "ref", obj.ref);
});
Success
Appends a valid JSON object to json
Failure
Does not return on failure (relies on internal string operations)
Usage example (Cross-references)
- In
JSON.h:706
:
} \
StrAppendf(&(j), "\"%s\":", k); \
JW_OBJ(j, writer); \
} while (0)
Str city = StrInitFromZstr("New York");
JW_OBJ(json, {
JW_STR_KV(json, "name", name);
JW_STR_KV(json, "city", city);
u32 year = 2024;
JW_OBJ(json, {
JW_INT_KV(json, "count", count);
JW_FLT_KV(json, "score", score);
bool visible = false;
JW_OBJ(json, {
JW_BOOL_KV(json, "enabled", enabled);
JW_BOOL_KV(json, "visible", visible);
Person person = {1001, StrInitFromZstr("Bob"), 25, true, 50000.0};
JW_OBJ(json, {
JW_INT_KV(json, "id", person.id);
JW_STR_KV(json, "name", person.name);
Config config = {false, 30, StrInitFromZstr("INFO")};
JW_OBJ(json, {
JW_BOOL_KV(json, "debug_mode", config.debug_mode);
JW_INT_KV(json, "timeout", config.timeout);
VecPushBack(&languages, lang3);
JW_OBJ(json, { JW_ARR_KV(json, "languages", languages, lang, { JW_STR(json, lang); }); });
const char* expected = "{\"languages\":[\"C\",\"Python\",\"Rust\"]}";
};
JW_OBJ(json, {
JW_OBJ_KV(json, "user", {
JW_STR_KV(json, "name", data.user.name);
VecPushBack(&product.tags, tag3);
JW_OBJ(json, {
JW_INT_KV(json, "id", product.id);
JW_STR_KV(json, "name", product.name);
// Write completely empty object
JW_OBJ(
json,
{
Vec(Str) empty_strings = VecInitWithDeepCopy(NULL, StrDeinit);
JW_OBJ(json, {
JW_ARR_KV(json, "numbers", empty_numbers, num, { JW_INT(json, num); });
JW_ARR_KV(json, "strings", empty_strings, str, { JW_STR(json, str); });
Str empty_desc = StrInit();
JW_OBJ(json, {
JW_STR_KV(json, "name", empty_name);
JW_STR_KV(json, "description", empty_desc);
f64 delta = -0.001;
JW_OBJ(json, {
JW_INT_KV(json, "temp", temp);
JW_FLT_KV(json, "balance", balance);
f64 small_float = 2.2250738585072014e-308;
JW_OBJ(json, {
JW_INT_KV(json, "big_int", big_int);
JW_FLT_KV(json, "big_float", big_float);
bool bool_false = false;
JW_OBJ(json, {
JW_INT_KV(json, "int_zero", int_zero);
JW_FLT_KV(json, "float_zero", float_zero);
Str data = StrInitFromZstr("line1\nline2\ttab");
JW_OBJ(json, {
JW_STR_KV(json, "path", path);
JW_STR_KV(json, "message", message);
Str tab = StrInitFromZstr("\t");
JW_OBJ(json, {
JW_STR_KV(json, "quotes", quotes);
JW_STR_KV(json, "backslash", backslash);
Vec(i32) empty_list = VecInit();
JW_OBJ(json, {
JW_OBJ_KV(
json,
i32 x_value = 1;
JW_OBJ(json, {
JW_OBJ_KV(
json,
i64 minus_one = -1;
JW_OBJ(json, {
JW_INT_KV(json, "max_int", max_int);
JW_INT_KV(json, "min_int", min_int);
f64 negative_tiny = -0.000001;
JW_OBJ(json, {
JW_FLT_KV(json, "tiny", tiny);
JW_FLT_KV(json, "huge", huge);
// Single integer
i32 single_int = 42;
JW_OBJ(json1, { JW_INT_KV(json1, "value", single_int); });
// Single string
// Single string
Str single_str = StrInitFromZstr("hello");
JW_OBJ(json2, { JW_STR_KV(json2, "text", single_str); });
// Single boolean
// Single boolean
bool single_bool = true;
JW_OBJ(json3, { JW_BOOL_KV(json3, "flag", single_bool); });
// Single float
// Single float
f64 single_float = 3.14;
JW_OBJ(json4, { JW_FLT_KV(json4, "pi", single_float); });
const char* expected1 = "{\"value\":42}";
};
JW_OBJ(json, {
JW_OBJ_KV(json, "user", {
JW_INT_KV(json, "id", data.user.id);
};
JW_OBJ(json, {
JW_OBJ_KV(json, "company", {
JW_OBJ_KV(json, "departments", {
VecPushBack(&response.data, sym);
JW_OBJ(json, {
JW_BOOL_KV(json, "status", response.status);
JW_STR_KV(json, "message", response.message);
VecPushBack(&functions, func2);
JW_OBJ(json, {
JW_ARR_KV(json, "functions", functions, func, {
JW_OBJ(json, {
JW_OBJ(json, {
JW_ARR_KV(json, "functions", functions, func, {
JW_OBJ(json, {
JW_INT_KV(json, "id", func.id);
JW_STR_KV(json, "name", func.name);
result.owned_by = StrInitFromZstr("user1");
JW_OBJ(json, {
JW_INT_KV(json, "binary_id", result.binary_id);
JW_STR_KV(json, "binary_name", result.binary_name);
VecPushBack(&symbols, sym2);
JW_OBJ(json, {
JW_OBJ_KV(json, "functions", {
VecForeach(&symbols, symbol, {
Str test_name = StrInitFromZstr("test");
JW_OBJ(json, {
JW_OBJ_KV(json, "level1", {
JW_OBJ_KV(json, "level2", {
VecPushBack(&booleans, bool3);
JW_OBJ(json, {
JW_ARR_KV(json, "numbers", numbers, num, { JW_INT(json, num); });
JW_ARR_KV(json, "strings", strings, str, { JW_STR(json, str); });
- In
RoundTrip.c:100
:
// Write to JSON
Str json = StrInit();
JW_OBJ(json, {
JW_INT_KV(json, "count", original.count);
JW_FLT_KV(json, "temperature", original.temperature);
- In
RoundTrip.c:172
:
// Write to JSON
Str json = StrInit();
JW_OBJ(json, {
JW_INT_KV(json, "big_int", original.big_int);
JW_INT_KV(json, "negative_int", original.negative_int);
- In
RoundTrip.c:242
:
// Write to JSON
Str json = StrInit();
JW_OBJ(json, {
JW_BOOL_KV(json, "flag1", original.flag1);
JW_BOOL_KV(json, "flag2", original.flag2);
- In
RoundTrip.c:299
:
// Write to JSON
Str json = StrInit();
JW_OBJ(json, {
JW_STR_KV(json, "empty", original.empty);
JW_STR_KV(json, "simple", original.simple);
- In
RoundTrip.c:374
:
// Write to JSON
Str json = StrInit();
JW_OBJ(json, {
JW_ARR_KV(json, "numbers", original_numbers, num, { JW_INT(json, num); });
JW_ARR_KV(json, "strings", original_strings, str, { JW_STR(json, str); });
- In
RoundTrip.c:459
:
// Write to JSON
Str json = StrInit();
JW_OBJ(json, {
JW_OBJ_KV(json, "user", {
JW_INT_KV(json, "id", original_person.id);
- In
RoundTrip.c:538
:
// Write to JSON
Str json = StrInit();
JW_OBJ(json, {
JW_OBJ_KV(json, "user", {
JW_INT_KV(json, "id", original.user.id);
- In
RoundTrip.c:654
:
// Write to JSON
Str json = StrInit();
JW_OBJ(json, {
JW_STR_KV(json, "empty_string", empty_str);
JW_ARR_KV(json, "empty_numbers", empty_numbers, num, { JW_INT(json, num); });
- In
RoundTrip.c:734
:
// Write to JSON
Str json = StrInit();
JW_OBJ(json, {
JW_INT_KV(json, "max_int", original.max_int);
JW_INT_KV(json, "min_int", original.min_int);