Save “state” of an Object in use, for further comparison (No Serialization)












0














    GenericObject MyObj = new GenericObject();

MyObjs.add(); // operation 1

// operation for saving the current state of "MyObj"

MyObjs.add(); // operation 2 (it is the same as 1, repeated)

// Comparison with previous saved state of "MyObj" -> this will return **false**

MyObjs.remove(); // operation 3 (this operation cancel the effect of operation 2)

// Comparison with previous saved state of "MyObj" -> this will return **true**


I would save the "state" of an object, how variables are when I store its state in some way and after use it to compare the future states of the object.



With Serialization I could serialize everytime the Object and than deserialize and compare but I can't use serialization. How is it possible with standard libraries?










share|improve this question





























    0














        GenericObject MyObj = new GenericObject();

    MyObjs.add(); // operation 1

    // operation for saving the current state of "MyObj"

    MyObjs.add(); // operation 2 (it is the same as 1, repeated)

    // Comparison with previous saved state of "MyObj" -> this will return **false**

    MyObjs.remove(); // operation 3 (this operation cancel the effect of operation 2)

    // Comparison with previous saved state of "MyObj" -> this will return **true**


    I would save the "state" of an object, how variables are when I store its state in some way and after use it to compare the future states of the object.



    With Serialization I could serialize everytime the Object and than deserialize and compare but I can't use serialization. How is it possible with standard libraries?










    share|improve this question



























      0












      0








      0







          GenericObject MyObj = new GenericObject();

      MyObjs.add(); // operation 1

      // operation for saving the current state of "MyObj"

      MyObjs.add(); // operation 2 (it is the same as 1, repeated)

      // Comparison with previous saved state of "MyObj" -> this will return **false**

      MyObjs.remove(); // operation 3 (this operation cancel the effect of operation 2)

      // Comparison with previous saved state of "MyObj" -> this will return **true**


      I would save the "state" of an object, how variables are when I store its state in some way and after use it to compare the future states of the object.



      With Serialization I could serialize everytime the Object and than deserialize and compare but I can't use serialization. How is it possible with standard libraries?










      share|improve this question















          GenericObject MyObj = new GenericObject();

      MyObjs.add(); // operation 1

      // operation for saving the current state of "MyObj"

      MyObjs.add(); // operation 2 (it is the same as 1, repeated)

      // Comparison with previous saved state of "MyObj" -> this will return **false**

      MyObjs.remove(); // operation 3 (this operation cancel the effect of operation 2)

      // Comparison with previous saved state of "MyObj" -> this will return **true**


      I would save the "state" of an object, how variables are when I store its state in some way and after use it to compare the future states of the object.



      With Serialization I could serialize everytime the Object and than deserialize and compare but I can't use serialization. How is it possible with standard libraries?







      java object compare comparison state






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 19 '18 at 9:24







      PiKort

















      asked Nov 18 '18 at 23:49









      PiKortPiKort

      103




      103
























          1 Answer
          1






          active

          oldest

          votes


















          0














          You can use Jackson-JSON library to dump an object's state in json format without implementing Serialization interface in your class and again load the object by reading from that json data and mapping back to the object type.



          Simply use ObjectMapper for this purpose.



          Example:



          //do some stuff here with your MyObj object

          ObjectMapper mapper = new ObjectMapper();
          String jsonState1 = mapper.writeValueAsString(MyObj);

          //do some stuff here

          String jsonState2 = mapper.writeValueAsString(MyObj);


          When you want to compare any of them, you can just do:



          GenericObject objState1 = mapper.readValue(jsonState1, GenericObject.class);


          Then do whatever comparison you want to do.



          Hope that helps!!



          Update:




          If you want to use standard Java library only, then you have no other
          choice than using Java Reflection API. With this API you can access
          the class variables and their getter and setter methods, in fact
          everything a class holds. But unfortunately you need to write whole
          bunch of codes to do that. But that's the only choice if you don't
          want to use Java Serialization.







          share|improve this answer























          • Your solution is good, but sorry I updated the question, I must use only Java standard library.
            – PiKort
            Nov 19 '18 at 9:26










          • @PiKort I have updated my answer. Just take a look...
            – Abu Faisal
            Nov 21 '18 at 0:43











          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%2f53366627%2fsave-state-of-an-object-in-use-for-further-comparison-no-serialization%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 use Jackson-JSON library to dump an object's state in json format without implementing Serialization interface in your class and again load the object by reading from that json data and mapping back to the object type.



          Simply use ObjectMapper for this purpose.



          Example:



          //do some stuff here with your MyObj object

          ObjectMapper mapper = new ObjectMapper();
          String jsonState1 = mapper.writeValueAsString(MyObj);

          //do some stuff here

          String jsonState2 = mapper.writeValueAsString(MyObj);


          When you want to compare any of them, you can just do:



          GenericObject objState1 = mapper.readValue(jsonState1, GenericObject.class);


          Then do whatever comparison you want to do.



          Hope that helps!!



          Update:




          If you want to use standard Java library only, then you have no other
          choice than using Java Reflection API. With this API you can access
          the class variables and their getter and setter methods, in fact
          everything a class holds. But unfortunately you need to write whole
          bunch of codes to do that. But that's the only choice if you don't
          want to use Java Serialization.







          share|improve this answer























          • Your solution is good, but sorry I updated the question, I must use only Java standard library.
            – PiKort
            Nov 19 '18 at 9:26










          • @PiKort I have updated my answer. Just take a look...
            – Abu Faisal
            Nov 21 '18 at 0:43
















          0














          You can use Jackson-JSON library to dump an object's state in json format without implementing Serialization interface in your class and again load the object by reading from that json data and mapping back to the object type.



          Simply use ObjectMapper for this purpose.



          Example:



          //do some stuff here with your MyObj object

          ObjectMapper mapper = new ObjectMapper();
          String jsonState1 = mapper.writeValueAsString(MyObj);

          //do some stuff here

          String jsonState2 = mapper.writeValueAsString(MyObj);


          When you want to compare any of them, you can just do:



          GenericObject objState1 = mapper.readValue(jsonState1, GenericObject.class);


          Then do whatever comparison you want to do.



          Hope that helps!!



          Update:




          If you want to use standard Java library only, then you have no other
          choice than using Java Reflection API. With this API you can access
          the class variables and their getter and setter methods, in fact
          everything a class holds. But unfortunately you need to write whole
          bunch of codes to do that. But that's the only choice if you don't
          want to use Java Serialization.







          share|improve this answer























          • Your solution is good, but sorry I updated the question, I must use only Java standard library.
            – PiKort
            Nov 19 '18 at 9:26










          • @PiKort I have updated my answer. Just take a look...
            – Abu Faisal
            Nov 21 '18 at 0:43














          0












          0








          0






          You can use Jackson-JSON library to dump an object's state in json format without implementing Serialization interface in your class and again load the object by reading from that json data and mapping back to the object type.



          Simply use ObjectMapper for this purpose.



          Example:



          //do some stuff here with your MyObj object

          ObjectMapper mapper = new ObjectMapper();
          String jsonState1 = mapper.writeValueAsString(MyObj);

          //do some stuff here

          String jsonState2 = mapper.writeValueAsString(MyObj);


          When you want to compare any of them, you can just do:



          GenericObject objState1 = mapper.readValue(jsonState1, GenericObject.class);


          Then do whatever comparison you want to do.



          Hope that helps!!



          Update:




          If you want to use standard Java library only, then you have no other
          choice than using Java Reflection API. With this API you can access
          the class variables and their getter and setter methods, in fact
          everything a class holds. But unfortunately you need to write whole
          bunch of codes to do that. But that's the only choice if you don't
          want to use Java Serialization.







          share|improve this answer














          You can use Jackson-JSON library to dump an object's state in json format without implementing Serialization interface in your class and again load the object by reading from that json data and mapping back to the object type.



          Simply use ObjectMapper for this purpose.



          Example:



          //do some stuff here with your MyObj object

          ObjectMapper mapper = new ObjectMapper();
          String jsonState1 = mapper.writeValueAsString(MyObj);

          //do some stuff here

          String jsonState2 = mapper.writeValueAsString(MyObj);


          When you want to compare any of them, you can just do:



          GenericObject objState1 = mapper.readValue(jsonState1, GenericObject.class);


          Then do whatever comparison you want to do.



          Hope that helps!!



          Update:




          If you want to use standard Java library only, then you have no other
          choice than using Java Reflection API. With this API you can access
          the class variables and their getter and setter methods, in fact
          everything a class holds. But unfortunately you need to write whole
          bunch of codes to do that. But that's the only choice if you don't
          want to use Java Serialization.








          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 20 '18 at 1:47

























          answered Nov 19 '18 at 0:34









          Abu FaisalAbu Faisal

          14716




          14716












          • Your solution is good, but sorry I updated the question, I must use only Java standard library.
            – PiKort
            Nov 19 '18 at 9:26










          • @PiKort I have updated my answer. Just take a look...
            – Abu Faisal
            Nov 21 '18 at 0:43


















          • Your solution is good, but sorry I updated the question, I must use only Java standard library.
            – PiKort
            Nov 19 '18 at 9:26










          • @PiKort I have updated my answer. Just take a look...
            – Abu Faisal
            Nov 21 '18 at 0:43
















          Your solution is good, but sorry I updated the question, I must use only Java standard library.
          – PiKort
          Nov 19 '18 at 9:26




          Your solution is good, but sorry I updated the question, I must use only Java standard library.
          – PiKort
          Nov 19 '18 at 9:26












          @PiKort I have updated my answer. Just take a look...
          – Abu Faisal
          Nov 21 '18 at 0:43




          @PiKort I have updated my answer. Just take a look...
          – Abu Faisal
          Nov 21 '18 at 0:43


















          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%2f53366627%2fsave-state-of-an-object-in-use-for-further-comparison-no-serialization%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

          mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

          How to change which sound is reproduced for terminal bell?

          Can I use Tabulator js library in my java Spring + Thymeleaf project?