Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/wellwelwel/jsonc.min
✨ Faster and safer JSON and JSONC minify, parse and stringify for JavaScript (Browser compatible) — 2.3KB.
https://github.com/wellwelwel/jsonc.min
dependency-free json jsonc jsonc-to-json minifier minify parse parser stringify zero-dependency
Last synced: 2 months ago
JSON representation
✨ Faster and safer JSON and JSONC minify, parse and stringify for JavaScript (Browser compatible) — 2.3KB.
- Host: GitHub
- URL: https://github.com/wellwelwel/jsonc.min
- Owner: wellwelwel
- License: mit
- Created: 2024-07-18T14:43:32.000Z (6 months ago)
- Default Branch: main
- Last Pushed: 2024-08-22T21:20:39.000Z (4 months ago)
- Last Synced: 2024-09-23T01:51:17.830Z (3 months ago)
- Topics: dependency-free, json, jsonc, jsonc-to-json, minifier, minify, parse, parser, stringify, zero-dependency
- Language: TypeScript
- Homepage:
- Size: 60.5 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
- Security: SECURITY.md
Awesome Lists containing this project
README
jsonc.min
[![NPM Version](https://img.shields.io/npm/v/jsonc.min.svg?label=&color=70a1ff&logo=npm&logoColor=white)](https://www.npmjs.com/package/jsonc.min)
[![GitHub Workflow Status (Node.js)](https://img.shields.io/github/actions/workflow/status/wellwelwel/jsonc.min/ci_node.yml?event=push&label=&branch=main&logo=nodedotjs&logoColor=535c68&color=badc58)](https://github.com/wellwelwel/jsonc.min/actions/workflows/ci_node.yml?query=branch%3Amain)
[![GitHub Workflow Status (Bun)](https://img.shields.io/github/actions/workflow/status/wellwelwel/jsonc.min/ci_bun.yml?event=push&label=&branch=main&logo=bun&logoColor=ffffff&color=f368e0)](https://github.com/wellwelwel/jsonc.min/actions/workflows/ci_bun.yml?query=branch%3Amain)
[![GitHub Workflow Status (Deno)](https://img.shields.io/github/actions/workflow/status/wellwelwel/jsonc.min/ci_deno.yml?event=push&label=&branch=main&logo=deno&logoColor=ffffff&color=079992)](https://github.com/wellwelwel/jsonc.min/actions/workflows/ci_deno.yml?query=branch%3Amain)✨ Faster and safer JSON and JSONC minify, parse and stringify for JavaScript (Browser compatible) — **2.3KB**.
## Why
#### 🔐 Safety
> Many _JSON_ minification packages rely on vulnerable _regex_, making them unsuitable for production.
**jsonc.min** prioritizes security by avoiding these pitfalls and offering a robust solution.
#### 🤝 Compatibility
**jsonc.min** ensures full compatibility with both **Node.js**, **Bun**, **Deno** and, browser environments.
All features work for both _JSON_ and _JSONC_.#### 🪶 Lightweight
Zero dependencies and optimized for production environments.
---
## Install
```bash
# Node.js
npm i jsonc.min
``````bash
# Bun
bun add jsonc.min
``````bash
# Deno
deno add npm:jsonc.min
```---
## Usage
### Import
#### ES Modules
```js
import { JSONC } from 'jsonc.min';
```#### CommonJS
```js
const { JSONC } = require('jsonc.min');
```#### Browser
```html
```
---
### `toJSON`
Convert from _JSONC_ to _JSON_.
```js
JSONC.toJSON('/* JSONC content */ { "test": true }');
// "{ test: true }"
```If the content is already _JSON_, it will just be preserved:
```js
JSONC.toJSON('{ "test": true }');
// "{ test: true }"
```---
### `minify`
Minify both _JSON_ and _JSONC_.
```js
const content = `
/**
* JSONC content
*/
{
"test": true // 🔬
}
`;JSONC.minify(content);
// "{test:true}"
``````js
const content = `
{
"test": true
}
`;JSONC.minify(content);
// "{test:true}"
```---
### `parse`
Parse both _JSON_ and _JSONC_.
```js
const content = `
/**
* JSONC content
*/
{
"test": true // 🔬
}
`;JSONC.parse(content);
// { test: true }
``````js
const content = `
{
"test": true
}
`;JSONC.parse(content);
// { test: true }
```- If your content is guaranteed to be a _JSON_, there is no advantage to using `JSONC.parse(content)` instead of `JSON.parse(content)`.
---
### `stringify`
Prettify both _JSON_ and _JSONC_.
> Use `JSON.stringify` behind the scenes.
```js
const content = '/** JSONC content */ { "test": true }';JSONC.stringify(content, null, 2);
// "{
// "test": true
// }"
``````js
const content = '{ "test": true }';JSONC.stringify(content, null, 2);
// "{
// "test": true
// }"
```---
## Examples
### Reading a JSON or JSONC file
```ts
import { readFile } from 'node:fs/promises';
import { JSONC } from 'jsonc.min';const content = await readFile('./file.jsonc', 'utf-8');
JSONC.parse(content);
```### Parsing a dynamic config file
For this example, let's assume a `.configrc` that can be both a _JSON_ or a _JSONC_, as well as looking for both `config.json` and `config.jsonc` files:
```ts
import { JSONC } from 'jsonc.min';
import { cwd } from 'node:process';
import { join } from 'node:path';
import { readFile } from 'node:fs/promises';export const getConfigs = async (customPath?: string) => {
const targetRoot = cwd();const expectedFiles = customPath
? [customPath]
: ['.configrc', 'config.json', 'config.jsonc'];for (const file of expectedFiles) {
const filePath = join(targetRoot, file);try {
const configsFile = await readFile(filePath, 'utf-8');// jsonc.min will parse both JSON and JSONC extensions, even if there is no extension.
return JSONC.parse(configsFile);
} catch {}return {};
}
};
```---
## Acknowledgements
[![Contributors](https://img.shields.io/github/contributors/wellwelwel/jsonc.min?label=Contributors)](https://github.com/wellwelwel/jsonc.min/graphs/contributors)