Merge 458da55387 into f1a5489e21
				
					
				
			This commit is contained in:
		
						commit
						715b7f640f
					
				| @ -1,6 +1,7 @@ | ||||
| import { tool as base64FileConverter } from './base64-file-converter'; | ||||
| import { tool as base64StringConverter } from './base64-string-converter'; | ||||
| import { tool as basicAuthGenerator } from './basic-auth-generator'; | ||||
| import { tool as jsonToJava } from './json-to-java'; | ||||
| 
 | ||||
| import { tool as asciiTextDrawer } from './ascii-text-drawer'; | ||||
| 
 | ||||
| @ -106,6 +107,7 @@ export const toolsByCategory: ToolCategory[] = [ | ||||
|       yamlToToml, | ||||
|       jsonToYaml, | ||||
|       jsonToToml, | ||||
|       jsonToJava, | ||||
|       listConverter, | ||||
|       tomlToJson, | ||||
|       tomlToYaml, | ||||
|  | ||||
							
								
								
									
										12
									
								
								src/tools/json-to-java/index.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								src/tools/json-to-java/index.ts
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,12 @@ | ||||
| import { ArrowsShuffle } from '@vicons/tabler'; | ||||
| import { defineTool } from '../tool'; | ||||
| 
 | ||||
| export const tool = defineTool({ | ||||
|   name: 'JSON to Java Entity', | ||||
|   path: '/json-to-java', | ||||
|   description: 'Convert JSON into Java entities', | ||||
|   keywords: ['json', 'to', 'java'], | ||||
|   component: () => import('./json-to-java.vue'), | ||||
|   icon: ArrowsShuffle, | ||||
|   createdAt: new Date('2024-07-03'), | ||||
| }); | ||||
							
								
								
									
										56
									
								
								src/tools/json-to-java/json-to-java.service.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										56
									
								
								src/tools/json-to-java/json-to-java.service.ts
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,56 @@ | ||||
| const equivalence: { [key: string]: string } = { | ||||
|   number: 'Long', | ||||
| }; | ||||
| 
 | ||||
| export function convert(className: string, data: string | null) { | ||||
|   if (data === null) { | ||||
|     return ''; | ||||
|   } | ||||
|   if (typeof data === 'string') { | ||||
|     data = JSON.parse(data); | ||||
|   } | ||||
|   if (className === '') { | ||||
|     className = 'Result'; | ||||
|   } | ||||
|   const keys = Object.keys(data); | ||||
|   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 = data[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); | ||||
| } | ||||
							
								
								
									
										49
									
								
								src/tools/json-to-java/json-to-java.vue
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										49
									
								
								src/tools/json-to-java/json-to-java.vue
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,49 @@ | ||||
| <script setup lang="ts"> | ||||
| import JSON5 from 'json5'; | ||||
| import { convert } from './json-to-java.service'; | ||||
| import type { UseValidationRule } from '@/composable/validation'; | ||||
| import TextareaCopyable from '@/components/TextareaCopyable.vue'; | ||||
| 
 | ||||
| const jsonInput = ref(''); | ||||
| const clazzInput = ref('Result'); | ||||
| const goOutput = computed(() => { | ||||
|   return jsonInput.value ? convert(clazzInput.value, jsonInput.value) : ''; | ||||
| }); | ||||
| 
 | ||||
| const rules: UseValidationRule<string>[] = [ | ||||
|   { | ||||
|     validator: (v: string) => v === '' || JSON5.parse(v), | ||||
|     message: 'Provided JSON is not valid.', | ||||
|   }, | ||||
| ]; | ||||
| </script> | ||||
| 
 | ||||
| <template> | ||||
|   <c-card title="JSON to Java Entity"> | ||||
|     <c-input-text | ||||
|       v-model:value="clazzInput" | ||||
|       placeholder="Custom class name" | ||||
|       raw-text | ||||
|       label="classname" | ||||
|       label-position="left" | ||||
|       label-width="50px" | ||||
|       mb-2 | ||||
|     /> | ||||
|     <c-input-text | ||||
|       v-model:value="jsonInput" | ||||
|       multiline | ||||
|       placeholder="Put your josn string here..." | ||||
|       rows="20" | ||||
|       label="JSON to Java" | ||||
|       :validation-rules="rules" | ||||
|       raw-text | ||||
|       mb-5 | ||||
|     /> | ||||
|   </c-card> | ||||
|   <c-card title="You Java String"> | ||||
|     <TextareaCopyable | ||||
|       :value="goOutput" | ||||
|       language="json" | ||||
|     /> | ||||
|   </c-card> | ||||
| </template> | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user