Skip to content

Commit 31e280c

Browse files
committed
feat(new tool): JSON <> XML
Fix CorentinTh#314
1 parent e1b4f9a commit 31e280c

File tree

8 files changed

+117
-20
lines changed

8 files changed

+117
-20
lines changed

components.d.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ declare module '@vue/runtime-core' {
110110
JsonMinify: typeof import('./src/tools/json-minify/json-minify.vue')['default']
111111
JsonToCsv: typeof import('./src/tools/json-to-csv/json-to-csv.vue')['default']
112112
JsonToToml: typeof import('./src/tools/json-to-toml/json-to-toml.vue')['default']
113+
JsonToXml: typeof import('./src/tools/json-to-xml/json-to-xml.vue')['default']
113114
JsonToYaml: typeof import('./src/tools/json-to-yaml-converter/json-to-yaml.vue')['default']
114115
JsonViewer: typeof import('./src/tools/json-viewer/json-viewer.vue')['default']
115116
JwtParser: typeof import('./src/tools/jwt-parser/jwt-parser.vue')['default']
@@ -130,21 +131,14 @@ declare module '@vue/runtime-core' {
130131
NCode: typeof import('naive-ui')['NCode']
131132
NCollapseTransition: typeof import('naive-ui')['NCollapseTransition']
132133
NConfigProvider: typeof import('naive-ui')['NConfigProvider']
133-
NDivider: typeof import('naive-ui')['NDivider']
134134
NEllipsis: typeof import('naive-ui')['NEllipsis']
135-
NFormItem: typeof import('naive-ui')['NFormItem']
136-
NGi: typeof import('naive-ui')['NGi']
137-
NGrid: typeof import('naive-ui')['NGrid']
138135
NH1: typeof import('naive-ui')['NH1']
139136
NH3: typeof import('naive-ui')['NH3']
140137
NIcon: typeof import('naive-ui')['NIcon']
141-
NInputNumber: typeof import('naive-ui')['NInputNumber']
142-
NLabel: typeof import('naive-ui')['NLabel']
143138
NLayout: typeof import('naive-ui')['NLayout']
144139
NLayoutSider: typeof import('naive-ui')['NLayoutSider']
145140
NMenu: typeof import('naive-ui')['NMenu']
146141
NScrollbar: typeof import('naive-ui')['NScrollbar']
147-
NSpin: typeof import('naive-ui')['NSpin']
148142
NumeronymGenerator: typeof import('./src/tools/numeronym-generator/numeronym-generator.vue')['default']
149143
OtpCodeGeneratorAndValidator: typeof import('./src/tools/otp-code-generator-and-validator/otp-code-generator-and-validator.vue')['default']
150144
PasswordStrengthAnalyser: typeof import('./src/tools/password-strength-analyser/password-strength-analyser.vue')['default']
@@ -186,6 +180,7 @@ declare module '@vue/runtime-core' {
186180
UuidGenerator: typeof import('./src/tools/uuid-generator/uuid-generator.vue')['default']
187181
WifiQrCodeGenerator: typeof import('./src/tools/wifi-qr-code-generator/wifi-qr-code-generator.vue')['default']
188182
XmlFormatter: typeof import('./src/tools/xml-formatter/xml-formatter.vue')['default']
183+
XmlToJson: typeof import('./src/tools/xml-to-json/xml-to-json.vue')['default']
189184
YamlToJson: typeof import('./src/tools/yaml-to-json-converter/yaml-to-json.vue')['default']
190185
YamlToToml: typeof import('./src/tools/yaml-to-toml/yaml-to-toml.vue')['default']
191186
YamlViewer: typeof import('./src/tools/yaml-viewer/yaml-viewer.vue')['default']

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
"vue-router": "^4.1.6",
9393
"vue-tsc": "^1.8.1",
9494
"xml-formatter": "^3.3.2",
95+
"xml-js": "^1.6.11",
9596
"yaml": "^2.2.1"
9697
},
9798
"devDependencies": {

pnpm-lock.yaml

Lines changed: 22 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/tools/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { tool as asciiTextDrawer } from './ascii-text-drawer';
66

77
import { tool as textToUnicode } from './text-to-unicode';
88
import { tool as safelinkDecoder } from './safelink-decoder';
9+
import { tool as xmlToJson } from './xml-to-json';
10+
import { tool as jsonToXml } from './json-to-xml';
911
import { tool as pdfSignatureChecker } from './pdf-signature-checker';
1012
import { tool as numeronymGenerator } from './numeronym-generator';
1113
import { tool as macAddressGenerator } from './mac-address-generator';
@@ -107,6 +109,8 @@ export const toolsByCategory: ToolCategory[] = [
107109
listConverter,
108110
tomlToJson,
109111
tomlToYaml,
112+
xmlToJson,
113+
jsonToXml,
110114
],
111115
},
112116
{

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

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 to XML',
6+
path: '/json-to-xml',
7+
description: 'Convert JSON to XML',
8+
keywords: ['json', 'xml'],
9+
component: () => import('./json-to-xml.vue'),
10+
icon: Braces,
11+
createdAt: new Date('2024-06-30'),
12+
});

src/tools/json-to-xml/json-to-xml.vue

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 convert from 'xml-js';
3+
import JSON5 from 'json5';
4+
import { withDefaultOnError } from '@/utils/defaults';
5+
import type { UseValidationRule } from '@/composable/validation';
6+
7+
const defaultValue = '{"a":{"_attributes":{"x":"1.234","y":"It\'s"}}}';
8+
function transformer(value: string) {
9+
return withDefaultOnError(() => {
10+
return convert.js2xml(JSON5.parse(value), { compact: true });
11+
}, '');
12+
}
13+
14+
const rules: UseValidationRule<string>[] = [
15+
{
16+
validator: (v: string) => v === '' || JSON5.parse(v),
17+
message: 'Provided JSON is not valid.',
18+
},
19+
];
20+
</script>
21+
22+
<template>
23+
<format-transformer
24+
input-label="Your JSON content"
25+
:input-default="defaultValue"
26+
input-placeholder="Paste your JSON content here..."
27+
output-label="Converted XML"
28+
output-language="xml"
29+
:transformer="transformer"
30+
:input-validation-rules="rules"
31+
/>
32+
</template>

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

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: 'XML to Json',
6+
path: '/xml-to-json',
7+
description: 'Convert XML to JSON',
8+
keywords: ['xml', 'json'],
9+
component: () => import('./xml-to-json.vue'),
10+
icon: Braces,
11+
createdAt: new Date('2024-06-30'),
12+
});

src/tools/xml-to-json/xml-to-json.vue

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 convert from 'xml-js';
3+
import { isValidXML } from '../xml-formatter/xml-formatter.service';
4+
import { withDefaultOnError } from '@/utils/defaults';
5+
import type { UseValidationRule } from '@/composable/validation';
6+
7+
const defaultValue = '<a x="1.234" y="It\'s"/>';
8+
function transformer(value: string) {
9+
return withDefaultOnError(() => {
10+
return JSON.stringify(convert.xml2js(value, { compact: true }), null, 2);
11+
}, '');
12+
}
13+
14+
const rules: UseValidationRule<string>[] = [
15+
{
16+
validator: isValidXML,
17+
message: 'Provided XML is not valid.',
18+
},
19+
];
20+
</script>
21+
22+
<template>
23+
<format-transformer
24+
input-label="Your XML content"
25+
:input-default="defaultValue"
26+
input-placeholder="Paste your XML content here..."
27+
output-label="Converted JSON"
28+
output-language="json"
29+
:transformer="transformer"
30+
:input-validation-rules="rules"
31+
/>
32+
</template>

0 commit comments

Comments
 (0)