1717package io .agentscope .core .util ;
1818
1919import com .fasterxml .jackson .core .type .TypeReference ;
20+ import com .fasterxml .jackson .databind .JavaType ;
2021import com .fasterxml .jackson .databind .ObjectMapper ;
2122import com .fasterxml .jackson .module .jsonSchema .JsonSchema ;
2223import com .fasterxml .jackson .module .jsonSchema .JsonSchemaGenerator ;
23- import java .util . List ;
24+ import java .lang . reflect . Type ;
2425import java .util .Map ;
2526
2627/**
2728 * Utility class for JSON Schema operations.
2829 *
29- * <p>This class provides utility methods for:
30+ * <p>
31+ * This class provides utility methods for:
3032 * <ul>
31- * <li>Generating JSON schemas from Java classes (for structured output)</li>
32- * <li>Converting between Maps and typed objects</li>
33- * <li>Mapping Java types to JSON Schema types</li>
33+ * <li>Generating JSON schemas from Java classes (for structured output)</li>
34+ * <li>Converting between Maps and typed objects</li>
35+ * <li>Mapping Java types to JSON Schema types</li>
3436 * </ul>
37+ *
3538 * @hidden
3639 */
3740public class JsonSchemaUtils {
@@ -42,13 +45,15 @@ public class JsonSchemaUtils {
4245
4346 /**
4447 * Generate JSON Schema from a Java class.
45- * This method is suitable for structured output scenarios where complex nested objects
48+ * This method is suitable for structured output scenarios where complex nested
49+ * objects
4650 * need to be converted to JSON Schema format.
4751 *
4852 * @param clazz The class to generate schema for
4953 * @return JSON Schema as a Map
5054 * @throws RuntimeException if schema generation fails due to reflection errors,
51- * Jackson configuration issues, or other processing errors
55+ * Jackson configuration issues, or other processing
56+ * errors
5257 */
5358 public static Map <String , Object > generateSchemaFromClass (Class <?> clazz ) {
5459 try {
@@ -59,16 +64,34 @@ public static Map<String, Object> generateSchemaFromClass(Class<?> clazz) {
5964 }
6065 }
6166
67+ /**
68+ * Generate JSON Schema from a Java Type (supports Generics).
69+ *
70+ * @param type The type to generate schema for
71+ * @return JSON Schema as a Map
72+ */
73+ public static Map <String , Object > generateSchemaFromType (Type type ) {
74+ try {
75+ JavaType javaType = objectMapper .constructType (type );
76+ JsonSchema schema = schemaGenerator .generateSchema (javaType );
77+ return objectMapper .convertValue (schema , new TypeReference <Map <String , Object >>() {});
78+ } catch (Exception e ) {
79+ throw new RuntimeException (
80+ "Failed to generate JSON schema for " + type .getTypeName (), e );
81+ }
82+ }
83+
6284 /**
6385 * Convert Map to typed object.
6486 *
65- * @param data The data map
87+ * @param data The data map
6688 * @param targetClass The target class
67- * @param <T> The type
89+ * @param <T> The type
6890 * @return Converted object
6991 * @throws IllegalStateException if the input data is null
70- * @throws RuntimeException if the conversion fails due to type mismatch,
71- * JSON parsing errors, or incompatible data structure
92+ * @throws RuntimeException if the conversion fails due to type mismatch,
93+ * JSON parsing errors, or incompatible data
94+ * structure
7295 */
7396 public static <T > T convertToObject (Object data , Class <T > targetClass ) {
7497 if (data == null ) {
@@ -81,33 +104,4 @@ public static <T> T convertToObject(Object data, Class<T> targetClass) {
81104 throw new RuntimeException ("Failed to convert metadata to " + targetClass .getName (), e );
82105 }
83106 }
84-
85- /**
86- * Map Java type to JSON Schema type.
87- * This is a simple type mapping suitable for basic tool parameters.
88- *
89- * @param clazz the Java class
90- * @return JSON type string
91- */
92- public static String mapJavaTypeToJsonType (Class <?> clazz ) {
93- if (clazz == String .class ) {
94- return "string" ;
95- } else if (clazz == Integer .class
96- || clazz == int .class
97- || clazz == Long .class
98- || clazz == long .class ) {
99- return "integer" ;
100- } else if (clazz == Double .class
101- || clazz == double .class
102- || clazz == Float .class
103- || clazz == float .class ) {
104- return "number" ;
105- } else if (clazz == Boolean .class || clazz == boolean .class ) {
106- return "boolean" ;
107- } else if (clazz .isArray () || List .class .isAssignableFrom (clazz )) {
108- return "array" ;
109- } else {
110- return "object" ;
111- }
112- }
113107}
0 commit comments