Ignore property of object in list property of a class

667 views
Skip to first unread message

Obby Kipchumba

unread,
Mar 20, 2017, 11:49:41 AM3/20/17
to jackson-user
Let;s say you have the following classes.

class Client {
     
List<Vehicle> vehicles;
}


Class Vehicle {
     
     
     
Client owner;
}



What I want to have is a field level annotation as such.

class Client {

     
// something like this
     
@JsonIgnoreProperties (forEach="owner")
     
List<Vehicle> vehicles;
}


Class Vehicle {

     
@JsonIgnoreProperties ("vehicles")
     
Client owner;
}


This is so as to prevent recursion based on the contaxt you are in. I mean If I have a client object and i'm getting it's vehicles, i don't want the owner details since I already have the client object but when i have a vehicle object I will need to know it's owner information but exclude the list of the vehicles the client has. Isn't this something that someone has needed at some point in time?

When you use at class level, like this

@JsonIgnoreProperties ("owner")
Class Vehicle {
      Client owner;
}

This will ignore  the owner all the time regardless of context. Can the above feature be made possible?



Tatu Saloranta

unread,
Mar 23, 2017, 8:03:33 PM3/23/17
to jackson-user
Actually it should already be possible to use `@JsonIgnoreProperties`
for properties as is, but I think it only affects property value
itself, and not contents of containers.
But it seems reasonable to assume that since Lists/arrays/Maps do not
have properties of their own (except in non-standard case of being
serialized "as POJOs"...), this annotation would refer to values. This
is similar to how `@JsonTypeInfo` works.

If there isn't yet an issue to support this, could you please file one
for `jackson-databind`.
I don't know how easy it would be to support this, but conceptually it
seems straightforward.

-+ Tatu +-
> --
> You received this message because you are subscribed to the Google Groups
> "jackson-user" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to jackson-user...@googlegroups.com.
> To post to this group, send email to jackso...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages