question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

@JsonUnwrapped does not work with a custom Serializer (StdSerializer<T>)

See original GitHub issue

I need to write a custom Serializer for one of my entities “Payment” and I have to implement it by extending StdSerializer:

class Payment {
}

class PaymentSerializer extends StdSerializer<Payment> {

    public PaymentSerializer() {
        this(null);
    }

    public PaymentSerializer(Class<Payment> t) {
        super(t);
    }

    @Override
    public void serialize(Payment value, JsonGenerator gen, SerializerProvider provider) throws IOException {
        // some logics
    }

}

Since I use Spring, I register this Serializer so Spring could identify it:

@Bean
public Jackson2ObjectMapperBuilder serializersObjectMapperBuilder() {
    SimpleModule module = new SimpleModule();
    module.addSerializer(Payment.class, applicationContext.getBean(PaymentSerializer.class));
    return new Jackson2ObjectMapperBuilder().modules(module);
}

Now I have a controller that returns data back to the client and it uses this Serializer without any problem:

@RestController
@RequestMapping("/payment")
class PaymentController {

    @GetMapping
    public List<Payment> getAll() {
        return Arrays.asList(new Payment());
    }

}

Since now, my Serializer works fine and everything is good.

The problem is with another entity “Order” which has Payment as a property with @JsonUnwrapped:

class Order {

    @JsonUnwrapped
    private Payment payment;

}

I need to unwrap the Payment inside the Order and I want to use the same PaymentSerializer but the problem is when I use this custom Serializer, the @JsonUnwrapped annotation will be ignored and the output will be something like this:

{
  "payment": {
    .....
  }
}

As I mentioned, I want to eliminate the “payment” field and unwrap it. I know that for emulating @JsonUnwrapped for a custom Serializer I need to extend UnwrappingBeanSerializer class, but as I mentioned at first, I need the standard Serializer too.

Changing my entity models is not an option for me.

Is there any way to accomplish this?

I use Spring Boot 2.1.3.RELEASE which I believe uses Jackson 2.9

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
renatoathaydescommented, Jan 13, 2021

In case someone needs help understanding how to implement a serializer supporting unwrapping, here’s what you need to do (code shown in Kotlin syntax):

internal object JSStringSerializer : StdSerializer<JSString>(JSString::class.java)
{

    override fun serialize(value: JSString, gen: JsonGenerator, provider: SerializerProvider?)
    {
        gen.writeStartObject()
        writeContents(gen, value)
        gen.writeEndObject()
    }

    override fun unwrappingSerializer(unwrapper: NameTransformer?): JsonSerializer<JSString>
    {
        return object : JsonSerializer<JSString>()
        {
            override fun serialize(value: JSString, gen: JsonGenerator, serializers: SerializerProvider?)
            {
                writeContents(gen, value)
            }

            override fun isUnwrappingSerializer() = true
        }
    }

    private fun writeContents(gen: JsonGenerator, value: JSString)
    {
        // write the object entries
       gen.writeFieldName("example")
       gen.writeString(value.value)
       // ...
    }

}

EDIT: at least this worked for my use case… not sure if I should’ve used the NameTransformer somehow but it did not seem to be required.

2reactions
cowtowncodercommented, Jan 8, 2021

Only standard POJOs handled with BeanSerializer / BeanDeserializer support use of @JsonUnwrapped without extra work. If you want it to work for custom types (ones with custom (de)serializers, you will need to do more work. Custom (de)serializers typically have to add explicit support for any and all annotations.

For serializers, you need to implement JsonSerializer.unwrappingSerializer() and for deserializers JsonDeserializer.unwrappingDeserializer(): these are factory methods that create instances that will handle all the complexities of dealing with changing structure. I would not recommend extending UnwrappingBeanSerializer; it is an internal class and unlikely to work for your case.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using @JsonUnwrapped and having a custom Serializer ...
I propose the following way to solve issue: We can compose two StdSerializer in single custom serializer. @Component class PaymentSerializer ...
Read more >
Using @JsonUnwrapped and having a custom Serializer ...
I propose the following way to solve issue: We can compose two StdSerializer in single custom serializer. @Component class PaymentSerializer extends ...
Read more >
Why does @JsonUnwrap does not work with custom ...
The root cause was that using a custom serializer (or apparently, a BeanSerializerModifier) causes Jackson to ignore the @JsonUnwrap annotation.
Read more >
Unwrapping custom Jackson Serializers
A custom serializer can be used in all cases where annotations on fields and classes are not enough to customize the JSON output....
Read more >
Jackson Annotations - @JsonUnwrapped - Tutorialspoint
Jackson Annotations - @JsonUnwrapped, @JsonUnwrapped is used to unwrap values of objects during serialization or de-serialization.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found