How can we prevent the serialization of fields of a sub type of a type parameter in jackson/json?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















The second junit test serializes an instance of a class which has a type parameter. The instance is of type ParametricType<SubType>. The serializer was initialized by using constructParametricType(ParametricType.class, SuperType.class) as type information. We can see that we initialized the serializer with the super type SuperType for the type parameter. We would now expect to get serialized json, containing only fields of the super type. But the field B of the sub type is also included.



It works in the first example where we do not wrap our SuperType in ParametricType.



So what are we missing in order to get only the super type fields as serialized output when we use a parametric type?



public static class SuperType {
public int A;
}

public static class SubType extends SuperType {
public int B;
}

public static class ParametricType<T> {
public T Value;
}

@Test
public void doNotSerializeSubClassFieldsTest() throws Exception {
var mapper = new ObjectMapper();
var writer = mapper.writerFor(SuperType.class);

var subType = new SubType();
subType.A = 1;
subType.B = 2;

var expectedValue = "{"A":1}";
var data = writer.writeValueAsString(subType);

assertEquals(expectedValue, data);
}

@Test
public void serializeNotSubclassAuthenticatedMessageTest() throws Exception {
var mapper = new ObjectMapper();
var javaType = mapper.getTypeFactory().constructParametricType(ParametricType.class, SuperType.class);
var writer = mapper.writerFor(javaType);

var subType = new SubType();
subType.A = 1;
subType.B = 2;
var parametricType = new ParametricType<>();
parametricType.Value = subType;

var expectedValue = "{"Value":{"A":1}}";
var data = writer.writeValueAsString(parametricType);

// it is actually "{"Value":{"A":1,"B":2}}"
assertEquals(expectedValue, data);
}









share|improve this question





























    1















    The second junit test serializes an instance of a class which has a type parameter. The instance is of type ParametricType<SubType>. The serializer was initialized by using constructParametricType(ParametricType.class, SuperType.class) as type information. We can see that we initialized the serializer with the super type SuperType for the type parameter. We would now expect to get serialized json, containing only fields of the super type. But the field B of the sub type is also included.



    It works in the first example where we do not wrap our SuperType in ParametricType.



    So what are we missing in order to get only the super type fields as serialized output when we use a parametric type?



    public static class SuperType {
    public int A;
    }

    public static class SubType extends SuperType {
    public int B;
    }

    public static class ParametricType<T> {
    public T Value;
    }

    @Test
    public void doNotSerializeSubClassFieldsTest() throws Exception {
    var mapper = new ObjectMapper();
    var writer = mapper.writerFor(SuperType.class);

    var subType = new SubType();
    subType.A = 1;
    subType.B = 2;

    var expectedValue = "{"A":1}";
    var data = writer.writeValueAsString(subType);

    assertEquals(expectedValue, data);
    }

    @Test
    public void serializeNotSubclassAuthenticatedMessageTest() throws Exception {
    var mapper = new ObjectMapper();
    var javaType = mapper.getTypeFactory().constructParametricType(ParametricType.class, SuperType.class);
    var writer = mapper.writerFor(javaType);

    var subType = new SubType();
    subType.A = 1;
    subType.B = 2;
    var parametricType = new ParametricType<>();
    parametricType.Value = subType;

    var expectedValue = "{"Value":{"A":1}}";
    var data = writer.writeValueAsString(parametricType);

    // it is actually "{"Value":{"A":1,"B":2}}"
    assertEquals(expectedValue, data);
    }









    share|improve this question

























      1












      1








      1


      1






      The second junit test serializes an instance of a class which has a type parameter. The instance is of type ParametricType<SubType>. The serializer was initialized by using constructParametricType(ParametricType.class, SuperType.class) as type information. We can see that we initialized the serializer with the super type SuperType for the type parameter. We would now expect to get serialized json, containing only fields of the super type. But the field B of the sub type is also included.



      It works in the first example where we do not wrap our SuperType in ParametricType.



      So what are we missing in order to get only the super type fields as serialized output when we use a parametric type?



      public static class SuperType {
      public int A;
      }

      public static class SubType extends SuperType {
      public int B;
      }

      public static class ParametricType<T> {
      public T Value;
      }

      @Test
      public void doNotSerializeSubClassFieldsTest() throws Exception {
      var mapper = new ObjectMapper();
      var writer = mapper.writerFor(SuperType.class);

      var subType = new SubType();
      subType.A = 1;
      subType.B = 2;

      var expectedValue = "{"A":1}";
      var data = writer.writeValueAsString(subType);

      assertEquals(expectedValue, data);
      }

      @Test
      public void serializeNotSubclassAuthenticatedMessageTest() throws Exception {
      var mapper = new ObjectMapper();
      var javaType = mapper.getTypeFactory().constructParametricType(ParametricType.class, SuperType.class);
      var writer = mapper.writerFor(javaType);

      var subType = new SubType();
      subType.A = 1;
      subType.B = 2;
      var parametricType = new ParametricType<>();
      parametricType.Value = subType;

      var expectedValue = "{"Value":{"A":1}}";
      var data = writer.writeValueAsString(parametricType);

      // it is actually "{"Value":{"A":1,"B":2}}"
      assertEquals(expectedValue, data);
      }









      share|improve this question














      The second junit test serializes an instance of a class which has a type parameter. The instance is of type ParametricType<SubType>. The serializer was initialized by using constructParametricType(ParametricType.class, SuperType.class) as type information. We can see that we initialized the serializer with the super type SuperType for the type parameter. We would now expect to get serialized json, containing only fields of the super type. But the field B of the sub type is also included.



      It works in the first example where we do not wrap our SuperType in ParametricType.



      So what are we missing in order to get only the super type fields as serialized output when we use a parametric type?



      public static class SuperType {
      public int A;
      }

      public static class SubType extends SuperType {
      public int B;
      }

      public static class ParametricType<T> {
      public T Value;
      }

      @Test
      public void doNotSerializeSubClassFieldsTest() throws Exception {
      var mapper = new ObjectMapper();
      var writer = mapper.writerFor(SuperType.class);

      var subType = new SubType();
      subType.A = 1;
      subType.B = 2;

      var expectedValue = "{"A":1}";
      var data = writer.writeValueAsString(subType);

      assertEquals(expectedValue, data);
      }

      @Test
      public void serializeNotSubclassAuthenticatedMessageTest() throws Exception {
      var mapper = new ObjectMapper();
      var javaType = mapper.getTypeFactory().constructParametricType(ParametricType.class, SuperType.class);
      var writer = mapper.writerFor(javaType);

      var subType = new SubType();
      subType.A = 1;
      subType.B = 2;
      var parametricType = new ParametricType<>();
      parametricType.Value = subType;

      var expectedValue = "{"Value":{"A":1}}";
      var data = writer.writeValueAsString(parametricType);

      // it is actually "{"Value":{"A":1,"B":2}}"
      assertEquals(expectedValue, data);
      }






      java json inheritance jackson type-parameter






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 16 '18 at 14:40









      sinsemellansinsemellan

      62




      62
























          0






          active

          oldest

          votes












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53339975%2fhow-can-we-prevent-the-serialization-of-fields-of-a-sub-type-of-a-type-parameter%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53339975%2fhow-can-we-prevent-the-serialization-of-fields-of-a-sub-type-of-a-type-parameter%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Bressuire

          Vorschmack

          Quarantine