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 java.beans.Transient anntoation #158

Open
onacit opened this issue Jul 19, 2020 · 2 comments
Open

Support java.beans.Transient anntoation #158

onacit opened this issue Jul 19, 2020 · 2 comments

Comments

@onacit
Copy link

onacit commented Jul 19, 2020

It seems that Genson doesn't support java.beans.Transient annotation, yet.

    //@com.owlike.genson.annotation.JsonIgnore
    @java.beans.Transient
    public int getSizeInBytes() {
    }

    public void setSizeInBytes(final int sizeInBytes) {
    }

    //@com.owlike.genson.annotation.JsonIgnore
    @java.beans.Transient
    public Format getFormatAsOneOfPredefined() {
    }

    public void setFormatAsOneOfPredefined(final Format formatAsEnum) {
    }

Here comes the output.

jackson: {"n":1,"size":1024,"format":"base64","apiKey":"6b1e65b9-4186-45c2-8981-b77a9842c4f0"}
gson:    {"n":1,"size":1024,"format":"base64","apiKey":"6b1e65b9-4186-45c2-8981-b77a9842c4f0"}
moshi:   {"apiKey":"6b1e65b9-4186-45c2-8981-b77a9842c4f0","format":"base64","n":1,"size":1024}
genson:  {"apiKey":"6b1e65b9-4186-45c2-8981-b77a9842c4f0","format":"base64","formatAsOneOfPredefined":"BASE64","n":1,"size":1024,"sizeInBytes":128}
@nicky9door
Copy link
Contributor

I think this could be implemented by hooking into the code that checks for the @JsonIgnore annotation and also check for the @Transient annotation. Ideally, this should be toggled in the genson builder to avoid breaking existing code.

Alternatively, this can be quickly addressed by creating a custom BeanMutatorAccessorResolver and passing it to GensonBuilder.with

static class TransientPropResolver implements BeanMutatorAccessorResolver {
		@Override
		public Trilean isCreator(Constructor<?> constructor, Class<?> fromClass){
			if(hasTransientAnnotation(constructor)){
				return Trilean.FALSE;
			}
			return Trilean.UNKNOWN;
		}

		@Override
		public Trilean isCreator(Method method, Class<?> fromClass){
			if(hasTransientAnnotation(method)){
				return Trilean.FALSE;
			}
			return Trilean.UNKNOWN;
		}

		@Override
		public Trilean isAccessor(Field field, Class<?> fromClass){
			if(hasTransientAnnotation(field)){
				return Trilean.FALSE;
			}
			return Trilean.UNKNOWN;
		}

		@Override
		public Trilean isAccessor(Method method, Class<?> fromClass){
			if(hasTransientAnnotation(method)){
				return Trilean.FALSE;
			}
			return Trilean.UNKNOWN;
		}

		@Override
		public Trilean isMutator(Field field, Class<?> fromClass){
			if(hasTransientAnnotation(field)){
				return Trilean.FALSE;
			}
			return Trilean.UNKNOWN;
		}

		@Override
		public Trilean isMutator(Method method, Class<?> fromClass){
			if(hasTransientAnnotation(method)){
				return Trilean.FALSE;
			}
			return Trilean.UNKNOWN;
		}

		private boolean hasTransientAnnotation(AccessibleObject ac){
			return ac.getAnnotation(Transient.class) != null;
		}
	}
Genson genson = new GensonBuilder()
				.with(new TransientPropResolver())
				.create();

@EugenCepoi
Copy link
Contributor

What @nicky9door suggests should work.

On a separate note, I'm not actively coding in Genson anymore but if people want to contribute and submit PRs I can review them and make new releases.

Thanks!

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

3 participants