Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,19 @@ import {
type MemoryAttribute = "assign" | "strong" | "copy";

const objectiveCStringEscape = (s: string): string =>
stringEscape(s).replace(/\\u0000/g, "\\000");
stringEscape(s).replace(
/\\\\|\\u00([0-9a-f]{2})/g,
(escaped, code: string | undefined) =>
code === undefined
? escaped
: Array.from(
new TextEncoder().encode(
String.fromCharCode(Number.parseInt(code, 16)),
),
)
.map((byte) => `\\${byte.toString(8).padStart(3, "0")}`)
.join(""),
);

const DEBUG = false;

Expand Down
15 changes: 15 additions & 0 deletions test/inputs/json/samples/objc-control-characters.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"literal": "\\u001b",
"values": [
"c0\u0001\u001b\u001f",
"c1\u007f\u0080\u0085\u009f",
"c0\u0001\u001b\u001f",
"c1\u007f\u0080\u0085\u009f",
"c0\u0001\u001b\u001f",
"c1\u007f\u0080\u0085\u009f",
"c0\u0001\u001b\u001f",
"c1\u007f\u0080\u0085\u009f",
"c0\u0001\u001b\u001f",
"c1\u007f\u0080\u0085\u009f"
]
}
10 changes: 7 additions & 3 deletions test/languages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -914,8 +914,7 @@ export const ObjectiveCLanguage: Language = {
output: "QTTopLevel.m",
topLevel: "QTTopLevel",
skipJSON: [
// Almost all strings work except any containing \u001b
// See https://goo.gl/L8HfUP
// Foundation drops BOM object keys during JSON decoding.
"blns-object.json",
],
skipMiscJSON: false,
Expand Down Expand Up @@ -1267,6 +1266,8 @@ export const KotlinLanguage: Language = {
output: "TopLevel.kt",
topLevel: "TopLevel",
skipJSON: [
// Klaxon emits unescaped control characters in enum values.
"objc-control-characters.json",
// Some odd property names prevent Klaxon from mapping to constructors
// https://github.com/cbeust/klaxon/issues/146
"blns-object.json",
Expand Down Expand Up @@ -1358,7 +1359,10 @@ export const KotlinJacksonLanguage: Language = {
],
output: "TopLevel.kt",
topLevel: "TopLevel",
skipJSON: [],
skipJSON: [
// The enum serializer does not escape control characters.
"objc-control-characters.json",
],
skipSchema: ["keyword-unions.schema"],
skipMiscJSON: false,
rendererOptions: { framework: "jackson" },
Expand Down
Loading