Skip to content

Commit 23f8d73

Browse files
authored
Merge pull request #408 from Pranavram22/main
added jsontojava
2 parents 8b59344 + 71fef5b commit 23f8d73

File tree

3 files changed

+194
-0
lines changed

3 files changed

+194
-0
lines changed

package-lock.json

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/app/convert/Json-to-Java/page.jsx

+181
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
"use client";
2+
import React, { useState } from "react";
3+
import { NavBar } from "@/components/navbar";
4+
5+
const Converter = () => {
6+
const [inputJSON, setInputJSON] = useState("");
7+
const [outputJava, setOutputJava] = useState("");
8+
const [isDarkMode, setIsDarkMode] = useState(false);
9+
const [copySuccess, setCopySuccess] = useState(false);
10+
11+
const handleCopy = () => {
12+
if (outputJava) {
13+
navigator.clipboard.writeText(outputJava);
14+
setCopySuccess(true);
15+
setTimeout(() => setCopySuccess(false), 3000);
16+
}
17+
};
18+
19+
const toggleTheme = () => {
20+
setIsDarkMode(!isDarkMode);
21+
};
22+
23+
const handleConvert = () => {
24+
try {
25+
const jsonObject = JSON.parse(inputJSON);
26+
const convertedCode = convertJsonToJava(jsonObject, "RootClass");
27+
setOutputJava(convertedCode);
28+
} catch (error) {
29+
setOutputJava(`Error: ${error.message}. Please check your JSON input.`);
30+
}
31+
};
32+
33+
const convertJsonToJava = (json, className) => {
34+
let javaCode = `public class ${className} {\n`;
35+
36+
for (const [key, value] of Object.entries(json)) {
37+
const type = getJavaType(value);
38+
const fieldName = toCamelCase(key);
39+
javaCode += ` private ${type} ${fieldName};\n`;
40+
}
41+
42+
// Generate getters and setters
43+
for (const [key, value] of Object.entries(json)) {
44+
const type = getJavaType(value);
45+
const fieldName = toCamelCase(key);
46+
const capitalizedFieldName =
47+
fieldName.charAt(0).toUpperCase() + fieldName.slice(1);
48+
49+
// Getter
50+
javaCode += `\n public ${type} get${capitalizedFieldName}() {\n`;
51+
javaCode += ` return ${fieldName};\n`;
52+
javaCode += ` }\n`;
53+
54+
// Setter
55+
javaCode += `\n public void set${capitalizedFieldName}(${type} ${fieldName}) {\n`;
56+
javaCode += ` this.${fieldName} = ${fieldName};\n`;
57+
javaCode += ` }\n`;
58+
}
59+
60+
javaCode += "}\n";
61+
62+
// Generate nested classes
63+
for (const [key, value] of Object.entries(json)) {
64+
if (
65+
typeof value === "object" &&
66+
value !== null &&
67+
!Array.isArray(value)
68+
) {
69+
const nestedClassName = key.charAt(0).toUpperCase() + key.slice(1);
70+
javaCode += "\n" + convertJsonToJava(value, nestedClassName);
71+
}
72+
}
73+
74+
return javaCode;
75+
};
76+
77+
const getJavaType = (value) => {
78+
if (value === null) return "Object";
79+
if (typeof value === "string") return "String";
80+
if (typeof value === "number") {
81+
return Number.isInteger(value) ? "int" : "double";
82+
}
83+
if (typeof value === "boolean") return "boolean";
84+
if (Array.isArray(value)) {
85+
if (value.length === 0) return "List<Object>";
86+
return `List<${getJavaType(value[0])}>`;
87+
}
88+
if (typeof value === "object") {
89+
return capitalizeFirstLetter(value.constructor.name);
90+
}
91+
return "Object";
92+
};
93+
94+
const toCamelCase = (str) => {
95+
return str.replace(/[^a-zA-Z0-9]+(.)/g, (m, chr) => chr.toUpperCase());
96+
};
97+
98+
const capitalizeFirstLetter = (string) => {
99+
return string.charAt(0).toUpperCase() + string.slice(1);
100+
};
101+
102+
return (
103+
<div
104+
className={`${isDarkMode ? "bg-gray-900 text-gray-400" : "bg-gray-100 text-gray-500"} min-h-screen w-full pb-2`}
105+
>
106+
<NavBar
107+
title="JSON to Java Converter"
108+
isDarkMode={isDarkMode}
109+
toggleTheme={toggleTheme}
110+
/>
111+
<h1 className="relative z-10 font-sans text-5xl font-bold text-center text-transparent md:text-7xl bg-clip-text bg-gradient-to-b from-neutral-200 to-neutral-600 mb-7 mt-4">
112+
JSON to Java Converter
113+
</h1>
114+
115+
<div className="flex flex-col md:flex-row gap-2">
116+
<textarea
117+
className={`w-full ml-7 mr-4 p-2 rounded-lg border border-gray-300 focus:ring-2 focus:ring-blue-500 focus:outline-none transition duration-200 ease-in-out placeholder-gray-400 shadow-sm hover:shadow-md resize-none ${isDarkMode ? "bg-gray-800 text-gray-200" : "bg-gray-200 text-gray-500"}`}
118+
rows="10"
119+
placeholder="Paste JSON here..."
120+
value={inputJSON}
121+
onChange={(e) => setInputJSON(e.target.value)}
122+
></textarea>
123+
124+
<div className="relative w-full mr-7">
125+
<textarea
126+
className={`w-full p-2 rounded-lg border border-gray-300 focus:ring-2 focus:ring-blue-500 focus:outline-none transition duration-200 ease-in-out placeholder-gray-400 shadow-sm hover:shadow-md resize-none ${isDarkMode ? "bg-gray-800 text-gray-200" : "bg-gray-200 text-gray-500"}`}
127+
rows="10"
128+
readOnly
129+
value={outputJava}
130+
></textarea>
131+
132+
<button
133+
className={`absolute top-2 right-2 ${
134+
copySuccess ? "bg-green-500" : "bg-gray-200 hover:bg-gray-300"
135+
} rounded p-2 focus:outline-none transition-colors duration-200`}
136+
onClick={handleCopy}
137+
>
138+
{copySuccess ? (
139+
<svg
140+
xmlns="http://www.w3.org/2000/svg"
141+
viewBox="0 0 24 24"
142+
fill="white"
143+
className="w-6 h-6"
144+
>
145+
<path
146+
fillRule="evenodd"
147+
d="M16.707 7.707a1 1 0 00-1.414-1.414L9 12.586l-2.293-2.293a1 1 0 10-1.414 1.414l3 3a1 1 0 001.414 0l7-7z"
148+
clipRule="evenodd"
149+
/>
150+
</svg>
151+
) : (
152+
<svg
153+
xmlns="http://www.w3.org/2000/svg"
154+
fill="none"
155+
viewBox="0 0 24 24"
156+
stroke="currentColor"
157+
className="w-6 h-6 text-gray-700"
158+
>
159+
<path
160+
strokeLinecap="round"
161+
strokeLinejoin="round"
162+
strokeWidth={2}
163+
d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z"
164+
/>
165+
</svg>
166+
)}
167+
</button>
168+
</div>
169+
</div>
170+
171+
<button
172+
className="bg-gradient-to-r from-blue-500 to-indigo-600 text-white px-6 py-3 mt-6 rounded-lg shadow-lg hover:from-blue-600 hover:to-indigo-700 hover:shadow-xl transform hover:scale-105 transition duration-300 ease-in-out focus:outline-none focus:ring-4 focus:ring-blue-300 ml-7"
173+
onClick={handleConvert}
174+
>
175+
Convert to Java
176+
</button>
177+
</div>
178+
);
179+
};
180+
181+
export default Converter;

src/db/tools.json

+6
Original file line numberDiff line numberDiff line change
@@ -154,5 +154,11 @@
154154
"name": "CSS To Tailwind Converter",
155155
"link": "/CSS-to-Tailwind-converter",
156156
"ctg": "other"
157+
},
158+
{
159+
"id": 27,
160+
"name": "JSON TO Java ",
161+
"link": "/convert/Json-to-Java",
162+
"ctg": "other"
157163
}
158164
]

0 commit comments

Comments
 (0)