jsonx

package
v0.6.13 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 13, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package jsonx provides best-effort JSON recovery for model output. Streamed tool arguments are often malformed — unescaped control characters, bad escapes, or a stream cut off mid-token — so a strict decode would discard everything. These helpers repair and complete such input so callers can salvage whatever was received instead of failing the surrounding response.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Complete

func Complete(raw string) (string, bool)

Complete closes the open containers and string of a truncated JSON document, returning a syntactically complete string. It tracks the longest prefix that, once its open containers are closed, parses as valid JSON, and discards any trailing partial token (an unterminated number, literal, key, or dangling separator). An unterminated string in a value position is closed in place so its received characters are kept. ok is false when no non-empty closeable prefix exists.

It salvages tool arguments whose JSON stream was cut short. Callers must still decode the result, which rejects any completion that does not parse.

func DecodeObject

func DecodeObject(raw string) (map[string]any, bool)

DecodeObject strictly decodes raw into a JSON object. ok is false when the input is not a valid, non-null JSON object.

func ParsePartialObject

func ParsePartialObject(raw string) (map[string]any, bool)

ParsePartialObject closes the open containers and strings of a truncated JSON document, then decodes the completed text as an object. ok is false when the input cannot be completed into a valid JSON object.

func Repair

func Repair(source string) string

Repair fixes malformed JSON string literals by escaping raw control characters inside strings and doubling backslashes before invalid escape characters, while preserving valid escapes and \uXXXX sequences. Only ASCII characters carry special meaning here, so iterating over bytes leaves multi-byte UTF-8 sequences untouched.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL