yasson icon indicating copy to clipboard operation
yasson copied to clipboard

Can Yasson support something like the @JsonIgnoreProperties of Jackson?

Open hei1233212000 opened this issue 8 years ago • 9 comments
trafficstars

I encountered an issue in Groovy - https://github.com/hei1233212000/yasson-groovy-not-working where Yasson is trying to invoke some getter which has method arguments needed. e.g: MetaClass.getAttribute(Class sender, Object receiver, String messageName, boolean useSuper)

hei1233212000 avatar Oct 18 '17 15:10 hei1233212000

I don't see problems doing it. @bravehorsie, any concerns?

m0mus avatar Dec 05 '17 21:12 m0mus

We do have @JsonbTransient to supply similar functionality.

There are no problems adding it, but the annotation itself should be added to jsonb spec project.

bravehorsie avatar Dec 06 '17 10:12 bravehorsie

Hi @bravehorsie, I see a little bit different between the @JsonIgnoreProperties and the @JsonbTransient

You can NOT mark the field or property as transient by @JsonbTransient when you could not modify the source code. (e.g: I extend the Class from from other projects or using the POGO, my example)

And I think it would be great to put it in the JSON-B spec but I hope Yasson could support it earlier than the spec is confirmed to be modified.

hei1233212000 avatar Dec 06 '17 10:12 hei1233212000

@hei1233212000 sure, I didn't mean they are identical.

If you can't modify the code, how would you apply @JsonbIgnoreProperties? It's meant to be used class level right?

bravehorsie avatar Dec 06 '17 11:12 bravehorsie

@bravehorsie yes, use the @jsonbignoreproperties in class level

hei1233212000 avatar Dec 06 '17 11:12 hei1233212000

I know I'm super late to this issue, but @hei1233212000 I believe the immediate issue you were facing was resolved a while back by PR #66.


Aside from the specific scenario, I agree that a general annotation may be useful at the class level.

For example if we have:

// Don't have access to this source
public class ThirdPartyClass {
  public String junk1;
  public String junk2;
  public String important;
}

We could use a class level annotation as discussed above to blacklist certain properties, such as:

@JsonbIgnoreProperties({"junk1", "junk2"})
public class MyClass extends ThirdPartyClass {}

However, I think it would be more useful to white-list properties rather than black-list them, for example:

@JsonbProperties({"important"})
public class MyClass extends ThirdPartyClass {}

@bravehorsie any thoughts on either of the above approaches?

aguibert avatar Nov 15 '18 04:11 aguibert

Thank you @aguibert !

Besides, I think white list and black list would be useful in different scenarios. For an example, the class has many fields but you only want to ignore one or two fields. :)

hei1233212000 avatar Nov 17 '18 04:11 hei1233212000

TBH, this sounds pretty much like a mix-in from Jackson: https://github.com/FasterXML/jackson-docs/wiki/JacksonMixInAnnotations

The advantage of jackson: Mix-Ins can also be applied to final classes (which would not work in your case).

bmarwell avatar Dec 12 '18 12:12 bmarwell

For a quick solution:

    @JsonbTransient
    public groovy.lang.MetaClass metaClass;

I think the current proper way is to create a custom serializer. @JsonbIgnoreProperties in the spec can be much nicer.

fatihpense avatar Aug 26 '21 02:08 fatihpense