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

JOHNZON-282: add support for @JsonbTypeAdapter at class level #53

Merged
merged 3 commits into from
Sep 28, 2019
Merged

JOHNZON-282: add support for @JsonbTypeAdapter at class level #53

merged 3 commits into from
Sep 28, 2019

Conversation

ArneLimburg
Copy link
Contributor

No description provided.

Copy link
Contributor

@rmannibucau rmannibucau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Arne

Thanks a lot for the PR!

I would move the reflection in its owned location (DecoratedType) and harness a bit more with one more test, otherwise it looks good to me.

Btw, did you run jsonb tck? I can tomorrow if you need help on it to include that in the coming 1.2.1.



import static org.hamcrest.CoreMatchers.*;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No wildcard please ;)

} else if (Class.class.isInstance(type)) {
return Class.class.cast(type);
} else /*if (ParameterizedType.class.isInstance(type))*/ {
return getRawType(ParameterizedType.class.cast(type).getRawType());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cant it loop?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking about it, it neither needs recursion nor a loop since getRawType() always returns a class object

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't think so, Collection<Collection<Collection<....>>> ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would return Collection.class

final JsonbTypeDeserializer deserializer = annotationHolder.getAnnotation(JsonbTypeDeserializer.class);
final JsonbTypeAdapter adapter = annotationHolder.getAnnotation(JsonbTypeAdapter.class);
final JsonbTypeAdapter typeAdapter = hasRawType ? getRawType(annotationHolder.getType()).getDeclaredAnnotation(JsonbTypeAdapter.class) : null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldnt it be done in the decorated type? There shouldnt be much type reflection there. Wdyt?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean the hasRawType and getRawType stuff? I take a look into that

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, today it is not super clean for the class (it is for fields and methods) but we support meta annotations and things like that, you can have a look to findFactory in the access mode and org.apache.johnzon.jsonb.JsonbAccessMode#getAnnotation. Guess you "just" want to reuse org.apache.johnzon.jsonb.JsonbAccessMode.ClassDecoratedType somehow here.


final Foo read = jsonb.fromJson(toString, Foo.class);
assertEquals(foo.bar.value, read.bar.value);
assertEquals(foo.dummy.value, read.dummy.value);
assertEquals(foo.baz.value, read.baz.value);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also add a test where baz is the root object too, we had issues with that on other metadata by the past.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll do

Copy link
Contributor

@rmannibucau rmannibucau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Arne,

Will try to rerun TCK over the weekend but looks almost perfect
Just put a comment on two minor things - i cna handle them if you prefer:

  1. (Trivial one) not sure why package line jumped on top of the header in the test
  2. Commented on the direct cast and i managed to be in a failling case (with libs creating custom impl of PT) so think it does not cost us much to harness our code even if it should have been more unlikely

That said, thanks a lot to have moved the code, really appreciated!

@@ -1033,6 +1037,20 @@ private boolean isNumberType(final Type type) {
return Number.class.isAssignableFrom(clazz) || clazz.isPrimitive();
}

private boolean hasRawType(final Type type) {
return Class.class.isInstance(type) || ParameterizedType.class.isInstance(type);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, i get the point where java.lang.reflect will highly likely return a Class (oracle jdk guarantees it) but this comes from a reader - which is configurable in the jsonb builder - so potentially a custom PT impl, not java.reflect backed.
Any issue harnessing the check (Class.isInstance(pt.getRawType()) to avoid a ClassCastException which would be a regression for end users?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem with that :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will not find the time to do the changes this weekend. So go ahead, if you like. Otherwise I'll do it next week.

@rmannibucau rmannibucau merged commit 605e775 into apache:master Sep 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants