Jackson - suppressing serialization(write) of properties dynamically

前端 未结 4 2107
难免孤独
难免孤独 2021-01-04 10:21

I am trying to convert java object to JSON object in Tomcat/jersey using Jackson. And want to suppress serialization(write) of certain properties dynamically.

I can

相关标签:
4条回答
  • 2021-01-04 10:59

    I don't see any way of doing that. If you need to dynamically decide which properties are marshalled, then I suggest you manually construct a Map of keys to values for your objects, and then pass that Map to Jackson, rather than passing the User object directly.

    0 讨论(0)
  • 2021-01-04 11:02

    Yes, JSON View is the way to go.

    If you e.g. need to let the client to decide which fields to marshal, this example might help: http://svn.codehaus.org/jackson/tags/1.6/1.6.3/src/sample/CustomSerializationView.java

    0 讨论(0)
  • 2021-01-04 11:02

    Check

    ObjectMapper.configure(SerialiationJson.Feature f, boolean value)
    

    and

    org.codehaus.jackson.annotate.JsonIgnore 
    

    annotation

    This will work only when you want all instances of a certain type to ignore id on serialization. If you truly want dynamic (aka per instance customization) you will probabily have to hack the jackson library yourself.

    0 讨论(0)
  • 2021-01-04 11:18

    Have you tried using JSON Views? Views allow annotation-based mechanism for defining different profiles, so if you just need slightly differing views for different users, this could work for you.

    0 讨论(0)
提交回复
热议问题