Files
it-tools/src/tools/json-to-java/json-to-java.service.ts
T
2026-09-06 09:02:45 +02:00

52 lines
1.4 KiB
TypeScript

const equivalence: { [key: string]: string } = {
number: 'Long',
};
export function convert(className: string, data: string | null) {
if (data === null) {
return '';
}
const obj = JSON.parse(data);
if (className === '') {
className = 'Result';
}
const keys = Object.keys(obj);
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 = obj[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);
}