I am currently utilizing Oracle JDK 1.8.0_65 with nashorn to execute some test cases, and I have come across a rather peculiar behavior when trying to parse an empty JSON Array.
Below is the script I am running in nashorn:
var testCase = {
start:function() {
// Case 1: a initialized from JavaScript Array
var a = [];
this.log.debug("a before:" + JSON.stringify(a) + " (length:" + a.length + ")");
a.push(15);
this.log.debug("a after:" + JSON.stringify(a) + " (length:" + a.length + ")");
// Case 2: b initialized by parsing a JSON Array
var b = JSON.parse("[]");
this.log.debug("b before:" + JSON.stringify(b) + " (length:" + b.length + ")");
b.push(15);
this.log.debug("b after:" + JSON.stringify(b) + " (length:" + b.length + ")");
}
};
and the resulting output is:
a before:[] (length:0)
a after:[15] (length:1)
b before:[] (length:0)
b after:[0,15] (length:2)
It appears to be a bug within the nashorn JSON parser. The returned Array does not genuinely seem to be empty, as there is a hidden "0" that emerges after the initial push operation.
I have been unable to locate any bug reports regarding this issue. Am I possibly misusing the JSON.parse method?
Thank you. J