FasterXML / FasterXML/jackson-docs

Possible useful name pattern for PTH documentation

Abierto
#10 1 comentario 0 reacciones 0 asignados Ver en GitHub
Lenguaje dominante
Sin datos de lenguaje
Estrellas
760
Forks
113
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

This is not a solution for every use case, in large part because the Reflections scan can take some time, but it's a pattern that may be generally applicable enough to describe in the documentation:

- Define an annotation for the subtypes, and meta-annotate with `@JsonTypeName`:
```
@JsonTypeName
@Target({ElementType.ANNOTATION_TYPE, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface FoocorpJsonType {
}
```

- Use `@FoocorpJsonType` on all subtypes to be registered with the mapper:
```
@FoocorpJsonType
public class ConcreteMessage extends AbstractMessage {}
```

- In a suitable location (such as a `@PostConstruct` method on a Spring configuration class, or wherever the `ObjectMapper` is built), use the `org.reflections:reflections` package and this magic spell:
```
Reflections reflections = new Reflections(
ConfigurationBuilder.build("com.foocorp.base.package")
);

mapper.registerSubtypes(reflections.getTypesAnnotatedWith(FoocorpJsonType.class).toArray(new Class[0]));
```

This will scan the classpath (limited for performance) for types annotated with the custom annotation and register them all with the mapper so they can be deserialized when seen.

Guía de contribución

No hay ninguna guía de contribución indexada para este repositorio

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.