Skip to content

Commit

Permalink
Allow access to serde registry from ObjectMapper (#948)
Browse files Browse the repository at this point in the history
currently if you create an ObjectMapper there is no way to access the associated SerdeRegistry. Some implementations (like in Micronaut Data) that create the default mapper need access to this. This simple PR adds a getter to retrieve it.
  • Loading branch information
graemerocher authored Oct 14, 2024
1 parent 75ce827 commit 7746c94
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 0 deletions.
9 changes: 9 additions & 0 deletions serde-api/src/main/java/io/micronaut/serde/ObjectMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ default ObjectMapper cloneWithConfiguration(
return this;
}

/**
* Returns the {@link SerdeRegistry} used by this object mapper, if possible.
*
* @return The serde registry
*/
default @NonNull SerdeRegistry getSerdeRegistry() {
throw new UnsupportedOperationException("No accessible SerdeRegistry");
}

/**
* Get the default ObjectMapper instance.
*
Expand Down
5 changes: 5 additions & 0 deletions serde-api/src/main/java/io/micronaut/serde/ObjectMappers.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ protected Set<String> getIncludedPackages() {
ObjectMapper objectMapper = context.getBean(ObjectMapper.class);
return new ObjectMapper.CloseableObjectMapper() {

@Override
public SerdeRegistry getSerdeRegistry() {
return objectMapper.getSerdeRegistry();
}

@Override
public <T> T readValueFromTree(JsonNode tree, Argument<T> type) throws IOException {
return objectMapper.readValueFromTree(tree, type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ protected AbstractBsonMapper(SerdeRegistry registry, SerdeConfiguration serdeCon
this.decoderContext = registry.newDecoderContext(view);
}

@Override
public SerdeRegistry getSerdeRegistry() {
return this.registry;
}

protected abstract BsonReader createBsonReader(ByteBuffer byteBuffer);

protected abstract AbstractBsonWriter createBsonWriter(OutputStream bsonOutput) throws IOException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ private JacksonJsonMapper(@NonNull SerdeRegistry registry,
this.specificSerializer = serializer;
}

@Override
public SerdeRegistry getSerdeRegistry() {
return this.registry;
}

@NonNull
@Override
public JsonMapper createSpecific(@NonNull Argument<?> type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ private JsonStreamMapper(@NonNull SerdeRegistry registry, @Nullable SerdeConfigu
this.view = view;
}

@Override
public SerdeRegistry getSerdeRegistry() {
return this.registry;
}

@Override
public ObjectMapper cloneWithConfiguration(@Nullable SerdeConfiguration configuration, @Nullable SerializationConfiguration serializationConfiguration, @Nullable DeserializationConfiguration deserializationConfiguration) {
return new JsonStreamMapper(registry.cloneWithConfiguration(configuration, serializationConfiguration, deserializationConfiguration), configuration == null ? serdeConfiguration : configuration, view);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ protected AbstractOracleJdbcJsonObjectMapper(SerdeRegistry registry, SerdeConfig
this.view = view;
}

@Override
public SerdeRegistry getSerdeRegistry() {
return this.registry;
}

abstract OracleJsonParser getJsonParser(InputStream inputStream);

abstract OracleJsonGenerator createJsonGenerator(OutputStream outputStream);
Expand Down

0 comments on commit 7746c94

Please sign in to comment.