Generate a Json from Strings using Jackson library

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
2
down vote

favorite












I am pretty new to Java and I got a requirement of converting Strings to a json structure. My below codes works fine but I have to create multiple objects and my code looks bit ugly. I want to know the better approach.



I want the below Json Structure from three Strings SourceApplication, messagType and payload



Required Json Structure




"request":
"header":
"sourceApplication": "SomeString"
"messageType": "SomeString"
,
"body":
"payload": "SomeString"





Below is my code.



POJO Class (For mapping)



package com.test.transformer;

import java.io.Serializable;


public class JsonMapper implements Serializable

private Request request;

public Request getRequest ()

return request;


public void setRequest (Request request)

this.request = request;



public static class Request implements Serializable

private Body body;

private Header header;

public Body getBody ()

return body;


public void setBody (Body body)

this.body = body;


public Header getHeader ()

return header;


public void setHeader (Header header)

this.header = header;





public static class Header implements Serializable

private String sourceApplication;
private String messageType;

public String getSourceApplication ()

return sourceApplication;


public void setSourceApplication (String sourceApplication)

this.sourceApplication = sourceApplication;


public String getMessageType ()

return messageType;


public void setMessageType (String messageType)

this.messageType = messageType;




public static class Body implements Serializable

private String payload;

public String getPayload ()

return payload;


public void setPayload (String payload)

this.payload = payload;






My Main Class



package com.test.transformer;

import org.codehaus.jackson.map.ObjectMapper;

import java.io.IOException;


public class JsonTest

public static void main(String args)

ObjectMapper mapper = new ObjectMapper();


String srcApp = "TestSrc";
String msgType = "msgtype";
String payload = "mainMsg";

JsonMapper.Header h = new JsonMapper.Header();
JsonMapper.Body b = new JsonMapper.Body();
JsonMapper.Request r = new JsonMapper.Request();

h.setSourceApplication(srcApp);
h.setMessageType(msgType);
b.setPayload(payload);

r.setHeader(h);
r.setBody(b);

JsonMapper j = new JsonMapper();

j.setRequest(r);

String jsonInString = null;
try
jsonInString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(j);
catch (IOException e)
e.printStackTrace();

System.out.println(jsonInString);








