Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass DeserializationContext to JsonDeserializer.getObjectIdReader #1825

Closed
reda-alaoui opened this issue Nov 14, 2017 · 4 comments
Closed

Pass DeserializationContext to JsonDeserializer.getObjectIdReader #1825

reda-alaoui opened this issue Nov 14, 2017 · 4 comments
Milestone

Comments

@reda-alaoui
Copy link
Contributor

I want to create a custom deserializer which have the sole purpose of providing a default value via Pageable getNullValue(DeserializationContext ctxt).

PageRequest class implements interface Pageable.

The deserializer:

class PageableDeserializer extends JsonDeserializer<Pageable> {
  @Override
  public Pageable deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
    return p.getCodec().readValue(p, PageRequest.class);
  }

  @Override
  public Pageable getNullValue(DeserializationContext ctxt) throws JsonMappingException {
    return Pageable.unpaged();
  }

 @Override
  public ObjectIdReader getObjectIdReader() {
    return super.getObjectIdReader();
  }
}

The deserializer must be able to keep working with @JsonCreator.
Now correct me if I am wrong but I have no way to retrieve the default ObjectIdReader without having the DeserializationContext in getObjectIdReader?

@cowtowncoder
Copy link
Member

Correct, you probably do need DeserializationContext for that.

But I am not 100% sure I follow from custom deserializer to need for access to object id reader?
I don't remember how one is constructed and passed to BeanDeserializer, but similar approach should probably be needed with custom handlers.

@reda-alaoui
Copy link
Contributor Author

reda-alaoui commented Nov 14, 2017

Maybe my conclusion wasn't good.

I was using a mixin for Pageable.

@JsonDeserialize(as = PageRequest.class)
public class PageableMixIn {

}

And another for PageRequest

public class PageRequestMixIn {
  public PageRequestMixIn() {}

  @JsonCreator
  public PageRequestMixIn(
      @JsonProperty("pageNumber") int page,
      @JsonProperty("pageSize") int size,
      @JsonProperty("sort") Sort sort) {}

}

I have this mixin also for PageImpl:

public class PageImplMixIn {

  @JsonUnwrapped @JsonProperty private Pageable pageable;
}

My issue is that when I replace the Pageable mixin with the custom deserializer shown in the first comment, jackson does not deserialize pageable property in PageImpl anymore.

I thought that the issue would come from a missing ObjectIdReader in the deserializer.

@reda-alaoui
Copy link
Contributor Author

reda-alaoui commented Nov 17, 2017

So ObjectIdReader had nothing to do with it.
In fact, I needed to create a delegating deserializer and delegate unwrappingDeserializer method.
Nevertheless, I kept the PR open for master branch.

@cowtowncoder
Copy link
Member

Ah ok. Yes, contextualization is... tricky. I wish I could simplify things, but there is a reason why this delegation model is used and I haven't been able to figure out simpler means to separate configuration by class configuration (from class annotations, by-type overrides) and by property overrides.

But I am glad you were able to handle things without change for your current needs.

@cowtowncoder cowtowncoder added this to the 3.0.0 milestone Mar 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants