4

I have an interface -

@JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include=JsonTypeInfo.As.PROPERTY, property="type")
interface Base { ... }

I have two derived classes - ClassA and ClassB. I am attempting to serialize and deserialize with Jackson ION to base type as follows -

class TestSerDeSer {
    private static ObjectMapper MAPPER = new IonObjectMapper();

    static {
        MAPPER.registerSubtypes(new NamedType(A.class, "A"));
        MAPPER.registerSubtypes(new NamedType(B.class, "B"));
      }

    public byte[] serialize(Base baseType) {
        try {
            return MAPPER.writeValueAsBytes(baseType);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    public Base deserialize(byte[] bytes) {
        Base base;
        try {
            base = MAPPER.readValue(bytes, Base.class);
            return base;
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

}

I am creating an Object of Class A and serializing and desrializing using above functions as

Base baseObj = new ClassA(...);
//serialization works fine
byte[] serializedBytes = serialize(baseObj);

//this line throws exception
Base deserializedBase = deserialize(serializedBytes);

The exception is -

Caused by: com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Missing type id when trying to resolve subtype of [simple type, class mypackage.path.Base]: missing type id property 'type'

I am registering subtypes in ObjectMapper. I also have the annotation for type in base interface. What is it that I am missing here?

krackoder
  • 2,841
  • 7
  • 42
  • 51

1 Answers1

2

Your example work with standard JSON with ObjectMapper however it fails when serialization format is switched to Ion with IonObjectMapper. Tested your example with com.fasterxml.jackson.dataformat:jackson-dataformat-ion:2.9.7, fails with the same exception.

There is an open issue [avro] Support @JsonSubTypes in schema generation and serialization #11 which implies that not all binary dataformats support subtypes. Open pull request [Ion] Better support for Ion type annotations. #109 implies that @JsonTypeInfo doesn't work when using Ion yet.

Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111
  • Thanks @KarolDowbecki Is there any workaround on that for now? – krackoder Oct 05 '18 at 20:32
  • @nishant not that I'm aware of. I'd ask in the pull request and reference this question. I think you found a bug in the library. – Karol Dowbecki Oct 05 '18 at 20:34
  • 1
    The open issue seems to be resolved. as mentioned by @GaryPhung. : **It seems the open issue he mentioned has now been resolved. The fix should be available as part of 2.12 of jackson-dataformats-binary** – Rishabh Kumar Mar 03 '21 at 16:46
  • @RishabhKumar Where do you see this is resolved in 2.12 of jackson-dataformats-binary? I still see the [Issue #11](https://github.com/FasterXML/jackson-dataformats-binary/issues/11) being open. – Saurabh Rane Aug 24 '21 at 19:10