Class JsonSchemaGenerator

java.lang.Object
com.tnsai.tools.method.JsonSchemaGenerator

public final class JsonSchemaGenerator extends Object
Builds JSON Schema fragments for ToolMethod parameters from their static Java Types.

The output shape matches what LLM tool-calling APIs expect — see for instance OpenAI's function.parameters, Anthropic's input_schema, Gemini's parameters. All three converge on the same JSON Schema subset.

Supported types

Java typeJSON Schema
String, char, Character"string"
int / Integer / long / Long / short / Short / byte / Byte"integer"
float / Float / double / Double"number"
boolean / Boolean"boolean"
Enum subclasses"string" + enum: [VALUES]
List<T>, Set<T>, T[]"array" + items: {…}
Map<String, V>"object" + additionalProperties: {…}
Other reference types"object" (no properties — recursive POJO support deferred)

Recursive POJO schema generation is intentionally out of scope for the Phase 1 implementation — most tool methods use primitives + collections of primitives. POJO parameters can be added later without breaking the contract: callers will see strictly more detail in the schema, never less.

Since:
0.5.7 (LangChain-style tool refactor — see issue #188)
  • Method Details

    • parametersSchema

      public static Map<String,Object> parametersSchema(ToolMethod tool)
      Build the full parameters object for a tool method — the value that goes under function.parameters in OpenAI's tool definition (or input_schema in Anthropic's).
      Parameters:
      tool - the discovered tool method
      Returns:
      a Map suitable for JSON serialization (Jackson, Gson, …)
    • parameterSchema

      public static Map<String,Object> parameterSchema(ToolParameter param)
      Build the schema for a single parameter — type + description + enum constraints when applicable.
    • typeSchema

      public static Map<String,Object> typeSchema(Type type)
      Map a Java Type to its JSON Schema fragment. Public for tests and reuse — most callers want parameterSchema(ToolParameter).