1 | 'use strict';
|
---|
2 | var $ = require('../internals/export');
|
---|
3 | var FREEZING = require('../internals/freezing');
|
---|
4 | var NATIVE_RAW_JSON = require('../internals/native-raw-json');
|
---|
5 | var getBuiltIn = require('../internals/get-built-in');
|
---|
6 | var call = require('../internals/function-call');
|
---|
7 | var uncurryThis = require('../internals/function-uncurry-this');
|
---|
8 | var isCallable = require('../internals/is-callable');
|
---|
9 | var isRawJSON = require('../internals/is-raw-json');
|
---|
10 | var toString = require('../internals/to-string');
|
---|
11 | var createProperty = require('../internals/create-property');
|
---|
12 | var parseJSONString = require('../internals/parse-json-string');
|
---|
13 | var getReplacerFunction = require('../internals/get-json-replacer-function');
|
---|
14 | var uid = require('../internals/uid');
|
---|
15 | var setInternalState = require('../internals/internal-state').set;
|
---|
16 |
|
---|
17 | var $String = String;
|
---|
18 | var $SyntaxError = SyntaxError;
|
---|
19 | var parse = getBuiltIn('JSON', 'parse');
|
---|
20 | var $stringify = getBuiltIn('JSON', 'stringify');
|
---|
21 | var create = getBuiltIn('Object', 'create');
|
---|
22 | var freeze = getBuiltIn('Object', 'freeze');
|
---|
23 | var at = uncurryThis(''.charAt);
|
---|
24 | var slice = uncurryThis(''.slice);
|
---|
25 | var push = uncurryThis([].push);
|
---|
26 |
|
---|
27 | var MARK = uid();
|
---|
28 | var MARK_LENGTH = MARK.length;
|
---|
29 | var ERROR_MESSAGE = 'Unacceptable as raw JSON';
|
---|
30 |
|
---|
31 | var isWhitespace = function (it) {
|
---|
32 | return it === ' ' || it === '\t' || it === '\n' || it === '\r';
|
---|
33 | };
|
---|
34 |
|
---|
35 | // `JSON.parse` method
|
---|
36 | // https://tc39.es/proposal-json-parse-with-source/#sec-json.israwjson
|
---|
37 | // https://github.com/tc39/proposal-json-parse-with-source
|
---|
38 | $({ target: 'JSON', stat: true, forced: !NATIVE_RAW_JSON }, {
|
---|
39 | rawJSON: function rawJSON(text) {
|
---|
40 | var jsonString = toString(text);
|
---|
41 | if (jsonString === '' || isWhitespace(at(jsonString, 0)) || isWhitespace(at(jsonString, jsonString.length - 1))) {
|
---|
42 | throw new $SyntaxError(ERROR_MESSAGE);
|
---|
43 | }
|
---|
44 | var parsed = parse(jsonString);
|
---|
45 | if (typeof parsed == 'object' && parsed !== null) throw new $SyntaxError(ERROR_MESSAGE);
|
---|
46 | var obj = create(null);
|
---|
47 | setInternalState(obj, { type: 'RawJSON' });
|
---|
48 | createProperty(obj, 'rawJSON', jsonString);
|
---|
49 | return FREEZING ? freeze(obj) : obj;
|
---|
50 | }
|
---|
51 | });
|
---|
52 |
|
---|
53 | // `JSON.stringify` method
|
---|
54 | // https://tc39.es/ecma262/#sec-json.stringify
|
---|
55 | // https://github.com/tc39/proposal-json-parse-with-source
|
---|
56 | if ($stringify) $({ target: 'JSON', stat: true, arity: 3, forced: !NATIVE_RAW_JSON }, {
|
---|
57 | stringify: function stringify(text, replacer, space) {
|
---|
58 | var replacerFunction = getReplacerFunction(replacer);
|
---|
59 | var rawStrings = [];
|
---|
60 |
|
---|
61 | var json = $stringify(text, function (key, value) {
|
---|
62 | // some old implementations (like WebKit) could pass numbers as keys
|
---|
63 | var v = isCallable(replacerFunction) ? call(replacerFunction, this, $String(key), value) : value;
|
---|
64 | return isRawJSON(v) ? MARK + (push(rawStrings, v.rawJSON) - 1) : v;
|
---|
65 | }, space);
|
---|
66 |
|
---|
67 | if (typeof json != 'string') return json;
|
---|
68 |
|
---|
69 | var result = '';
|
---|
70 | var length = json.length;
|
---|
71 |
|
---|
72 | for (var i = 0; i < length; i++) {
|
---|
73 | var chr = at(json, i);
|
---|
74 | if (chr === '"') {
|
---|
75 | var end = parseJSONString(json, ++i).end - 1;
|
---|
76 | var string = slice(json, i, end);
|
---|
77 | result += slice(string, 0, MARK_LENGTH) === MARK
|
---|
78 | ? rawStrings[slice(string, MARK_LENGTH)]
|
---|
79 | : '"' + string + '"';
|
---|
80 | i = end;
|
---|
81 | } else result += chr;
|
---|
82 | }
|
---|
83 |
|
---|
84 | return result;
|
---|
85 | }
|
---|
86 | });
|
---|