JSON.stringify(obj) and JSON.parse(str) are new to 6.1 because of the updated Rhino engine. We moved to 6.1 just for this as much as anything else (and ended up liking a ton of other 6.1 features) because in our CMS we do this stuff all the time.
If you insist in 6.0 or earlier, you can use plugins.serialize.toJSON(obj) -- the trick is to know the issues with types and then the pattern to the "crap" (I agree) it produces when applying JSON.parse(str) to it client-side.
1- Properties will have an underscore at the beginning when parsed client-side:
- Code: Select all
// server
var x = {
defaultView : 'agendaWeek',
firstDay: 1, //0 = sunday 1= monday
weekends: "true", // I changed value to a string
allDayText: 'gehele dag'
}
return plugins.serialize.toJSON(x)
// web client
var x = JSON.parse(str)
console.log(x._defaultView) // "agendaWeek"
console.log(x._firstDay) // "1"
console.log(x._weekends) // "true"
console.log(x._allDayText) "gehele dag"
2- Change boolean value to strings before serializing...ie, true becomes "true".
3- Change number values to strings before serializing. Do this if you need to make sure to get the correct rounding. Integers ok to leave as integers.
4- Change dates to some string equivalent before serializing.
After de-serializing on the client-side, you will need to convert strings back into appropriate types if you're going to further use them as javascript objects (calculations and such). Handling dates this way is especially tedious.
Pain in the ass eh? So move to 6.1