Skip to content

Commit f7c2d89

Browse files
committed
Merge branch 'up/fix/json-to-object' into chore/all-my-stuffs
2 parents e934a9b + f118582 commit f7c2d89

File tree

6 files changed

+83
-2
lines changed

6 files changed

+83
-2
lines changed

locales/en.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,4 +411,7 @@ tools:
411411

412412
text-to-binary:
413413
title: Text to ASCII binary
414-
description: Convert text to its ASCII binary representation and vice-versa.
414+
description: Convert text to its ASCII binary representation and vice-versa.
415+
json-to-object:
416+
title: JSON to object
417+
description: Parse and convert JSON to object.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@
213213
"slashes": "^3.0.12",
214214
"spdx-satisfies": "^5.0.1",
215215
"sql-formatter": "^13.0.0",
216+
"stringify-object": "^5.0.0",
216217
"sshpk": "^1.18.0",
217218
"svg2png-wasm": "^1.4.1",
218219
"svgo": "^3.3.2",
@@ -280,6 +281,7 @@
280281
"@types/potrace": "^2.1.5",
281282
"@types/punycode": "^2.1.4",
282283
"@types/qrcode": "^1.5.0",
284+
"@types/stringify-object": "^4.0.5",
283285
"@types/spdx-satisfies": "^0.1.2",
284286
"@types/sshpk": "^1.17.4",
285287
"@types/turndown": "^5.0.5",

pnpm-lock.yaml

Lines changed: 30 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/tools/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ import { tool as pdfUnlock } from './pdf-unlock';
233233
import { tool as ipCidrToRange } from './ip-cidr-to-range';
234234
import { tool as ipRangeToCidr } from './ip-range-to-cidr';
235235
import { tool as ipv6AddressConverter } from './ipv6-address-converter';
236+
import { tool as jsonToObject } from './json-to-object';
236237

237238
export const toolsByCategory: ToolCategory[] = [
238239
{
@@ -385,6 +386,7 @@ export const toolsByCategory: ToolCategory[] = [
385386
jsonLinter,
386387
jsonToCsv,
387388
csvToJson,
389+
jsonToObject,
388390
sqlPrettify,
389391
chmodCalculator,
390392
dockerRunToDockerComposeConverter,

src/tools/json-to-object/index.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Braces } from '@vicons/tabler';
2+
import { defineTool } from '../tool';
3+
import { translate } from '@/plugins/i18n.plugin';
4+
5+
export const tool = defineTool({
6+
name: translate('tools.json-to-object.title'),
7+
path: '/json-to-object',
8+
description: translate('tools.json-to-object.description'),
9+
keywords: ['json', 'parse', 'object', 'convert', 'transform'],
10+
component: () => import('./json-to-object.vue'),
11+
icon: Braces,
12+
createdAt: new Date('2024-08-16'),
13+
});
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<script setup lang="ts">
2+
import JSON5 from 'json5';
3+
import stringifyObject from 'stringify-object';
4+
import type { UseValidationRule } from '@/composable/validation';
5+
import { isNotThrowing } from '@/utils/boolean';
6+
import { withDefaultOnError } from '@/utils/defaults';
7+
8+
function transformer(value: string) {
9+
return withDefaultOnError(() => stringifyObject(JSON5.parse(value), {
10+
indent: ' ',
11+
singleQuotes: false,
12+
}), '');
13+
}
14+
15+
const rules: UseValidationRule<string>[] = [
16+
{
17+
validator: (value: string) => value === '' || isNotThrowing(() => stringifyObject(JSON5.parse(value))),
18+
message: 'Provided JSON is not valid.',
19+
},
20+
];
21+
</script>
22+
23+
<template>
24+
<format-transformer
25+
input-label="Your JSON"
26+
input-placeholder="Paste your JSON here..."
27+
output-label="Object from your JSON"
28+
output-language="js"
29+
:input-validation-rules="rules"
30+
:transformer="transformer"
31+
/>
32+
</template>

0 commit comments

Comments
 (0)