Jackson Demo from Groovy Console


/ Published in: Groovy
Save to your folder(s)

Make sure Jackson core and Mappers are on classpath. So who can explain why userImage has a size of 7. I assume the byte-Array is encoded. What format?


Copy this code and paste it in your HTML
  1. import org.codehaus.jackson.map.ObjectMapper
  2.  
  3. class User
  4. {
  5. String first;
  6. String last;
  7. }
  8.  
  9. Name name
  10. Boolean verified
  11. String gender
  12. byte[] userImage
  13. }
  14.  
  15. String json = """{
  16. "name" : { "first" : "Joe", "last" : "Sixpack" },
  17. "gender" : "MALE",
  18. "verified" : false,
  19. "userImage" : "Rm9vYmFyIQ=="
  20. }
  21. """
  22.  
  23. ObjectMapper mapper = new ObjectMapper();
  24. User user = mapper.readValue(json, User.class);
  25.  
  26. assert user.name.first == "Joe"
  27. assert user.name.last == "Sixpack"
  28. assert user.gender == "MALE"
  29. assert user.verified == false
  30. assert user.userImage.size() == 7

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.