How to set default properties to ignore for json serialization

16 views
Skip to first unread message

Alexander

unread,
Mar 7, 2020, 2:15:06 PM3/7/20
to jackson-user
Instead of annotating many classes with this annotation, i would like to set the ignoreproperties globally by default for atleast the properties with these names:

@JsonIgnoreProperties({
"JavassistLazyInitializer",
"hibernateLazyInitializer",
"handler"})

Pointing me how this can be done will be greatly appreciated

Alexander

unread,
Mar 7, 2020, 2:15:06 PM3/7/20
to jackson-user
How can i set default properties to ignore for json serialization 

Tatu Saloranta

unread,
Mar 7, 2020, 2:24:52 PM3/7/20
to jackson-user
There is no mechanism to ignore specifically named properties from every class to serialize, but there are some alternatives to using `@JsonIgnoreProperties` on a class:

1. Config overrides: you can apply equivalent of annotation on specific classes:

   mapper.configOverride(Point.class)

            .setIgnorals(JsonIgnoreProperties.Value.forIgnoredProperties("x", "y", "z"));


2. If you want to ignore all properties with specific type of value, either

    @JsonIgnoreType on class (possibly via mix-in annotations)

     or another config override

   mapper.configOverride(AnotherType.class)
      .setIsIgnoredType(true);

3. You can "fake" finding `@JsonIgnoreProperties` on every type, to effectively get what you
   want by sub-classing `JacksonAnnotationIntrospector` method

    public JsonIgnoreProperties.Value findPropertyIgnorals(Annotated ac) {

        if (ac instanceof AnnotatedClass) { // may also check it's for specific class(es)

           return JsonIgnoreProperties.Value.forIgnoredProperties("x", "y", "z");

        }

        return JsonIgnoreProperties.Value.empty();

    }


Last method is probably what you want to use here.

Hope this helps!

-+ Tatu +-

 
Reply all
Reply to author
Forward
0 new messages