### jcon_start - Initialize JSON Document Source: https://context7.com/buckleypaul/jcon/llms.txt Initializes the JSON emitter, resets internal state, installs the putc callback, and emits the opening brace of the root object. The `minify` parameter controls whether output is compact (no whitespace) or pretty-printed with indentation. ```APIDOC ## jcon_start - Initialize JSON Document ### Description Initializes the JSON emitter, resets internal state, installs the putc callback, and emits the opening brace of the root object. The `minify` parameter controls whether output is compact (no whitespace) or pretty-printed with indentation. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```c #include "jcon.h" #include // Simple putc callback that writes to stdout static int stdout_putc(void *ctx, char c) { (void)ctx; // unused in this example return putchar(c) != EOF ? 0 : -1; } int main(void) { // Start pretty-printed JSON document jcon_start(false, stdout_putc, NULL); // Add content... jcon_add_string("message", "Hello World"); jcon_status_t status = jcon_end(); // Output: // { // "message": "Hello World" // } // Start minified JSON document jcon_start(true, stdout_putc, NULL); jcon_add_string("message", "Compact"); jcon_end(); // Output: {"message":"Compact"} return status == JCON_OK ? 0 : 1; } ``` ### Response #### Success Response (200) None #### Response Example None ``` -------------------------------- ### Complete JCON Integration with UART Output Source: https://context7.com/buckleypaul/jcon/llms.txt This example demonstrates integrating JCON with a UART driver, including context passing for the UART handle and basic error handling for transmission failures. It shows how to send status reports with various data types and nested structures. ```c #include "jcon.h" #include // Example UART context structure typedef struct { volatile uint32_t *data_reg; volatile uint32_t *status_reg; uint32_t timeout_ms; } uart_handle_t; // UART putc with timeout static int uart_putc(void *ctx, char c) { uart_handle_t *uart = (uart_handle_t *)ctx; uint32_t start = get_tick_ms(); // Wait for TX ready with timeout while (!(*uart->status_reg & TX_READY)) { if ((get_tick_ms() - start) > uart->timeout_ms) { return -1; // Timeout error } } *uart->data_reg = (uint32_t)c; return 0; // Success } void send_status_report(uart_handle_t *uart) { // Pass UART handle as context jcon_start(true, uart_putc, uart); jcon_add_string("firmware", "v1.2.3"); jcon_add_uint("uptime_ms", get_tick_ms()); jcon_object_start("sensors"); jcon_add_int("temp", read_temperature()); jcon_add_uint("pressure", read_pressure()); jcon_object_end(); jcon_array_start("errors"); uint32_t errors = get_error_flags(); if (errors & ERR_OVERTEMP) jcon_add_string(NULL, "overtemp"); if (errors & ERR_LOWBATT) jcon_add_string(NULL, "low_battery"); if (errors & ERR_COMMS) jcon_add_string(NULL, "comm_fault"); jcon_array_end(); jcon_status_t status = jcon_end(); if (status == JCON_ERR_IO) { // Handle UART transmission failure log_error("JSON transmission failed"); } } ``` -------------------------------- ### Finalize JSON Document with jcon_end Source: https://context7.com/buckleypaul/jcon/llms.txt Closes the root object and returns the final status. The writer remains inactive until the next start call. ```c #include "jcon.h" static int buffer_putc(void *ctx, char c) { char **buf = (char **)ctx; *(*buf)++ = c; return 0; } int main(void) { char output[256]; char *ptr = output; jcon_start(true, buffer_putc, &ptr); jcon_add_int("count", 42); jcon_status_t status = jcon_end(); *ptr = '\0'; // null-terminate if (status == JCON_OK) { printf("Generated: %s\n", output); // {"count":42} } else if (status == JCON_ERR_IO) { printf("I/O error during emission\n"); } else if (status == JCON_ERR_USAGE) { printf("API misuse detected\n"); } return 0; } ``` -------------------------------- ### Build and Run jcon hello sample Source: https://github.com/buckleypaul/jcon/blob/main/samples/zephyr/hello_jcon/README.rst Build the sample for native_sim and run it. If jcon is not a listed module, use ZEPHYR_EXTRA_MODULES to specify its path. ```console west build -b native_sim samples/zephyr/hello_jcon west build -t run ``` ```console west build -b native_sim samples/zephyr/hello_jcon \ -- -DZEPHYR_EXTRA_MODULES=/absolute/path/to/jcon west build -t run ``` -------------------------------- ### Initialize JSON Document with jcon_start Source: https://context7.com/buckleypaul/jcon/llms.txt Initializes the emitter and sets the output format. The putc callback handles character-at-a-time output to the desired sink. ```c #include "jcon.h" #include // Simple putc callback that writes to stdout static int stdout_putc(void *ctx, char c) { (void)ctx; // unused in this example return putchar(c) != EOF ? 0 : -1; } int main(void) { // Start pretty-printed JSON document jcon_start(false, stdout_putc, NULL); // Add content... jcon_add_string("message", "Hello World"); jcon_status_t status = jcon_end(); // Output: // { // "message": "Hello World" // } // Start minified JSON document jcon_start(true, stdout_putc, NULL); jcon_add_string("message", "Compact"); jcon_end(); // Output: {"message":"Compact"} return status == JCON_OK ? 0 : 1; } ``` -------------------------------- ### Expected Output of jcon hello sample Source: https://github.com/buckleypaul/jcon/blob/main/samples/zephyr/hello_jcon/README.rst The expected output includes the board name, uptime in milliseconds, features, and the jcon status. ```text jcon hello sample { "board": "native_sim", "uptime_ms": 3, "features": [ "streaming", "zero-heap" ] } jcon status: 0 ``` -------------------------------- ### Build jcon with Makefile Source: https://github.com/buckleypaul/jcon/blob/main/README.md Commands to run tests or build the object file for linking. ```sh make # run debug, release (NDEBUG), and float test configurations make lib # build/jcon.o for consumer linking ``` -------------------------------- ### Emit JSON using jcon Source: https://github.com/buckleypaul/jcon/blob/main/README.md Demonstrates defining a putc sink and using the jcon API to construct a JSON object. ```c static int uart_putc(void *ctx, char c) { (void)ctx; return uart_write(&c, 1) == 1 ? 0 : -1; } jcon_start(false, uart_putc, NULL); jcon_add("id", 42); jcon_array_start("log"); jcon_add(NULL, "boot"); jcon_array_end(); jcon_end(); ``` -------------------------------- ### Zephyr RTOS Integration with printk Source: https://context7.com/buckleypaul/jcon/llms.txt This C code demonstrates JCON integration as a Zephyr module, outputting JSON via `printk`. Ensure Zephyr kernel and sys headers are available. ```c #include #include #include static int printk_putc(void *ctx, char c) { (void)ctx; printk("%c", c); return 0; } int main(void) { printk("System boot\n"); jcon_start(false, printk_putc, NULL); jcon_add_string("board", CONFIG_BOARD); jcon_add_uint("uptime_ms", k_uptime_get_32()); jcon_array_start("features"); jcon_add_string(NULL, "streaming"); jcon_add_string(NULL, "zero-heap"); jcon_array_end(); jcon_object_start("memory"); jcon_add_uint("heap_free", k_mem_pool_free_get()); jcon_object_end(); jcon_status_t st = jcon_end(); printk("\njcon status: %d\n", (int)st); return 0; } ``` -------------------------------- ### Enable jcon in Zephyr Source: https://github.com/buckleypaul/jcon/blob/main/README.md Configuration setting for Zephyr projects. ```text CONFIG_JCON=y ``` -------------------------------- ### Basic Zephyr CMake Configuration Source: https://github.com/buckleypaul/jcon/blob/main/samples/zephyr/hello_jcon/CMakeLists.txt This CMakeLists.txt file configures a Zephyr RTOS project. It finds the Zephyr package and specifies the main source file for the application. ```cmake cmake_minimum_required(VERSION 3.20.0) find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) project(hello_jcon) target_sources(app PRIVATE src/main.c) ``` -------------------------------- ### Integrate jcon with CMake Source: https://github.com/buckleypaul/jcon/blob/main/README.md Add jcon as a subdirectory and link it to a target. ```cmake add_subdirectory(path/to/jcon) target_link_libraries(my_app PRIVATE jcon::jcon) ``` -------------------------------- ### Emit Signed and Unsigned Integers with jcon Source: https://context7.com/buckleypaul/jcon/llms.txt Use jcon_add_int and jcon_add_uint to emit signed and unsigned integers. Explicit width variants like jcon_add_int32, jcon_add_uint32, jcon_add_int64, and jcon_add_uint64 are available for precise control. ```c #include "jcon.h" #include void emit_system_stats(void) { jcon_start(true, uart_putc, NULL); // Basic integers jcon_add_int("temperature", -15); jcon_add_uint("uptime_sec", 3600u); // Explicit width integers jcon_add_int32("error_code", INT32_MIN); jcon_add_uint32("crc32", UINT32_MAX); jcon_add_int64("timestamp_ns", -9223372036854775807LL); jcon_add_uint64("total_bytes", 18446744073709551615ULL); jcon_end(); // Output: {"temperature":-15,"uptime_sec":3600,"error_code":-2147483648, // "crc32":4294967295,"timestamp_ns":-9223372036854775807, // "total_bytes":18446744073709551615} } ``` -------------------------------- ### Emit Values with jcon_add Macro Source: https://context7.com/buckleypaul/jcon/llms.txt Uses C11 _Generic to automatically select the correct emitter function based on the variable type. Pass NULL as the name when adding items to an array. ```c #include "jcon.h" int main(void) { jcon_start(true, stdout_putc, NULL); // Generic dispatch selects the right emitter automatically int count = 42; unsigned int flags = 0xFF; bool enabled = true; char grade = 'A'; const char *name = "sensor"; jcon_add("count", count); // -> jcon_add_int jcon_add("flags", flags); // -> jcon_add_uint jcon_add("enabled", enabled); // -> jcon_add_bool jcon_add("grade", grade); // -> jcon_add_char jcon_add("name", name); // -> jcon_add_string // 64-bit types long long big = 9223372036854775807LL; jcon_add("big", big); // -> jcon_add_int64 jcon_end(); // Output: {"count":42,"flags":255,"enabled":true,"grade":"A","name":"sensor","big":9223372036854775807} return 0; } ``` -------------------------------- ### Ring Buffer Sink for Buffered JSON Output Source: https://context7.com/buckleypaul/jcon/llms.txt Use this C code with a ring buffer for buffered JSON output when the sink might not be immediately ready. It handles potential buffer overflows. ```c #include "jcon.h" #include #include #define RING_SIZE 512 typedef struct { char data[RING_SIZE]; size_t head; size_t tail; } ring_buffer_t; static bool ring_put(ring_buffer_t *rb, char c) { size_t next = (rb->head + 1) % RING_SIZE; if (next == rb->tail) return false; // Full rb->data[rb->head] = c; rb->head = next; return true; } static int ring_putc(void *ctx, char c) { return ring_put((ring_buffer_t *)ctx, c) ? 0 : -1; } // Usage void buffer_json_message(ring_buffer_t *rb, int id, const char *msg) { jcon_start(true, ring_putc, rb); jcon_add_int("id", id); jcon_add_string("msg", msg); jcon_add_uint("ts", get_timestamp()); if (jcon_end() == JCON_ERR_IO) { // Ring buffer full - message truncated handle_buffer_overflow(); } } ``` -------------------------------- ### Emit Floating-Point Values with JCON Source: https://context7.com/buckleypaul/jcon/llms.txt Use jcon_add_float and jcon_add_double to emit floating-point values. Ensure JCON_ENABLE_FLOAT is defined at compile time. The _Generic dispatch allows using jcon_add for automatic type detection. ```c // Compile with -DJCON_ENABLE_FLOAT #include "jcon.h" void emit_sensor_readings(void) { jcon_start(true, uart_putc, NULL); float temperature = 23.5f; double pressure = 101325.123456; jcon_add_float("temp_c", temperature); jcon_add_double("pressure_pa", pressure); // With _Generic dispatch jcon_add("humidity", 65.5); // -> jcon_add_double jcon_add("voltage", 3.3f); // -> jcon_add_float jcon_end(); // Output: {"temp_c":23.5,"pressure_pa":101325,"humidity":65.5,"voltage":3.3} } ``` -------------------------------- ### Configure JCON in CMake Source: https://github.com/buckleypaul/jcon/blob/main/zephyr/CMakeLists.txt Use this CMake logic to include JCON in a Zephyr build system. It handles source file registration, include paths, and conditional feature flags. ```cmake if(CONFIG_JCON) zephyr_library() zephyr_library_sources(${ZEPHYR_CURRENT_MODULE_DIR}/src/jcon.c) zephyr_include_directories(${ZEPHYR_CURRENT_MODULE_DIR}/include) zephyr_library_compile_definitions(JCON_MAX_DEPTH=${CONFIG_JCON_MAX_DEPTH}) if(CONFIG_JCON_ENABLE_FLOAT) zephyr_library_compile_definitions(JCON_ENABLE_FLOAT) endif() endif() ``` -------------------------------- ### Create JSON Arrays with JCON Source: https://context7.com/buckleypaul/jcon/llms.txt Use jcon_array_start and jcon_array_end to create JSON arrays. Elements inside arrays use NULL for the name parameter. This function supports arrays of integers, mixed types, and arrays containing objects. ```c #include "jcon.h" void emit_telemetry(int *samples, size_t count) { jcon_start(false, uart_putc, NULL); jcon_add_string("type", "telemetry"); // Simple array of integers jcon_array_start("samples"); for (size_t i = 0; i < count; i++) { jcon_add_int(NULL, samples[i]); // NULL name inside array } jcon_array_end(); // Array of mixed types jcon_array_start("mixed"); jcon_add_int(NULL, 1); jcon_add_string(NULL, "two"); jcon_add_bool(NULL, true); jcon_add_null(NULL); jcon_array_end(); // Array containing objects jcon_array_start("events"); jcon_object_start(NULL); // NULL name for object inside array jcon_add_string("event", "start"); jcon_add_int("time", 0); jcon_object_end(); jcon_object_start(NULL); jcon_add_string("event", "stop"); jcon_add_int("time", 100); jcon_object_end(); jcon_array_end(); jcon_end(); // Output: // { // "type": "telemetry", // "samples": [10, 20, 30], // "mixed": [1, "two", true, null], // "events": [ // {"event": "start", "time": 0}, // {"event": "stop", "time": 100} // ] // } } ``` -------------------------------- ### Raw JSON Emitter Source: https://context7.com/buckleypaul/jcon/llms.txt Emits a raw string without quotes. ```APIDOC ## jcon_add_raw ### Description Emits a raw string without quotes, useful for pre-formatted JSON fragments, numbers, or literal JSON values. ### Parameters - **key** (const char*) - Required - The JSON key name. - **value** (const char*) - Required - The raw JSON string to emit. ``` -------------------------------- ### Create Nested JSON Objects with JCON Source: https://context7.com/buckleypaul/jcon/llms.txt Use jcon_object_start and jcon_object_end to create nested JSON objects. When inside an object, the name parameter must be non-NULL; when inside an array, pass NULL. ```c #include "jcon.h" void emit_device_info(void) { jcon_start(false, uart_putc, NULL); jcon_add_string("device_id", "MCU-001"); // Nested object for configuration jcon_object_start("config"); jcon_add_int("baud_rate", 115200); jcon_add_bool("echo", true); // Deeply nested object jcon_object_start("pins"); jcon_add_int("tx", 1); jcon_add_int("rx", 2); jcon_object_end(); jcon_object_end(); jcon_add_string("status", "active"); jcon_end(); // Output: // { // "device_id": "MCU-001", // "config": { // "baud_rate": 115200, // "echo": true, // "pins": { // "tx": 1, // "rx": 2 // } // }, // "status": "active" // } } ``` -------------------------------- ### String Emitter Source: https://context7.com/buckleypaul/jcon/llms.txt Emits a string value wrapped in double quotes. ```APIDOC ## jcon_add_string ### Description Emits a string value. Note that the string is emitted verbatim; the caller must ensure it contains no unescaped quotes, backslashes, or control characters. ### Parameters - **key** (const char*) - Required - The JSON key name. - **value** (const char*) - Required - The string value to emit. ``` -------------------------------- ### Floating Point Emitters Source: https://context7.com/buckleypaul/jcon/llms.txt Functions to emit floating-point values using %g format. Requires JCON_ENABLE_FLOAT to be defined. ```APIDOC ## jcon_add_float / jcon_add_double ### Description Emits a floating-point value to the current JSON stream. ### Parameters - **name** (const char*) - Required - The key for the JSON object field. - **value** (float/double) - Required - The numeric value to emit. ### Example ```c jcon_add_float("temp_c", 23.5f); jcon_add_double("pressure_pa", 101325.123456); ``` ``` -------------------------------- ### Integer Emitters Source: https://context7.com/buckleypaul/jcon/llms.txt Functions to emit signed and unsigned integer values, including explicit width variants. ```APIDOC ## jcon_add_int / jcon_add_uint ### Description Emits signed and unsigned integer values. Typed variants include jcon_add_int32, jcon_add_uint32, jcon_add_int64, and jcon_add_uint64 for explicit width control. ### Parameters - **key** (const char*) - Required - The JSON key name. - **value** (int/uint/int32_t/uint32_t/int64_t/uint64_t) - Required - The integer value to emit. ``` -------------------------------- ### jcon_status - Check Current Status Source: https://context7.com/buckleypaul/jcon/llms.txt Peeks at the sticky status without closing the document. Useful when the caller wants to bail out early on I/O failure without waiting until `jcon_end()`. ```APIDOC ## jcon_status - Check Current Status ### Description Peeks at the sticky status without closing the document. Useful when the caller wants to bail out early on I/O failure without waiting until `jcon_end()`. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```c #include "jcon.h" void emit_sensor_data(int *readings, size_t count) { jcon_start(true, uart_putc, NULL); jcon_array_start("readings"); for (size_t i = 0; i < count; i++) { jcon_add_int(NULL, readings[i]); // Check for early failure if (jcon_status() != JCON_OK) { // I/O error occurred, bail out break; } } jcon_array_end(); jcon_end(); } ``` ### Response #### Success Response (200) - **status** (jcon_status_t) - The current sticky status of the JSON emitter. #### Response Example None ``` -------------------------------- ### Check Status with jcon_status Source: https://context7.com/buckleypaul/jcon/llms.txt Allows checking for errors during emission without closing the document. Useful for early termination on I/O failures. ```c #include "jcon.h" void emit_sensor_data(int *readings, size_t count) { jcon_start(true, uart_putc, NULL); jcon_array_start("readings"); for (size_t i = 0; i < count; i++) { jcon_add_int(NULL, readings[i]); // Check for early failure if (jcon_status() != JCON_OK) { // I/O error occurred, bail out break; } } jcon_array_end(); jcon_end(); } ``` -------------------------------- ### Arrays Source: https://context7.com/buckleypaul/jcon/llms.txt Functions to create JSON arrays. Elements inside arrays use NULL for the name parameter. ```APIDOC ## jcon_array_start / jcon_array_end ### Description Starts and ends a JSON array. Elements added within the array block should use NULL as the name parameter. ### Parameters - **name** (const char*) - Required - The key for the array, or NULL if nested. ### Example ```c jcon_array_start("samples"); jcon_add_int(NULL, 10); jcon_add_int(NULL, 20); jcon_array_end(); ``` ``` -------------------------------- ### Boolean Emitter Source: https://context7.com/buckleypaul/jcon/llms.txt Emits JSON true or false literals. ```APIDOC ## jcon_add_bool ### Description Emits a JSON boolean literal. For generic dispatch, use a bool variable or explicit cast. ### Parameters - **key** (const char*) - Required - The JSON key name. - **value** (bool) - Required - The boolean value to emit. ``` -------------------------------- ### jcon_end - Finalize JSON Document Source: https://context7.com/buckleypaul/jcon/llms.txt Closes the root JSON object by emitting the closing brace (plus a trailing newline when pretty-printing) and returns the final sticky status. After this call, the writer is inactive until the next `jcon_start()`. ```APIDOC ## jcon_end - Finalize JSON Document ### Description Closes the root JSON object by emitting the closing brace (plus a trailing newline when pretty-printing) and returns the final sticky status. After this call, the writer is inactive until the next `jcon_start()`. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```c #include "jcon.h" static int buffer_putc(void *ctx, char c) { char **buf = (char **)ctx; *(*buf)++ = c; return 0; } int main(void) { char output[256]; char *ptr = output; jcon_start(true, buffer_putc, &ptr); jcon_add_int("count", 42); jcon_status_t status = jcon_end(); *ptr = '\0'; // null-terminate if (status == JCON_OK) { printf("Generated: %s\n", output); // {"count":42} } else if (status == JCON_ERR_IO) { printf("I/O error during emission\n"); } else if (status == JCON_ERR_USAGE) { printf("API misuse detected\n"); } return 0; } ``` ### Response #### Success Response (200) - **status** (jcon_status_t) - The final sticky status of the JSON emission. #### Response Example None ``` -------------------------------- ### jcon_add - Generic Value Emitter Source: https://context7.com/buckleypaul/jcon/llms.txt The `jcon_add(name, value)` macro uses C11 `_Generic` to dispatch to the appropriate typed emitter based on the value's type at compile time. Inside arrays, pass `NULL` for the name; inside objects, name must be non-NULL. ```APIDOC ## jcon_add - Generic Value Emitter ### Description The `jcon_add(name, value)` macro uses C11 `_Generic` to dispatch to the appropriate typed emitter based on the value's type at compile time. Inside arrays, pass `NULL` for the name; inside objects, name must be non-NULL. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```c #include "jcon.h" int main(void) { jcon_start(true, stdout_putc, NULL); // Generic dispatch selects the right emitter automatically int count = 42; unsigned int flags = 0xFF; bool enabled = true; char grade = 'A'; const char *name = "sensor"; jcon_add("count", count); // -> jcon_add_int jcon_add("flags", flags); // -> jcon_add_uint jcon_add("enabled", enabled); // -> jcon_add_bool jcon_add("grade", grade); // -> jcon_add_char jcon_add("name", name); // -> jcon_add_string // 64-bit types long long big = 9223372036854775807LL; jcon_add("big", big); // -> jcon_add_int64 jcon_end(); // Output: {"count":42,"flags":255,"enabled":true,"grade":"A","name":"sensor","big":9223372036854775807} return 0; } ``` ### Response #### Success Response (200) None #### Response Example None ``` -------------------------------- ### Character Emitter Source: https://context7.com/buckleypaul/jcon/llms.txt Emits a single character as a JSON string. ```APIDOC ## jcon_add_char ### Description Emits a single character as a JSON string (wrapped in quotes). ### Parameters - **key** (const char*) - Required - The JSON key name. - **value** (char) - Required - The character to emit. ``` -------------------------------- ### Null Emitter Source: https://context7.com/buckleypaul/jcon/llms.txt Emits the JSON null literal. ```APIDOC ## jcon_add_null ### Description Emits the JSON null literal for absent or undefined values. ### Parameters - **key** (const char*) - Required - The JSON key name. ``` -------------------------------- ### Emit Raw JSON with jcon_add_raw Source: https://context7.com/buckleypaul/jcon/llms.txt Emits a raw string without quotes, suitable for embedding pre-formatted JSON fragments, numbers, or literal JSON values. This bypasses jcon's standard formatting and escaping. ```c #include "jcon.h" #include void emit_with_raw_values(double precise_value) { char formatted[32]; jcon_start(true, uart_putc, NULL); // Emit a pre-formatted floating point with specific precision snprintf(formatted, sizeof(formatted), "%.10f", precise_value); jcon_add_raw("precise", formatted); // Embed raw JSON literal jcon_add_raw("coords", "[1.5, 2.5, 3.5]"); // Embed a raw object jcon_add_raw("nested", "{\"x\":1,\"y\":2}"); jcon_end(); // Output: {"precise":3.1415926536,"coords":[1.5, 2.5, 3.5],"nested":{"x":1,"y":2}} } ``` -------------------------------- ### Nested Objects Source: https://context7.com/buckleypaul/jcon/llms.txt Functions to create nested JSON objects within the document structure. ```APIDOC ## jcon_object_start / jcon_object_end ### Description Starts and ends a nested JSON object. When inside an object, the name must be provided; when inside an array, pass NULL. ### Parameters - **name** (const char*) - Required - The key for the object, or NULL if inside an array. ### Example ```c jcon_object_start("config"); jcon_add_int("baud_rate", 115200); jcon_object_end(); ``` ``` -------------------------------- ### Emit Booleans with jcon_add_bool Source: https://context7.com/buckleypaul/jcon/llms.txt Emits JSON true or false literals. For generic dispatch, use a bool variable or an explicit cast to ensure correct handling. ```c #include "jcon.h" void emit_device_status(bool power_on, bool error_flag) { jcon_start(true, uart_putc, NULL); jcon_add_bool("power", power_on); jcon_add_bool("error", error_flag); jcon_add_bool("ready", true); // Direct call works // For generic dispatch, use a bool variable or cast bool active = true; jcon_add("active", active); // Dispatches to jcon_add_bool jcon_add("locked", (bool)false);// Cast ensures bool dispatch jcon_end(); // Output: {"power":true,"error":false,"ready":true,"active":true,"locked":false} } ``` -------------------------------- ### Emit Strings with jcon_add_string Source: https://context7.com/buckleypaul/jcon/llms.txt Emits a string value wrapped in double quotes. The caller is responsible for ensuring the string contains no unescaped quotes, backslashes, or control characters, as strings are emitted verbatim. ```c #include "jcon.h" void emit_log_entry(const char *level, const char *message) { jcon_start(false, uart_putc, NULL); jcon_add_string("level", level); jcon_add_string("message", message); jcon_add_string("source", "main.c"); // IMPORTANT: Strings are NOT escaped - caller responsibility // Safe: jcon_add_string("text", "hello world"); // Unsafe: jcon_add_string("text", "has \"quotes\""); // Invalid JSON! // For pre-escaped or raw JSON, use jcon_add_raw jcon_end(); // Output: // { // "level": "INFO", // "message": "System started", // "source": "main.c" // } } ``` -------------------------------- ### Emit Null Values with jcon_add_null Source: https://context7.com/buckleypaul/jcon/llms.txt Emits the JSON null literal. Use this function when a value is absent or undefined, ensuring correct JSON representation for optional fields. ```c #include "jcon.h" void emit_optional_fields(const char *email, const char *phone) { jcon_start(true, uart_putc, NULL); if (email != NULL) { jcon_add_string("email", email); } else { jcon_add_null("email"); } if (phone != NULL) { jcon_add_string("phone", phone); } else { jcon_add_null("phone"); } jcon_end(); // Output with email="test@example.com", phone=NULL: // {"email":"test@example.com","phone":null} } ``` -------------------------------- ### Emit Single Characters with jcon_add_char Source: https://context7.com/buckleypaul/jcon/llms.txt Emits a single character as a JSON string (wrapped in quotes). This is useful for representing character data within JSON. ```c #include "jcon.h" void emit_grade_report(char letter_grade, int score) { jcon_start(true, uart_putc, NULL); jcon_add_char("grade", letter_grade); jcon_add_int("score", score); jcon_add_char("status", score >= 60 ? 'P' : 'F'); jcon_end(); // Output: {"grade":"A","score":95,"status":"P"} } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.