Reference

API reference

Every export of seroval, with its signature and options.

Serialize to JavaScript

FunctionReturnsGuide
serialize(value, options?)stringSerialize to JavaScript
serializeAsync(value, options?)Promise<string>Serialize to JavaScript
deserialize(source)the valueSerialize to JavaScript

Options: plugins, disabledFeatures, depthLimit, compactArrayBufferViews.

JSON

FunctionReturnsGuide
toJSON(value, options?)SerovalJSONJSON
toJSONAsync(value, options?)Promise<SerovalJSON>JSON
fromJSON(tree, options?)the valueJSON
compileJSON(tree, options?)stringJSON

fromJSON options: plugins, disabledFeatures, depthLimit, maxBase64Length. compileJSON options: plugins.

Cross-reference

FunctionReturnsGuide
crossSerialize(value, options?)stringCross-reference
crossSerializeAsync(value, options?)Promise<string>Cross-reference
crossSerializeStream(value, options)stop functionStreaming
toCrossJSON(value, options?)SerovalNodeCross-reference
toCrossJSONAsync(value, options?)Promise<SerovalNode>Cross-reference
toCrossJSONStream(value, options)stop functionStreaming
fromCrossJSON(node, options)the valueCross-reference
createCrossDeserializer(options)CrossDeserializerStreaming
getCrossReferenceHeader(scopeId?)stringCross-reference
new Serializer(options)SerializerStreaming

Streams

ExportDescriptionGuide
createStream()Creates a stream that keeps every value.Streams
isStream(value)Whether a value is a seroval stream.
createLiveStream(options?)Creates a single-reader stream with backpressure. Returns { stream, producer }.Live streams
isLiveStream(value)Whether a value is a live stream.

References and plugins

ExportDescriptionGuide
createReference(id, value)Sends value by its key.Isomorphic references
new OpaqueReference(value, replacement?)Sends replacement instead of value.OpaqueReference
createPlugin(plugin)Defines a plugin, with type inference.Writing plugins
FeatureFeature flags for disabledFeatures.Compatibility

Options

All options in one place. Each function lists the ones it accepts above.

OptionTypeDefaultDescription
pluginsPlugin[]nonePlugins for extra types.
disabledFeaturesnumber0Feature flags to turn off, combined with |.
depthLimitnumber1000Maximum nesting depth.
compactArrayBufferViewsbooleanfalseSend only the bytes each typed array covers.
refsMapa new MapValues already sent, shared between cross-reference calls.
scopeIdstringnoneWrites to $R[scopeId] instead of $R.
assignSharedRefsOnlybooleanfalsecrossSerialize and crossSerializeAsync only. Stores only the values the output reads more than once in $R.
maxBase64Lengthnumber1000000Largest encoded ArrayBuffer that JSON decoding accepts.
onSerialize(chunk, initial)functionrequiredReceives each script chunk of a stream.
onParse(node, initial)functionrequiredReceives each JSON node of a stream.
onDone()functionnoneRuns when a stream finishes.
onError(error)functionnoneRuns when a stream fails.

Types

seroval also exports TypeScript types for the values above, including SerovalJSON, SerovalNode, Plugin, SerializeOptions, SerializeAsyncOptions, ToJSONOptions, ToJSONAsyncOptions, CrossSerializeOptions, Stream, StreamListener, LiveStream, LiveStreamProducer, LiveStreamConsumer, CrossDeserializer and the options type of each function.