https://github.com/pioner92/decodable-js
Json parse for JavaScript
https://github.com/pioner92/decodable-js
decodable decodable-objects decode javascript js json parse typescript
Last synced: 2 months ago
JSON representation
Json parse for JavaScript
- Host: GitHub
- URL: https://github.com/pioner92/decodable-js
- Owner: pioner92
- Created: 2021-05-22T12:06:44.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2024-01-16T07:24:45.000Z (over 1 year ago)
- Last Synced: 2025-03-14T15:17:10.707Z (3 months ago)
- Topics: decodable, decodable-objects, decode, javascript, js, json, parse, typescript
- Language: TypeScript
- Homepage:
- Size: 55.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# decodable-js
## Overview
`decodable-js` is a JavaScript library inspired by Swift's approach to JSON decoding. It verifies the types within JSON data, and based on configuration, can either raise an error for type mismatches or ignore the misaligned fields. It also allows selective data extraction and offers the option to convert between strings and numbers and vice versa.## Installation
To integrate `decodable-js` into your project, run one of the following commands:
```bash
# If you use yarn:
yarn add decodable-js# Or if you prefer npm:
npm install decodable-js
```## Usage
Here's how you can use `decodable-js` in your project:
```typescript
import { decodable, T } from 'decodable-js';// Define your JSON structure
const JsonStruct = {
age: T.number,
address: T.string,
visible: T.boolean,
numbers: [T.string],
salary: T.number_$ // optional
// Add more fields as required
}// Your JSON data
const jsonData = {
age: 12,
address: '123 Cherry Lane',
visible: true,
numbers: ['one', 'two', 'three'],
rest: ['Will bi skipped']
// Additional data...
}// Decode the data with type enforcement
const result = decodable({data: jsonData, struct: JsonStruct});// Output the result
console.log(result);
// {
// age: 12,
// address: '123 Cherry Lane',
// visible: true,
// numbers: ['one', 'two', 'three'],
// }
```
```typescriptconst JsonStruct = {
numbers: [T.string],
}const jsonData = {
numbers: [1, '2', '3'], // 1 will be converted to string
}const result = decodable({
data: jsonData,
struct: JsonStruct,
enableConvert: true // enable convert
});// Output the result
console.log(result);
// {
// age: 12,
// address: '123 Cherry Lane',
// visible: true,
// numbers: ['1', '2', '3'], // converted to string
// }
```## API Reference
### Types
- T.number
- T.string
- T.boolean
- T.object
- T.null = null
- T.undefined
### Optional types (type || undefined)
- T.number_$
- T.string_$
- T.boolean_$
- T.null_$
- T.object_$The main function `decodable()` is used to decode JSON data:
```typescript
const result = decodable({
data: { index:1 },
struct: {
index:T.number,
value: T.string_$ // optional
},
enableConvert: false,
silentMode: false
});
```- `data: {} | Array` - The JSON data to decode.
- `struct: {} | Array` - The structure that `data` should be decoded into.
- `enableConvert: boolean` - If true, enables conversion between strings and numbers (defaults to false).
- `silentMode: boolean` - If false, throws an error when data does not match the structure (defaults to false).## Author
- **Alex Shumihin** - Initial work and maintenance.
For any feedback or issues, please open a GitHub issue or submit a pull request.