Map JSON to existing pojo object










0















I have recently joined the web services world and have started to work on create and update of hibernate entities using json inputs.



The following api converts a json input to new pojo object:



Pojo newObject=mapper.readValue(jsonInput,Pojo.class);


This work well with create apis.



Now what about update apis:



I have a big pojo and i don't want to get into long method setting each value into pojo object from json input



i want something like:



Pojo existingPojo=getFromDatabase();

existingPojo=mapper.readValue(updateJsonValues,existingPojo);

saveToDatabase(existingPojo);


So whatever attributes updateJsonValues has ,they get updated into existingPojo.



This would be great help.Thanks in advance.










share|improve this question

















  • 2





    You can use BeanUtils or mapstruct library.

    – Raheela Aslam
    Nov 15 '18 at 12:35






  • 1





    You can just overwrite your existing POJO with a new POJO and the same id, same outcome

    – T A
    Nov 15 '18 at 12:36











  • Agree (with @TA ), overwrite is the way to go. Why do you want to read the existing pojo values from the DB and then update it?

    – Ankur Chrungoo
    Nov 15 '18 at 12:37
















0















I have recently joined the web services world and have started to work on create and update of hibernate entities using json inputs.



The following api converts a json input to new pojo object:



Pojo newObject=mapper.readValue(jsonInput,Pojo.class);


This work well with create apis.



Now what about update apis:



I have a big pojo and i don't want to get into long method setting each value into pojo object from json input



i want something like:



Pojo existingPojo=getFromDatabase();

existingPojo=mapper.readValue(updateJsonValues,existingPojo);

saveToDatabase(existingPojo);


So whatever attributes updateJsonValues has ,they get updated into existingPojo.



This would be great help.Thanks in advance.










share|improve this question

















  • 2





    You can use BeanUtils or mapstruct library.

    – Raheela Aslam
    Nov 15 '18 at 12:35






  • 1





    You can just overwrite your existing POJO with a new POJO and the same id, same outcome

    – T A
    Nov 15 '18 at 12:36











  • Agree (with @TA ), overwrite is the way to go. Why do you want to read the existing pojo values from the DB and then update it?

    – Ankur Chrungoo
    Nov 15 '18 at 12:37














0












0








0


1






I have recently joined the web services world and have started to work on create and update of hibernate entities using json inputs.



The following api converts a json input to new pojo object:



Pojo newObject=mapper.readValue(jsonInput,Pojo.class);


This work well with create apis.



Now what about update apis:



I have a big pojo and i don't want to get into long method setting each value into pojo object from json input



i want something like:



Pojo existingPojo=getFromDatabase();

existingPojo=mapper.readValue(updateJsonValues,existingPojo);

saveToDatabase(existingPojo);


So whatever attributes updateJsonValues has ,they get updated into existingPojo.



This would be great help.Thanks in advance.










share|improve this question














I have recently joined the web services world and have started to work on create and update of hibernate entities using json inputs.



The following api converts a json input to new pojo object:



Pojo newObject=mapper.readValue(jsonInput,Pojo.class);


This work well with create apis.



Now what about update apis:



I have a big pojo and i don't want to get into long method setting each value into pojo object from json input



i want something like:



Pojo existingPojo=getFromDatabase();

existingPojo=mapper.readValue(updateJsonValues,existingPojo);

saveToDatabase(existingPojo);


So whatever attributes updateJsonValues has ,they get updated into existingPojo.



This would be great help.Thanks in advance.







java json jackson






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 12:33









nsharmansharma

688




688







  • 2





    You can use BeanUtils or mapstruct library.

    – Raheela Aslam
    Nov 15 '18 at 12:35






  • 1





    You can just overwrite your existing POJO with a new POJO and the same id, same outcome

    – T A
    Nov 15 '18 at 12:36











  • Agree (with @TA ), overwrite is the way to go. Why do you want to read the existing pojo values from the DB and then update it?

    – Ankur Chrungoo
    Nov 15 '18 at 12:37













  • 2





    You can use BeanUtils or mapstruct library.

    – Raheela Aslam
    Nov 15 '18 at 12:35






  • 1





    You can just overwrite your existing POJO with a new POJO and the same id, same outcome

    – T A
    Nov 15 '18 at 12:36











  • Agree (with @TA ), overwrite is the way to go. Why do you want to read the existing pojo values from the DB and then update it?

    – Ankur Chrungoo
    Nov 15 '18 at 12:37








2




2





You can use BeanUtils or mapstruct library.