share|improve this question

























    up vote
    2
    down vote

    favorite












    I am pretty new to Java and I got a requirement of converting Strings to a json structure. My below codes works fine but I have to create multiple objects and my code looks bit ugly. I want to know the better approach.



    I want the below Json Structure from three Strings SourceApplication, messagType and payload



    Required Json Structure




    "request":
    "header":
    "sourceApplication": "SomeString"
    "messageType": "SomeString"
    ,
    "body":
    "payload": "SomeString"





    Below is my code.



    POJO Class (For mapping)



    package com.test.transformer;

    import java.io.Serializable;


    public class JsonMapper implements Serializable

    private Request request;

    public Request getRequest ()

    return request;


    public void setRequest (Request request)

    this.request = request;



    public static class Request implements Serializable

    private Body body;

    private Header header;

    public Body getBody ()

    return body;


    public void setBody (Body body)

    this.body = body;


    public Header getHeader ()

    return header;


    public void setHeader (Header header)

    this.header = header;





    public static class Header implements Serializable

    private String sourceApplication;
    private String messageType;

    public String getSourceApplication ()

    return sourceApplication;


    public void setSourceApplication (String sourceApplication)

    this.sourceApplication = sourceApplication;


    public String getMessageType ()

    return messageType;


    public void setMessageType (String messageType)

    this.messageType = messageType;




    public static class Body implements Serializable

    private String payload;

    public String getPayload ()

    return payload;


    public void setPayload (String payload)

    this.payload = payload;






    My Main Class



    package com.test.transformer;

    import org.codehaus.jackson.map.ObjectMapper;

    import java.io.IOException;


    public class JsonTest

    public static void main(String args)

    ObjectMapper mapper = new ObjectMapper();


    String srcApp = "TestSrc";
    String msgType = "msgtype";
    String payload = "mainMsg";

    JsonMapper.Header h = new JsonMapper.Header();
    JsonMapper.Body b = new JsonMapper.Body();
    JsonMapper.Request r = new JsonMapper.Request();

    h.setSourceApplication(srcApp);
    h.setMessageType(msgType);
    b.setPayload(payload);

    r.setHeader(h);
    r.setBody(b);

    JsonMapper j = new JsonMapper();

    j.setRequest(r);

    String jsonInString = null;
    try
    jsonInString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(j);
    catch (IOException e)
    e.printStackTrace();

    System.out.println(jsonInString);








    share|improve this question





















      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I am pretty new to Java and I got a requirement of converting Strings to a json structure. My below codes works fine but I have to create multiple objects and my code looks bit ugly. I want to know the better approach.



      I want the below Json Structure from three Strings SourceApplication, messagType and payload



      Required Json Structure




      "request":
      "header":
      "sourceApplication": "SomeString"
      "messageType": "SomeString"
      ,
      "body":
      "payload": "SomeString"





      Below is my code.



      POJO Class (For mapping)



      package com.test.transformer;

      import java.io.Serializable;


      public class JsonMapper implements Serializable

      private Request request;

      public Request getRequest ()

      return request;


      public void setRequest (Request request)

      this.request = request;



      public static class Request implements Serializable

      private Body body;

      private Header header;

      public Body getBody ()

      return body;


      public void setBody (Body body)

      this.body = body;


      public Header getHeader ()

      return header;


      public void setHeader (Header header)

      this.header = header;





      public static class Header implements Serializable

      private String sourceApplication;
      private String messageType;

      public String getSourceApplication ()

      return sourceApplication;


      public void setSourceApplication (String sourceApplication)

      this.sourceApplication = sourceApplication;


      public String getMessageType ()

      return messageType;


      public void setMessageType (String messageType)

      this.messageType = messageType;




      public static class Body implements Serializable

      private String payload;

      public String getPayload ()

      return payload;


      public void setPayload (String payload)

      this.payload = payload;






      My Main Class



      package com.test.transformer;

      import org.codehaus.jackson.map.ObjectMapper;

      import java.io.IOException;


      public class JsonTest

      public static void main(String args)

      ObjectMapper mapper = new ObjectMapper();


      String srcApp = "TestSrc";
      String msgType = "msgtype";
      String payload = "mainMsg";

      JsonMapper.Header h = new JsonMapper.Header();
      JsonMapper.Body b = new JsonMapper.Body();
      JsonMapper.Request r = new JsonMapper.Request();

      h.setSourceApplication(srcApp);
      h.setMessageType(msgType);
      b.setPayload(payload);

      r.setHeader(h);
      r.setBody(b);

      JsonMapper j = new JsonMapper();

      j.setRequest(r);

      String jsonInString = null;
      try
      jsonInString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(j);
      catch (IOException e)
      e.printStackTrace();

      System.out.println(jsonInString);








      share|improve this question











      I am pretty new to Java and I got a requirement of converting Strings to a json structure. My below codes works fine but I have to create multiple objects and my code looks bit ugly. I want to know the better approach.



      I want the below Json Structure from three Strings SourceApplication, messagType and payload



      Required Json Structure




      "request":
      "header":
      "sourceApplication": "SomeString"
      "messageType": "SomeString"
      ,
      "body":
      "payload": "SomeString"





      Below is my code.



      POJO Class (For mapping)



      package com.test.transformer;

      import java.io.Serializable;


      public class JsonMapper implements Serializable

      private Request request;

      public Request getRequest ()

      return request;


      public void setRequest (Request request)

      this.request = request;



      public static class Request implements Serializable

      private Body body;

      private Header header;

      public Body getBody ()

      return body;


      public void setBody (Body body)

      this.body = body;


      public Header getHeader ()

      return header;


      public void setHeader (Header header)

      this.header = header;





      public static class Header implements Serializable

      private String sourceApplication;
      private String messageType;

      public String getSourceApplication ()

      return sourceApplication;


      public void setSourceApplication (String sourceApplication)

      this.sourceApplication = sourceApplication;


      public String getMessageType ()

      return messageType;


      public void setMessageType (String messageType)

      this.messageType = messageType;




      public static class Body implements Serializable

      private String payload;

      public String getPayload ()

      return payload;


      public void setPayload (String payload)

      this.payload = payload;






      My Main Class



      package com.test.transformer;

      import org.codehaus.jackson.map.ObjectMapper;

      import java.io.IOException;


      public class JsonTest

      public static void main(String args)

      ObjectMapper mapper = new ObjectMapper();


      String srcApp = "TestSrc";
      String msgType = "msgtype";
      String payload = "mainMsg";

      JsonMapper.Header h = new JsonMapper.Header();
      JsonMapper.Body b = new JsonMapper.Body();
      JsonMapper.Request r = new JsonMapper.Request();

      h.setSourceApplication(srcApp);
      h.setMessageType(msgType);
      b.setPayload(payload);

      r.setHeader(h);
      r.setBody(b);

      JsonMapper j = new JsonMapper();

      j.setRequest(r);

      String jsonInString = null;
      try
      jsonInString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(j);
      catch (IOException e)
      e.printStackTrace();

      System.out.println(jsonInString);










      share|improve this question










      share|improve this question




      share|improve this question









      asked Apr 19 at 1:32









      sparker

      132




      132




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          A few things:



          • Use descriptive variable names. A common Java convention is a camelCase version of the class name such as JsonMapper jsonMapper = new JsonMapper();

          • Unwrap your classes into separate files. This makes the code much easier to read and reuse.


          • test in your package name is very unusual, and probably not intended.

          • Test classes typically have the same name as the class they test suffixed with Test, such as JsonMapperTest.

          • Tests are not written in main methods.

          • Having getters and setters for everything is a code smell - not necessarily bad, but often misused. Your application doesn't do anything yet, so it's difficult to tell whether any of them will be necessary, but in general I would not add getters and setters until I know I need them.





          share|improve this answer























          • Thanks for the feedback. Other than standard things, the way I implemented is fine right? I will take care of things you mentioned.
            – sparker
            Apr 19 at 2:02










          • Once you do you could post another question here to get further feedback. It's a bit easier to give feedback when the overall structure is in order.
            – l0b0
            Apr 19 at 2:06










          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          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: "196"
          ;
          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',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f192416%2fgenerate-a-json-from-strings-using-jackson-library%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          A few things:



          • Use descriptive variable names. A common Java convention is a camelCase version of the class name such as JsonMapper jsonMapper = new JsonMapper();

          • Unwrap your classes into separate files. This makes the code much easier to read and reuse.


          • test in your package name is very unusual, and probably not intended.

          • Test classes typically have the same name as the class they test suffixed with Test, such as JsonMapperTest.

          • Tests are not written in main methods.

          • Having getters and setters for everything is a code smell - not necessarily bad, but often misused. Your application doesn't do anything yet, so it's difficult to tell whether any of them will be necessary, but in general I would not add getters and setters until I know I need them.





          share|improve this answer























          • Thanks for the feedback. Other than standard things, the way I implemented is fine right? I will take care of things you mentioned.
            – sparker
            Apr 19 at 2:02










          • Once you do you could post another question here to get further feedback. It's a bit easier to give feedback when the overall structure is in order.
            – l0b0
            Apr 19 at 2:06














          up vote
          1
          down vote



          accepted










          A few things:



          • Use descriptive variable names. A common Java convention is a camelCase version of the class name such as JsonMapper jsonMapper = new JsonMapper();

          • Unwrap your classes into separate files. This makes the code much easier to read and reuse.


          • test in your package name is very unusual, and probably not intended.

          • Test classes typically have the same name as the class they test suffixed with Test, such as JsonMapperTest.

          • Tests are not written in main methods.

          • Having getters and setters for everything is a code smell - not necessarily bad, but often misused. Your application doesn't do anything yet, so it's difficult to tell whether any of them will be necessary, but in general I would not add getters and setters until I know I need them.





          share|improve this answer























          • Thanks for the feedback. Other than standard things, the way I implemented is fine right? I will take care of things you mentioned.
            – sparker
            Apr 19 at 2:02










          • Once you do you could post another question here to get further feedback. It's a bit easier to give feedback when the overall structure is in order.
            – l0b0
            Apr 19 at 2:06












          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          A few things:



          • Use descriptive variable names. A common Java convention is a camelCase version of the class name such as JsonMapper jsonMapper = new JsonMapper();

          • Unwrap your classes into separate files. This makes the code much easier to read and reuse.


          • test in your package name is very unusual, and probably not intended.

          • Test classes typically have the same name as the class they test suffixed with Test, such as JsonMapperTest.

          • Tests are not written in main methods.

          • Having getters and setters for everything is a code smell - not necessarily bad, but often misused. Your application doesn't do anything yet, so it's difficult to tell whether any of them will be necessary, but in general I would not add getters and setters until I know I need them.





          share|improve this answer















          A few things:



          • Use descriptive variable names. A common Java convention is a camelCase version of the class name such as JsonMapper jsonMapper = new JsonMapper();

          • Unwrap your classes into separate files. This makes the code much easier to read and reuse.


          • test in your package name is very unusual, and probably not intended.

          • Test classes typically have the same name as the class they test suffixed with Test, such as JsonMapperTest.

          • Tests are not written in main methods.

          • Having getters and setters for everything is a code smell - not necessarily bad, but often misused. Your application doesn't do anything yet, so it's difficult to tell whether any of them will be necessary, but in general I would not add getters and setters until I know I need them.






          share|improve this answer















          share|improve this answer



          share|improve this answer








          edited Apr 19 at 2:16


























          answered Apr 19 at 1:57









          l0b0

          3,580922




          3,580922











          • Thanks for the feedback. Other than standard things, the way I implemented is fine right? I will take care of things you mentioned.
            – sparker
            Apr 19 at 2:02










          • Once you do you could post another question here to get further feedback. It's a bit easier to give feedback when the overall structure is in order.
            – l0b0
            Apr 19 at 2:06
















          • Thanks for the feedback. Other than standard things, the way I implemented is fine right? I will take care of things you mentioned.
            – sparker
            Apr 19 at 2:02










          • Once you do you could post another question here to get further feedback. It's a bit easier to give feedback when the overall structure is in order.
            – l0b0
            Apr 19 at 2:06















          Thanks for the feedback. Other than standard things, the way I implemented is fine right? I will take care of things you mentioned.
          – sparker
          Apr 19 at 2:02




          Thanks for the feedback. Other than standard things, the way I implemented is fine right? I will take care of things you mentioned.
          – sparker
          Apr 19 at 2:02












          Once you do you could post another question here to get further feedback. It's a bit easier to give feedback when the overall structure is in order.
          – l0b0
          Apr 19 at 2:06




          Once you do you could post another question here to get further feedback. It's a bit easier to give feedback when the overall structure is in order.
          – l0b0
          Apr 19 at 2:06












           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f192416%2fgenerate-a-json-from-strings-using-jackson-library%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Python Lists

          Aion

          JavaScript Array Iteration Methods