Bun

namespace

JSON5

namespace JSON5

JSON5 related APIs

  • function parse(
    input: string
    ): unknown;

    Parse a JSON5 string into a JavaScript value.

    JSON5 is a superset of JSON based on ECMAScript 5.1 that supports comments, trailing commas, unquoted keys, single-quoted strings, hex numbers, Infinity, NaN, and more.

    @param input

    The JSON5 string to parse

    @returns

    A JavaScript value

    import { JSON5 } from "bun";
    
    const result = JSON5.parse(`{
      // This is a comment
      name: 'my-app',
      version: '1.0.0', // trailing comma is allowed
      hex: 0xDEADbeef,
      half: .5,
      infinity: Infinity,
    }`);
    
  • function stringify(
    input: unknown,
    replacer?: null,
    space?: string | number
    ): undefined | string;

    Convert a JavaScript value into a JSON5 string. Object keys that are valid identifiers are unquoted, strings use double quotes, Infinity and NaN are represented as literals, and indented output includes trailing commas.

    @param input

    The JavaScript value to stringify.

    @param replacer

    Currently not supported.

    @param space

    A number for how many spaces each level of indentation gets, or a string used as indentation. The number is clamped between 0 and 10, and the first 10 characters of the string are used.

    @returns

    A JSON5 string, or undefined if the input is undefined, a function, or a symbol.

    import { JSON5 } from "bun";
    
    console.log(JSON5.stringify({ a: 1, b: "two" }));
    // {a:1,b:"two"}
    
    console.log(JSON5.stringify({ a: 1, b: 2 }, null, 2));
    // {
    //   a: 1,
    //   b: 2,
    // }