Home > database >  Unable to remove tag when serializing object to yaml using jackson-dataformat-yaml
Unable to remove tag when serializing object to yaml using jackson-dataformat-yaml

Time:12-10

I use jackson (jackson-databinder & jackson-dataformat-yaml) to serialize polymorphism classes to both json and yaml. And I use one class property as type resolver. I can remove the class metadata info in json, but in yaml it still contain the class meta info in tag. How can I remove that. Here's my sample code:

  @JsonTypeInfo(use = JsonTypeInfo.Id.NAME,
      include = JsonTypeInfo.As.EXISTING_PROPERTY,
      property = "type")
  @JsonSubTypes({
      @Type(value = Car.class, name = "car"),
      @Type(value = Truck.class, name = "truck") })
  public interface Vehicle {


    String getName();
  }

  @Value
  public static class Car implements Vehicle {
    String name;
    String type = "car";

    @JsonCreator
    public Car(@JsonProperty("name") final String name) {
      this.name = requireNonNull(name);
    }
  }

  @Value
  public static class Truck implements Vehicle {
    String name;
    String type = "truck";

    @JsonCreator
    public Truck(@JsonProperty("name") final String name) {
      this.name = requireNonNull(name);
    }
  }

  @Value
  public static class Vehicles {
    List<Vehicle> vehicles;

    @JsonCreator
    public Vehicles(@JsonProperty("vehicles") final List<Vehicle> vehicles) {
      super();
      this.vehicles = requireNonNull(vehicles);
    }
  }


  public static void main(String[] args) throws JsonProcessingException {
    ObjectMapper MAPPER = new ObjectMapper();
    ObjectMapper YAML_MAPPER = YAMLMapper.builder()
        .disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER)
        .build();

    final Vehicles vehicles = new Vehicles(ImmutableList.of(new Car("Dodge"), new Truck("Scania")));
    final String json = MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(vehicles);
    System.out.println(json);

    final String yaml = YAML_MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(vehicles);
    System.out.println(yaml);

  }

And here's json and yaml output:

{
  "vehicles" : [ {
    "name" : "Dodge",
    "type" : "car"
  }, {
    "name" : "Scania",
    "type" : "truck"
  } ]
}
vehicles:
- !<car>
  name: "Dodge"
  type: "car"
- !<truck>
  name: "Scania"
  type: "truck"

There's no class meta info in json output. But in yaml, there're still the tag which contains the class meta info. Is it possible to remove that in yaml as json? Thanks

CodePudding user response:

You can disable the YAMLGenerator.Feature#USE_NATIVE_OBJECT_ID yaml feature that is enabled by default for indicate types in the serialization. So in the construction of your ObjectMapper YAML_MAPPER mapper you can disable this feature like below obtaining the expected result:

ObjectMapper YAML_MAPPER = YAMLMapper.builder()
                .disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER) 
                .disable(YAMLGenerator.Feature.USE_NATIVE_TYPE_ID)
                .build();
  • Related