mirror of
https://github.com/CorentinTh/it-tools.git
synced 2025-04-20 14:56:17 -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
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