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

Support AnnotatedType/TYPE_USE annotations #5

Open
kaqqao opened this issue Jan 8, 2018 · 6 comments
Open

Support AnnotatedType/TYPE_USE annotations #5

kaqqao opened this issue Jan 8, 2018 · 6 comments

Comments

@kaqqao
Copy link

kaqqao commented Jan 8, 2018

Not sure how and if this project is active, but here's a suggestion.
Since this library is Java 8+ only, it would make a lot of sense that it supports AnnotatedType when deserializing, especially if the support for user-defined (de)serializers is added.
This would allow fine grained logic in the custom (de)serializers.
E.g. A custom (de)serializer could be written to process types such as List<@Base64 Book> by Base64-de/encoding the JSON value before the usual (de)serialization.

I've been looking for a Java JSON library with such capabilities for use in my project, and exactly 0 exist. I maintain a library for advanced reflection that works with AnnotatedType but don't really have the capacity to develop a JsSON library as well, so I'm opening this suggestion on projects that are Java 8+ only or in early development (as this one seems to be).

@brettwooldridge
Copy link
Owner

@kaqqao HikariJSON is not dead, but in deep hibernation. 😁 Having said that, I believe that jackson can do everything you are asking for above with respect to custom (de)serializers. Have you investigated it?

@kaqqao
Copy link
Author

kaqqao commented Jan 9, 2018

Jackson unfortunately can't handle this case, because it targets Java <8. Still, the author is open to this in the upcoming Jackson 3, so I'll attempt to contribute this extension.
I was just hoping to get more library authors interested :) Especially for the libraries that are already focused on Java 8.

@brettwooldridge
Copy link
Owner

@kaqqao I use jackson, and to me it still seems possible to create a custom deserializer that itself is Java 8 aware.

Pardon the Kotlin, but this is from some of my company's code:

var json: String = ""
      @JsonRawValue
      get() = field
      @JsonDeserialize(using = MyCustomDeserializer::class)
      set(value) {field = value}

... this could easily be annotated with a Java 8 type annotation such as:

@Base64
var json: String = ""
      @JsonRawValue
      get() = field
      @JsonDeserialize(using = Jsr308Deserializer::class)
      set(value) {field = value}

... where Jsr308Deserializer performs AnnotatedType reflection on the target, sees @Base64, and dispatches accordingly.

Have you tried it?

@kaqqao
Copy link
Author

kaqqao commented Jan 9, 2018

It is possible to recover annotations on fields/methods, yes, but TYPE_USE annotations are not preserved in Jackson's JavaType (class it uses to capture generic types etc). As a result, contextual types (as Jackson calls top-level types and generic type arguments) are not possible to (de)serialize in this way. E.g. you can not register a deserializer that will globally catch @Base64 annotated types. For a field of type List<@Base64 Thing> you could register a custom deserializer instance that knows how to deal with that specific field, and it would have to deal with the entire type (i.e. List<@Base64 Thing>), but you couldn't register it globally so that Jackson processes the List part normally and triggers the custom logic for @Base64 Thing only, for any field anywhere automatically. Additionally, you can't do mapper.readValue(json, mapper.getTypeFactory().construct(annotatedType), so top-level annotations are always lost. It's a bit hard to explain if you're not looking into Jackson's code at the same time. JsonDeserializer only has access to JavaType which does not contain annotations. This would need to be in the core classes, that's why a Java <8 compatible library can't support it.

I've already talked to the Jackson's author, he confirmed my understanding...

@kaqqao
Copy link
Author

kaqqao commented Jan 9, 2018

Btw, feel free to close the issue if you don't really want it open for a hibernating project or don't want the feature in general.

And thanks for the responses!

@brettwooldridge
Copy link
Owner

@kaqqao Thanks for the clarification. I get it. I'm familar with some of the jackson internals. I'll leave this open, as it sounds useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants