JSON deserialiser to set custom properties in POJO












0















I'm using Jackson for json mapping on java POJOs. What I want is to set two properties in POJO from a value in JSON by splitting the value.



{
"email": "xyz@hello.com",
}


and POJO is



public class TestPojo { 

@JsonProperty("email")
private String emailAddress;

/*is there any annotation available that I can split the email
address with a delimiter which is '@' to first and second
properties*/
private String first; //gives value xyz
private String second;//gives value hello.com
}


Thanks for your help in advance.










share|improve this question





























    0















    I'm using Jackson for json mapping on java POJOs. What I want is to set two properties in POJO from a value in JSON by splitting the value.



    {
    "email": "xyz@hello.com",
    }


    and POJO is



    public class TestPojo { 

    @JsonProperty("email")
    private String emailAddress;

    /*is there any annotation available that I can split the email
    address with a delimiter which is '@' to first and second
    properties*/
    private String first; //gives value xyz
    private String second;//gives value hello.com
    }


    Thanks for your help in advance.










    share|improve this question



























      0












      0








      0








      I'm using Jackson for json mapping on java POJOs. What I want is to set two properties in POJO from a value in JSON by splitting the value.



      {
      "email": "xyz@hello.com",
      }


      and POJO is



      public class TestPojo { 

      @JsonProperty("email")
      private String emailAddress;

      /*is there any annotation available that I can split the email
      address with a delimiter which is '@' to first and second
      properties*/
      private String first; //gives value xyz
      private String second;//gives value hello.com
      }


      Thanks for your help in advance.










      share|improve this question
















      I'm using Jackson for json mapping on java POJOs. What I want is to set two properties in POJO from a value in JSON by splitting the value.



      {
      "email": "xyz@hello.com",
      }


      and POJO is



      public class TestPojo { 

      @JsonProperty("email")
      private String emailAddress;

      /*is there any annotation available that I can split the email
      address with a delimiter which is '@' to first and second
      properties*/
      private String first; //gives value xyz
      private String second;//gives value hello.com
      }


      Thanks for your help in advance.







      java json parsing jackson deserialization






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 14:27









      Hülya

      462119




      462119










      asked Nov 13 '18 at 14:13









      anzanz

      7319




      7319
























          1 Answer
          1






          active

          oldest

          votes


















          0














          You can hijack that logic in your public setter.
          For instance:



          class MyPojo {
          // no need for this annotation here actually, covered by setter
          // at least for deserialization
          @JsonProperty
          String email;
          String first;
          String last;

          @JsonProperty("email")
          public void setEmail(String email) {
          this.email = email;
          String split = email.split("@");
          // TODO check length etc.
          this.first = split[0];
          this.last = split[1];
          }
          // just for testing
          @Override
          public String toString() {
          return String.format(
          "email: %s, first: %s, last: %s%n", email, first, last
          );
          }
          }


          Then, somewhere else...



          String json = "{ "email": "xyz@hello.com"}";
          ObjectMapper om = new ObjectMapper();
          MyPojo pojo = om.readValue(json, MyPojo.class);
          System.out.println(pojo);


          Output



          email: xyz@hello.com, first: xyz, last: hello.com





          share|improve this answer


























          • Unfortunately, I am using an auto-generated code with abstract methods in my base pojo. So cannot hijack setter here.

            – anz
            Nov 13 '18 at 14:20






          • 1





            @anz then you could use a custom de-serializer, and set the first and last values there. This would require that you have setter methods for those, and of course will be a lot more tedious than just editing your pojo...

            – Mena
            Nov 13 '18 at 14:22











          • Even then, how can the deserializer will get invoked? I don't have those properties present in the json.

            – anz
            Nov 13 '18 at 14:25











          • @anz you don't need to annotate you pojo with @JsonDeserialize if you can't modify it, you can register the de-serializer to your ObjectMapper. Edit take a look here if you're looking for a decent tutorial.

            – Mena
            Nov 13 '18 at 14:30













          • I tried with ObjectMapper, but it's expecting to return the same POJO object from the deserialize method. Is there any other way by modifying at property level through deserialization?

            – anz
            Nov 13 '18 at 14:49











          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%2f53282961%2fjson-deserialiser-to-set-custom-properties-in-pojo%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














          You can hijack that logic in your public setter.
          For instance:



          class MyPojo {
          // no need for this annotation here actually, covered by setter
          // at least for deserialization
          @JsonProperty
          String email;
          String first;
          String last;

          @JsonProperty("email")
          public void setEmail(String email) {
          this.email = email;
          String split = email.split("@");
          // TODO check length etc.
          this.first = split[0];
          this.last = split[1];
          }
          // just for testing
          @Override
          public String toString() {
          return String.format(
          "email: %s, first: %s, last: %s%n", email, first, last
          );
          }
          }


          Then, somewhere else...



          String json = "{ "email": "xyz@hello.com"}";
          ObjectMapper om = new ObjectMapper();
          MyPojo pojo = om.readValue(json, MyPojo.class);
          System.out.println(pojo);


          Output



          email: xyz@hello.com, first: xyz, last: hello.com





          share|improve this answer


























          • Unfortunately, I am using an auto-generated code with abstract methods in my base pojo. So cannot hijack setter here.

            – anz
            Nov 13 '18 at 14:20






          • 1





            @anz then you could use a custom de-serializer, and set the first and last values there. This would require that you have setter methods for those, and of course will be a lot more tedious than just editing your pojo...

            – Mena
            Nov 13 '18 at 14:22











          • Even then, how can the deserializer will get invoked? I don't have those properties present in the json.

            – anz
            Nov 13 '18 at 14:25











          • @anz you don't need to annotate you pojo with @JsonDeserialize if you can't modify it, you can register the de-serializer to your ObjectMapper. Edit take a look here if you're looking for a decent tutorial.

            – Mena
            Nov 13 '18 at 14:30













          • I tried with ObjectMapper, but it's expecting to return the same POJO object from the deserialize method. Is there any other way by modifying at property level through deserialization?

            – anz
            Nov 13 '18 at 14:49
















          0














          You can hijack that logic in your public setter.
          For instance:



          class MyPojo {
          // no need for this annotation here actually, covered by setter
          // at least for deserialization
          @JsonProperty
          String email;
          String first;
          String last;

          @JsonProperty("email")
          public void setEmail(String email) {
          this.email = email;
          String split = email.split("@");
          // TODO check length etc.
          this.first = split[0];
          this.last = split[1];
          }
          // just for testing
          @Override
          public String toString() {
          return String.format(
          "email: %s, first: %s, last: %s%n", email, first, last
          );
          }
          }


          Then, somewhere else...



          String json = "{ "email": "xyz@hello.com"}";
          ObjectMapper om = new ObjectMapper();
          MyPojo pojo = om.readValue(json, MyPojo.class);
          System.out.println(pojo);


          Output



          email: xyz@hello.com, first: xyz, last: hello.com





          share|improve this answer


























          • Unfortunately, I am using an auto-generated code with abstract methods in my base pojo. So cannot hijack setter here.

            – anz
            Nov 13 '18 at 14:20






          • 1





            @anz then you could use a custom de-serializer, and set the first and last values there. This would require that you have setter methods for those, and of course will be a lot more tedious than just editing your pojo...

            – Mena
            Nov 13 '18 at 14:22











          • Even then, how can the deserializer will get invoked? I don't have those properties present in the json.

            – anz
            Nov 13 '18 at 14:25











          • @anz you don't need to annotate you pojo with @JsonDeserialize if you can't modify it, you can register the de-serializer to your ObjectMapper. Edit take a look here if you're looking for a decent tutorial.

            – Mena
            Nov 13 '18 at 14:30













          • I tried with ObjectMapper, but it's expecting to return the same POJO object from the deserialize method. Is there any other way by modifying at property level through deserialization?

            – anz
            Nov 13 '18 at 14:49














          0












          0








          0







          You can hijack that logic in your public setter.
          For instance:



          class MyPojo {
          // no need for this annotation here actually, covered by setter
          // at least for deserialization
          @JsonProperty
          String email;
          String first;
          String last;

          @JsonProperty("email")
          public void setEmail(String email) {
          this.email = email;
          String split = email.split("@");
          // TODO check length etc.
          this.first = split[0];
          this.last = split[1];
          }
          // just for testing
          @Override
          public String toString() {
          return String.format(
          "email: %s, first: %s, last: %s%n", email, first, last
          );
          }
          }


          Then, somewhere else...



          String json = "{ "email": "xyz@hello.com"}";
          ObjectMapper om = new ObjectMapper();
          MyPojo pojo = om.readValue(json, MyPojo.class);
          System.out.println(pojo);


          Output



          email: xyz@hello.com, first: xyz, last: hello.com





          share|improve this answer















          You can hijack that logic in your public setter.
          For instance:



          class MyPojo {
          // no need for this annotation here actually, covered by setter
          // at least for deserialization
          @JsonProperty
          String email;
          String first;
          String last;

          @JsonProperty("email")
          public void setEmail(String email) {
          this.email = email;
          String split = email.split("@");
          // TODO check length etc.
          this.first = split[0];
          this.last = split[1];
          }
          // just for testing
          @Override
          public String toString() {
          return String.format(
          "email: %s, first: %s, last: %s%n", email, first, last
          );
          }
          }


          Then, somewhere else...



          String json = "{ "email": "xyz@hello.com"}";
          ObjectMapper om = new ObjectMapper();
          MyPojo pojo = om.readValue(json, MyPojo.class);
          System.out.println(pojo);


          Output



          email: xyz@hello.com, first: xyz, last: hello.com






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 13 '18 at 14:20

























          answered Nov 13 '18 at 14:19









          MenaMena

          40.2k116679




          40.2k116679













          • Unfortunately, I am using an auto-generated code with abstract methods in my base pojo. So cannot hijack setter here.

            – anz
            Nov 13 '18 at 14:20






          • 1





            @anz then you could use a custom de-serializer, and set the first and last values there. This would require that you have setter methods for those, and of course will be a lot more tedious than just editing your pojo...

            – Mena
            Nov 13 '18 at 14:22











          • Even then, how can the deserializer will get invoked? I don't have those properties present in the json.

            – anz
            Nov 13 '18 at 14:25











          • @anz you don't need to annotate you pojo with @JsonDeserialize if you can't modify it, you can register the de-serializer to your ObjectMapper. Edit take a look here if you're looking for a decent tutorial.

            – Mena
            Nov 13 '18 at 14:30













          • I tried with ObjectMapper, but it's expecting to return the same POJO object from the deserialize method. Is there any other way by modifying at property level through deserialization?

            – anz
            Nov 13 '18 at 14:49



















          • Unfortunately, I am using an auto-generated code with abstract methods in my base pojo. So cannot hijack setter here.

            – anz
            Nov 13 '18 at 14:20






          • 1





            @anz then you could use a custom de-serializer, and set the first and last values there. This would require that you have setter methods for those, and of course will be a lot more tedious than just editing your pojo...

            – Mena
            Nov 13 '18 at 14:22











          • Even then, how can the deserializer will get invoked? I don't have those properties present in the json.

            – anz
            Nov 13 '18 at 14:25











          • @anz you don't need to annotate you pojo with @JsonDeserialize if you can't modify it, you can register the de-serializer to your ObjectMapper. Edit take a look here if you're looking for a decent tutorial.

            – Mena
            Nov 13 '18 at 14:30













          • I tried with ObjectMapper, but it's expecting to return the same POJO object from the deserialize method. Is there any other way by modifying at property level through deserialization?

            – anz
            Nov 13 '18 at 14:49

















          Unfortunately, I am using an auto-generated code with abstract methods in my base pojo. So cannot hijack setter here.

          – anz
          Nov 13 '18 at 14:20





          Unfortunately, I am using an auto-generated code with abstract methods in my base pojo. So cannot hijack setter here.

          – anz
          Nov 13 '18 at 14:20




          1




          1





          @anz then you could use a custom de-serializer, and set the first and last values there. This would require that you have setter methods for those, and of course will be a lot more tedious than just editing your pojo...

          – Mena
          Nov 13 '18 at 14:22





          @anz then you could use a custom de-serializer, and set the first and last values there. This would require that you have setter methods for those, and of course will be a lot more tedious than just editing your pojo...

          – Mena
          Nov 13 '18 at 14:22













          Even then, how can the deserializer will get invoked? I don't have those properties present in the json.

          – anz
          Nov 13 '18 at 14:25





          Even then, how can the deserializer will get invoked? I don't have those properties present in the json.

          – anz
          Nov 13 '18 at 14:25













          @anz you don't need to annotate you pojo with @JsonDeserialize if you can't modify it, you can register the de-serializer to your ObjectMapper. Edit take a look here if you're looking for a decent tutorial.

          – Mena
          Nov 13 '18 at 14:30







          @anz you don't need to annotate you pojo with @JsonDeserialize if you can't modify it, you can register the de-serializer to your ObjectMapper. Edit take a look here if you're looking for a decent tutorial.

          – Mena
          Nov 13 '18 at 14:30















          I tried with ObjectMapper, but it's expecting to return the same POJO object from the deserialize method. Is there any other way by modifying at property level through deserialization?

          – anz
          Nov 13 '18 at 14:49





          I tried with ObjectMapper, but it's expecting to return the same POJO object from the deserialize method. Is there any other way by modifying at property level through deserialization?

          – anz
          Nov 13 '18 at 14:49


















          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%2f53282961%2fjson-deserialiser-to-set-custom-properties-in-pojo%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

          Florida Star v. B. J. F.

          Danny Elfman

          Lugert, Oklahoma