method ReplSession.toJSON
ReplSession.toJSON(expression: string): Promise<string | null>

What an expression is worth in the page, as JSON — null where the page cannot say.

The one way to get a VALUE back rather than a rendering of one: eval answers with what to print, and { name: 'Ada' } printed is not {"name":"Ada"}. The expression is parenthesised on the way out, so an object literal is an object rather than a block with a label in it.

null covers everything it cannot answer — an expression that throws, a value JSON has no word for, a closed session — because a caller always has something else to do with the text.

import type { ReplSession } from './session.ts';

// Defined, not invoked: a real session owns a browser and a bound port.
async function bodyFor(session: ReplSession) {
  await session.toJSON('{ name: "Ada" }'); // '{"name":"Ada"}'
  await session.toJSON('new Date(0)'); // '"1970-01-01T00:00:00.000Z"' — the page's own toJSON

  return await session.toJSON('nothingDeclared'); // null — the page could not say
}

Parameters

expression: string

Return Type

Promise<string | null>

Usage

import { type ReplSession } from "lib/repl/session.ts";