MCPcopy Create free account
hub / github.com/xpf0000/FlyEnv / JsonToJava

Function JsonToJava

src/render/util/transform/Java.ts:13–111  ·  view source on GitHub ↗
(json: any)

Source from the content-addressed store, hash-verified

11}
12
13export const JsonToJava = (json: any) => {
14 const kotlinTransformationLines: string[] = JsonToKotlin(json).split('\n')
15
16 // use Kotlin transformation to convert JSON to Java
17 let javaTransformation: string = ''
18 let currentClass: string = ''
19 let variableNames: string[] = []
20 let variableTypes: string[] = []
21
22 kotlinTransformationLines.forEach((line: string) => {
23 const originalLine = line
24 line = line.trim()
25
26 if (line === ')') {
27 // Class is closing so generate constructor, getters and setters for
28 // the current class, close the class and reset running values
29 const args: string[] = []
30 const getters: string[] = []
31 const setters: string[] = []
32
33 // Create args for constructor, getters and setters
34 for (let i = 0; i < variableNames.length; i++) {
35 const type = variableTypes[i]
36 const variableName = variableNames[i]
37 const titleCaseVariable = variableName.charAt(0).toUpperCase() + variableName.substring(1)
38 args.push(`${type} ${variableName}`)
39 getters.push(
40 `\tpublic ${type} get${titleCaseVariable}() {\n\t\treturn this.${variableName};\n\t}\n`
41 )
42 setters.push(
43 `\tpublic void set${titleCaseVariable}(${type} ${variableName}) {\n\t\tthis.${variableName} = ${variableName};\n\t}\n`
44 )
45 }
46
47 // Create constructor
48 let constructor = `\tpublic ${currentClass}(${args.join(', ')}) {`
49 const properties: string[] = []
50 variableNames.forEach((variable) => {
51 properties.push(`this.${variable} = ${variable};`)
52 })
53 constructor += `\n\t\t${properties.join('\n\t\t')}\n\t}\n`
54 javaTransformation += `\n${constructor}\n${getters.join('\n')}\n${setters.join('\n')}}`
55
56 // Reset running values
57 currentClass = ''
58 variableNames = []
59 variableTypes = []
60 } else if (line.startsWith('data class ')) {
61 // Change the start of a class from 'data class Root(' to 'public class Root {'
62 const classNameStartIndex = 11
63 const classNameEndIndex = line.indexOf('(')
64 const className = line.substring(classNameStartIndex, classNameEndIndex)
65 javaTransformation += `public class ${className} {`
66 currentClass = className
67 } else if (line.startsWith('val')) {
68 // If this is a variable, change 'val name: String' to 'private String name;'
69 // followed by respective getters, setters for the variable
70 const processedLine = line.replace('?', '')

Callers 1

jsonToJavaFunction · 0.90

Calls 2

JsonToKotlinFunction · 0.85
pushMethod · 0.80

Tested by

no test coverage detected