FasterXML / FasterXML/jackson-module-scala

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

Offen
#442 4 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
Scala
Sterne
506
Forks
138
Ø Merge
3 Std. 6 Min.
Gemergte PRs (30 T.)
48

Beschreibung

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"))
))));
}
}
```

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.