– Raheela Aslam
Nov 15 '18 at 12:35





You can use BeanUtils or mapstruct library.

– Raheela Aslam
Nov 15 '18 at 12:35




1




1





You can just overwrite your existing POJO with a new POJO and the same id, same outcome

– T A
Nov 15 '18 at 12:36





You can just overwrite your existing POJO with a new POJO and the same id, same outcome

– T A
Nov 15 '18 at 12:36













Agree (with @TA ), overwrite is the way to go. Why do you want to read the existing pojo values from the DB and then update it?

– Ankur Chrungoo
Nov 15 '18 at 12:37






Agree (with @TA ), overwrite is the way to go. Why do you want to read the existing pojo values from the DB and then update it?

– Ankur Chrungoo
Nov 15 '18 at 12:37













2 Answers
2






active

oldest

votes


















1














The story is that this is what ObjectMapper-like things inherently do all the time, there is no other way: an object is instantiated first, and then it is updated from the JSON.

The only obstacle is that there is no readValue()-like shortcut for it (it could be something like updateValue()), so it is a few character longer, you need to use readerForUpdating() to get a suitable reader, and then its readValue():



import com.fasterxml.jackson.databind.ObjectMapper;

public class Test
public String message="Nope";
public String target="Nope";
public String toString()
return message+" "+target+"!";


public static void main(String args) throws Exception
Test test=new Test();
System.out.println(test);
ObjectMapper mapper=new ObjectMapper();
mapper.readerForUpdating(test).readValue(""message":"Hello"");
System.out.println(test);
mapper.readerForUpdating(test).readValue(""target":"World"");
System.out.println(test);




Output:




Nope Nope!
Hello Nope!
Hello World!






Edit: if it is needed repeatedly, the reader can be stored and re-used of course:

ObjectReader reader=mapper.readerForUpdating(test);
reader.readValue(""message":"Hello"");
System.out.println(test);
reader.readValue(""target":"World"");
System.out.println(test);





share|improve this answer

























  • This works good for me. Thanks for the quick help!

    – nsharma
    Nov 18 '18 at 7:24


















0














I was having the same question and after a lot of digging into the dirt I came across an open source library, MapStruct. It helps in java beans mapping and generates code for you at the time your application boots up. It worked fine for me. Give it a go.






