8

I am getting the following output when serializing an object to yml via Jackson:

---
commands:
  dev: !<foo.bar.baz.DevCommand>

However, what I want is:

---
commands:
  dev: 
    type: foo.bar.baz.DevCommand

I am able to deserialize that fine. That is to say, the deserialization part works as intended. I have put the following annotation everywhere I can think of:

@JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, include=JsonTypeInfo.As.PROPERTY, property="type")

Including on the DevCommand interface, on DevCommand the concrete class, on the type which has the commands map (both the field and the getters/setters).

What do I need to do to force Jackson to use the type format I want?

mtyson
  • 8,196
  • 16
  • 66
  • 106
  • From doc of JsonTypeInfo: *Annotation used for configuring details of if and how type information is used with **JSON** serialization and deserialization* – since you serialize to YAML, it is simply not applicable and Jackson uses YAML's tag system instead. – flyx Oct 27 '16 at 11:56
  • 1
    @flyx But it uses the JsonTypeInfo annotation to configure the deserialization of the yaml.... – mtyson Oct 27 '16 at 14:48

1 Answers1

12

Yaml has type information built in already, so Jackson uses that by default. From this issue, the fix is to disable using the native type id.

YAML has native Type Ids and Object Ids, so by default those are used (assuming this is what users prefer). But you can disable this with:

YAMLGenerator.Feature.USE_NATIVE_TYPE_ID

and specifically disabling that; something like:

YAMLFactory f = new YAMLFactory();
f.disable(YAMLGenerator.Feature.USE_NATIVE_TYPE_ID);
ObjectMapper m = new ObjectMapper(f);

or, for convenience

YAMLMapper m = new YAMLMapper()
 disable(YAMLGenerator.Feature.USE_NATIVE_TYPE_ID);
Michael Deardeuff
  • 10,386
  • 5
  • 51
  • 74