Repository navigation
Conversation
Since 3.4.0 the parse queue swaps every nested container for the shared `ignore` placeholder and calls the reviver on the parent before those placeholders are filled in, so a reviver that reads a nested value (or builds a new object from it) sees empty objects and the revived children are assigned to the discarded instance. Revive every queued container first, then walk the queue backwards to hand each container to the reviver. A child is always queued after its parent, so the reverse order assigns bottom-up without recursion and each reviver receives fully revived nested values, matching JSON.parse. Fixes WebReflection#109
A container is queued before the containers it first references, which is what makes the reverse walk safe; it is not a general parent-before-child order for shared references.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Since 3.4.0,
parse(text, reviver)calls the reviver for an object/array while its nested containers are still the internalignoreplaceholder. A reviver that reads a nested value, or returns a new object built fromvalue, sees{}and the revived children are later assigned to the discarded original instance.Fixes #109.
Root cause
resolverreplaces each not-yet-seen container withignoreand queues{o, k, r}. The queue loop then calls$.call(o, k, revive(r)), butrevive(r)only fills inr's primitive keys, so its own containers are stillignorewhen the reviver runs.Fix
Revive every queued container first, then walk the queue backwards and hand each container to the reviver. A child is always queued after its parent, so the reverse order assigns bottom-up without recursion, and every reviver receives fully revived nested values — the same thing
JSON.parseguarantees.The two loops do the same amount of work as before (one
reviveper queued container, one assignment per queued key), just in a different order. As already noted in #88, the reviver callback order may change; this change makes it closer toJSON.parse, where children are revived before their parent.Tests
Added two cases to
test/index.js:Point(1,2)instead ofPoint(undefined,undefined));log4js'LoggingEvent.deserialise(err.response.status === 500instead of{}).Both fail on
mainwith the current assertions and pass with the patch.Validation run locally:
npm run build— cjs/esm/es/min builds and the fulltest/index.jssuite pass with 100% statement/branch coverage onindex.js.node test/recursion.js— passes.JSON.parse(JSON.stringify(tree), sameReviver)— 0 mismatches.