From f00c3a02cf6fa1252620e515d8a4a1182ce204a2 Mon Sep 17 00:00:00 2001 From: Pooja Kulkarni Date: Sat, 31 Dec 2022 01:18:42 +0530 Subject: [PATCH] feat: Adding JSON to Java transformation feature (#345) * feat: Adding JSON to Java transformation feature * Added constructor generation in Java model classes * Adding missing semi colon during constructor creation * Fixing import and generic typing error * Fixing JSON property indentation issue --- pages/json-to-java.tsx | 138 +++++++++++++++++++++++++++++++++++++++++ utils/routes.tsx | 6 ++ 2 files changed, 144 insertions(+) create mode 100644 pages/json-to-java.tsx diff --git a/pages/json-to-java.tsx b/pages/json-to-java.tsx new file mode 100644 index 0000000..0669c07 --- /dev/null +++ b/pages/json-to-java.tsx @@ -0,0 +1,138 @@ +import ConversionPanel from "@components/ConversionPanel"; +import * as React from "react"; +import { useCallback } from "react"; + +export default function JsonToJava() { + const transformer = useCallback(async ({ value }) => { + const { run } = await import("json_typegen_wasm"); + const kotlinTransformationLines: string[] = run( + "Root", + value, + JSON.stringify({ + output_mode: "kotlin" + }) + ).split("\n"); + + // use Kotlin transformation to convert JSON to Java + let javaTransformation: string = ""; + let currentClass: string = ""; + let variableNames: string[] = []; + let variableTypes: string[] = []; + + kotlinTransformationLines.forEach((line: string) => { + const originalLine = line; + line = line.trim(); + + if (line === ")") { + // Class is closing so generate constructor, getters and setters for + // the current class, close the class and reset running values + let args: string[] = []; + let getters: string[] = []; + let setters: string[] = []; + + // Create args for constructor, getters and setters + for (let i = 0; i < variableNames.length; i++) { + const type = variableTypes[i]; + const variableName = variableNames[i]; + const titleCaseVariable = + variableName.charAt(0).toUpperCase() + variableName.substring(1); + args.push(`${type} ${variableName}`); + getters.push( + `\tpublic ${type} get${titleCaseVariable}() {\n\t\treturn this.${variableName};\n\t}\n` + ); + setters.push( + `\tpublic void set${titleCaseVariable}(${type} ${variableName}) {\n\t\tthis.${variableName} = ${variableName};\n\t}\n` + ); + } + + // Create constructor + let constructor = `\tpublic ${currentClass}(${args.join(", ")}) {`; + let properties: string[] = []; + variableNames.forEach(variable => { + properties.push(`this.${variable} = ${variable};`); + }); + constructor += `\n\t\t${properties.join("\n\t\t")}\n\t}\n`; + javaTransformation += `\n${constructor}\n${getters.join( + "\n" + )}\n${setters.join("\n")}}`; + + // Reset running values + currentClass = ""; + variableNames = []; + variableTypes = []; + } else if (line.startsWith("data class ")) { + // Change the start of a class from 'data class Root(' to 'public class Root {' + const classNameStartIndex = 11; + const classNameEndIndex = line.indexOf("("); + const className = line.substring( + classNameStartIndex, + classNameEndIndex + ); + javaTransformation += `public class ${className} {`; + currentClass = className; + } else if (line.startsWith("val")) { + // If this is a variable, change 'val name: String' to 'private String name;' + // followed by respective getters, setters for the variable + const processedLine = line.replace("?", ""); + const variableStartIndex = 4; // length of string "val " + const variableEndIndex = processedLine.indexOf(":"); + const variable: string = processedLine.substring( + variableStartIndex, + variableEndIndex + ); + const typeStartIndex = processedLine.indexOf(":") + 2; + let type: string = processedLine.substring( + typeStartIndex, + processedLine.length - 1 + ); + + // Update kotlin generic typing to Java generic typing + type = type.replace("?", ""); + type = type.replace("", ""); + + // Save variables and their types to be later used in constructor, getter, setter generation + variableNames.push(variable); + variableTypes.push(type); + javaTransformation += `\tprivate ${type} ${variable};`; + } else if (line.startsWith("typealias")) { + // If this is a kotlin typealias, make it into a class + const classNameStartIndex = 10; // length of string "typealias " + const classNameEndIndex = line.indexOf(" ="); + const className = line.substring( + classNameStartIndex, + classNameEndIndex + ); + const typeNameEndIndex = line.indexOf("=") + 2; + const type = line.substring(typeNameEndIndex, line.length - 1); // ignore the semi-colon + const variable = + className.charAt(0).toLowerCase() + className.substring(1); + + const titleCaseVariable = className; + const getters = `\tpublic ${type} get${titleCaseVariable}() {\n\t\treturn this.${variable};\n\t}\n\n`; + const setters = `\tpublic void set${titleCaseVariable}(${type} ${variable}) {\n\t\tthis.${variable} = ${variable};\n\t}\n\n`; + const constructor = `\tpublic ${className}(${type} ${variable}) {\n\t\tthis.${variable} = ${variable};\n\t}\n`; + javaTransformation += `public class ${className} {\n\tprivate ${type} ${variable};\n`; + javaTransformation += `\n${constructor}\n${getters}${setters}}`; + } else if (line.startsWith("import")) { + javaTransformation += `${line};`; + } else { + // If there's any other line, it is most probably a 'next line character', so just append it + javaTransformation += originalLine; + } + + javaTransformation += "\n"; + }); + + return javaTransformation; + }, []); + + return ( + + ); +} diff --git a/utils/routes.tsx b/utils/routes.tsx index 2e9c787..6ba7099 100644 --- a/utils/routes.tsx +++ b/utils/routes.tsx @@ -130,6 +130,12 @@ export const categorizedRoutes = [ packageUrl: "https://www.npmjs.com/package/json_typegen_wasm", packageName: "json_typegen_wasm" }, + { + label: "to Java", + path: "/json-to-java", + packageUrl: "https://www.npmjs.com/package/json_typegen_wasm", + packageName: "json_typegen_wasm" + }, { label: "to JSON Schema", path: "/json-to-json-schema",