FasterXML / FasterXML/jackson-modules-base

Guice7 Module usage with XmlMapper and JsonMapper concurrently

Đang mở
#265 4 bình luận 0 reaction 0 người được giao Xem trên GitHub
2.19 pr-welcome
Ngôn ngữ chính
Java
Star
180
Fork
80
Merge trung bình
3 giờ 26 phút
Pull request đã merge (30 ngày)
1

Mô tả

I've an application where I need to use the XmlMapper and JsonMapper in the same global guice injector. Therefore I need a way to distinguish between those two mappers. I know of two options here: (1) I could make use of `@Named("xml")` and ObjectMapper superclass to distinguish between them. Yet, (2) I like to use the type safe variant by defining the relevant subclasses (JsonMapper, XmlMapper) with Guice. I decided to go with the second route as this allows me to make use of type checking and has a generally easier Guice interfacing.

Unfortunately, in combination with Guice7 Module this requires to do the following messy initialization:

```Java
final var jsonKey = (Key) Key.get(JsonMapper.class).ofType((Type) JsonMapper.class);
final var jsonMapper = JsonMapper.builder()
.addModule(new JavaTimeModule())
.build();
final var xmlKey = (Key) Key.get(XmlMapper.class).ofType((Type) XmlMapper.class);
final var xmlMapper = XmlMapper.builder()
.addModule(new JavaTimeModule())
.build();
Guice.createInjector(
new ObjectMapperModule(jsonKey).withObjectMapper(jsonMapper),
new ObjectMapperModule(xmlKey)..withObjectMapper(xmlMapper));
```

Since the `new ObjectMapperModule(jsonKey)` constructor only allows keys of type `Key` without subclass types it forces me to do ugly type and casting Magic. Therefore I propose to add a constructor to `ObjectMapperModule` with a definition like `Key`:

```Java
public ObjectMapperModule(Key objectMapperKey)
{
...
}
```

Which would lead to the following API:

```Java
var injector = Guice.createInjector(new ObjectMapperModule(Key.get(XmlMapper.class)).withObjectMapper(xmlMapper));
XmlMapper mapper = injector.getInstance(XmlMapper.class);
```

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.