|
| 1 | +/* |
| 2 | + * Copyright 2026 - 2026 the original author or authors. |
| 3 | + */ |
| 4 | + |
| 5 | +package io.modelcontextprotocol.json.jackson2; |
| 6 | + |
| 7 | +import java.io.IOException; |
| 8 | + |
| 9 | +import com.fasterxml.jackson.databind.JavaType; |
| 10 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 11 | + |
| 12 | +import io.modelcontextprotocol.json.McpJsonMapper; |
| 13 | +import io.modelcontextprotocol.json.TypeRef; |
| 14 | + |
| 15 | +/** |
| 16 | + * Jackson-based implementation of JsonMapper. Wraps a Jackson ObjectMapper but keeps the |
| 17 | + * SDK decoupled from Jackson at the API level. |
| 18 | + */ |
| 19 | +public final class JacksonMcpJsonMapper implements McpJsonMapper { |
| 20 | + |
| 21 | + private final ObjectMapper objectMapper; |
| 22 | + |
| 23 | + /** |
| 24 | + * Constructs a new JacksonMcpJsonMapper instance with the given ObjectMapper. |
| 25 | + * @param objectMapper the ObjectMapper to be used for JSON serialization and |
| 26 | + * deserialization. Must not be null. |
| 27 | + * @throws IllegalArgumentException if the provided ObjectMapper is null. |
| 28 | + */ |
| 29 | + public JacksonMcpJsonMapper(ObjectMapper objectMapper) { |
| 30 | + if (objectMapper == null) { |
| 31 | + throw new IllegalArgumentException("ObjectMapper must not be null"); |
| 32 | + } |
| 33 | + this.objectMapper = objectMapper; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Returns the underlying Jackson {@link ObjectMapper} used for JSON serialization and |
| 38 | + * deserialization. |
| 39 | + * @return the ObjectMapper instance |
| 40 | + */ |
| 41 | + public ObjectMapper getObjectMapper() { |
| 42 | + return objectMapper; |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public <T> T readValue(String content, Class<T> type) throws IOException { |
| 47 | + return objectMapper.readValue(content, type); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public <T> T readValue(byte[] content, Class<T> type) throws IOException { |
| 52 | + return objectMapper.readValue(content, type); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public <T> T readValue(String content, TypeRef<T> type) throws IOException { |
| 57 | + JavaType javaType = objectMapper.getTypeFactory().constructType(type.getType()); |
| 58 | + return objectMapper.readValue(content, javaType); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public <T> T readValue(byte[] content, TypeRef<T> type) throws IOException { |
| 63 | + JavaType javaType = objectMapper.getTypeFactory().constructType(type.getType()); |
| 64 | + return objectMapper.readValue(content, javaType); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public <T> T convertValue(Object fromValue, Class<T> type) { |
| 69 | + return objectMapper.convertValue(fromValue, type); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public <T> T convertValue(Object fromValue, TypeRef<T> type) { |
| 74 | + JavaType javaType = objectMapper.getTypeFactory().constructType(type.getType()); |
| 75 | + return objectMapper.convertValue(fromValue, javaType); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public String writeValueAsString(Object value) throws IOException { |
| 80 | + return objectMapper.writeValueAsString(value); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public byte[] writeValueAsBytes(Object value) throws IOException { |
| 85 | + return objectMapper.writeValueAsBytes(value); |
| 86 | + } |
| 87 | + |
| 88 | +} |
0 commit comments