Skip to content

Commit

Permalink
feat(new tools): JSON to XML and XML to JSON (#1231)
Browse files Browse the repository at this point in the history
* feat(new tool): JSON <> XML

Fix #314

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

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

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

---------

Co-authored-by: Corentin THOMASSET <[email protected]>
  • Loading branch information
sharevb and CorentinTh authored Aug 9, 2024
1 parent e1b4f9a commit f1a5489
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 20 deletions.
9 changes: 2 additions & 7 deletions components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ declare module '@vue/runtime-core' {
JsonMinify: typeof import('./src/tools/json-minify/json-minify.vue')['default']
JsonToCsv: typeof import('./src/tools/json-to-csv/json-to-csv.vue')['default']
JsonToToml: typeof import('./src/tools/json-to-toml/json-to-toml.vue')['default']
JsonToXml: typeof import('./src/tools/json-to-xml/json-to-xml.vue')['default']
JsonToYaml: typeof import('./src/tools/json-to-yaml-converter/json-to-yaml.vue')['default']
JsonViewer: typeof import('./src/tools/json-viewer/json-viewer.vue')['default']
JwtParser: typeof import('./src/tools/jwt-parser/jwt-parser.vue')['default']
Expand All @@ -130,21 +131,14 @@ declare module '@vue/runtime-core' {
NCode: typeof import('naive-ui')['NCode']
NCollapseTransition: typeof import('naive-ui')['NCollapseTransition']
NConfigProvider: typeof import('naive-ui')['NConfigProvider']
NDivider: typeof import('naive-ui')['NDivider']
NEllipsis: typeof import('naive-ui')['NEllipsis']
NFormItem: typeof import('naive-ui')['NFormItem']
NGi: typeof import('naive-ui')['NGi']
NGrid: typeof import('naive-ui')['NGrid']
NH1: typeof import('naive-ui')['NH1']
NH3: typeof import('naive-ui')['NH3']
NIcon: typeof import('naive-ui')['NIcon']
NInputNumber: typeof import('naive-ui')['NInputNumber']
NLabel: typeof import('naive-ui')['NLabel']
NLayout: typeof import('naive-ui')['NLayout']
NLayoutSider: typeof import('naive-ui')['NLayoutSider']
NMenu: typeof import('naive-ui')['NMenu']
NScrollbar: typeof import('naive-ui')['NScrollbar']
NSpin: typeof import('naive-ui')['NSpin']
NumeronymGenerator: typeof import('./src/tools/numeronym-generator/numeronym-generator.vue')['default']
OtpCodeGeneratorAndValidator: typeof import('./src/tools/otp-code-generator-and-validator/otp-code-generator-and-validator.vue')['default']
PasswordStrengthAnalyser: typeof import('./src/tools/password-strength-analyser/password-strength-analyser.vue')['default']
Expand Down Expand Up @@ -186,6 +180,7 @@ declare module '@vue/runtime-core' {
UuidGenerator: typeof import('./src/tools/uuid-generator/uuid-generator.vue')['default']
WifiQrCodeGenerator: typeof import('./src/tools/wifi-qr-code-generator/wifi-qr-code-generator.vue')['default']
XmlFormatter: typeof import('./src/tools/xml-formatter/xml-formatter.vue')['default']
XmlToJson: typeof import('./src/tools/xml-to-json/xml-to-json.vue')['default']
YamlToJson: typeof import('./src/tools/yaml-to-json-converter/yaml-to-json.vue')['default']
YamlToToml: typeof import('./src/tools/yaml-to-toml/yaml-to-toml.vue')['default']
YamlViewer: typeof import('./src/tools/yaml-viewer/yaml-viewer.vue')['default']
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
"vue-router": "^4.1.6",
"vue-tsc": "^1.8.1",
"xml-formatter": "^3.3.2",
"xml-js": "^1.6.11",
"yaml": "^2.2.1"
},
"devDependencies": {
Expand Down
35 changes: 22 additions & 13 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/tools/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { tool as asciiTextDrawer } from './ascii-text-drawer';

import { tool as textToUnicode } from './text-to-unicode';
import { tool as safelinkDecoder } from './safelink-decoder';
import { tool as xmlToJson } from './xml-to-json';
import { tool as jsonToXml } from './json-to-xml';
import { tool as pdfSignatureChecker } from './pdf-signature-checker';
import { tool as numeronymGenerator } from './numeronym-generator';
import { tool as macAddressGenerator } from './mac-address-generator';
Expand Down Expand Up @@ -107,6 +109,8 @@ export const toolsByCategory: ToolCategory[] = [
listConverter,
tomlToJson,
tomlToYaml,
xmlToJson,
jsonToXml,
],
},
{
Expand Down
12 changes: 12 additions & 0 deletions src/tools/json-to-xml/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Braces } from '@vicons/tabler';
import { defineTool } from '../tool';

export const tool = defineTool({
name: 'JSON to XML',
path: '/json-to-xml',
description: 'Convert JSON to XML',
keywords: ['json', 'xml'],
component: () => import('./json-to-xml.vue'),
icon: Braces,
createdAt: new Date('2024-08-09'),
});
32 changes: 32 additions & 0 deletions src/tools/json-to-xml/json-to-xml.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<script setup lang="ts">
import convert from 'xml-js';
import JSON5 from 'json5';
import { withDefaultOnError } from '@/utils/defaults';
import type { UseValidationRule } from '@/composable/validation';
const defaultValue = '{"a":{"_attributes":{"x":"1.234","y":"It\'s"}}}';
function transformer(value: string) {
return withDefaultOnError(() => {
return convert.js2xml(JSON5.parse(value), { compact: true });
}, '');
}
const rules: UseValidationRule<string>[] = [
{
validator: (v: string) => v === '' || JSON5.parse(v),
message: 'Provided JSON is not valid.',
},
];
</script>

<template>
<format-transformer
input-label="Your JSON content"
:input-default="defaultValue"
input-placeholder="Paste your JSON content here..."
output-label="Converted XML"
output-language="xml"
:transformer="transformer"
:input-validation-rules="rules"
/>
</template>
12 changes: 12 additions & 0 deletions src/tools/xml-to-json/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Braces } from '@vicons/tabler';
import { defineTool } from '../tool';

export const tool = defineTool({
name: 'XML to JSON',
path: '/xml-to-json',
description: 'Convert XML to JSON',
keywords: ['xml', 'json'],
component: () => import('./xml-to-json.vue'),
icon: Braces,
createdAt: new Date('2024-08-09'),
});
32 changes: 32 additions & 0 deletions src/tools/xml-to-json/xml-to-json.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<script setup lang="ts">
import convert from 'xml-js';
import { isValidXML } from '../xml-formatter/xml-formatter.service';
import { withDefaultOnError } from '@/utils/defaults';
import type { UseValidationRule } from '@/composable/validation';
const defaultValue = '<a x="1.234" y="It\'s"/>';
function transformer(value: string) {
return withDefaultOnError(() => {
return JSON.stringify(convert.xml2js(value, { compact: true }), null, 2);
}, '');
}
const rules: UseValidationRule<string>[] = [
{
validator: isValidXML,
message: 'Provided XML is not valid.',
},
];
</script>

<template>
<format-transformer
input-label="Your XML content"
:input-default="defaultValue"
input-placeholder="Paste your XML content here..."
output-label="Converted JSON"
output-language="json"
:transformer="transformer"
:input-validation-rules="rules"
/>
</template>

0 comments on commit f1a5489

Please sign in to comment.