https://github.com/neo-ciber94/seria
A library for serialize/deserialize beyond JSON
https://github.com/neo-ciber94/seria
async deserialization formdata json react serialization stream
Last synced: 8 months ago
JSON representation
A library for serialize/deserialize beyond JSON
- Host: GitHub
- URL: https://github.com/neo-ciber94/seria
- Owner: Neo-Ciber94
- License: mit
- Created: 2024-03-20T02:50:11.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-06-20T02:26:21.000Z (over 1 year ago)
- Last Synced: 2025-01-29T14:16:30.261Z (9 months ago)
- Topics: async, deserialization, formdata, json, react, serialization, stream
- Language: TypeScript
- Homepage: https://neo-ciber94.github.io/seria/
- Size: 2.49 MB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
seria
Seria is a serialization and deserialization library that goes beyond the conventional capabilities of JSON. It provides seamless handling for various data types, including those that JSON cannot handle directly.
> This library is inspired on the new serialization capabilities `react` provides for server actions.
## Installation
```bash
npm install seria
``````bash
yarn add seria
``````bash
pnpm add seria
```## Usage
### Serialization and Deserialization
```ts
import * as seria from "seria";const json = seria.stringify(value);
const value = seria.parse(json);
```### FormData Handling
Seria also supports encoding and decoding FormData:
```ts
import { encode, decode } from "seria/form-data";const formData = encode(value);
const value = decode(formData);
```### Stream Handling
Seria provides stream-based serialization and deserialization:
```ts
import * as seria from "seria";const stream = seria.stringifyToStream(value);
const result = await seria.parseFromStream(stream);
```If you serialize a value that contains any `Promise` you need to serialize using a stream or use `seria.stringifyAsync` which resolve all the promises.
## Supported Data Types
Types supported by `seria` in comparison with the standard `JSON` object.
| Data Type | seria.stringify/parse | JSON.stringify/parse |
| -------------- | --------------------- | -------------------- |
| string | ✅ | ✅ |
| number | ✅ | ✅ |
| boolean | ✅ | ✅ |
| null | ✅ | ✅ |
| undefined | ✅ | ❌ |
| Date | ✅ | ❌ |
| BigInt | ✅ | ❌ |
| Promise | ✅ | ❌ |
| AsyncGenerator | ✅ | ❌ |
| Symbol | ✅ | ❌ |
| Set | ✅ | ❌ |
| Map | ✅ | ❌ |
| Error | ✅ | ❌ |
| ArrayBuffer | ✅ | ❌ |
| TypedArrays\* | ✅ | ❌ |
| DataView | ✅ | ❌ |
| File\* | ✅ | ❌ |
| FormData\* | ✅ | ❌ |> Typed Arrays https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Typed_arrays
> `File` and `FormData` are supported on `seria/form-data`.
`seria` also handles:
- `Infinity`, `-Infinity`, `NaN`, `-0`
- Cyclical references: `obj.self = obj`
- Repeated references: `[obj, new Set([obj]), new Map([["key", obj]])]`
- Custom types using `replacers` and `revivers`## See also
These libraries were used as references to improve `seria` features.
- [devalue](https://www.npmjs.com/package/devalue)
- [superjson](https://www.npmjs.com/package/seroval)