Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JSON formatter to DevToys 2.0 #1013

Merged
merged 2 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 9 additions & 0 deletions src/app/dev/DevToys.Tools/DevToys.Tools.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@
<AutoGen>True</AutoGen>
<DependentUpon>GlobalStrings.resx</DependentUpon>
</Compile>
<Compile Update="Tools\Formatters\Json\JsonFormatter.Designer.cs">
<DependentUpon>JsonFormatter.resx</DependentUpon>
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
</Compile>
<Compile Update="Tools\Formatters\Xml\XmlFormatter.Designer.cs">
<DependentUpon>XmlFormatter.resx</DependentUpon>
<DesignTime>True</DesignTime>
Expand Down Expand Up @@ -173,6 +178,10 @@
<LastGenOutput>JsonTableConverter.Designer.cs</LastGenOutput>
<Generator>ResXFileCodeGenerator</Generator>
</EmbeddedResource>
<EmbeddedResource Update="Tools\Formatters\Json\JsonFormatter.resx">
<LastGenOutput>JsonFormatter.Designer.cs</LastGenOutput>
<Generator>ResXFileCodeGenerator</Generator>
</EmbeddedResource>
<EmbeddedResource Update="Tools\Formatters\Xml\XmlFormatter.resx">
<LastGenOutput>XmlFormatter.Designer.cs</LastGenOutput>
<Generator>ResXFileCodeGenerator</Generator>
Expand Down
14 changes: 6 additions & 8 deletions src/app/dev/DevToys.Tools/Helpers/JsonHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ internal static partial class JsonHelper
/// </summary>
internal static bool IsValid(string? input, ILogger logger)
{
input = input?.Trim();

if (input == null)
{
return true;
Expand Down Expand Up @@ -48,16 +46,16 @@ internal static bool IsValid(string? input, ILogger logger)
/// <summary>
/// Format a string to the specified JSON format.
/// </summary>
internal static async ValueTask<string> Format(
internal static async Task<ResultInfo<string>> Format(
lwillia marked this conversation as resolved.
Show resolved Hide resolved
string? input,
Indentation indentationMode,
bool sortProperties,
ILogger logger,
CancellationToken cancellationToken)
{
if (input == null || !IsValid(input, logger))
if (string.IsNullOrWhiteSpace(input))
{
return string.Empty;
return new(string.Empty, false);
}

try
Expand Down Expand Up @@ -124,16 +122,16 @@ internal static async ValueTask<string> Format(
jToken.WriteTo(jsonTextWriter);
}

return stringBuilder.ToString();
return new(stringBuilder.ToString(), true);
}
catch (JsonReaderException ex)
{
return ex.Message;
return new(ex.Message, false);
}
catch (Exception ex)
{
logger.LogError(ex, "Invalid JSON format 'indentationMode'", indentationMode);
return ex.Message;
return new(ex.Message, false);
}
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading