mirror of
https://github.com/CorentinTh/it-tools.git
synced 2025-04-24 00:36:14 -04:00
feat(new tools): JSON to XML and XML to JSON (#1231)
* feat(new tool): JSON <> XML Fix https://github.com/CorentinTh/it-tools/issues/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 <corentin.thomasset74@gmail.com>
This commit is contained in:
parent
e1b4f9aafe
commit
f1a5489e21
8 changed files with 117 additions and 20 deletions
|
@ -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';
|
||||
|
@ -107,6 +109,8 @@ export const toolsByCategory: ToolCategory[] = [
|
|||
listConverter,
|
||||
tomlToJson,
|
||||
tomlToYaml,
|
||||
xmlToJson,
|
||||
jsonToXml,
|
||||
],
|
||||
},
|
||||
{
|
||||
|
|
12
src/tools/json-to-xml/index.ts
Normal file
12
src/tools/json-to-xml/index.ts
Normal file
|
@ -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
src/tools/json-to-xml/json-to-xml.vue
Normal file
32
src/tools/json-to-xml/json-to-xml.vue
Normal file
|
@ -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
src/tools/xml-to-json/index.ts
Normal file
12
src/tools/xml-to-json/index.ts
Normal file
|
@ -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
src/tools/xml-to-json/xml-to-json.vue
Normal file
32
src/tools/xml-to-json/xml-to-json.vue
Normal file
|
@ -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>
|
Loading…
Add table
Add a link
Reference in a new issue