Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix problem where date comparison always yields a patch. #2

Closed
wants to merge 1 commit into from
Closed

Conversation

jrogelstad
Copy link

When you do a comparison on an object that includes dates, all dates end up resulting in patches. This is because JSON.parse(JSON.stringify)) is used to generate an internal cache object, but that does not result in an object with identical values. Dates get converted to strings:

d = new Date();
foo = {x: 10, y: "test", z: d};
jsonpatch.observe(foo);
bar = jsonpatch.observe(foo);
jsonpatch.generate(bar);

The proposed fix is imperfect insofar as it does not correct the date to string conversion but instead just rolls with it by giving the object variable to be compared against the original cached version the same treatment. This works for us, and probably most people, because the end goal is to generate JSON patches to be transmitted as text where the dates will end up becoming strings anyway.

This is a really simple and slick implementation of rfc6902 by the way. Thanks for publishing it.

Modify generate function to ensure dates are the same type.
@jrogelstad
Copy link
Author

This fix doesn't work in cases where an item in an array is deleted because the conversion back and forth changes the nature of that as well. The problem still exists, but closing pull request until I have a better solution.

@jrogelstad jrogelstad closed this Apr 27, 2013
@Starcounter-Jack
Copy link
Owner

Hi John,

Thanks for the pull request and the kind words. Let me know if you have any ideas on this. I've added this as an issue in the tracker (#4).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants