API reference > @kontsedal/olas-core > serializeForScript
serializeForScript() function
Serialize a value as a JavaScript expression that is safe inside an inline<script>. Use it for server state inlined into HTML, such as aroot.dehydrate() payload:
ts
const html = `<script>window.__OLAS_STATE__ = ${serializeForScript(root.dehydrate())}</script>`The result is JSON.parse("…") over the JSON, with every character that could end the string, the script or an attribute written as a \uXXXX escape. Two reasons for JSON.parse over a plain object literal: - a key named __proto__ stays an own property, where an object literal would make its value the object's prototype; - U+2028 and U+2029 cannot reach the script source raw.
Throws what JSON.stringify throws, for a BigInt or a cycle.
Signature:
typescript
export declare function serializeForScript(value: unknown): string;Parameters
Parameter | Type | Description |
|---|---|---|
value | unknown |
Returns:
string