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