Save “state” of an Object in use, for further comparison (No Serialization)
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
add a comment |
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
add a comment |
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
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
java object compare comparison state
edited Nov 19 '18 at 9:24
PiKort
asked Nov 18 '18 at 23:49
PiKortPiKort
103
103
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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.
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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