mirror of
https://github.com/CorentinTh/it-tools.git
synced 2024-09-11 13:37:53 +03:00
Compare commits
3 Commits
195e6ded9f
...
116881215b
Author | SHA1 | Date | |
---|---|---|---|
|
116881215b | ||
|
e1b4f9aafe | ||
|
458da55387 |
@ -1,6 +1,7 @@
|
||||
import { tool as base64FileConverter } from './base64-file-converter';
|
||||
import { tool as base64StringConverter } from './base64-string-converter';
|
||||
import { tool as basicAuthGenerator } from './basic-auth-generator';
|
||||
import { tool as jsonToJava } from './json-to-java';
|
||||
|
||||
import { tool as asciiTextDrawer } from './ascii-text-drawer';
|
||||
|
||||
@ -104,6 +105,7 @@ export const toolsByCategory: ToolCategory[] = [
|
||||
yamlToToml,
|
||||
jsonToYaml,
|
||||
jsonToToml,
|
||||
jsonToJava,
|
||||
listConverter,
|
||||
tomlToJson,
|
||||
tomlToYaml,
|
||||
|
12
src/tools/json-to-java/index.ts
Normal file
12
src/tools/json-to-java/index.ts
Normal file
@ -0,0 +1,12 @@
|
||||
import { ArrowsShuffle } from '@vicons/tabler';
|
||||
import { defineTool } from '../tool';
|
||||
|
||||
export const tool = defineTool({
|
||||
name: 'JSON to Java Entity',
|
||||
path: '/json-to-java',
|
||||
description: 'Convert JSON into Java entities',
|
||||
keywords: ['json', 'to', 'java'],
|
||||
component: () => import('./json-to-java.vue'),
|
||||
icon: ArrowsShuffle,
|
||||
createdAt: new Date('2024-07-03'),
|
||||
});
|
56
src/tools/json-to-java/json-to-java.service.ts
Normal file
56
src/tools/json-to-java/json-to-java.service.ts
Normal file
@ -0,0 +1,56 @@
|
||||
const equivalence: { [key: string]: string } = {
|
||||
number: 'Long',
|
||||
};
|
||||
|
||||
export function convert(className: string, data: string | null) {
|
||||
if (data === null) {
|
||||
return '';
|
||||
}
|
||||
if (typeof data === 'string') {
|
||||
data = JSON.parse(data);
|
||||
}
|
||||
if (className === '') {
|
||||
className = 'Result';
|
||||
}
|
||||
const keys = Object.keys(data);
|
||||
const objects: { [key: string]: any } = {};
|
||||
className = capitalizeFirstLetter(className);
|
||||
let result = `public class ${className} {\n`;
|
||||
for (const i in keys) {
|
||||
const key = keys[i];
|
||||
const value = data[key];
|
||||
let type = typeof value as string;
|
||||
if (Array.isArray(value)) {
|
||||
const typeName = capitalizeFirstLetter(fixListClass(key));
|
||||
type = `List<${typeName}>`;
|
||||
objects[typeName] = value[0];
|
||||
}
|
||||
else if (type === 'object') {
|
||||
type = capitalizeFirstLetter(key);
|
||||
objects[type] = value;
|
||||
}
|
||||
else if (equivalence[type] !== undefined) {
|
||||
type = equivalence[type];
|
||||
}
|
||||
else {
|
||||
type = capitalizeFirstLetter(type);
|
||||
}
|
||||
result += `\tpublic ${type} ${key};\n`;
|
||||
}
|
||||
result += '}\n';
|
||||
for (const clazzname in objects) {
|
||||
result += convert(clazzname, objects[clazzname]);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function fixListClass(string: string) {
|
||||
if (string.endsWith('s')) {
|
||||
return string.substring(0, string.length - 1);
|
||||
}
|
||||
return string;
|
||||
}
|
||||
|
||||
function capitalizeFirstLetter(string: string) {
|
||||
return string.charAt(0).toUpperCase() + string.slice(1);
|
||||
}
|
49
src/tools/json-to-java/json-to-java.vue
Normal file
49
src/tools/json-to-java/json-to-java.vue
Normal file
@ -0,0 +1,49 @@
|
||||
<script setup lang="ts">
|
||||
import JSON5 from 'json5';
|
||||
import { convert } from './json-to-java.service';
|
||||
import type { UseValidationRule } from '@/composable/validation';
|
||||
import TextareaCopyable from '@/components/TextareaCopyable.vue';
|
||||
|
||||
const jsonInput = ref('');
|
||||
const clazzInput = ref('Result');
|
||||
const goOutput = computed(() => {
|
||||
return jsonInput.value ? convert(clazzInput.value, jsonInput.value) : '';
|
||||
});
|
||||
|
||||
const rules: UseValidationRule<string>[] = [
|
||||
{
|
||||
validator: (v: string) => v === '' || JSON5.parse(v),
|
||||
message: 'Provided JSON is not valid.',
|
||||
},
|
||||
];
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<c-card title="JSON to Java Entity">
|
||||
<c-input-text
|
||||
v-model:value="clazzInput"
|
||||
placeholder="Custom class name"
|
||||
raw-text
|
||||
label="classname"
|
||||
label-position="left"
|
||||
label-width="50px"
|
||||
mb-2
|
||||
/>
|
||||
<c-input-text
|
||||
v-model:value="jsonInput"
|
||||
multiline
|
||||
placeholder="Put your josn string here..."
|
||||
rows="20"
|
||||
label="JSON to Java"
|
||||
:validation-rules="rules"
|
||||
raw-text
|
||||
mb-5
|
||||
/>
|
||||
</c-card>
|
||||
<c-card title="You Java String">
|
||||
<TextareaCopyable
|
||||
:value="goOutput"
|
||||
language="json"
|
||||
/>
|
||||
</c-card>
|
||||
</template>
|
@ -2,6 +2,7 @@
|
||||
import { generateLoremIpsum } from './lorem-ipsum-generator.service';
|
||||
import { useCopy } from '@/composable/copy';
|
||||
import { randIntFromInterval } from '@/utils/random';
|
||||
import { computedRefreshable } from '@/composable/computedRefreshable';
|
||||
|
||||
const paragraphs = ref(1);
|
||||
const sentences = ref([3, 8]);
|
||||
@ -9,7 +10,7 @@ const words = ref([8, 15]);
|
||||
const startWithLoremIpsum = ref(true);
|
||||
const asHTML = ref(false);
|
||||
|
||||
const loremIpsumText = computed(() =>
|
||||
const [loremIpsumText, refreshLoremIpsum] = computedRefreshable(() =>
|
||||
generateLoremIpsum({
|
||||
paragraphCount: paragraphs.value,
|
||||
asHTML: asHTML.value,
|
||||
@ -18,6 +19,7 @@ const loremIpsumText = computed(() =>
|
||||
startWithLoremIpsum: startWithLoremIpsum.value,
|
||||
}),
|
||||
);
|
||||
|
||||
const { copy } = useCopy({ source: loremIpsumText, text: 'Lorem ipsum copied to the clipboard' });
|
||||
</script>
|
||||
|
||||
@ -41,10 +43,13 @@ const { copy } = useCopy({ source: loremIpsumText, text: 'Lorem ipsum copied to
|
||||
|
||||
<c-input-text :value="loremIpsumText" multiline placeholder="Your lorem ipsum..." readonly mt-5 rows="5" />
|
||||
|
||||
<div mt-5 flex justify-center>
|
||||
<div mt-5 flex justify-center gap-3>
|
||||
<c-button autofocus @click="copy()">
|
||||
Copy
|
||||
</c-button>
|
||||
<c-button @click="refreshLoremIpsum">
|
||||
Refresh
|
||||
</c-button>
|
||||
</div>
|
||||
</c-card>
|
||||
</template>
|
||||
|
Loading…
Reference in New Issue
Block a user