Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/ayonli_jsext/json/index.ts>parseAs

A JavaScript extension package for building strong and modern applications.
Latest
function parseAs
import { parseAs } from "https://deno.land/x/ayonli_jsext@v0.9.72/json/index.ts";

Converts a JSON string into an object of the given type.

The type can be String, Number, BigInt, Boolean, Date, Array, Object or any class constructor (including Error types). If the class has a static fromJSON(data: any): T method, it will be invoked to create the instance (unless the parsed data is null, which will be skipped without further processing).

This function generally does not perform loose conversion between types, for example, parseAs('"123"', Number) will not work, it only reverses to the same type before the data are encoded.

However, for compatibility support, there are some exceptions allowed, which are:

  • string => Date
  • number or string => bigint
  • array => Buffer or TypedArray (e.g. Uint8Array), when the data only contains integers.
  • object => Buffer or TypedArray (e.g. Uint8Array), if the data are encoded by JSON.stringify().
  • customized in fromJSON()

If the data cannot be converted to the given type, this function returns null.

Parameters

text: string
type: StringConstructor

Returns

string | null

Parameters

text: string
type: NumberConstructor

Returns

number | null

Parameters

text: string
type: BigIntConstructor

Returns

bigint | null

Parameters

text: string
type: BooleanConstructor

Returns

boolean | null

Parameters

text: string
type: Constructor<T> & { fromJSON?(data: any): T; }

Returns

T | null