|
10 | 10 | import org.jspecify.annotations.Nullable; |
11 | 11 |
|
12 | 12 | /** |
13 | | - * Abstraction for JSON serialization/deserialization to decouple the SDK from any |
14 | | - * specific JSON library. |
| 13 | + * Reads and writes the JSON of ACP messages for the SDK, so that acp-core itself depends on no JSON |
| 14 | + * library. The transports use it to write each message they send, to read each message that |
| 15 | + * arrives, and to convert a message's params or result into the |
| 16 | + * {@link com.agentclientprotocol.sdk.spec.AcpSchema} record the method expects. Most applications |
| 17 | + * never call it: a transport created without a mapper gets one from {@link #createDefault()}. |
15 | 18 | * |
16 | | - * <p> |
17 | | - * {@code acp-core} contains no implementation. One comes from a JSON module on the |
18 | | - * classpath: {@code acp-json-jackson2} (Jackson 2, which the transport and agent-support |
19 | | - * modules bring in) or {@code acp-json-jackson3} (Jackson 3). Each registers an |
20 | | - * {@link AcpJsonMapperSupplier} discovered via {@link ServiceLoader}. |
21 | | - * </p> |
| 19 | + * <p>The implementation comes from a JSON module on the classpath: {@code acp-json-jackson2} |
| 20 | + * ({@code JacksonAcpJsonMapper}, Jackson 2) or {@code acp-json-jackson3} |
| 21 | + * ({@code Jackson3AcpJsonMapper}, Jackson 3). {@code acp-agent-support}, |
| 22 | + * {@code acp-streamable-http-jetty} and {@code acp-test} bring in {@code acp-json-jackson2}; with |
| 23 | + * acp-core alone, add one of the two. Each module registers an {@link AcpJsonMapperSupplier}, and |
| 24 | + * {@link #createDefault()} picks one. To configure Jackson yourself, create the module's mapper |
| 25 | + * around your own Jackson mapper and pass it to the transport's constructor. |
22 | 26 | * |
23 | | - * <p> |
24 | | - * Alternative implementations (e.g., for Micronaut serialization) can be provided by |
25 | | - * implementing {@link AcpJsonMapperSupplier} and registering it in |
26 | | - * {@code META-INF/services/com.agentclientprotocol.sdk.json.AcpJsonMapperSupplier}. |
27 | | - * </p> |
| 27 | + * <p>Implementations: the schema records carry Jackson annotations ({@code @JsonProperty}, |
| 28 | + * {@code @JsonInclude}, {@code @JsonTypeInfo}, {@code @JsonSubTypes}, {@code @JsonCreator}, |
| 29 | + * {@code @JsonValue}, {@code @JsonAnyGetter} and {@code @JsonAnySetter}), so a mapper must honour |
| 30 | + * them to read and write ACP. A transport calls its mapper from several threads at once, so it must |
| 31 | + * be safe for concurrent use. Reads and writes report failures as {@link IOException}; the SDK |
| 32 | + * answers text that is not JSON with a parse error ({@code -32700}). A conversion whose value does |
| 33 | + * not fit the target type must fail with {@link IllegalArgumentException}, which the SDK answers as |
| 34 | + * invalid params ({@code -32602}). Register an implementation through an |
| 35 | + * {@link AcpJsonMapperSupplier}. |
28 | 36 | * |
29 | 37 | * @author Mark Pollack |
30 | 38 | */ |
31 | 39 | public interface AcpJsonMapper { |
32 | 40 |
|
33 | 41 | /** |
34 | | - * Creates a default AcpJsonMapper from the {@link AcpJsonMapperSupplier}s found via |
35 | | - * {@link ServiceLoader}. The choice is deterministic: |
| 42 | + * Returns a new mapper from the {@link AcpJsonMapperSupplier}s that {@link ServiceLoader} finds |
| 43 | + * with the current thread's context class loader. The choice does not depend on classpath |
| 44 | + * order: |
36 | 45 | * <ol> |
37 | | - * <li>if the system property {@code acp.json.mapper.supplier} is set, the supplier |
38 | | - * with exactly that fully qualified class name is used (and it is an error if there is |
39 | | - * none);</li> |
40 | | - * <li>otherwise the supplier with the highest {@link AcpJsonMapperSupplier#priority()} |
41 | | - * is used, ties broken by class name. With both SDK modules present, |
42 | | - * {@code acp-json-jackson3} (priority -100) wins over {@code acp-json-jackson2} |
43 | | - * (-200); an application supplier that keeps the default priority (0) wins over |
44 | | - * both.</li> |
| 46 | + * <li>if the system property {@code acp.json.mapper.supplier} is set (and not blank), the |
| 47 | + * supplier whose class has exactly that fully qualified name is used;</li> |
| 48 | + * <li>otherwise the supplier with the highest {@link AcpJsonMapperSupplier#priority()} is used, |
| 49 | + * ties broken by class name. With both SDK modules present, {@code acp-json-jackson3} (priority |
| 50 | + * {@code -100}) wins over {@code acp-json-jackson2} ({@code -200}); an application supplier |
| 51 | + * that keeps the default priority ({@code 0}) wins over both.</li> |
45 | 52 | * </ol> |
46 | | - * @return a new AcpJsonMapper instance |
47 | | - * @throws java.util.ServiceConfigurationError if no supplier is found, or none |
48 | | - * matches the system property |
| 53 | + * |
| 54 | + * <p>Each call looks the suppliers up again and asks the chosen one for a mapper; the SDK's |
| 55 | + * suppliers build a new one every time, so keep the result rather than calling this per |
| 56 | + * message. |
| 57 | + * @return the chosen supplier's mapper |
| 58 | + * @throws java.util.ServiceConfigurationError if no supplier is found, or none has the class |
| 59 | + * named by the system property |
| 60 | + * @throws IllegalStateException if the chosen SDK module finds a Jackson older than it supports |
| 61 | + * (2.18.1 for Jackson 2, 3.0.0 for Jackson 3) |
49 | 62 | */ |
50 | 63 | static AcpJsonMapper createDefault() { |
51 | 64 | return AcpJsonMapperSelector.select().get(); |
52 | 65 | } |
53 | 66 |
|
54 | 67 | /** |
55 | | - * Deserialize JSON string into a target type. |
56 | | - * @param content JSON as String |
57 | | - * @param type target class |
58 | | - * @return deserialized instance, or {@code null} when the JSON is the {@code null} literal |
59 | | - * @param <T> generic type |
60 | | - * @throws IOException on parse errors |
| 68 | + * Reads a JSON text as the given type. |
| 69 | + * @param content the JSON text |
| 70 | + * @param type the type to read |
| 71 | + * @param <T> the type to read |
| 72 | + * @return the value, or {@code null} when the text is the JSON literal {@code null} |
| 73 | + * @throws IOException if the text is not JSON or does not fit the type |
61 | 74 | */ |
62 | 75 | <T> @Nullable T readValue(String content, Class<T> type) throws IOException; |
63 | 76 |
|
64 | 77 | /** |
65 | | - * Deserialize JSON bytes into a target type. |
66 | | - * @param content JSON as bytes |
67 | | - * @param type target class |
68 | | - * @return deserialized instance, or {@code null} when the JSON is the {@code null} literal |
69 | | - * @param <T> generic type |
70 | | - * @throws IOException on parse errors |
| 78 | + * Reads JSON bytes as the given type, like {@link #readValue(String, Class)}. |
| 79 | + * @param content the JSON bytes |
| 80 | + * @param type the type to read |
| 81 | + * @param <T> the type to read |
| 82 | + * @return the value, or {@code null} when the bytes are the JSON literal {@code null} |
| 83 | + * @throws IOException if the bytes are not JSON or do not fit the type |
71 | 84 | */ |
72 | 85 | <T> @Nullable T readValue(byte[] content, Class<T> type) throws IOException; |
73 | 86 |
|
74 | 87 | /** |
75 | | - * Deserialize JSON string into a parameterized target type. |
76 | | - * @param content JSON as String |
77 | | - * @param type parameterized type reference |
78 | | - * @return deserialized instance, or {@code null} when the JSON is the {@code null} literal |
79 | | - * @param <T> generic type |
80 | | - * @throws IOException on parse errors |
| 88 | + * Reads a JSON text as a generic type, such as {@code List<AcpSchema.ContentBlock>}, that a |
| 89 | + * {@link TypeRef} captures. |
| 90 | + * @param content the JSON text |
| 91 | + * @param type the type to read |
| 92 | + * @param <T> the type to read |
| 93 | + * @return the value, or {@code null} when the text is the JSON literal {@code null} |
| 94 | + * @throws IOException if the text is not JSON or does not fit the type |
81 | 95 | */ |
82 | 96 | <T> @Nullable T readValue(String content, TypeRef<T> type) throws IOException; |
83 | 97 |
|
84 | 98 | /** |
85 | | - * Deserialize JSON bytes into a parameterized target type. |
86 | | - * @param content JSON as bytes |
87 | | - * @param type parameterized type reference |
88 | | - * @return deserialized instance, or {@code null} when the JSON is the {@code null} literal |
89 | | - * @param <T> generic type |
90 | | - * @throws IOException on parse errors |
| 99 | + * Reads JSON bytes as a generic type that a {@link TypeRef} captures, like |
| 100 | + * {@link #readValue(String, TypeRef)}. |
| 101 | + * @param content the JSON bytes |
| 102 | + * @param type the type to read |
| 103 | + * @param <T> the type to read |
| 104 | + * @return the value, or {@code null} when the bytes are the JSON literal {@code null} |
| 105 | + * @throws IOException if the bytes are not JSON or do not fit the type |
91 | 106 | */ |
92 | 107 | <T> @Nullable T readValue(byte[] content, TypeRef<T> type) throws IOException; |
93 | 108 |
|
94 | 109 | /** |
95 | | - * Convert a value to a given type, useful for mapping nested JSON structures. |
96 | | - * @param fromValue source value |
97 | | - * @param type target class |
98 | | - * @return converted value |
99 | | - * @param <T> generic type |
| 110 | + * Converts a value to the given type as if it were written as JSON and read back. The |
| 111 | + * transports use it to turn the params or result of a message, read as maps and lists, into the |
| 112 | + * record the method expects. |
| 113 | + * @param fromValue the value, usually maps and lists read from JSON |
| 114 | + * @param type the type to convert to |
| 115 | + * @param <T> the type to convert to |
| 116 | + * @return the converted value |
| 117 | + * @throws IllegalArgumentException if the value does not fit the type |
100 | 118 | */ |
101 | 119 | <T> T convertValue(Object fromValue, Class<T> type); |
102 | 120 |
|
103 | 121 | /** |
104 | | - * Convert a value to a given parameterized type. |
105 | | - * @param fromValue source value |
106 | | - * @param type target type reference |
107 | | - * @return converted value |
108 | | - * @param <T> generic type |
| 122 | + * Converts a value to a generic type that a {@link TypeRef} captures, like |
| 123 | + * {@link #convertValue(Object, Class)}. |
| 124 | + * @param fromValue the value, usually maps and lists read from JSON |
| 125 | + * @param type the type to convert to |
| 126 | + * @param <T> the type to convert to |
| 127 | + * @return the converted value |
| 128 | + * @throws IllegalArgumentException if the value does not fit the type |
109 | 129 | */ |
110 | 130 | <T> T convertValue(Object fromValue, TypeRef<T> type); |
111 | 131 |
|
112 | 132 | /** |
113 | | - * Serialize an object to JSON string. |
114 | | - * @param value object to serialize |
115 | | - * @return JSON as String |
116 | | - * @throws IOException on serialization errors |
| 133 | + * Writes a value, such as a JSON-RPC message, as JSON text. |
| 134 | + * @param value the value to write |
| 135 | + * @return the JSON text |
| 136 | + * @throws IOException if the value cannot be written as JSON |
117 | 137 | */ |
118 | 138 | String writeValueAsString(Object value) throws IOException; |
119 | 139 |
|
120 | 140 | /** |
121 | | - * Serialize an object to JSON bytes. |
122 | | - * @param value object to serialize |
123 | | - * @return JSON as bytes |
124 | | - * @throws IOException on serialization errors |
| 141 | + * Writes a value as JSON bytes in UTF-8. |
| 142 | + * @param value the value to write |
| 143 | + * @return the JSON bytes |
| 144 | + * @throws IOException if the value cannot be written as JSON |
125 | 145 | */ |
126 | 146 | byte[] writeValueAsBytes(Object value) throws IOException; |
127 | 147 |
|
|
0 commit comments