8

As stated this happens in Safari while in Chrome and Firefox it's working fine.

I think it's because the object that's turned into json has two properties that contain a moment object. I changed them to date object and the stringify function passed.

The strange thing is that if I try doing JSON.stringify(moment()) it is working, so I am not sure how to debug this and find where the issue is.

Here is a screenshot of the error in Safari:

error

Edit:

After some debugging I noticed that this error happens after a dhtmlxwindow onclose event. I load a partial view in a dhtmlx window and in there I create this object which gets pushed into a list intialized into the parent view before closing the window.

Before I close the window I do JSON.stringify(parent.addedContracts) and it's working fine after the onclose event happens (I don't have override code there) the list has that same object (I checked all properties) but the stringify fails for the same list.

Edit:

Creation of the object that's get added to the list goes like this:

var contractStartDate = moment(contractStartDateCalendar.getDate(true), "L");
var contractEndDate = moment(contractEndDateCalendar.getDate(true), "L");

var newContract = {
    Id                  : uniqueId,
    FunctionDesc        : $("#contractFunction").val(),
    ContractHours       : $("#contractHours").val(),
    AdditionalCostFactor: $("#contractAdditionalCostFactor").val().replace(',', '.'),
    VacationFormula     : contractHolidayCostFactor,
    StartDate           : contractStartDate,
    EndDate             : contractEndDate,
    Notes               : Encoder.htmlEncode(tinyMCE.get('contractNotes').getContent()),
    DaysOfWeek          : workingDaysString,
    PlusMinus           : $("#contractTypeDropdown option:selected").data("plusminus"),
    SalaryCalculation   : $("#contractTypeDropdown option:selected").data("salarycalculation"),
    ContractTypeId      : $("#contractTypeDropdown").val(),
    ContractTypeName    : $("#contractTypeDropdown option:selected").text(),
    UploadedImageUrl    : uploadedSignedContractUrl
};

parent.addedContracts.push(newContract);

After this line the JSON.stringify is ok, but when the close event happens on the dhtmlxwindow something changes and the stringify fails.

polarblau
  • 17,649
  • 7
  • 63
  • 84
Aleks
  • 3,906
  • 3
  • 24
  • 29
  • You could try stringifying the moments in the object before stringifying it? – springbo Jul 17 '14 at 13:09
  • That would fix it but i don't want to pass all the objects and their properties before saving it because i know it should work and that will just slow down the save process. – Aleks Jul 17 '14 at 13:19
  • Just to cover the basics, what version of Safari are you trying this in? – J E Carter II Jul 17 '14 at 13:38
  • There are also some documented cautions on String from Moment objects: http://momentjs.com/docs/#/parsing/string/ – J E Carter II Jul 17 '14 at 13:40
  • But the _isValid property is true and this is creating moment from string so this is not my case. I am using 5.1.7 version on windows 8. – Aleks Jul 17 '14 at 13:58
  • How are you generating the JSON object? _isValid as a property set on a JSON string generated elsewhere wouldn't mean anything for the actual run-time validity of the object. It would be valuable, however, if you are generating your moment object, then adding it to the JSON object on the client side. This is the sort of thing that would catch me while I'm "in the trenches" so I thought it worth confirming. – J E Carter II Jul 17 '14 at 14:23
  • @JECarterII made a second edit. – Aleks Jul 17 '14 at 14:29
  • Next question... is the JSON string in the scope of the dhtml window? Could it be going out of scope as you are trying to access it? – J E Carter II Jul 17 '14 at 15:16
  • Maybe include some portion of your script above that is closing the dhtmlxwindow. – J E Carter II Jul 17 '14 at 15:39
  • 4
    Make a fiddle or give us a live link where the problem is reproduced so we don't have to waste time setting this all up to help you. This problem is sort of odd and it would be silly to try to debug without actually debugging. –  Jul 28 '14 at 15:59
  • It's not clear to me what you are stringifying but do you understand you can't stringify an object if it links to window, if a geter might fail, or if it's not acyclic ? Related : http://stackoverflow.com/questions/13861254/json-stringify-deep-objects – Denys Séguret Jul 28 '14 at 16:04

1 Answers1

1

Regardless of your implementation (and since I can't check an example of your code that works in Safari but fails in Chrome), if you think the implementation of JSON is at fault in Safari, consider using a JSON polyfill https://bestiejs.github.io/json3/

As for future questions, please consider showing us minimal examples of what is not working. Not only will this possibly reveal the answer you were seeking, if it doesn't it will make it easier for us to depict the problem.

froginvasion
  • 833
  • 6
  • 19