Java 正在尝试将自定义通用gson反序列化程序迁移到jackson

Java 正在尝试将自定义通用gson反序列化程序迁移到jackson,java,json,jackson,retrofit,retrofit2,Java,Json,Jackson,Retrofit,Retrofit2,目前正在与GSON反序列化,并使用改装GsonConverterFactory进行改装: GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapter(new TypeToken<Map<Book, Collection<Author>>>(){}.getType(), new BooksDeserializer(context)); Gson gson = gsonB

目前正在与GSON反序列化,并使用改装GsonConverterFactory进行改装:

GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(new TypeToken<Map<Book, Collection<Author>>>(){}.getType(), new BooksDeserializer(context));
Gson gson = gsonBuilder.create();

Retrofit retrofit = new Retrofit.Builder()
    .baseUrl(url)
    .addConverterFactory(GsonConverterFactory.create(gson))
    .build();

BookService service = retrofit.create(BookService.class);
Response<Map<Book, Collection<Author>>> response = service.getBooks().execute();    
GsonBuilder GsonBuilder=new GsonBuilder();
registerTypeAdapter(新的TypeToken(){}.getType(),新的BooksDeserializer(上下文));
Gson-Gson=gsonBuilder.create();
改装改装=新改装.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
BookService service=reformation.create(BookService.class);
Response=service.getBooks().execute();
我想使用改造提供的JacksonConverterFactory?我需要提供一个Jackson映射器。有没有一种方法可以像我使用GSON那样向映射器提供类型信息

SimpleModule simpleModule = new SimpleModule();
// TODO provide mapper with info needed to deserialize 
// Map<Book, Collection<Author>>
mapper.registerModule(simpleModule);

Retrofit retrofit = new Retrofit.Builder()
    .baseUrl(url)
    .addConverterFactory(JacksonConverterFactory.create(mapper))
    .build();

BookService service = retrofit.create(BookService.class);
Response<Map<Book, Collection<Author>>> response = service.getBooks().execute();
SimpleModule SimpleModule=new SimpleModule();
//TODO为映射程序提供反序列化所需的信息
//地图
映射器注册表模块(simpleModule);
改装改装=新改装.Builder()
.baseUrl(url)
.addConverterFactory(JacksonConverterFactory.create(映射器))
.build();
BookService service=reformation.create(BookService.class);
Response=service.getBooks().execute();
具体来看TODO,我可以告诉映射程序使用此反序列化程序吗

public class BooksDeserializer extends JsonDeserializer<Map<Book, Collection<Author>>> {

    @Override
    public Map<Book, Collection<Author>> deserialize(JsonParser parser, DeserializationContext context) throws IOException, JsonProcessingException {
        // deserialize here
    }
}
公共类反序列化程序扩展JsonDeserializer{
@凌驾
公共映射反序列化(JsonParser解析器,反序列化上下文)引发IOException,JsonProcessingException{
//在这里反序列化
}
}

根据,即反序列化程序需要能够反序列化作为所提供类的子类的对象
TypeReference我认为这里存在误解:您没有实现
JsonDeserializer
,仅仅因为您想绑定到某个类型。这就是杰克逊自己所做的。 如果出于某种原因需要自定义反序列化程序(或序列化程序),它将只处理嵌套类型的特定部分;用于
Book
Author
Map
的反序列化程序,但对于嵌套类型
Map
反序列化程序委托给键和值处理程序。它不注册为组合


无论如何:我相当肯定自定义反序列化器不是这里的答案,而是一种包装
ObjectMapper
基本用法的方法,可以将JSON作为给定类型读取。但在不了解更多改装信息的情况下,我不知道具体是什么。

更新了问题,尝试更具体地说明我要做什么。具体地看TODO,我可以告诉映射者使用此反序列化器吗?不,我很抱歉:正如我在最初的回答中所说的,在运行时没有足够的信息来将反序列化程序映射到像
MapIf这样的东西。这确实是真的。不是没有足够的信息,而是Jackson不支持它。格森处理得很好。我想我会坚持使用GSON。这是我面临的问题,我通过创建另一个类以同样的方式解决了它。出于好奇,我研究了它的工作原理,正如预期的那样,它会进行大量的反射巫毒,以便在运行时解析类型;我希望谷歌在代码的其他地方自由地使用了这种黑魔法,这也证实了这一点。换言之,如果你想要花式的分辨率,你必须为此付费。
@XmlRootElement
public class SerializableBookMapWrapper {

    private Map<Book, Collection<Author>> wrapped;

    public SerializableBookMapWrapper(final Map<Book, Collection<Author>> wrapped) {
        this.wrapped = wrapped;
    }

    public Map<Book, Collection<Author>> getWrapped() {
        return wrapped;
    }

    public void setWrapped(final Map<Book, Collection<Author>> wrapped) {
        this.wrapped = wrapped;
    }
}