FasterXML / FasterXML/jackson-module-scala

Nested sequence of polymorphic type serialized without type info (ver 2.10.2)

Abierto
#442 4 comentarios 0 reacciones 0 asignados Ver en GitHub
Lenguaje dominante
Scala
Estrellas
506
Forks
138
Merge medio
3 h 6 min
PR fusionados (30 d)
48

Descripción

The following code works as expected
```scala
import com.fasterxml.jackson.annotation.{JsonSubTypes, JsonTypeInfo}
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.scala.DefaultScalaModule

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes(Array(
new JsonSubTypes.Type(value = classOf[Ingredient.Meat], name = "meat"),
new JsonSubTypes.Type(value = classOf[Ingredient.Vegetable], name = "veggie")
))
trait Ingredient
object Ingredient {
case class Meat(of: String) extends Ingredient
case class Vegetable(name: String) extends Ingredient
}

case class Burrito(filling: Seq[Ingredient])

case class BurritoTray(content: Seq[Burrito])

object Main extends App {
val mapper = new ObjectMapper().registerModule(DefaultScalaModule)
println(mapper.writeValueAsString(BurritoTray(Seq(
Burrito(Seq(Ingredient.Meat("chicken"), Ingredient.Vegetable("salad"))),
Burrito(Seq(Ingredient.Vegetable("salad"), Ingredient.Vegetable("pickles")))
))))
}
```
printing the following output
```json
{"content":[{"filling":[{"type":"meat","of":"chicken"},{"type":"veggie","name":"salad"}]},{"filling":[{"type":"veggie","name":"salad"},{"type":"veggie","name":"pickles"}]}]}
```
But if the `Burrito` class is replaced by a simple `Seq[Ingredient]`
```scala
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes(Array(
new JsonSubTypes.Type(value = classOf[Ingredient.Meat], name = "meat"),
new JsonSubTypes.Type(value = classOf[Ingredient.Vegetable], name = "veggie")
))
trait Ingredient
object Ingredient {
case class Meat(of: String) extends Ingredient
case class Vegetable(name: String) extends Ingredient
}

case class BurritoTray(content: Seq[Seq[Ingredient]])

object Main extends App {
val mapper = new ObjectMapper().registerModule(DefaultScalaModule)
println(mapper.writeValueAsString(BurritoTray(Seq(
Seq(Ingredient.Meat("chicken"), Ingredient.Vegetable("salad")),
Seq(Ingredient.Vegetable("salad"), Ingredient.Vegetable("pickles"))
))))
}
```
the type information is omitted in the resulting json
```json
{"content":[[{"of":"chicken"},{"name":"salad"}],[{"name":"salad"},{"name":"pickles"}]]}
```
This issue is not present in equivalent java code
```java
public class JacksonNestedCollection {
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = Meat.class, name = "Meat"),
@JsonSubTypes.Type(value = Veggie.class, name = "Veggie")
})
private interface Ingredient {}
private static class Meat implements Ingredient {
public final String of;
public Meat(String of) {
this.of = of;
}
}
private static class Veggie implements Ingredient {
public final String name;
public Veggie(String name) {
this.name = name;
}
}

private static final class BurritoTray {
public final List> content;
public BurritoTray(List> content) {
this.content = content;
}
}

public static void main(String[] args) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
System.out.println(mapper.writeValueAsString(new BurritoTray(Arrays.asList(
Arrays.asList(new Meat("chicken"), new Veggie("salad")),
Arrays.asList(new Veggie("salad"), new Veggie("pickles"))
))));
}
}
```

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.