{
"version": "4.0",
"category": "encode",
"description": "Object encoding – simple objects, nested objects, key encoding",
"tests": [
{
"name": "preserves key order in objects",
"input": {
"id": 123,
"name": "Ada",
"active": true
},
"expected": "id: 123\nname: Ada\nactive: true",
"specSection": "8"
},
{
"name": "encodes null values in objects",
"input": {
"id": 123,
"value": null
},
"expected": "id: 123\nvalue: null",
"specSection": "8"
},
{
"name": "encodes empty objects as empty string",
"input": {},
"expected": "",
"specSection": "8"
},
{
"name": "quotes string value with colon",
"input": {
"note": "a:b"
},
"expected": "note: \"a:b\"",
"specSection": "7.2"
},
{
"name": "quotes string value with comma",
"input": {
"note": "a,b"
},
"expected": "note: \"a,b\"",
"specSection": "7.2"
},
{
"name": "quotes string value with newline",
"input": {
"text": "line1\nline2"
},
"expected": "text: \"line1\\nline2\"",
"specSection": "7.2"
},
{
"name": "quotes string value with embedded quotes",
"input": {
"text": "say \"hello\""
},
"expected": "text: \"say \\\"hello\\\"\"",
"specSection": "7.2"
},
{
"name": "quotes string value with leading space",
"input": {
"text": " padded "
},
"expected": "text: \" padded \"",
"specSection": "7.2"
},
{
"name": "quotes string value with only spaces",
"input": {
"text": " "
},
"expected": "text: \" \"",
"specSection": "7.2"
},
{
"name": "quotes string value that looks like true",
"input": {
"v": "true"
},
"expected": "v: \"true\"",
"specSection": "7.2"
},
{
"name": "quotes string value that looks like number",
"input": {
"v": "42"
},
"expected": "v: \"42\"",
"specSection": "7.2"
},
{
"name": "quotes string value that looks like negative decimal",
"input": {
"v": "-7.5"
},
"expected": "v: \"-7.5\"",
"specSection": "7.2"
},
{
"name": "quotes key with colon",
"input": {
"order:id": 7
},
"expected": "\"order:id\": 7",
"specSection": "7.3"
},
{
"name": "quotes key with brackets",
"input": {
"[index]": 5
},
"expected": "\"[index]\": 5",
"specSection": "7.3"
},
{
"name": "quotes key with braces",
"input": {
"{key}": 5
},
"expected": "\"{key}\": 5",
"specSection": "7.3"
},
{
"name": "quotes key with comma",
"input": {
"a,b": 1
},
"expected": "\"a,b\": 1",
"specSection": "7.3"
},
{
"name": "quotes key with spaces",
"input": {
"full name": "Ada"
},
"expected": "\"full name\": Ada",
"specSection": "7.3"
},
{
"name": "quotes key with leading hyphen",
"input": {
"-lead": 1
},
"expected": "\"-lead\": 1",
"specSection": "7.3"
},
{
"name": "quotes key with leading and trailing spaces",
"input": {
" a ": 1
},
"expected": "\" a \": 1",
"specSection": "7.3"
},
{
"name": "quotes numeric key",
"input": {
"123": "x"
},
"expected": "\"123\": x",
"specSection": "7.3"
},
{
"name": "quotes empty string key",
"input": {
"": 1
},
"expected": "\"\": 1",
"specSection": "7.3"
},
{
"name": "quotes non-ASCII key",
"input": {
"café": 1
},
"expected": "\"café\": 1",
"specSection": "7.3",
"note": "The unquoted-key pattern is ASCII-only, so every non-ASCII key is quoted"
},
{
"name": "quotes CJK key",
"input": {
"名前": "x"
},
"expected": "\"名前\": x",
"specSection": "7.3"
},
{
"name": "escapes newline in key",
"input": {
"line\nbreak": 1
},
"expected": "\"line\\nbreak\": 1",
"specSection": "7.1"
},
{
"name": "escapes tab in key",
"input": {
"tab\there": 2
},
"expected": "\"tab\\there\": 2",
"specSection": "7.1"
},
{
"name": "escapes quotes in key",
"input": {
"he said \"hi\"": 1
},
"expected": "\"he said \\\"hi\\\"\": 1",
"specSection": "7.1"
},
{
"name": "escapes U+0004 control character in key via \\uXXXX",
"input": {
"a\u0004b": 1
},
"expected": "\"a\\u0004b\": 1",
"specSection": "7.1"
},
{
"name": "escapes U+001F control character in key via \\uXXXX",
"input": {
"x\u001fy": 2
},
"expected": "\"x\\u001fy\": 2",
"specSection": "7.1"
},
{
"name": "encodes deeply nested objects",
"input": {
"a": {
"b": {
"c": "deep"
}
}
},
"expected": "a:\n b:\n c: deep",
"specSection": "8"
},
{
"name": "encodes empty nested object",
"input": {
"user": {}
},
"expected": "user:",
"specSection": "8"
},
{
"name": "encodes __proto__ own property as an ordinary key",
"input": {
"__proto__": "polluted",
"safe": true
},
"expected": "__proto__: polluted\nsafe: true",
"specSection": "15",
"note": "No key has special meaning, so the encoder emits prototype-named own entries like any other (§8)"
},
{
"name": "encodes constructor and prototype own properties as ordinary keys",
"input": {
"constructor": 1,
"prototype": 2
},
"expected": "constructor: 1\nprototype: 2",
"specSection": "15"
},
{
"name": "encodes __proto__ as a tabular field name",
"input": {
"rows": [
{
"__proto__": "a",
"x": 1
},
{
"__proto__": "b",
"x": 2
}
]
},
"expected": "rows[2]{__proto__,x}:\n a,1\n b,2",
"specSection": "15"
},
{
"name": "quotes hash-leading string in object field value",
"input": {
"note": "#x"
},
"expected": "note: \"#x\"",
"specSection": "7.2"
}
]
}