diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-01-13 06:24:07 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-01-13 06:24:07 +0800 |
| commit | 7d59c15b0efd2aa5a27aef356a265f850a2e7e2f (patch) | |
| tree | 16f30ff5a7d5d871f0358f792988f369cc532b45 /CommandLineWrapper/JsonResults/JsonResultGetter.cs | |
| parent | b202287c51438d64f769dd72c579664493ba9109 (diff) | |
Add JSON result getters for command outputs
Diffstat (limited to 'CommandLineWrapper/JsonResults/JsonResultGetter.cs')
| -rw-r--r-- | CommandLineWrapper/JsonResults/JsonResultGetter.cs | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/CommandLineWrapper/JsonResults/JsonResultGetter.cs b/CommandLineWrapper/JsonResults/JsonResultGetter.cs new file mode 100644 index 0000000..e1bf607 --- /dev/null +++ b/CommandLineWrapper/JsonResults/JsonResultGetter.cs @@ -0,0 +1,31 @@ +using static JVCSCommandInvoker; + +namespace CommandLineWrapper.JsonResults +{ + public abstract class JsonResultGetter<TJsonFormat> + { + protected abstract Task<InvokeResult> ExecCommand(JVCSWorkspace workspace); + + public async Task<TJsonFormat?> Get(JVCSWorkspace workspace) + { + var result = await ExecCommand(workspace); + var output = result.StandardOutput; + + string trimmedOutput = output?.Trim() ?? "{}"; + if (!string.IsNullOrEmpty(trimmedOutput) && trimmedOutput.StartsWith("{") && trimmedOutput.EndsWith("}")) + { + var options = new System.Text.Json.JsonSerializerOptions + { + PropertyNameCaseInsensitive = true, + PropertyNamingPolicy = null, + }; + + options.Converters.Add(new System.Text.Json.Serialization.JsonStringEnumConverter()); + + return System.Text.Json.JsonSerializer.Deserialize<TJsonFormat>(trimmedOutput, options); + } + + return default(TJsonFormat); + } + } +} |
