Advertisement
Guest User

Untitled

a guest
Jul 18th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 0.97 KB | None | 0 0
  1. package jackson
  2.  
  3. import com.fasterxml.jackson.annotation.JsonInclude
  4. import com.fasterxml.jackson.databind.ObjectMapper
  5. import spock.lang.Specification
  6.  
  7. class ObjectMapperTest extends Specification {
  8.     def "should not include nulls"() {
  9.         given:
  10.         def json = '''
  11.                {
  12.                    "a": {},
  13.                    "b": [],
  14.                    "c": "",
  15.                    "d": 0,
  16.                    "e": null
  17.                }
  18.        '''
  19.  
  20.         def obj = [
  21.                 "a": [:],
  22.                 "b": [],
  23.                 "c": "",
  24.                 "d": 0,
  25.                 "e": null
  26.         ]
  27.  
  28.         def jsonMapper = new ObjectMapper()
  29.                 .setSerializationInclusion(JsonInclude.Include.NON_NULL)
  30.         def tree = jsonMapper.readTree(json)
  31.  
  32.  
  33.         when:
  34.         def a1 = jsonMapper.writeValueAsString(tree)
  35.         def a2 = jsonMapper.writeValueAsString(obj)
  36.  
  37.         then:
  38.         a1 == a2
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement