Empty arrays from JSON.parse() contain a hidden element
Jay Oster
jay at pubnub.com
Wed Mar 30 23:49:58 UTC 2016
Please disregard. Found the relevant issues in Jira.
JDK9: https://bugs.openjdk.java.net/browse/JDK-8149334
JDK8: https://bugs.openjdk.java.net/browse/JDK-8149443
On Wed, Mar 30, 2016 at 4:34 PM, Jay Oster <jay at pubnub.com> wrote:
> Just a bug report, here. I'll let the jjs session speak for itself.
>
> $ jjs -version
> nashorn 1.8.0_77
> jjs> var a = JSON.parse("[]")
> jjs> a.length
> 0
> jjs> JSON.stringify(a)
> []
> jjs> a.push("foo")
> 1
> jjs> a.length
> 2
> jjs> JSON.stringify(a)
> [0,"foo"]
>
>
> For the moment, I have a workaround that replaces JSON.parse() with a
> function that recursively replaces empty arrays with array literals. Since
> they don't exhibit the same bad behavior:
>
> jjs> var b = []
> jjs> b.length
> 0
> jjs> JSON.stringify(b)
> []
> jjs> b.push("foo")
> 1
> jjs> b.length
> 1
> jjs> JSON.stringify(b)
> ["foo"]
>
>
>
More information about the nashorn-dev
mailing list