fix(core): project nulls keep list and text defaults; knee off survives; duplicate keys are JSON errors

An explicit null on a list or text member left it null and crashed renders
(batch, excluded frames). A null highlight knee was not written, so a rewrite
turned it back on. A member given twice threw ArgumentException past every
JsonException handler.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-02 22:59:00 +00:00
1 parent 91bf442c26
commit 1cd94af0f0
2 files changed
+52 -5

No files matched your search

+20 -5
View File
@@ -84,6 +84,8 @@ public sealed record ExposureSettings
public double ExposureOffsetEv { get; init; }
/// <summary>Soft highlight knee (linear), null = hard clip at the encoder.</summary>
/// <remarks>Written even when null: a missing value reads back as the default knee, which would turn "off" on.</remarks>
[JsonIgnore(Condition = JsonIgnoreCondition.Never)]
public double? HighlightKnee { get; init; } = 0.9;
}
@@ -187,7 +189,7 @@ public sealed record ProjectSettings
/// keep their defaults. The JSON is laid over the default project first, because generated deserialisation leaves
/// missing init-only members null or false rather than at their initial values.
/// </summary>
/// <exception cref="JsonException">The text is not a JSON object.</exception>
/// <exception cref="JsonException">The text is not a JSON object, or repeats a member.</exception>
/// <exception cref="InvalidDataException">The project was written by a newer Titano.</exception>
public static ProjectSettings FromJson(string json)
{
@@ -196,7 +198,15 @@ public sealed record ProjectSettings
switch (JsonNode.Parse(json, options))
{
case JsonObject given:
Overlay(merged, given);
try
{
Overlay(merged, given);
}
catch (ArgumentException ex)
{
// A parsed object builds its member table on first use and rejects a name given twice (in any casing).
throw new JsonException("A project member is given more than once.", ex);
}
break;
case not null:
throw new JsonException("A project must be a JSON object.");
@@ -207,13 +217,18 @@ public sealed record ProjectSettings
return settings;
}
/// <summary>Copies <paramref name="source"/> into <paramref name="target"/>, merging nested objects; a null never replaces an object.</summary>
/// <summary>
/// Copies <paramref name="source"/> into <paramref name="target"/>, merging nested objects. A null keeps a default that
/// is an object, a list or a text, since none of those members is nullable; elsewhere it clears a nullable value.
/// </summary>
private static void Overlay(JsonObject target, JsonObject source)
{
foreach (var (name, value) in source)
{
if (value is JsonObject child && target[name] is JsonObject existing) Overlay(existing, child);
else if (value is not null || target[name] is not JsonObject) target[name] = value?.DeepClone();
var current = target[name];
if (value is JsonObject child && current is JsonObject section) Overlay(section, child);
else if (value is not null || !(current is JsonObject or JsonArray || current?.GetValueKind() == JsonValueKind.String))
target[name] = value?.DeepClone();
}
}
}
@@ -85,6 +85,38 @@ public sealed class ProjectSettingsTests
Assert.Equal(1, cleared.Timing.RemapCurve.Count);
}
[Test]
public void NullHighlightKneeSurvivesTheRoundTrip()
{
// "No knee, hard clip" must not come back as the default knee when the project is rewritten.
var off = new ProjectSettings { Exposure = new ExposureSettings { HighlightKnee = null } };
Assert.Contains("\"highlightKnee\": null", off.ToJson());
Assert.Null(ProjectSettings.FromJson(off.ToJson()).Exposure.HighlightKnee);
Assert.Equal(0.9, ProjectSettings.FromJson(new ProjectSettings().ToJson()).Exposure.HighlightKnee);
}
[Test]
public void NullListsAndTextsKeepTheirDefaults()
{
var project = ProjectSettings.FromJson(
"""{ "excludedFrames": null, "export": { "batch": null, "nameTemplate": null }, "timing": { "remapCurve": null }, "camera": { "keyframes": null } }""");
Assert.Count(0, project.ExcludedFrames);
Assert.Count(0, project.Export.Batch);
Assert.Equal("{date} {title} - {preset}", project.Export.NameTemplate);
Assert.Count(2, project.Timing.RemapCurve);
Assert.Count(0, project.Camera.Keyframes);
// Nullable members without a default still clear.
Assert.Null(ProjectSettings.FromJson("""{ "export": { "folder": null }, "heroFrame": null }""").HeroFrame);
}
[Test]
public void DuplicateKeysAreAJsonError()
{
Assert.Throws<System.Text.Json.JsonException>(() => ProjectSettings.FromJson("""{ "version": 1, "version": 1 }"""));
Assert.Throws<System.Text.Json.JsonException>(() => ProjectSettings.FromJson("""{ "Export": {}, "export": {} }"""));
Assert.Throws<System.Text.Json.JsonException>(() => ProjectSettings.FromJson("""{ "export": { "batch": [], "Batch": [] } }"""));
}
[Test]
public void ProjectMustBeAnObject() =>
Assert.Throws<System.Text.Json.JsonException>(() => ProjectSettings.FromJson("[1, 2]"));