undefined not an acceptable parser token
Brought to you by:
aalmiray
JSONTokenizer accepts "true" "false" and "null" but not "undefined".
I have a jquery.json library which produces the value JSON text:
{ "key": undefined }
But this throws the exception:
net.sf.json.JSONException: Unquotted string 'undefined'
at net.sf.json.util.JSONTokener.nextValue(JSONTokener.java:460)
This could trivially be handled by changing line 408 to:
if( s.equals( "null" ) || s.equals( "undefined" ) ){
return JSONNull.getInstance();
}
Technically the value 'undefined' is not valid JSON but valid JavaScript.
I'm thinking that by adding a setJavaScriptCompatible() flag to JsonConfig would be enough to open up the serialization mechanism to be javascript friendly.
FIXED.
Make sure you set jsonConfig.setJavascriptCompliant() to true before serializing. undefined will be treated as JSONNull.