2

Consider the following JSON object:

{
  "selector": {
    "id": "77112778040"
  },
  "data": {
    "foo": "bar",
    "_dmType": "http://www.example.com/foo/bar#SomeObject"
  },
  "_dmType": "http://www.example.com/something#OtherObject"
}

Examples of Java objects (skipped boilerplate code):

package com.example.something;

public class OtherObject {

    private Selector selector;
    private Object data;

}

package com.example.something;

public class Selector {

    private String id;

}

package com.example.foo.bar;

public class SomeObject {

    private String foo;

}

Of course I know how to map those _dmType values to proper java classes (thore are just namespaces and object type names from classes generated from xds files).

I tried with custom StdTypeResolverBuilder and TypeIdResolverBase. It adds custom property to all objects wchich is redundant - I want to add cutom type property only for root type and types wchich are extension of other types.

And What about deserialization? It works only when all objects in tree have _dmType. My classes do not have @JsonTypeInfo annotation, I'm using JacksonAnnotationIntrospector.

Mateusz Stefaniak
  • 856
  • 1
  • 9
  • 20
  • 1
    You can try to write your custom bean serialisers and deserialisers to add and read custom field - `_dmType`. You can register and reuse default serialisers/deserialisers by implementing custom `BeanSerializerModifier` and `BeanDeserializerModifier`. See: [Jackson custom serialization and deserialization](https://stackoverflow.com/questions/55249054/jackson-custom-serialization-and-deserialization). Also, I am not sure I understand your model. Could you provide at least one example of `OtherObject`? What is the type of `data` property? – Michał Ziober Nov 14 '19 at 00:40
  • 1
    Examples of Java classes added. I was thinking about extending `StdTypeResolverBuilder` but I would rather use `_dmType` when present and let jackson do the job when not and this class doean not provide this much functionality. – Mateusz Stefaniak Nov 14 '19 at 23:21

0 Answers0