Skip to content

Commit be7ea2c

Browse files
committed
improve handling of strings wrapped to rawJSON
1 parent eb0327f commit be7ea2c

File tree

1 file changed

+10
-17
lines changed

1 file changed

+10
-17
lines changed

packages/core-js/modules/esnext.json.raw-json.js

+10-17
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ var createProperty = require('../internals/create-property');
1212
var parseJSONString = require('../internals/parse-json-string');
1313
var getReplacerFunction = require('../internals/get-json-replacer-function');
1414
var uid = require('../internals/uid');
15-
var InternalStateModule = require('../internals/internal-state');
16-
17-
var getInternalState = InternalStateModule.get;
18-
var setInternalState = InternalStateModule.set;
15+
var setInternalState = require('../internals/internal-state').set;
1916

2017
var $String = String;
2118
var $SyntaxError = SyntaxError;
@@ -26,6 +23,7 @@ var freeze = getBuiltIn('Object', 'freeze');
2623
var at = uncurryThis(''.charAt);
2724
var slice = uncurryThis(''.slice);
2825
var exec = uncurryThis(/./.exec);
26+
var push = uncurryThis([].push);
2927

3028
var MARK = uid();
3129
var MARK_LENGTH = MARK.length;
@@ -42,14 +40,9 @@ $({ target: 'JSON', stat: true, forced: !NATIVE_RAW_JSON }, {
4240
throw $SyntaxError(ERROR_MESSAGE);
4341
}
4442
var parsed = parse(jsonString);
45-
var dataType = typeof parsed;
46-
if (dataType == 'object' && parsed !== null) throw $SyntaxError(ERROR_MESSAGE);
43+
if (typeof parsed == 'object' && parsed !== null) throw $SyntaxError(ERROR_MESSAGE);
4744
var obj = create(null);
48-
setInternalState(obj, {
49-
type: 'RawJSON',
50-
dataType: dataType,
51-
data: dataType == 'string' ? parsed : jsonString
52-
});
45+
setInternalState(obj, { type: 'RawJSON' });
5346
createProperty(obj, 'rawJSON', jsonString);
5447
return FREEZING ? freeze(obj) : obj;
5548
}
@@ -61,13 +54,12 @@ $({ target: 'JSON', stat: true, forced: !NATIVE_RAW_JSON }, {
6154
if ($stringify) $({ target: 'JSON', stat: true, arity: 3, forced: !NATIVE_RAW_JSON }, {
6255
stringify: function stringify(text, replacer, space) {
6356
var replacerFunction = getReplacerFunction(replacer);
57+
var rawStrings = [];
6458

6559
var json = $stringify(text, function (key, value) {
6660
// some old implementations (like WebKit) could pass numbers as keys
67-
if (isCallable(replacerFunction)) value = call(replacerFunction, this, $String(key), value);
68-
if (!isRawJSON(value)) return value;
69-
var state = getInternalState(value);
70-
return state.dataType == 'string' ? state.data : MARK + state.data;
61+
var v = isCallable(replacerFunction) ? call(replacerFunction, this, $String(key), value) : value;
62+
return isRawJSON(v) ? MARK + (push(rawStrings, v.rawJSON) - 1) : v;
7163
}, space);
7264

7365
if (typeof json != 'string') return json;
@@ -77,15 +69,16 @@ if ($stringify) $({ target: 'JSON', stat: true, arity: 3, forced: !NATIVE_RAW_JS
7769

7870
for (var i = 0; i < length; i++) {
7971
var chr = at(json, i);
80-
if (chr === '"') {
72+
if (chr == '"') {
8173
var end = parseJSONString(json, ++i).end - 1;
8274
var string = slice(json, i, end);
8375
result += slice(string, 0, MARK_LENGTH) == MARK
84-
? slice(string, MARK_LENGTH)
76+
? rawStrings[slice(string, MARK_LENGTH)]
8577
: '"' + string + '"';
8678
i = end;
8779
} else result += chr;
8880
}
81+
8982
return result;
9083
}
9184
});

0 commit comments

Comments
 (0)