Skip to content

Commit 4bc867b

Browse files
committed
feat(new tool): JSON string converter
1 parent f962c41 commit 4bc867b

File tree

4 files changed

+74
-0
lines changed

4 files changed

+74
-0
lines changed

locales/en.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,10 @@ tools:
332332
title: JSON minify
333333
description: Minify and compress your JSON by removing unnecessary whitespace.
334334

335+
json-string-converter:
336+
title: JSON string converter
337+
description: Convert your plain text or JavaScript objects into JSON string format and vice versa.
338+
335339
ulid-generator:
336340
title: ULID generator
337341
description: Generate random Universally Unique Lexicographically Sortable Identifier (ULID).

src/tools/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { tool as base64FileConverter } from './base64-file-converter';
22
import { tool as base64StringConverter } from './base64-string-converter';
33
import { tool as basicAuthGenerator } from './basic-auth-generator';
4+
import { tool as jsonStringConverter } from './json-string-converter';
45
import { tool as emailNormalizer } from './email-normalizer';
56

67
import { tool as asciiTextDrawer } from './ascii-text-drawer';
@@ -151,6 +152,7 @@ export const toolsByCategory: ToolCategory[] = [
151152
crontabGenerator,
152153
jsonViewer,
153154
jsonMinify,
155+
jsonStringConverter,
154156
jsonToCsv,
155157
sqlPrettify,
156158
chmodCalculator,
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Braces } from '@vicons/tabler';
2+
import { defineTool } from '../tool';
3+
4+
export const tool = defineTool({
5+
name: 'Json string converter',
6+
path: '/json-string-converter',
7+
description: '',
8+
keywords: ['json', 'string', 'converter'],
9+
component: () => import('./json-string-converter.vue'),
10+
icon: Braces,
11+
createdAt: new Date('2024-10-17'),
12+
});
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<script setup lang="ts">
2+
import type { UseValidationRule } from '@/composable/validation';
3+
import { withDefaultOnError } from '@/utils/defaults';
4+
5+
const defaultValue = '{\n\t"hello": [\n\t\t"world"\n\t]\n}';
6+
7+
// Define a reactive variable to track the selected transformation mode
8+
const selectedMode = ref('stringify');
9+
10+
// Create functions for both stringification and parsing
11+
const stringifyTransformer = (value: string) => withDefaultOnError(() => JSON.stringify(value), '');
12+
const parseTransformer = (value: string) => withDefaultOnError(() => JSON.parse(value).toString(), '');
13+
14+
// Dynamically select the transformer based on the selected mode
15+
const transformer = computed(() => {
16+
if (selectedMode.value === 'stringify') {
17+
return stringifyTransformer;
18+
}
19+
else {
20+
return parseTransformer;
21+
}
22+
});
23+
24+
const rules: UseValidationRule<string>[] = [
25+
{
26+
validator: (v: string) => v === '' || (selectedMode.value === 'stringify' ? JSON.stringify(v) : JSON.parse(v)),
27+
message: 'Provided text is not valid. (Make sure your JSON is in double quotes)',
28+
},
29+
];
30+
31+
// Dropdown options
32+
const dropdownOptions = computed(() => [
33+
{ label: 'JSON Stringify', value: 'stringify' },
34+
{ label: 'JSON Parse', value: 'parse' },
35+
]);
36+
</script>
37+
38+
<template>
39+
<c-card>
40+
<c-select
41+
v-model:value="selectedMode"
42+
label="Transformation Mode"
43+
:options="dropdownOptions"
44+
/>
45+
</c-card>
46+
<div />
47+
<format-transformer
48+
input-label="Your text / JSON string"
49+
:input-default="defaultValue"
50+
input-placeholder="Paste your text here..."
51+
output-label="JSON string conversion of your input"
52+
output-language="string"
53+
:input-validation-rules="rules"
54+
:transformer="transformer"
55+
/>
56+
</template>

0 commit comments

Comments
 (0)