share|improve this answer






















    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%2f53319618%2fmap-json-to-existing-pojo-object%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    The story is that this is what ObjectMapper-like things inherently do all the time, there is no other way: an object is instantiated first, and then it is updated from the JSON.

    The only obstacle is that there is no readValue()-like shortcut for it (it could be something like updateValue()), so it is a few character longer, you need to use readerForUpdating() to get a suitable reader, and then its readValue():



    import com.fasterxml.jackson.databind.ObjectMapper;

    public class Test
    public String message="Nope";
    public String target="Nope";
    public String toString()
    return message+" "+target+"!";


    public static void main(String args) throws Exception
    Test test=new Test();
    System.out.println(test);
    ObjectMapper mapper=new ObjectMapper();
    mapper.readerForUpdating(test).readValue(""message":"Hello"");
    System.out.println(test);
    mapper.readerForUpdating(test).readValue(""target":"World"");
    System.out.println(test);




    Output:




    Nope Nope!
    Hello Nope!
    Hello World!






    Edit: if it is needed repeatedly, the reader can be stored and re-used of course:

    ObjectReader reader=mapper.readerForUpdating(test);
    reader.readValue(""message":"Hello"");
    System.out.println(test);
    reader.readValue(""target":"World"");
    System.out.println(test);





    share|improve this answer

























    • This works good for me. Thanks for the quick help!

      – nsharma
      Nov 18 '18 at 7:24















    1














    The story is that this is what ObjectMapper-like things inherently do all the time, there is no other way: an object is instantiated first, and then it is updated from the JSON.

    The only obstacle is that there is no readValue()-like shortcut for it (it could be something like updateValue()), so it is a few character longer, you need to use readerForUpdating() to get a suitable reader, and then its readValue():



    import com.fasterxml.jackson.databind.ObjectMapper;

    public class Test
    public String message="Nope";
    public String target="Nope";
    public String toString()
    return message+" "+target+"!";


    public static void main(String args) throws Exception
    Test test=new Test();
    System.out.println(test);
    ObjectMapper mapper=new ObjectMapper();
    mapper.readerForUpdating(test).readValue(""message":"Hello"");
    System.out.println(test);
    mapper.readerForUpdating(test).readValue(""target":"World"");
    System.out.println(test);




    Output:




    Nope Nope!
    Hello Nope!
    Hello World!






    Edit: if it is needed repeatedly, the reader can be stored and re-used of course:

    ObjectReader reader=mapper.readerForUpdating(test);
    reader.readValue(""message":"Hello"");
    System.out.println(test);
    reader.readValue(""target":"World"");
    System.out.println(test);





    share|improve this answer

























    • This works good for me. Thanks for the quick help!

      – nsharma
      Nov 18 '18 at 7:24













    1












    1








    1







    The story is that this is what ObjectMapper-like things inherently do all the time, there is no other way: an object is instantiated first, and then it is updated from the JSON.

    The only obstacle is that there is no readValue()-like shortcut for it (it could be something like updateValue()), so it is a few character longer, you need to use readerForUpdating() to get a suitable reader, and then its readValue():



    import com.fasterxml.jackson.databind.ObjectMapper;

    public class Test
    public String message="Nope";
    public String target="Nope";
    public String toString()
    return message+" "+target+"!";


    public static void main(String args) throws Exception
    Test test=new Test();
    System.out.println(test);
    ObjectMapper mapper=new ObjectMapper();
    mapper.readerForUpdating(test).readValue(""message":"Hello"");
    System.out.println(test);
    mapper.readerForUpdating(test).readValue(""target":"World"");
    System.out.println(test);




    Output:




    Nope Nope!
    Hello Nope!
    Hello World!






    Edit: if it is needed repeatedly, the reader can be stored and re-used of course:

    ObjectReader reader=mapper.readerForUpdating(test);
    reader.readValue(""message":"Hello"");
    System.out.println(test);
    reader.readValue(""target":"World"");
    System.out.println(test);





    share|improve this answer















    The story is that this is what ObjectMapper-like things inherently do all the time, there is no other way: an object is instantiated first, and then it is updated from the JSON.

    The only obstacle is that there is no readValue()-like shortcut for it (it could be something like updateValue()), so it is a few character longer, you need to use readerForUpdating() to get a suitable reader, and then its readValue():



    import com.fasterxml.jackson.databind.ObjectMapper;

    public class Test
    public String message="Nope";
    public String target="Nope";
    public String toString()
    return message+" "+target+"!";


    public static void main(String args) throws Exception
    Test test=new Test();
    System.out.println(test);
    ObjectMapper mapper=new ObjectMapper();
    mapper.readerForUpdating(test).readValue(""message":"Hello"");
    System.out.println(test);
    mapper.readerForUpdating(test).readValue(""target":"World"");
    System.out.println(test);




    Output:




    Nope Nope!
    Hello Nope!
    Hello World!






    Edit: if it is needed repeatedly, the reader can be stored and re-used of course:

    ObjectReader reader=mapper.readerForUpdating(test);
    reader.readValue(""message":"Hello"");
    System.out.println(test);
    reader.readValue(""target":"World"");
    System.out.println(test);






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 15 '18 at 15:58

























    answered Nov 15 '18 at 15:52









    tevemadartevemadar

    4,3932725




    4,3932725












    • This works good for me. Thanks for the quick help!

      – nsharma
      Nov 18 '18 at 7:24

















    • This works good for me. Thanks for the quick help!

      – nsharma
      Nov 18 '18 at 7:24
















    This works good for me. Thanks for the quick help!

    – nsharma
    Nov 18 '18 at 7:24





    This works good for me. Thanks for the quick help!

    – nsharma
    Nov 18 '18 at 7:24













    0














    I was having the same question and after a lot of digging into the dirt I came across an open source library, MapStruct. It helps in java beans mapping and generates code for you at the time your application boots up. It worked fine for me. Give it a go.






    share|improve this answer



























      0














      I was having the same question and after a lot of digging into the dirt I came across an open source library, MapStruct. It helps in java beans mapping and generates code for you at the time your application boots up. It worked fine for me. Give it a go.






      share|improve this answer

























        0












        0








        0







        I was having the same question and after a lot of digging into the dirt I came across an open source library, MapStruct. It helps in java beans mapping and generates code for you at the time your application boots up. It worked fine for me. Give it a go.






        share|improve this answer













        I was having the same question and after a lot of digging into the dirt I came across an open source library, MapStruct. It helps in java beans mapping and generates code for you at the time your application boots up. It worked fine for me. Give it a go.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 13:46









        abj1305abj1305

        18310




        18310



























            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%2f53319618%2fmap-json-to-existing-pojo-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







            Popular posts from this blog

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            政党

            天津地下鉄3号線