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

diffJson with dates, returns empty curly braces #102

Closed
dr-dimitru opened this issue Feb 13, 2016 · 6 comments
Closed

diffJson with dates, returns empty curly braces #102

dr-dimitru opened this issue Feb 13, 2016 · 6 comments

Comments

@dr-dimitru
Copy link

Example:

var after = {
  "fingerprint" : "string1",
  "valid_to" : new Date("2016-08-14T23:59:59Z"),
  "valid_from" : new Date("2016-02-12T00:00:00Z")
};

var before = {
  "fingerprint" : "string 2",
  "valid_from" : new Date("2016-02-12T00:00:00Z"),
  "valid_to" : new Date("2016-02-20T03:58:53Z")
};

JsDiff.diffJson(before, after);

Result:

[
  {
    "count": 1,
    "value": "{\n"
  },
  {
    "count": 1,
    "removed": true,
    "value": "  \"fingerprint\": \"string 2\",\n"
  },
  {
    "count": 1,
    "added": true,
    "value": "  \"fingerprint\": \"string1\",\n"
  },
  {
    "count": 3,
    "value": "  \"valid_from\": {},\n  \"valid_to\": {}\n}"
  }
]

Expected result:

[
  {
    "count": 1,
    "value": "{\n"
  },
  {
    "count": 1,
    "removed": true,
    "value": "  \"fingerprint\": \"string 2\",\n"
  },
  {
    "count": 2,
    "added": true,
    "value": "  \"fingerprint\": \"string 1\",\n  \"valid_to\": \"2016-11-20T03:58:53.000Z\",\n"
  },
  {
    "count": 1,
    "value": "  \"valid_from\": \"2015-11-20T01:36:42.000Z\",\n"
  },
  {
    "count": 1,
    "removed": true,
    "value": "  \"valid_to\": \"2016-02-14T03:58:53.000Z\"\n"
  },
  {
    "count": 1,
    "value": "}"
  }
]
@dr-dimitru
Copy link
Author

Maybe I'm missing something...

@kpdecker
Copy link
Owner

Date's can not be expressed in json, unless converted to a string or other primitive format. This is operating as expected, unfortunately.

@dr-dimitru
Copy link
Author

Sorry, but I'm emphatically disagree, see next test:

var obj = {str: 'asd', date: new Date()};
JSON.stringify(obj);
// result:
// "{\"str\":\"asd\",\"date\":\"2016-02-24T10:41:40.220Z\"}"

@kpdecker
Copy link
Owner

My comment that Date is not supported in JSON stands, but we should add some sort of toString operation to our JSON serializer to match the failover pattern of the standard serializer.

@kpdecker kpdecker reopened this Feb 24, 2016
@dr-dimitru
Copy link
Author

@kpdecker thank you. It should call toString on all objects, as it may be some custom class, what actually is happiness inside JSON.stringify() and objects without toString method returns as [Object object]

@kpdecker
Copy link
Owner

Released in 2.2.2

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

No branches or pull requests

2 participants