Subclass as json parent object










2















I have a requirement where I need a subclass as object while creating a json payload.



EventBase



public class EventBase 
@JsonProperty("event_id")
private String id;
public String getId()
return id;


public void setId(String id)
this.id = id;





PaymentCapture (the sub class)



@JsonTypeName("resource")
public class PaymentCapture extends EventBase
@JsonProperty("parent_payment")
private String parentPayment;

public String getParentPayment()
return parentPayment;


public void setParentPayment(String parentPayment)
this.parentPayment = parentPayment;




And I need a json payload in below form:




"id": "someId",
"resource":
"parent_payment": "23434"




I can understand this violates inheritance relationship, but just want to know if there is any solution available or not.










share|improve this question




























    2















    I have a requirement where I need a subclass as object while creating a json payload.



    EventBase



    public class EventBase 
    @JsonProperty("event_id")
    private String id;
    public String getId()
    return id;


    public void setId(String id)
    this.id = id;





    PaymentCapture (the sub class)



    @JsonTypeName("resource")
    public class PaymentCapture extends EventBase
    @JsonProperty("parent_payment")
    private String parentPayment;

    public String getParentPayment()
    return parentPayment;


    public void setParentPayment(String parentPayment)
    this.parentPayment = parentPayment;




    And I need a json payload in below form:




    "id": "someId",
    "resource":
    "parent_payment": "23434"




    I can understand this violates inheritance relationship, but just want to know if there is any solution available or not.










    share|improve this question


























      2












      2








      2








      I have a requirement where I need a subclass as object while creating a json payload.



      EventBase



      public class EventBase 
      @JsonProperty("event_id")
      private String id;
      public String getId()
      return id;


      public void setId(String id)
      this.id = id;





      PaymentCapture (the sub class)



      @JsonTypeName("resource")
      public class PaymentCapture extends EventBase
      @JsonProperty("parent_payment")
      private String parentPayment;

      public String getParentPayment()
      return parentPayment;


      public void setParentPayment(String parentPayment)
      this.parentPayment = parentPayment;




      And I need a json payload in below form:




      "id": "someId",
      "resource":
      "parent_payment": "23434"




      I can understand this violates inheritance relationship, but just want to know if there is any solution available or not.










      share|improve this question
















      I have a requirement where I need a subclass as object while creating a json payload.



      EventBase



      public class EventBase 
      @JsonProperty("event_id")
      private String id;
      public String getId()
      return id;


      public void setId(String id)
      this.id = id;





      PaymentCapture (the sub class)



      @JsonTypeName("resource")
      public class PaymentCapture extends EventBase
      @JsonProperty("parent_payment")
      private String parentPayment;

      public String getParentPayment()
      return parentPayment;


      public void setParentPayment(String parentPayment)
      this.parentPayment = parentPayment;




      And I need a json payload in below form:




      "id": "someId",
      "resource":
      "parent_payment": "23434"




      I can understand this violates inheritance relationship, but just want to know if there is any solution available or not.







      java json jackson fasterxml






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 16 '18 at 14:17









      pirho

      5,056111932




      5,056111932










      asked Nov 15 '18 at 13:25









      foxt7otfoxt7ot

      1,6541328




      1,6541328






















          1 Answer
          1






          active

          oldest

          votes


















          0














          The closest I could get when having similar problem was creating an adapter class. This solution prints one extra property which might be possible to be ignored if for example some inheritance was allowed but I assume that not and use just the declared classes in addition to the adapter, which is like:



          @RequiredArgsConstructor
          public class PaymentCaptureAdapterClass
          @NonNull
          @JsonProperty
          private PaymentCapture resource;

          @JsonProperty
          private String getId()
          return resource.getId();




          using this with code:



          ObjectMapper om = new ObjectMapper();
          om.enable(SerializationFeature.INDENT_OUTPUT);
          PaymentCapture pc = new PaymentCapture();

          pc.setId("someId");
          pc.setParentPayment("23434");

          log.info("n", om.writeValueAsString(new AdapterClass(pc)));


          prints something like:




          "resource" :
          "event_id" : "someId", // might be able to be ignored
          "parent_payment" : "23434"
          ,
          "id" : "someId"






          share|improve this answer























          • Thanks for solution, I also thought in similar sort of way but still want to wait if someone has some standard solution for the problem.

            – foxt7ot
            Nov 16 '18 at 4:48











          • @foxt7ot Np. I am curious also how someone would solve this in other way.

            – pirho
            Nov 16 '18 at 14:13










          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%2f53320524%2fsubclass-as-json-parent-object%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          The closest I could get when having similar problem was creating an adapter class. This solution prints one extra property which might be possible to be ignored if for example some inheritance was allowed but I assume that not and use just the declared classes in addition to the adapter, which is like:



          @RequiredArgsConstructor
          public class PaymentCaptureAdapterClass
          @NonNull
          @JsonProperty
          private PaymentCapture resource;

          @JsonProperty
          private String getId()
          return resource.getId();




          using this with code:



          ObjectMapper om = new ObjectMapper();
          om.enable(SerializationFeature.INDENT_OUTPUT);
          PaymentCapture pc = new PaymentCapture();

          pc.setId("someId");
          pc.setParentPayment("23434");

          log.info("n", om.writeValueAsString(new AdapterClass(pc)));


          prints something like:




          "resource" :
          "event_id" : "someId", // might be able to be ignored
          "parent_payment" : "23434"
          ,
          "id" : "someId"






          share|improve this answer























          • Thanks for solution, I also thought in similar sort of way but still want to wait if someone has some standard solution for the problem.

            – foxt7ot
            Nov 16 '18 at 4:48











          • @foxt7ot Np. I am curious also how someone would solve this in other way.

            – pirho
            Nov 16 '18 at 14:13















          0














          The closest I could get when having similar problem was creating an adapter class. This solution prints one extra property which might be possible to be ignored if for example some inheritance was allowed but I assume that not and use just the declared classes in addition to the adapter, which is like:



          @RequiredArgsConstructor
          public class PaymentCaptureAdapterClass
          @NonNull
          @JsonProperty
          private PaymentCapture resource;

          @JsonProperty
          private String getId()
          return resource.getId();




          using this with code:



          ObjectMapper om = new ObjectMapper();
          om.enable(SerializationFeature.INDENT_OUTPUT);
          PaymentCapture pc = new PaymentCapture();

          pc.setId("someId");
          pc.setParentPayment("23434");

          log.info("n", om.writeValueAsString(new AdapterClass(pc)));


          prints something like:




          "resource" :
          "event_id" : "someId", // might be able to be ignored
          "parent_payment" : "23434"
          ,
          "id" : "someId"






          share|improve this answer























          • Thanks for solution, I also thought in similar sort of way but still want to wait if someone has some standard solution for the problem.

            – foxt7ot
            Nov 16 '18 at 4:48











          • @foxt7ot Np. I am curious also how someone would solve this in other way.

            – pirho
            Nov 16 '18 at 14:13













          0












          0








          0







          The closest I could get when having similar problem was creating an adapter class. This solution prints one extra property which might be possible to be ignored if for example some inheritance was allowed but I assume that not and use just the declared classes in addition to the adapter, which is like:



          @RequiredArgsConstructor
          public class PaymentCaptureAdapterClass
          @NonNull
          @JsonProperty
          private PaymentCapture resource;

          @JsonProperty
          private String getId()
          return resource.getId();




          using this with code:



          ObjectMapper om = new ObjectMapper();
          om.enable(SerializationFeature.INDENT_OUTPUT);
          PaymentCapture pc = new PaymentCapture();

          pc.setId("someId");
          pc.setParentPayment("23434");

          log.info("n", om.writeValueAsString(new AdapterClass(pc)));


          prints something like:




          "resource" :
          "event_id" : "someId", // might be able to be ignored
          "parent_payment" : "23434"
          ,
          "id" : "someId"






          share|improve this answer













          The closest I could get when having similar problem was creating an adapter class. This solution prints one extra property which might be possible to be ignored if for example some inheritance was allowed but I assume that not and use just the declared classes in addition to the adapter, which is like:



          @RequiredArgsConstructor
          public class PaymentCaptureAdapterClass
          @NonNull
          @JsonProperty
          private PaymentCapture resource;

          @JsonProperty
          private String getId()
          return resource.getId();




          using this with code:



          ObjectMapper om = new ObjectMapper();
          om.enable(SerializationFeature.INDENT_OUTPUT);
          PaymentCapture pc = new PaymentCapture();

          pc.setId("someId");
          pc.setParentPayment("23434");

          log.info("n", om.writeValueAsString(new AdapterClass(pc)));


          prints something like:




          "resource" :
          "event_id" : "someId", // might be able to be ignored
          "parent_payment" : "23434"
          ,
          "id" : "someId"







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 '18 at 21:30









          pirhopirho

          5,056111932




          5,056111932












          • Thanks for solution, I also thought in similar sort of way but still want to wait if someone has some standard solution for the problem.

            – foxt7ot
            Nov 16 '18 at 4:48











          • @foxt7ot Np. I am curious also how someone would solve this in other way.

            – pirho
            Nov 16 '18 at 14:13

















          • Thanks for solution, I also thought in similar sort of way but still want to wait if someone has some standard solution for the problem.

            – foxt7ot
            Nov 16 '18 at 4:48











          • @foxt7ot Np. I am curious also how someone would solve this in other way.

            – pirho
            Nov 16 '18 at 14:13
















          Thanks for solution, I also thought in similar sort of way but still want to wait if someone has some standard solution for the problem.

          – foxt7ot
          Nov 16 '18 at 4:48





          Thanks for solution, I also thought in similar sort of way but still want to wait if someone has some standard solution for the problem.

          – foxt7ot
          Nov 16 '18 at 4:48













          @foxt7ot Np. I am curious also how someone would solve this in other way.

          – pirho
          Nov 16 '18 at 14:13





          @foxt7ot Np. I am curious also how someone would solve this in other way.

          – pirho
          Nov 16 '18 at 14:13



















          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%2f53320524%2fsubclass-as-json-parent-object%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







          這個網誌中的熱門文章

          Barbados

          How to read a connectionString WITH PROVIDER in .NET Core?

          Node.js Script on GitHub Pages or Amazon S3