|
| 1 | +package com.github.codeboyzhou.mcp.declarative.enums; |
| 2 | + |
| 3 | +/** |
| 4 | + * This enum is used to map Java types to JSON schema data types. |
| 5 | + * |
| 6 | + * @author codeboyzhou |
| 7 | + */ |
| 8 | +public enum JavaTypeToJsonSchemaMapper { |
| 9 | + |
| 10 | + /** Java {@code String} is mapped to JSON schema data type {@code string}. */ |
| 11 | + STRING(String.class, "string"), |
| 12 | + |
| 13 | + /** Java {@code Number} is mapped to JSON schema data type {@code number}. */ |
| 14 | + NUMBER(Number.class, "number"), |
| 15 | + |
| 16 | + /** Java {@code long} is mapped to JSON schema data type {@code number}. */ |
| 17 | + LONG(long.class, "number"), |
| 18 | + |
| 19 | + /** Java {@code Long} is mapped to JSON schema data type {@code number}. */ |
| 20 | + LONG_CLASS(Long.class, "number"), |
| 21 | + |
| 22 | + /** Java {@code float} is mapped to JSON schema data type {@code number}. */ |
| 23 | + FLOAT(float.class, "number"), |
| 24 | + |
| 25 | + /** Java {@code Float} is mapped to JSON schema data type {@code number}. */ |
| 26 | + FLOAT_CLASS(Float.class, "number"), |
| 27 | + |
| 28 | + /** Java {@code double} is mapped to JSON schema data type {@code number}. */ |
| 29 | + DOUBLE(double.class, "number"), |
| 30 | + |
| 31 | + /** Java {@code Double} is mapped to JSON schema data type {@code number}. */ |
| 32 | + DOUBLE_CLASS(Double.class, "number"), |
| 33 | + |
| 34 | + /** Java {@code int} is mapped to JSON schema data type {@code integer}. */ |
| 35 | + INT(int.class, "integer"), |
| 36 | + |
| 37 | + /** Java {@code Integer} is mapped to JSON schema data type {@code integer}. */ |
| 38 | + INTEGER(Integer.class, "integer"), |
| 39 | + |
| 40 | + /** Java {@code boolean} is mapped to JSON schema data type {@code boolean}. */ |
| 41 | + BOOLEAN(boolean.class, "boolean"), |
| 42 | + |
| 43 | + /** Java {@code Boolean} is mapped to JSON schema data type {@code boolean}. */ |
| 44 | + BOOLEAN_CLASS(Boolean.class, "boolean"), |
| 45 | + |
| 46 | + /** Java {@code Object} is mapped to JSON schema data type {@code object}. */ |
| 47 | + OBJECT(Object.class, "object"), |
| 48 | + ; |
| 49 | + |
| 50 | + /** The Java class that is mapped to the JSON schema data type. */ |
| 51 | + private final Class<?> javaType; |
| 52 | + |
| 53 | + /** The JSON schema data type. */ |
| 54 | + private final String jsonSchemaType; |
| 55 | + |
| 56 | + /** |
| 57 | + * Creates a new instance of {@code JsonSchemaDataType} with the specified Java class and JSON |
| 58 | + * schema data type. |
| 59 | + * |
| 60 | + * @param javaType the Java class that is mapped to the JSON schema data type |
| 61 | + * @param jsonSchemaType the JSON schema data type |
| 62 | + */ |
| 63 | + JavaTypeToJsonSchemaMapper(Class<?> javaType, String jsonSchemaType) { |
| 64 | + this.javaType = javaType; |
| 65 | + this.jsonSchemaType = jsonSchemaType; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Returns the JSON schema data type. |
| 70 | + * |
| 71 | + * @return the JSON schema data type |
| 72 | + */ |
| 73 | + public String getJsonSchemaType() { |
| 74 | + return jsonSchemaType; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Returns the JSON schema data type for the specified Java class. If the Java class is not |
| 79 | + * supported or mapped to a JSON schema data type, {@code "string"} will be returned. |
| 80 | + * |
| 81 | + * @param javaType the Java class |
| 82 | + * @return the JSON schema data type |
| 83 | + */ |
| 84 | + public static String getJsonSchemaType(Class<?> javaType) { |
| 85 | + JavaTypeToJsonSchemaMapper[] values = values(); |
| 86 | + for (JavaTypeToJsonSchemaMapper mapper : values) { |
| 87 | + if (mapper.javaType.equals(javaType)) { |
| 88 | + return mapper.jsonSchemaType; |
| 89 | + } |
| 90 | + } |
| 91 | + return STRING.jsonSchemaType; |
| 92 | + } |
| 93 | +} |
0 commit comments