@rbxts/jsnatives



JSON.stringify

Converts a JavaScript value to a JSON string.

Signature

function stringify<T>(data: T): string

Description

The stringify method converts a JavaScript object or value to a JSON string. The method accepts a generic type parameter to ensure type safety of the input value.

Examples

Basic usage

const obj = {a: 1, b: 2, c: 3};
const jsonString = JSON.stringify(obj);
print(jsonString); // {"a":1,"b":2,"c":3}

Working with proxies

const proxiedObj = new Proxy({a: 1, b: 2, c: 3}, {});
const jsonString = JSON.stringify(proxiedObj);
print(jsonString); // {"a":1,"b":2,"c":3}

Working with different data structures

const map = new Map([["a", 1], ["b", 2], ["c", 3]]);
const set = new Set([1, 2, 3]);
const array = [1, 2, 3];
print(JSON.stringify(map)); // {"a":1,"b":2,"c":3}
print(JSON.stringify(set)); // {"1":true,"2":true,"3":true}
print(JSON.stringify(array)); // [1,2,3]