Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gorhom/codable
A strict json parser inspired by Swift Codable
https://github.com/gorhom/codable
codable decodable encodable json parser ts-codable typescript
Last synced: 2 months ago
JSON representation
A strict json parser inspired by Swift Codable
- Host: GitHub
- URL: https://github.com/gorhom/codable
- Owner: gorhom
- License: mit
- Created: 2019-08-06T08:54:09.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-04-18T07:58:42.000Z (over 4 years ago)
- Last Synced: 2024-10-14T10:18:19.851Z (3 months ago)
- Topics: codable, decodable, encodable, json, parser, ts-codable, typescript
- Language: TypeScript
- Homepage:
- Size: 1.02 MB
- Stars: 27
- Watchers: 3
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Codable [![npm](https://img.shields.io/npm/v/@gorhom/codable)](https://www.npmjs.com/package/@gorhom/codable) [![npm bundle size](https://img.shields.io/bundlephobia/minzip/@gorhom/codable)](https://www.npmjs.com/package/@gorhom/codable) [![npm](https://badgen.net/npm/dependents/@gorhom/codable)](https://www.npmjs.com/package/@gorhom/codable)
A strict json parser inspired by [Swift Codable](https://developer.apple.com/documentation/swift/codable/) ❤️
![Alt text](docs/cover.png 'Cover')
## Features
Works almost same as [Swift Codable](https://developer.apple.com/documentation/swift/codable), but with some extra implementation to fit `TypeScript` / `JavaScript` environment.
- Decodes `JSON` payload with pre-defined scheme.
- Custom keys parsing.
- Strict types checking.
- Nested `Codable` parsing.## Supported Types
Types design was inspired by [MobX State Tree](https://github.com/mobxjs/mobx-state-tree#types-overview) ❤️
- Primitives
- String
- Number
- Boolean
- Complex
- Codable
- Optional
- Array
- Date## Installation
```bash
yarn add @gorhom/codable
# or
npm install @gorhom/codable
```## Usage
```ts
import { BaseCodable, types, decode, encode } from '@gorhom/codable';
import dayjs from 'dayjs';class Post extends BaseCodable {
title!: string;
isActive?: boolean;
date!: Date;
}Post.CodingProperties = {
title: types.string,
isActive: {
type: types.optional(types.boolean),
key: 'active',
},
date: types.date(dayjs),
};class User extends BaseCodable {
id!: number;
username!: string;
posts!: Post[];
}User.CodingProperties = {
id: types.number,
username: types.string,
posts: types.array(Post),
};const jsonPayload = {
id: 123,
username: 'Gorhom',
posts: [
{
title: 'dummy post',
active: true,
date: '2020-02-15T16:00:00.000Z',
},
{
title: 'deleted post',
active: false,
date: '2020-02-10T16:00:00.000Z',
},
],
};const user: User = decode(User, jsonPayload);
// now encode it back 🙈
// ⚠️ date encoding still in progressconst userJson = encode(user)
```## TODO
- [x] Add [Swift Decodable](https://developer.apple.com/documentation/swift/decodable) functionality.
- [x] Add [Swift Encodable](https://developer.apple.com/documentation/swift/encodable) functionality.
- [ ] Write API docs.## Built With
- [TSdx](https://github.com/jaredpalmer/tsdx)
- [TypeScript](https://github.com/Microsoft/TypeScript)## Author
- [Mo Gorhom](https://twitter.com/gorhom)