Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/OpenClaw.SetupEngine/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public static async Task<int> Main(string[] args)
logPath = config.LogPath,
journalPath
};
var json = System.Text.Json.JsonSerializer.Serialize(jsonResult, new System.Text.Json.JsonSerializerOptions { WriteIndented = true });
var json = System.Text.Json.JsonSerializer.Serialize(jsonResult, SetupConfig.JsonWriteOptions);
await AtomicFile.WriteAllTextAsync(jsonOutput, json);
}

Expand Down
11 changes: 10 additions & 1 deletion src/OpenClaw.SetupEngine/SetupContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ public SetupConfig ApplyUiDefaults(bool rollbackOnFailure = true)
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
WriteIndented = true
};

/// <summary>
/// Minimal write-only options for producing human-readable JSON files.
/// Shared to avoid repeated heap allocation at call sites.
/// </summary>
internal static readonly JsonSerializerOptions JsonWriteOptions = new()
{
WriteIndented = true
};
}

// ─── WSL Configuration ───
Expand Down Expand Up @@ -220,7 +229,7 @@ public void MergeIntoSettingsFile(string settingsPath)
settings.TryAdd(kvp.Key, kvp.Value);

Directory.CreateDirectory(Path.GetDirectoryName(settingsPath)!);
var json = JsonSerializer.Serialize(settings, new JsonSerializerOptions { WriteIndented = true });
var json = JsonSerializer.Serialize(settings, SetupConfig.JsonWriteOptions);
AtomicFile.WriteAllText(settingsPath, json);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/OpenClaw.SetupEngine/SetupSteps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2213,7 +2213,7 @@ private static async Task WriteSetupStateAsync(SetupContext ctx, CancellationTok
History = Array.Empty<object>()
};

var json = System.Text.Json.JsonSerializer.Serialize(state, new System.Text.Json.JsonSerializerOptions { WriteIndented = true });
var json = System.Text.Json.JsonSerializer.Serialize(state, SetupConfig.JsonWriteOptions);
await AtomicFile.WriteAllTextAsync(statePath, json, ct);
ctx.Logger.Info($"Wrote setup-state.json: DistroName={ctx.DistroName}");
}
Expand Down Expand Up @@ -2286,7 +2286,7 @@ private static void WriteKeepaliveMarker(SetupContext ctx, string markerPath, in
StartTimeUtc = DateTimeOffset.UtcNow,
ProcessName = "wsl"
};
var json = System.Text.Json.JsonSerializer.Serialize(marker, new System.Text.Json.JsonSerializerOptions { WriteIndented = true });
var json = System.Text.Json.JsonSerializer.Serialize(marker, SetupConfig.JsonWriteOptions);
AtomicFile.WriteAllText(markerPath, json);
ctx.Logger.Info($"Wrote keepalive marker: {markerPath}");
}
Expand Down
2 changes: 1 addition & 1 deletion src/OpenClaw.SetupEngine/SetupWizardRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ private string WriteAnswerTemplate(IReadOnlyList<WizardTemplateStep> discoveredS
Steps = discoveredSteps
};

var json = JsonSerializer.Serialize(template, new JsonSerializerOptions { WriteIndented = true });
var json = JsonSerializer.Serialize(template, SetupConfig.JsonWriteOptions);
AtomicFile.WriteAllText(basePath, json);
_ctx.Logger.Info($"Wizard answer template written: {basePath}");
return basePath;
Expand Down
3 changes: 1 addition & 2 deletions src/OpenClaw.SetupEngine/TrayArtifactCleanup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,7 @@ internal static void ResetOnboardingSettings(string appDataDir, SetupLogger logg

if (changed)
{
var updatedJson = System.Text.Json.JsonSerializer.Serialize(dict,
new System.Text.Json.JsonSerializerOptions { WriteIndented = true });
var updatedJson = System.Text.Json.JsonSerializer.Serialize(dict, SetupConfig.JsonWriteOptions);
AtomicFile.WriteAllText(settingsPath, updatedJson);
logger.Info(preserveNodeSettings
? "[Uninstall] Reset onboarding settings (GatewayUrl)"
Expand Down
2 changes: 1 addition & 1 deletion src/OpenClaw.Shared/ChannelConfigPatchBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public static ChannelPatchBuildResult BuildPatch(

// Reserialize β†’ re-parse β†’ clone so the returned JsonElement isn't
// tied to a JsonDocument that goes out of scope on caller side.
var json = JsonSerializer.Serialize(root, new JsonSerializerOptions { WriteIndented = true });
var json = JsonSerializer.Serialize(root, JsonSerializerOptionsCache.WriteIndented);
using var doc = JsonDocument.Parse(json);
return new ChannelPatchBuildResult { Patch = doc.RootElement.Clone() };
}
Expand Down
2 changes: 1 addition & 1 deletion src/OpenClaw.Shared/DeviceIdentity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ private void StoreNodeDeviceTokenCore(string token, string[]? scopes)
/// </summary>
private static void AtomicWriteKeyFile(string path, DeviceKeyData data)
{
var json = JsonSerializer.Serialize(data, new JsonSerializerOptions { WriteIndented = true });
var json = JsonSerializer.Serialize(data, JsonSerializerOptionsCache.WriteIndented);
var dir = Path.GetDirectoryName(path);
var tempDir = string.IsNullOrEmpty(dir) ? Environment.CurrentDirectory : dir;
var tempPath = Path.Combine(tempDir, $".{Path.GetFileName(path)}.{Guid.NewGuid():N}.tmp");
Expand Down
20 changes: 20 additions & 0 deletions src/OpenClaw.Shared/JsonSerializerOptionsCache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Text.Json;

namespace OpenClaw.Shared;

/// <summary>
/// Shared, reusable <see cref="JsonSerializerOptions"/> singletons.
/// Prefer these over inline <c>new JsonSerializerOptions { … }</c> allocations
/// at call sites to avoid repeated heap allocation and settings drift.
/// </summary>
internal static class JsonSerializerOptionsCache
{
/// <summary>
/// Pretty-print JSON with no additional overrides.
/// Suitable for writing human-readable configuration and diagnostic files.
/// </summary>
internal static readonly JsonSerializerOptions WriteIndented = new()
{
WriteIndented = true
};
}
2 changes: 1 addition & 1 deletion src/OpenClaw.Shared/Models.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1789,7 +1789,7 @@ public string DataJson
if (_cachedDataJson != null) return _cachedDataJson;
try
{
_cachedDataJson = JsonSerializer.Serialize(Data, new JsonSerializerOptions { WriteIndented = true });
_cachedDataJson = JsonSerializer.Serialize(Data, JsonSerializerOptionsCache.WriteIndented);
}
catch
{
Expand Down