|
4 | 4 |
|
5 | 5 | package io.modelcontextprotocol.spec;
|
6 | 6 |
|
| 7 | +import java.io.BufferedReader; |
7 | 8 | import java.io.IOException;
|
| 9 | +import java.io.Reader; |
| 10 | +import java.io.StringReader; |
8 | 11 | import java.util.ArrayList;
|
9 | 12 | import java.util.HashMap;
|
10 | 13 | import java.util.List;
|
@@ -140,32 +143,36 @@ public sealed interface Request
|
140 | 143 | /**
|
141 | 144 | * Deserializes a JSON string into a JSONRPCMessage object.
|
142 | 145 | * @param objectMapper The ObjectMapper instance to use for deserialization
|
143 |
| - * @param jsonText The JSON string to deserialize |
| 146 | + * @param inputStream The JSON string to deserialize |
144 | 147 | * @return A JSONRPCMessage instance using either the {@link JSONRPCRequest},
|
145 | 148 | * {@link JSONRPCNotification}, or {@link JSONRPCResponse} classes.
|
146 |
| - * @throws IOException If there's an error during deserialization |
147 | 149 | * @throws IllegalArgumentException If the JSON structure doesn't match any known
|
148 | 150 | * message type
|
149 | 151 | */
|
150 |
| - public static JSONRPCMessage deserializeJsonRpcMessage(ObjectMapper objectMapper, String jsonText) |
151 |
| - throws IOException { |
152 |
| - |
153 |
| - logger.debug("Received JSON message: {}", jsonText); |
154 |
| - |
155 |
| - var map = objectMapper.readValue(jsonText, MAP_TYPE_REF); |
156 |
| - |
157 |
| - // Determine message type based on specific JSON structure |
158 |
| - if (map.containsKey("method") && map.containsKey("id")) { |
159 |
| - return objectMapper.convertValue(map, JSONRPCRequest.class); |
160 |
| - } |
161 |
| - else if (map.containsKey("method") && !map.containsKey("id")) { |
162 |
| - return objectMapper.convertValue(map, JSONRPCNotification.class); |
| 152 | + public static JSONRPCMessage deserializeJsonRpcMessage(ObjectMapper objectMapper, BufferedReader inputStream) { |
| 153 | + try { |
| 154 | + var map = objectMapper.readValue(inputStream, MAP_TYPE_REF); |
| 155 | + // Determine message type based on specific JSON structure |
| 156 | + if (map.containsKey("method") && map.containsKey("id")) { |
| 157 | + return objectMapper.convertValue(map, JSONRPCRequest.class); |
| 158 | + } |
| 159 | + else if (map.containsKey("method") && !map.containsKey("id")) { |
| 160 | + return objectMapper.convertValue(map, JSONRPCNotification.class); |
| 161 | + } |
| 162 | + else if (map.containsKey("result") || map.containsKey("error")) { |
| 163 | + return objectMapper.convertValue(map, JSONRPCResponse.class); |
| 164 | + } |
| 165 | + throw new IllegalArgumentException("Cannot deserialize JSONRPCMessage: " + map); |
163 | 166 | }
|
164 |
| - else if (map.containsKey("result") || map.containsKey("error")) { |
165 |
| - return objectMapper.convertValue(map, JSONRPCResponse.class); |
| 167 | + catch (IOException e) { |
| 168 | + throw new java.io.UncheckedIOException(e); |
166 | 169 | }
|
| 170 | + } |
167 | 171 |
|
168 |
| - throw new IllegalArgumentException("Cannot deserialize JSONRPCMessage: " + jsonText); |
| 172 | + public static JSONRPCMessage deserializeJsonRpcMessage(ObjectMapper objectMapper, String input) { |
| 173 | + Reader inputString = new StringReader(input); |
| 174 | + BufferedReader reader = new BufferedReader(inputString); |
| 175 | + return deserializeJsonRpcMessage(objectMapper, reader); |
169 | 176 | }
|
170 | 177 |
|
171 | 178 | // ---------------------------
|
|
0 commit comments