# Serialization and codecs

URL: https://docs.folkways.io/docs/js-sdk/serialization

Encode payloads, consume raw bytes and use external application codecs.

See [Payloads and schemas](/docs/realtime-api/payloads) for the platform model. The JavaScript SDK accepts ordinary objects, strings, ArrayBuffer and typed-array views. Without an explicit content type, bytes remain Bytes, strings become Text, and other JSON-serializable values become JSON.

```ts check
import { encodePayload, decodePayload } from '@folkwaysio/realtime';

const encoded = encodePayload({ reading: 42 });
const decoded = decodePayload(encoded.contentType, encoded.payload);
console.log(decoded);

const binary = encodePayload(new Uint8Array([1, 2, 3]), 'flatbuffers');
console.log(binary.contentType, binary.payload.byteLength);
```

The binary array above illustrates labeling only; it is not a valid FlatBuffers table.

With explicit json, a string is taken as already serialized JSON; objects are serialized. Explicit text accepts a string or bytes. Other content types require bytes. JSON BigInt values are converted to decimal strings; unsupported top-level values and circular objects fail serialization. If received JSON cannot be parsed, the SDK returns its raw bytes rather than a validated object. Text is UTF-8 decoded. Other content types return bytes.

Use message.contentType and message.payload for binary decoding, and treat raw payload arrays as read-only. External encoders and runtime validators remain application responsibilities. See [FlatBuffers](/docs/realtime-api/payloads/flatbuffers), [Protobuf](/docs/realtime-api/payloads/other-formats) and [schema validation](/docs/realtime-api/payloads/schemas).
