Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Jackson Serializer for Wrapper Object

Tags:

java

json

jackson

I have the provider interface

interface IProvider<T> {
    T locate();
}

and a class containing a field of type IProvider (can be another type for other fields).

class MyObject {
    MyLocator<String> field;
}

I need to serialize instances of MyObject to JSON using Jackson 1.7. The output must be the same as if MyObject.field had been a String (i.e. no reference to ILocator).

I can't figure out how to build the custom serializer required to achieve this. Here is the structure I am trying to use for this task:

class MyLocatorSerializer extends SerializerBase<MyLocator<?>> {
    public MyLocatorSerializer() {
        super(MyLocator.class, false);
    }

    @Override
    public void serialize(MyLocator<?> a_value, JsonGenerator a_jgen,
            SerializerProvider a_provider) throws IOException, JsonGenerationException {
        // Insert code here to serialize a_value.locate(), whatever its type
    }

    @Override
    public JsonNode getSchema(SerializerProvider a_provider, Type a_typeHint)
            throws JsonMappingException {
        // What should I return here? I can't find documentation regarding the different schema types...
    }
}

The custom serializer would be registered using

SimpleModule module = new SimpleModule("MyModule", new Version(1, 0, 0, null));
module.addSerializer(new MyLocatorSerializer());
objectMapper.registerModule(module);
like image 595
bernie Avatar asked Sep 02 '26 20:09

bernie


2 Answers

Apologies if I misunderstand the question, but would this be as simple as just using @JsonValue on 'Locate' method, instead of writing a custom serializer? What @JsonValue does is take value of a property as is, and use it instead of creating a JSON Object: often this is used for serializing a POJO as a simple String or number, like so:

public class StringWrapper {
   @JsonValue public String value;
}

so that for class like:

public class POJO {
   public StringWrapper wrapped;
}

we would get serialization like:

{
  "wrapper" : "string value of 'value'"
}

instead of what would otherwise be seen:

{
   "wrapper" : {
      "value" : "... string value ... "
   }
}

Annotation can be used for any types of values obviously.

like image 109
StaxMan Avatar answered Sep 05 '26 11:09

StaxMan


Another answer using mix-in annotations following the comment from Staxman.

static class JacksonCustomModule extends SimpleModule {
    public JacksonCustomModule() {
        super("JacksonCustomModule", new Version(1, 0, 0, null));
    }

    @Override
    public void setupModule(SetupContext context) {
        context.setMixInAnnotations(IProvider.class, IProviderMixIn.class);
        super.setupModule(context);
    }

    interface IProviderMixIn<T> {
        @JsonValue
        T locate();
    }
}

Activate the module with:

objectMapper.registerModule(new JacksonCustomModule());
like image 43
bernie Avatar answered Sep 05 '26 09:09

bernie