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?
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With