Json — Safe Parsing & Stringifying
Standard JSON.parse and JSON.stringify rely on runtime exceptions (SyntaxError when parsing
malformed text, TypeError when stringifying circular structures or invalid types).
Json wraps these operations in typed Result containers, eliminating runtime exceptions at your
application boundaries.
Safe Parsing with Json.parse
Section titled “Safe Parsing with Json.parse”Json.parse parses a raw JSON string into unknown and captures syntax errors inside an
Err(SyntaxError):
Safe Stringifying with Json.stringify
Section titled “Safe Stringifying with Json.stringify”Json.stringify serializes a value to JSON and captures serialization errors inside an
Err(TypeError):
Problems it solves
Section titled “Problems it solves”- Safe parsing of untrusted webhook and API payloads: Calling native
JSON.parse()on malformed HTTP request bodies or corrupted cache entries throws runtime exceptions that crash endpoints if not surrounded bytry/catch.Json.parsereturns a typedResult<Error, unknown>, turning parse errors into ordinary failure variants. - Resilient serialization for local storage and caching: Serializing dynamic application state
to
localStorageor network sockets can throw unexpected exceptions if objects contain circular references or unsupported types.Json.stringifyreturns aResult<Error, string>, preventing unhandled runtime crashes during state persistence. - Seamless pipeline integration: Raw JSON strings can be parsed and piped directly into schema
validators,
Refinementguards, orMaybe/Resultpipelines without temporary variables or enclosing try-catch statements.