Skip to content

Commit 458da55

Browse files
committed
add new tools json-to-java entity
1 parent b430bae commit 458da55

File tree

4 files changed

+119
-0
lines changed

4 files changed

+119
-0
lines changed

src/tools/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { tool as base64FileConverter } from './base64-file-converter';
22
import { tool as base64StringConverter } from './base64-string-converter';
33
import { tool as basicAuthGenerator } from './basic-auth-generator';
4+
import { tool as jsonToJava } from './json-to-java';
45

56
import { tool as asciiTextDrawer } from './ascii-text-drawer';
67

@@ -104,6 +105,7 @@ export const toolsByCategory: ToolCategory[] = [
104105
yamlToToml,
105106
jsonToYaml,
106107
jsonToToml,
108+
jsonToJava,
107109
listConverter,
108110
tomlToJson,
109111
tomlToYaml,

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { ArrowsShuffle } from '@vicons/tabler';
2+
import { defineTool } from '../tool';
3+
4+
export const tool = defineTool({
5+
name: 'JSON to Java Entity',
6+
path: '/json-to-java',
7+
description: 'Convert JSON into Java entities',
8+
keywords: ['json', 'to', 'java'],
9+
component: () => import('./json-to-java.vue'),
10+
icon: ArrowsShuffle,
11+
createdAt: new Date('2024-07-03'),
12+
});
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
const equivalence: { [key: string]: string } = {
2+
number: 'Long',
3+
};
4+
5+
export function convert(className: string, data: string | null) {
6+
if (data === null) {
7+
return '';
8+
}
9+
if (typeof data === 'string') {
10+
data = JSON.parse(data);
11+
}
12+
if (className === '') {
13+
className = 'Result';
14+
}
15+
const keys = Object.keys(data);
16+
const objects: { [key: string]: any } = {};
17+
className = capitalizeFirstLetter(className);
18+
let result = `public class ${className} {\n`;
19+
for (const i in keys) {
20+
const key = keys[i];
21+
const value = data[key];
22+
let type = typeof value as string;
23+
if (Array.isArray(value)) {
24+
const typeName = capitalizeFirstLetter(fixListClass(key));
25+
type = `List<${typeName}>`;
26+
objects[typeName] = value[0];
27+
}
28+
else if (type === 'object') {
29+
type = capitalizeFirstLetter(key);
30+
objects[type] = value;
31+
}
32+
else if (equivalence[type] !== undefined) {
33+
type = equivalence[type];
34+
}
35+
else {
36+
type = capitalizeFirstLetter(type);
37+
}
38+
result += `\tpublic ${type} ${key};\n`;
39+
}
40+
result += '}\n';
41+
for (const clazzname in objects) {
42+
result += convert(clazzname, objects[clazzname]);
43+
}
44+
return result;
45+
}
46+
47+
function fixListClass(string: string) {
48+
if (string.endsWith('s')) {
49+
return string.substring(0, string.length - 1);
50+
}
51+
return string;
52+
}
53+
54+
function capitalizeFirstLetter(string: string) {
55+
return string.charAt(0).toUpperCase() + string.slice(1);
56+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<script setup lang="ts">
2+
import JSON5 from 'json5';
3+
import { convert } from './json-to-java.service';
4+
import type { UseValidationRule } from '@/composable/validation';
5+
import TextareaCopyable from '@/components/TextareaCopyable.vue';
6+
7+
const jsonInput = ref('');
8+
const clazzInput = ref('Result');
9+
const goOutput = computed(() => {
10+
return jsonInput.value ? convert(clazzInput.value, jsonInput.value) : '';
11+
});
12+
13+
const rules: UseValidationRule<string>[] = [
14+
{
15+
validator: (v: string) => v === '' || JSON5.parse(v),
16+
message: 'Provided JSON is not valid.',
17+
},
18+
];
19+
</script>
20+
21+
<template>
22+
<c-card title="JSON to Java Entity">
23+
<c-input-text
24+
v-model:value="clazzInput"
25+
placeholder="Custom class name"
26+
raw-text
27+
label="classname"
28+
label-position="left"
29+
label-width="50px"
30+
mb-2
31+
/>
32+
<c-input-text
33+
v-model:value="jsonInput"
34+
multiline
35+
placeholder="Put your josn string here..."
36+
rows="20"
37+
label="JSON to Java"
38+
:validation-rules="rules"
39+
raw-text
40+
mb-5
41+
/>
42+
</c-card>
43+
<c-card title="You Java String">
44+
<TextareaCopyable
45+
:value="goOutput"
46+
language="json"
47+
/>
48+
</c-card>
49+
</template>

0 commit comments

Comments
 (0)