diff options
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); + } + } +} |
