Skip to content

reviver visits elements added to an array during revival #390

Open

Description

@jordanbtucker

When a reviver adds elements to the array it's being called on, JSON5.parse visits the new elements too. JSON.parse reads the array length once before revival, so it skips them.

const reviver = function (key, value) {
    if (key === '0') this.push(10)
    return typeof value === 'number' ? value * 2 : value
}

JSON.parse('[1]', reviver)  // [ 2, 10 ]
JSON5.parse('[1]', reviver) // [ 2, 20 ]

internalize in lib/parse.js re-reads value.length on each iteration. Reading it once before the loop would match JSON.parse. This is the parse-side version of #371.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions