Skip to content

Commit 97b5367

Browse files
authored
Do not write any escaped surrogate pairs to inference JSONs (#888)
1 parent bbb6e5d commit 97b5367

4 files changed

Lines changed: 12 additions & 8 deletions

File tree

src/Machine/src/Serval.Machine.Shared/Services/PreprocessBuildJob.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@ IOptionsMonitor<BuildJobOptions> options
1919
)
2020
where TEngine : ITrainingEngine
2121
{
22-
// Using UnsafeRelaxedJsonEscaping to avoid escaping surrogate pairs which can result in invalid UTF-8.
22+
// Using JavaScriptEncoder.Create(UnicodeRanges.All) to avoid escaping surrogate pairs
23+
// (including those outside of the BMP) which can result in invalid UTF-8.
2324
// This is safe since the data written by this writer is only read internally and only as UTF-8 encoded JSON.
2425
protected static readonly JsonWriterOptions InferenceWriterOptions = new()
2526
{
2627
Indented = true,
27-
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
28+
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All),
2829
};
2930

3031
internal BuildJobRunnerType TrainJobRunnerType { get; init; } = BuildJobRunnerType.ClearML;

src/Machine/src/Serval.Machine.Shared/Services/SmtTransferTrainBuildJob.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ public class SmtTransferTrainBuildJob(
1212
ITransferEngineFactory transferEngineFactory
1313
) : HangfireBuildJob<TranslationEngine>(platformService, engines, dataAccessContext, buildJobService, logger)
1414
{
15-
// Using UnsafeRelaxedJsonEscaping to avoid escaping surrogate pairs which can result in invalid UTF-8.
15+
// Using JavaScriptEncoder.Create(UnicodeRanges.All) to avoid escaping surrogate pairs
16+
// (including those outside of the BMP) which can result in invalid UTF-8.
1617
// This is safe since the data written by this writer is only read internally and only as UTF-8 encoded JSON.
17-
private static readonly JsonWriterOptions PretranslateWriterOptions = new()
18+
protected static readonly JsonWriterOptions PretranslateWriterOptions = new()
1819
{
1920
Indented = true,
20-
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
21+
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All),
2122
};
2223
private static readonly JsonSerializerOptions JsonSerializerOptions = new()
2324
{

src/Machine/src/Serval.Machine.Shared/Services/StatisticalTrainBuildJob.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ public class StatisticalTrainBuildJob(
1010
IWordAlignmentModelFactory wordAlignmentModelFactory
1111
) : HangfireBuildJob<WordAlignmentEngine>(platformService, engines, dataAccessContext, buildJobService, logger)
1212
{
13-
// Using UnsafeRelaxedJsonEscaping to avoid escaping surrogate pairs which can result in invalid UTF-8.
13+
// Using JavaScriptEncoder.Create(UnicodeRanges.All) to avoid escaping surrogate pairs
14+
// (including those outside of the BMP) which can result in invalid UTF-8.
1415
// This is safe since the data written by this writer is only read internally and only as UTF-8 encoded JSON.
15-
private static readonly JsonWriterOptions WordAlignmentWriterOptions = new()
16+
protected static readonly JsonWriterOptions WordAlignmentWriterOptions = new()
1617
{
1718
Indented = true,
18-
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
19+
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All),
1920
};
2021
private static readonly JsonSerializerOptions JsonSerializerOptions = new()
2122
{

src/Machine/src/Serval.Machine.Shared/Usings.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
global using System.Text.Json;
1919
global using System.Text.Json.Nodes;
2020
global using System.Text.Json.Serialization;
21+
global using System.Text.Unicode;
2122
global using Amazon;
2223
global using Amazon.Runtime;
2324
global using Amazon.S3;

0 commit comments

Comments
 (0)