On May 5, 5:57 am, "Jon Paal [MSMD]" <Jon nospam Paal @ everywhere dot
comwrote:
thanks,
this will work, and if I still need formal json I'll go back to hard coding names....
Cool.
It's not stated clearly enough at json.org :
http://tools.ietf.org/html/rfc4627 : line 75 :
"A JSON text is a serialized object *** or array. ***"
But what parses as valid json ?
<html>
<head>
<style>
bad {
color: red;
}
blue {
color: blue;
}
</style>
<script src="http://www.JSON.org/json2.js">
/*
Please do NOT link to json.org,
use your own copy instead.
*/
</script>
</head>
<body>
<!--567890123456789 012345678901234 567890123456789 012345678901234 567890
-->
<script>
(function () {
var testIt = function (p) {
var i, name = prefix = msg = "";
var datesAsObjects = function (key, value) {
/*
see the reviver function in the source code :
json.org/json2.js line ~104..
this is an example function that intercepts Date(mm/dd/yyyy)
and turns it into a date object instead of a string.
It's NOT part of the json standard, it's just a quick
example of a reviver function. (and it has a bug).
*/
var d;
if (typeof value === 'string' &&
value.slice(0, 5) === 'Date(' &&
value.slice(-1) === ')') {
d = new Date(value.slic e(5, -1));
if (d) {
return d;
}
}
return value;
}
try {
data = JSON.parse(p, datesAsObjects) ;
/*
See the source : json.org/json2.js
*/
if (typeof data === 'object') {
if (data.construct or === Array) {
msg = "an object : Array : ";
for (i=0;i<data.len gth;i++) {
msg += prefix+"e["+i+"]: "+typeof data[i]+" = "+data[i];
prefix = ", ";
}
} else if (data.construct or === Object) {
msg = "an object : Object : ";
for (name in data) {
if (data.hasOwnPro perty(name)) {
msg += prefix+name+": "+typeof data[name]+" = "+data[name];
}
}
} else {
msg="an object whose constructor is : "+data.construc tor;
}
} else {
msg = "a " + typeof data + ": " + data;
}
document.write( "The string "+p+" was parsed as");
document.write( " VALID json.\nIt produced " + msg + "<br>");
} catch (e) {
document.write( "<bad>The string "+p+" is NOT");
document.write( " valid json.</bad><br>");
}
};
/*
In theory, just arrays and objects,
in practice :
let's see what's what (valid json?) :
*/
testIt('01/15/2008'); testIt('[01/15/2008]');
testIt('15/01/2008'); testIt('[15/01/2008]');
testIt('"01/15/2008"'); testIt('["01/15/2008"]');
testIt('"15/01/2008"'); testIt('["15/01/2008"]');
testIt('Date(15/01/2008)'); testIt('[Date(15/01/2008)]');
testIt('Date(01/15/2008)'); testIt('[Date(01/15/2008)]');
/*
the next one gives the date wrong.
*/
document.write( "<blue>");
testIt('"Date(1 5/01/2008)"'); testIt('["Date(15/01/2008)"]');
document.write( "</blue>");
testIt('"Date(0 1/15/2008)"'); testIt('["Date(01/15/2008)"]');
testIt('true'); testIt('[true]');
testIt('True'); testIt('[True]');
testIt('false') ; testIt('[false]');
testIt('False') ; testIt('[False]');
testIt('null'); testIt('[null]');
testIt('Null'); testIt('[Null]');
testIt('"true"' ); testIt('["true"]');
testIt('"True"' ); testIt('["True"]');
testIt('"false" '); testIt('["false"]');
testIt('"False" '); testIt('["False"]');
testIt('"null"' ); testIt('["null"]');
testIt('"Null"' ); testIt('["Null"]');
testIt('Hi there !'); testIt('[Hi there !]');
testIt('"Hi there !"'); testIt('["Hi there !"]');
testIt('99'); testIt('[99]');
testIt('99.98') ; testIt('[99.98]');
testIt('9.98e-16'); testIt('[9.98e-16]');
testIt('"99"'); testIt('["99"]');
testIt('"99.98" '); testIt('["99.98"]');
testIt('"9.98e-16"'); testIt('["9.98e-16"]');
testIt('[["Nancy","Davoli o"],["Jon","Paal "]]');
/*
the next one gives an invalid date.
*/
document.write( "<blue>");
testIt('["Date(01/15/2008)","Date()" ,"Date","99" ,99]');
document.write( "</blue>");
testIt('{"aProp erty":99}');
testIt('"functi on(){alert(wind ow.location.hre f)}"');
testIt('functio n(){alert(windo w.location.href )}');
testIt('"window .aGlobalVar=99" '); testIt('window. aGlobalVar=99') ;
/*
HTH
--Jorge.
*/
})();
</script>
</body>
</html>