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.

Implement POJO serializer "decorator" abstraction to allow for augmenting POJO Serializations

See original GitHub issue

(note: also consider possibly allowing decorators for Collection/array types, scalars)


One recurring feature request is that of ability to add more “virtual” properties to serialize. Although it is relatively easy to modify behavior of existing properties (or anything that can be accessed via existing fields and/or methods), it is surprisingly difficult to add anything that “isn’t there”.

But it would be quite easy to allow addition of handlers called before and after serializing properties of POJOs (pre/post hooks); right after START_OBJECT and before END_OBJECT.

I think what is needed is:

  • New handler type that contains methods to call
  • Addition of a new property in @JsonSerialize to define handler to use

Another possible improvement (which could be deferred for future) would be to allow registering such handler externally. One challenge there is the question of how to match handler to type(s); as well as if and how to chain these. Chaining would make sense to prevent modules from accidentally overriding/removing handlers; but might cause issues of its own.

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:22 (11 by maintainers)

github_iconTop GitHub Comments

8reactions
sheavnercommented, May 22, 2015

Adding a comment for people who end up here via http://jira.codehaus.org/browse/JACKSON-645 or http://jira.codehaus.org/browse/JACKSON-538 and are looking for a method which is called after a deserializer completes. I was able to achieve the desired effect by including an annotation and writing a converter which uses the same class as input and output.

@JsonDeserialize(converter=MyClassSanitizer.class)  // invoked after class is fully deserialized
public class MyClass {
    public String field1;
}

import com.fasterxml.jackson.databind.util.StdConverter;
public class MyClassSanitizer extends StdConverter<MyClass,MyClass> {
  @Override
  public MyClass convert(MyClass var1) {
    var.field1 = munge(var.field1);
    return var1;
  }
}
1reaction
lemmingapexcommented, Oct 4, 2016

For those looking for a way to add functionality before and after deserialization, this worked perfectly for my needs:

http://stackoverflow.com/questions/18313323/how-do-i-call-the-default-deserializer-from-a-custom-deserializer-in-jackson/18405958#18405958

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why should we make our POJO's Serializable in Java?
I've seen a lot of questions on how to Serialize POJO's in java using different frameworks, but I wanted to really understand the...
Read more >
The Pennsylvania State University
serialization techniques are improved, allowing more access to information ... plain old Java objects (POJO) and the flexible binary methodology developed.
Read more >
Overview (Apache Juneau 8.2.0)
Built on top of Servlet and Apache HttpClient APIs that allow you to use the newest ... Use one of the default serializers...
Read more >
Advanced Concepts of Java Object Serialization
An Overview. Serialization makes any POJO persistable by converting it into a byte stream. The byte stream then can be stored in a...
Read more >
Jackson - Quick Guide - Tutorialspoint
Jackson is a simple java based library to serialize java objects to JSON and vice versa. Features. Easy to use. - jackson API...
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