Guest User

Untitled

a guest
Nov 16th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. public static class SuperType {
  2. public int A;
  3. }
  4.  
  5. public static class SubType extends SuperType {
  6. public int B;
  7. }
  8.  
  9. public static class ParametricType<T> {
  10. public T Value;
  11. }
  12.  
  13. @Test
  14. public void doNotSerializeSubClassFieldsTest() throws Exception {
  15. var mapper = new ObjectMapper();
  16. var writer = mapper.writerFor(SuperType.class);
  17.  
  18. var subType = new SubType();
  19. subType.A = 1;
  20. subType.B = 2;
  21.  
  22. var expectedValue = "{"A":1}";
  23. var data = writer.writeValueAsString(subType);
  24.  
  25. assertEquals(expectedValue, data);
  26. }
  27.  
  28. @Test
  29. public void serializeNotSubclassAuthenticatedMessageTest() throws Exception {
  30. var mapper = new ObjectMapper();
  31. var javaType = mapper.getTypeFactory().constructParametricType(ParametricType.class, SuperType.class);
  32. var writer = mapper.writerFor(javaType);
  33.  
  34. var subType = new SubType();
  35. subType.A = 1;
  36. subType.B = 2;
  37. var parametricType = new ParametricType<>();
  38. parametricType.Value = subType;
  39.  
  40. var expectedValue = "{"Value":{"A":1}}";
  41. var data = writer.writeValueAsString(parametricType);
  42.  
  43. // it is actually "{"Value":{"A":1,"B":2}}"
  44. assertEquals(expectedValue, data);
  45. }
Add Comment
Please, Sign In to add comment