summaryrefslogtreecommitdiffstats
path: root/js/src/tests/test262/built-ins/JSON/stringify/value-string-escape-ascii.js
blob: 3ec26e928d0c23c8ee1178a9ef5a6ae1fa3fa323 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright (c) 2018 Ecma International.  All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-quotejsonstring
description: >
    JSON.stringify property names and values containing ASCII
    characters that require escaping
---*/

var char_to_json = {
  '"': '\\"',
  "\\": "\\\\",
  "\x00": "\\u0000",
  "\x01": "\\u0001",
  "\x02": "\\u0002",
  "\x03": "\\u0003",
  "\x04": "\\u0004",
  "\x05": "\\u0005",
  "\x06": "\\u0006",
  "\x07": "\\u0007",
  "\x08": "\\b",
  "\x09": "\\t",
  "\x0A": "\\n",
  "\x0B": "\\u000b",
  "\x0C": "\\f",
  "\x0D": "\\r",
  "\x0E": "\\u000e",
  "\x0F": "\\u000f",
  "\x10": "\\u0010",
  "\x11": "\\u0011",
  "\x12": "\\u0012",
  "\x13": "\\u0013",
  "\x14": "\\u0014",
  "\x15": "\\u0015",
  "\x16": "\\u0016",
  "\x17": "\\u0017",
  "\x18": "\\u0018",
  "\x19": "\\u0019",
  "\x1A": "\\u001a",
  "\x1B": "\\u001b",
  "\x1C": "\\u001c",
  "\x1D": "\\u001d",
  "\x1E": "\\u001e",
  "\x1F": "\\u001f"
}

var chars = Object.keys(char_to_json).join("");
var chars_reversed = Object.keys(char_to_json).reverse().join("");
var jsonChars = Object.values(char_to_json).join("");
var jsonChars_reversed = Object.values(char_to_json).reverse().join("");
var json = JSON.stringify({
  ["name" + chars + chars_reversed]: chars_reversed + chars + "value"
});

for (var char in char_to_json) {
  var count = json.split(char_to_json[char]).length - 1;
  assert.sameValue(count, 4,
    "Every ASCII 0x" + char.charCodeAt(0).toString(16) + " serializes to " + char_to_json[char]);
}

assert.sameValue(
  json,
  `{"${"name" + jsonChars + jsonChars_reversed}":"${jsonChars_reversed + jsonChars + "value"}"}`,
  "JSON.stringify(objectUsingControlCharacters)"
);

reportCompare(0, 0);