2.0.3 • Published 4 months ago

decodable-js v2.0.3

Weekly downloads
-
License
ISC
Repository
github
Last release
4 months ago

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:

# 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:

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'],
// }
const 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:

const result = decodable({
    data: { index:1 },
    struct: { 
        index:T.number,
        value: T.string_$ // optional
    },
    enableConvert: false,
    silentMode: false
});
  • data: {} | Array<any> - The JSON data to decode.
  • struct: {} | Array<any> - 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.

2.0.3

4 months ago

2.0.2

4 months ago

1.1.16

3 years ago

1.1.9

3 years ago

1.1.12

3 years ago

1.1.11

3 years ago

1.1.10

3 years ago

1.1.15

3 years ago

1.1.14

3 years ago

1.1.13

3 years ago

1.1.8

3 years ago

1.1.7

3 years ago

1.1.6

3 years ago

1.1.5

3 years ago

1.1.4

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.0.16

3 years ago

1.0.15

3 years ago

1.0.14

3 years ago

1.0.13

3 years ago

1.0.12

3 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago