Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for stringify array replacer mozilla/rhino#725 #876

Merged
merged 1 commit into from
May 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 26 additions & 12 deletions src/org/mozilla/javascript/NativeJSON.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Stack;
import org.mozilla.javascript.json.JsonParser;
Expand Down Expand Up @@ -198,7 +198,7 @@ private static class StringifyState {
String indent,
String gap,
Callable replacer,
List<Object> propertyList) {
Object[] propertyList) {
this.cx = cx;
this.scope = scope;

Expand All @@ -212,7 +212,7 @@ private static class StringifyState {
String indent;
String gap;
Callable replacer;
List<Object> propertyList;
Object[] propertyList;

Context cx;
Scriptable scope;
Expand All @@ -223,22 +223,36 @@ public static Object stringify(
String indent = "";
String gap = "";

List<Object> propertyList = null;
Object[] propertyList = null;
Callable replacerFunction = null;

if (replacer instanceof Callable) {
replacerFunction = (Callable) replacer;
} else if (replacer instanceof NativeArray) {
propertyList = new LinkedList<Object>();
LinkedHashSet<Object> propertySet = new LinkedHashSet<Object>();
NativeArray replacerArray = (NativeArray) replacer;
for (int i : replacerArray.getIndexIds()) {
Object v = replacerArray.get(i, replacerArray);
if (v instanceof String || v instanceof Number) {
propertyList.add(v);
} else if (v instanceof NativeString || v instanceof NativeNumber) {
propertyList.add(ScriptRuntime.toString(v));
if (v instanceof String) {
propertySet.add(v);
} else if (v instanceof Number
|| v instanceof NativeString
|| v instanceof NativeNumber) {
// TODO: This should also apply to subclasses of NativeString and NativeNumber
// once the class, extends, and super keywords are implemented
propertySet.add(ScriptRuntime.toString(v));
}
}
// After items have been converted to strings and duplicates removed, transform to an
// array and convert indexed keys back to Integers as required for later processing
propertyList = new Object[propertySet.size()];
int i = 0;
for (Object prop : propertySet) {
ScriptRuntime.StringIdOrIndex idOrIndex = ScriptRuntime.toStringIdOrIndex(cx, prop);
// This will always be a String or Integer
propertyList[i++] =
(idOrIndex.stringId == null) ? idOrIndex.index : idOrIndex.stringId;
}
}

if (space instanceof NativeNumber) {
Expand Down Expand Up @@ -374,12 +388,12 @@ private static String jo(Scriptable value, StringifyState state) {
state.indent = state.indent + state.gap;
Object[] k = null;
if (state.propertyList != null) {
k = state.propertyList.toArray();
tonygermano marked this conversation as resolved.
Show resolved Hide resolved
k = state.propertyList;
} else {
k = value.getIds();
}

List<Object> partial = new LinkedList<Object>();
Collection<Object> partial = new LinkedList<Object>();

for (Object p : k) {
Object strP = str(p, value, state);
Expand Down Expand Up @@ -420,7 +434,7 @@ private static String ja(NativeArray value, StringifyState state) {

String stepback = state.indent;
state.indent = state.indent + state.gap;
List<Object> partial = new LinkedList<Object>();
Collection<Object> partial = new LinkedList<Object>();

long len = value.getLength();
for (long index = 0; index < len; index++) {
Expand Down
28 changes: 28 additions & 0 deletions testsrc/jstests/json-stringify-array-replacer.jstest
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

// This test is a stand-in for the following until they are run by the Test262SuiteTest.
// https://github.com/tc39/test262/blob/70bc32edab22b44db9d671ce505db8842ae200b6/test/built-ins/JSON/stringify/replacer-array-duplicates.js
// https://github.com/tc39/test262/blob/70bc32edab22b44db9d671ce505db8842ae200b6/test/built-ins/JSON/stringify/replacer-array-number.js


function assertEqual(actual, expected) {
if (actual !== expected) throw 'expected: <' + expected + '> but found <' + actual + '>';
}

var obj = {"0":1, "1":2, "2":3, "name":4};
var actual = JSON.stringify(obj, ["0", "1", "name", "name"]);
var expected = JSON.stringify({"0":1, "1":2, "name":4});
assertEqual(expected, actual);

var obj = {"2":"b","3":"c","1":"a"};
var expected = JSON.stringify({"1":"a","2":"b","3":"c"});

var actual = JSON.stringify(obj, ["1","2","3"]);
assertEqual(expected, actual);

var actual = JSON.stringify(obj, [1,2,3]);
assertEqual(expected, actual);

"success"