A Swiss Army knife for developers.
C# blazorcross-platformcsharpdesktopdesktop-appdesktop-applicationdeveloper-toolsextensiblefluentfluent-designlinuxmacosmicasyntax-highlightingtypescriptwindowswindows-10windows-11winui
New tool JSON>Table/CSV/Excel (#1003)
sakana280bea87111ebb7
diff --git a/src/app/dev/DevToys.Api/Tool/SmartDetection/PredefinedCommonDataTypeNames.cs b/src/app/dev/DevToys.Api/Tool/SmartDetection/PredefinedCommonDataTypeNames.csindex c1f998e35..22ee6e574 100644--- a/src/app/dev/DevToys.Api/Tool/SmartDetection/PredefinedCommonDataTypeNames.cs+++ b/src/app/dev/DevToys.Api/Tool/SmartDetection/PredefinedCommonDataTypeNames.cs@@ -4,6 +4,7 @@ public static class PredefinedCommonDataTypeNames{public const string Text = "text";public const string Json = "json";+ public const string JsonArray = "jsonArray";public const string Yaml = "yaml";public const string Base64Text = "base64Text";public const string Base64Image = "base64Image";diff --git a/src/app/dev/DevToys.Tools/DevToys.Tools.csproj b/src/app/dev/DevToys.Tools/DevToys.Tools.csprojindex 56439c36c..7d5c326af 100644--- a/src/app/dev/DevToys.Tools/DevToys.Tools.csproj+++ b/src/app/dev/DevToys.Tools/DevToys.Tools.csproj@@ -34,6 +34,11 @@<AutoGen>True</AutoGen><DependentUpon>GlobalStrings.resx</DependentUpon></Compile>+ <Compile Update="Tools\Converters\JsonTable\JsonTableConverter.Designer.cs">+ <DependentUpon>JsonTableConverter.resx</DependentUpon>+ <DesignTime>True</DesignTime>+ <AutoGen>True</AutoGen>+ </Compile><Compile Update="Tools\EncodersDecoders\Base64Image\Base64ImageEncoderDecoder.Designer.cs"><DependentUpon>Base64ImageEncoderDecoder.resx</DependentUpon><DesignTime>True</DesignTime>@@ -145,6 +150,10 @@<Generator>ResXFileCodeGenerator</Generator><LastGenOutput>GlobalStrings.Designer.cs</LastGenOutput></EmbeddedResource>+ <EmbeddedResource Update="Tools\Converters\JsonTable\JsonTableConverter.resx">+ <LastGenOutput>JsonTableConverter.Designer.cs</LastGenOutput>+ <Generator>ResXFileCodeGenerator</Generator>+ </EmbeddedResource><EmbeddedResource Update="Tools\EncodersDecoders\Base64Image\Base64ImageEncoderDecoder.resx"><LastGenOutput>Base64ImageEncoderDecoder.Designer.cs</LastGenOutput><Generator>ResXFileCodeGenerator</Generator>diff --git a/src/app/dev/DevToys.Tools/Helpers/JsonTableHelper.cs b/src/app/dev/DevToys.Tools/Helpers/JsonTableHelper.csnew file mode 100644index 000000000..c57d60e10--- /dev/null+++ b/src/app/dev/DevToys.Tools/Helpers/JsonTableHelper.cs@@ -0,0 +1,129 @@+using System.Data;+using System.Text;+using Newtonsoft.Json;+using Newtonsoft.Json.Linq;++namespace DevToys.Tools.Helpers;++internal static class JsonTableHelper+{+ internal static ConvertResult ConvertFromJson(string? text, CopyFormat format, CancellationToken cancellationToken)+ {+ char separator = format switch+ {+ CopyFormat.TSV => '\t',+ CopyFormat.CSV => ',',+ CopyFormat.FSV => ';',+ _ => throw new NotSupportedException($"Unhandled {nameof(CopyFormat)}: {format}"),+ };++ JObject[]? array = ParseJsonArray(text);+ if (array == null)+ {+ return new(null, "", ConvertResultError.NotJsonArray);+ }++ JObject[] flattened = array.Select(o => FlattenJsonObject(o)).ToArray();++ string[] properties = flattened+ .SelectMany(o => o.Properties())+ .Select(p => p.Name)+ .Distinct()+ .ToArray();++ if (properties.Length == 0)+ {+ return new(null, "", ConvertResultError.NoProperties);+ }++ var table = new DataGridContents(properties, new());++ var clipboard = new StringBuilder();+ clipboard.AppendLine(string.Join(separator, properties));++ foreach (JObject obj in flattened)+ {+ cancellationToken.ThrowIfCancellationRequested();++ string[] values = properties+ .Select(p => obj[p]?.ToString() ?? "") // JObject indexer conveniently returns null for unknown properties+ .ToArray();++ table.Rows.Add(values);+ clipboard.AppendLine(string.Join(separator, values));+ }++ return new(table, clipboard.ToString(), ConvertResultError.None);+ }++ internal record ConvertResult(DataGridContents? Data, string Text, ConvertResultError Error);++ internal record DataGridContents(string[] Headings, List<string[]> Rows);++ internal enum ConvertResultError { None, NotJsonArray, NoProperties }++ internal enum CopyFormat+ {+ /// <summary>+ /// Tab separated values+ /// </summary>+ TSV,++ /// <summary>+ /// Comma separated values+ /// </summary>+ CSV,++ /// <summary>+ /// Semicolon separated values (CSV French)+ /// </summary>+ FSV,+ }++ /// <summary>+ /// Parse the text to an array of JObject, or null if the text does not represent a JSON array of objects.+ /// </summary>+ /// <param name="text"></param>+ /// <returns></returns>+ private static JObject[]? ParseJsonArray(string? text)+ {+ try+ {+ // Coalesce to empty string to prevent ArgumentNullException (returns null instead).+ var array = JsonConvert.DeserializeObject(text ?? "") as JArray;+ return array?.Cast<JObject>().ToArray();+ }+ catch (JsonException)+ {+ return null;+ }+ catch (InvalidCastException)+ {+ return null;+ }+ }++ internal static JObject FlattenJsonObject(JObject json)+ {+ var flattened = new JObject();++ foreach (KeyValuePair<string, JToken?> kv in json)+ {+ if (kv.Value is JObject jobj)+ {+ // Flatten objects by prefixing their property names with the parent property name, underscore separated.+ foreach (KeyValuePair<string, JToken?> kv2 in FlattenJsonObject(jobj))+ {+ flattened.Add($"{kv.Key}_{kv2.Key}", kv2.Value);+ }+ }+ else if (kv.Value is JValue)+ {+ flattened[kv.Key] = kv.Value;+ }+ // else strip out any array values+ }++ return flattened;+ }+}diff --git a/src/app/dev/DevToys.Tools/SmartDetection/JsonArrayDataTypeDetector.cs b/src/app/dev/DevToys.Tools/SmartDetection/JsonArrayDataTypeDetector.csnew file mode 100644index 000000000..3e0ce26ad--- /dev/null+++ b/src/app/dev/DevToys.Tools/SmartDetection/JsonArrayDataTypeDetector.cs@@ -0,0 +1,52 @@+using Microsoft.Extensions.Logging;+using Newtonsoft.Json.Linq;++namespace DevToys.Tools.SmartDetection;++/// <summary>+/// Detect non-empty JSON arrays.+/// </summary>+[Export(typeof(IDataTypeDetector))]+[DataTypeName(PredefinedCommonDataTypeNames.JsonArray, baseName: PredefinedCommonDataTypeNames.Json)]+internal sealed partial class JsonArrayDataTypeDetector : IDataTypeDetector+{+ private readonly ILogger _logger;++ [ImportingConstructor]+ public JsonArrayDataTypeDetector()+ {+ _logger = this.Log();+ }++ public ValueTask<DataDetectionResult> TryDetectDataAsync(+ object data,+ DataDetectionResult? resultFromBaseDetector,+ CancellationToken cancellationToken)+ {+ if (resultFromBaseDetector is not null+ && resultFromBaseDetector.Data is string dataString+ && IsJsonArray(dataString))+ {+ return ValueTask.FromResult(new DataDetectionResult(Success: true, Data: dataString));+ }++ return ValueTask.FromResult(DataDetectionResult.Unsuccessful);+ }++ private static bool IsJsonArray(string data)+ {+ try+ {+ var jtoken = JToken.Parse(data);+ return jtoken is JArray ja && ja.Count > 0;+ }+ catch (Exception)+ {+ // Exception in parsing json. It likely mean the text isn't a JSON.+ return false;+ }+ }++ [LoggerMessage(1, LogLevel.Error, "An unexpected exception happened while trying to detect some JSON Array data.")]+ partial void LogError(Exception ex);+}diff --git a/src/app/dev/DevToys.Tools/Tools/Converters/JsonTable/JsonTableConverter.Designer.cs b/src/app/dev/DevToys.Tools/Tools/Converters/JsonTable/JsonTableConverter.Designer.csnew file mode 100644index 000000000..02407e677--- /dev/null+++ b/src/app/dev/DevToys.Tools/Tools/Converters/JsonTable/JsonTableConverter.Designer.cs@@ -0,0 +1,216 @@+//------------------------------------------------------------------------------+// <auto-generated>+// This code was generated by a tool.+// Runtime Version:4.0.30319.42000+//+// Changes to this file may cause incorrect behavior and will be lost if+// the code is regenerated.+// </auto-generated>+//------------------------------------------------------------------------------++namespace DevToys.Tools.Tools.Converters.JsonTable {+ using System;+++ /// <summary>+ /// A strongly-typed resource class, for looking up localized strings, etc.+ /// </summary>+ // This class was auto-generated by the StronglyTypedResourceBuilder+ // class via a tool like ResGen or Visual Studio.+ // To add or remove a member, edit your .ResX file then rerun ResGen+ // with the /str option, or rebuild your VS project.+ [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")]+ [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]+ [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]+ internal class JsonTableConverter {++ private static global::System.Resources.ResourceManager resourceMan;++ private static global::System.Globalization.CultureInfo resourceCulture;++ [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]+ internal JsonTableConverter() {+ }++ /// <summary>+ /// Returns the cached ResourceManager instance used by this class.+ /// </summary>+ [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]+ internal static global::System.Resources.ResourceManager ResourceManager {+ get {+ if (object.ReferenceEquals(resourceMan, null)) {+ global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("DevToys.Tools.Tools.Converters.JsonTable.JsonTableConverter", typeof(JsonTableConverter).Assembly);+ resourceMan = temp;+ }+ return resourceMan;+ }+ }++ /// <summary>+ /// Overrides the current thread's CurrentUICulture property for all+ /// resource lookups using this strongly typed resource class.+ /// </summary>+ [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]+ internal static global::System.Globalization.CultureInfo Culture {+ get {+ return resourceCulture;+ }+ set {+ resourceCulture = value;+ }+ }++ /// <summary>+ /// Looks up a localized string similar to JSON array to tabular form converter tool.+ /// </summary>+ internal static string AccessibleName {+ get {+ return ResourceManager.GetString("AccessibleName", resourceCulture);+ }+ }++ /// <summary>+ /// Looks up a localized string similar to Select which format is copied to clipboard.+ /// </summary>+ internal static string ClipboardFormatDescription {+ get {+ return ResourceManager.GetString("ClipboardFormatDescription", resourceCulture);+ }+ }++ /// <summary>+ /// Looks up a localized string similar to Clipboard format.+ /// </summary>+ internal static string ClipboardFormatTitle {+ get {+ return ResourceManager.GetString("ClipboardFormatTitle", resourceCulture);+ }+ }++ /// <summary>+ /// Looks up a localized string similar to Configuration.+ /// </summary>+ internal static string Configuration {+ get {+ return ResourceManager.GetString("Configuration", resourceCulture);+ }+ }++ /// <summary>+ /// Looks up a localized string similar to Comma separated values (CSV).+ /// </summary>+ internal static string CopyFormatCSV {+ get {+ return ResourceManager.GetString("CopyFormatCSV", resourceCulture);+ }+ }++ /// <summary>+ /// Looks up a localized string similar to Semicolon separated values (CSV French).+ /// </summary>+ internal static string CopyFormatFSV {+ get {+ return ResourceManager.GetString("CopyFormatFSV", resourceCulture);+ }+ }++ /// <summary>+ /// Looks up a localized string similar to Tab separated values (TSV).+ /// </summary>+ internal static string CopyFormatTSV {+ get {+ return ResourceManager.GetString("CopyFormatTSV", resourceCulture);+ }+ }++ /// <summary>+ /// Looks up a localized string similar to Convert a JSON array to tabular format, export to CSV or TSV.+ /// </summary>+ internal static string Description {+ get {+ return ResourceManager.GetString("Description", resourceCulture);+ }+ }++ /// <summary>+ /// Looks up a localized string similar to Input.+ /// </summary>+ internal static string Input {+ get {+ return ResourceManager.GetString("Input", resourceCulture);+ }+ }++ /// <summary>+ /// Looks up a localized string similar to Text or path to a file to convert from JSON.+ /// </summary>+ internal static string InputOptionDescription {+ get {+ return ResourceManager.GetString("InputOptionDescription", resourceCulture);+ }+ }++ /// <summary>+ /// Looks up a localized string similar to Please provide a valid JSON array of objects.+ /// </summary>+ internal static string JsonError {+ get {+ return ResourceManager.GetString("JsonError", resourceCulture);+ }+ }++ /// <summary>+ /// Looks up a localized string similar to JSON Array to Table.+ /// </summary>+ internal static string LongDisplayTitle {+ get {+ return ResourceManager.GetString("LongDisplayTitle", resourceCulture);+ }+ }++ /// <summary>+ /// Looks up a localized string similar to Output.+ /// </summary>+ internal static string Output {+ get {+ return ResourceManager.GetString("Output", resourceCulture);+ }+ }++ /// <summary>+ /// Looks up a localized string similar to File where the result should be persisted..+ /// </summary>+ internal static string OutputFileOptionDescription {+ get {+ return ResourceManager.GetString("OutputFileOptionDescription", resourceCulture);+ }+ }++ /// <summary>+ /// Looks up a localized string similar to Select which format to output.+ /// </summary>+ internal static string OutputFormatDescription {+ get {+ return ResourceManager.GetString("OutputFormatDescription", resourceCulture);+ }+ }++ /// <summary>+ /// Looks up a localized string similar to JSON,CSV,TSV,French,Excel,Tabular,Data.+ /// </summary>+ internal static string SearchKeywords {+ get {+ return ResourceManager.GetString("SearchKeywords", resourceCulture);+ }+ }++ /// <summary>+ /// Looks up a localized string similar to JSON > Table.+ /// </summary>+ internal static string ShortDisplayTitle {+ get {+ return ResourceManager.GetString("ShortDisplayTitle", resourceCulture);+ }+ }+ }+}diff --git a/src/app/dev/DevToys.Tools/Tools/Converters/JsonTable/JsonTableConverter.resx b/src/app/dev/DevToys.Tools/Tools/Converters/JsonTable/JsonTableConverter.resxnew file mode 100644index 000000000..2e44ed8c8--- /dev/null+++ b/src/app/dev/DevToys.Tools/Tools/Converters/JsonTable/JsonTableConverter.resx@@ -0,0 +1,178 @@+<?xml version="1.0" encoding="utf-8"?>+<root>+ <!--+ Microsoft ResX Schema++ Version 2.0++ The primary goals of this format is to allow a simple XML format+ that is mostly human readable. The generation and parsing of the+ various data types are done through the TypeConverter classes+ associated with the data types.++ Example:++ ... ado.net/XML headers & schema ...+ <resheader name="resmimetype">text/microsoft-resx</resheader>+ <resheader name="version">2.0</resheader>+ <resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>+ <resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>+ <data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>+ <data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>+ <data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">+ <value>[base64 mime encoded serialized .NET Framework object]</value>+ </data>+ <data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">+ <value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>+ <comment>This is a comment</comment>+ </data>++ There are any number of "resheader" rows that contain simple+ name/value pairs.++ Each data row contains a name, and value. The row also contains a+ type or mimetype. Type corresponds to a .NET class that support+ text/value conversion through the TypeConverter architecture.+ Classes that don't support this are serialized and stored with the+ mimetype set.++ The mimetype is used for serialized objects, and tells the+ ResXResourceReader how to depersist the object. This is currently not+ extensible. For a given mimetype the value must be set accordingly:++ Note - application/x-microsoft.net.object.binary.base64 is the format+ that the ResXResourceWriter will generate, however the reader can+ read any of the formats listed below.++ mimetype: application/x-microsoft.net.object.binary.base64+ value : The object must be serialized with+ : System.Runtime.Serialization.Formatters.Binary.BinaryFormatter+ : and then encoded with base64 encoding.++ mimetype: application/x-microsoft.net.object.soap.base64+ value : The object must be serialized with+ : System.Runtime.Serialization.Formatters.Soap.SoapFormatter+ : and then encoded with base64 encoding.++ mimetype: application/x-microsoft.net.object.bytearray.base64+ value : The object must be serialized into a byte array+ : using a System.ComponentModel.TypeConverter+ : and then encoded with base64 encoding.+ -->+ <xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">+ <xsd:import namespace="http://www.w3.org/XML/1998/namespace" />+ <xsd:element name="root" msdata:IsDataSet="true">+ <xsd:complexType>+ <xsd:choice maxOccurs="unbounded">+ <xsd:element name="metadata">+ <xsd:complexType>+ <xsd:sequence>+ <xsd:element name="value" type="xsd:string" minOccurs="0" />+ </xsd:sequence>+ <xsd:attribute name="name" use="required" type="xsd:string" />+ <xsd:attribute name="type" type="xsd:string" />+ <xsd:attribute name="mimetype" type="xsd:string" />+ <xsd:attribute ref="xml:space" />+ </xsd:complexType>+ </xsd:element>+ <xsd:element name="assembly">+ <xsd:complexType>+ <xsd:attribute name="alias" type="xsd:string" />+ <xsd:attribute name="name" type="xsd:string" />+ </xsd:complexType>+ </xsd:element>+ <xsd:element name="data">+ <xsd:complexType>+ <xsd:sequence>+ <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />+ <xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />+ </xsd:sequence>+ <xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />+ <xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />+ <xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />+ <xsd:attribute ref="xml:space" />+ </xsd:complexType>+ </xsd:element>+ <xsd:element name="resheader">+ <xsd:complexType>+ <xsd:sequence>+ <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />+ </xsd:sequence>+ <xsd:attribute name="name" type="xsd:string" use="required" />+ </xsd:complexType>+ </xsd:element>+ </xsd:choice>+ </xsd:complexType>+ </xsd:element>+ </xsd:schema>+ <resheader name="resmimetype">+ <value>text/microsoft-resx</value>+ </resheader>+ <resheader name="version">+ <value>2.0</value>+ </resheader>+ <resheader name="reader">+ <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>+ </resheader>+ <resheader name="writer">+ <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>+ </resheader>+ <data name="AccessibleName" xml:space="preserve">+ <value>JSON array to tabular form converter tool</value>+ </data>+ <data name="LongDisplayTitle" xml:space="preserve">+ <value>JSON Array to Table</value>+ <comment>Grid view title</comment>+ </data>+ <data name="Description" xml:space="preserve">+ <value>Convert a JSON array to tabular format, export to CSV or TSV</value>+ <comment>Grid view description</comment>+ </data>+ <data name="ShortDisplayTitle" xml:space="preserve">+ <value>JSON > Table</value>+ <comment>Sidebar heading</comment>+ </data>+ <data name="SearchKeywords" xml:space="preserve">+ <value>JSON,CSV,TSV,French,Excel,Tabular,Data</value>+ </data>+ <data name="Configuration" xml:space="preserve">+ <value>Configuration</value>+ </data>+ <data name="Input" xml:space="preserve">+ <value>Input</value>+ </data>+ <data name="Output" xml:space="preserve">+ <value>Output</value>+ </data>+ <data name="JsonError" xml:space="preserve">+ <value>Please provide a valid JSON array of objects</value>+ <comment>Displayed when input isn't a valid JSON array</comment>+ </data>+ <data name="ClipboardFormatDescription" xml:space="preserve">+ <value>Select which format is copied to clipboard</value>+ </data>+ <data name="ClipboardFormatTitle" xml:space="preserve">+ <value>Clipboard format</value>+ </data>+ <data name="CopyFormatCSV" xml:space="preserve">+ <value>Comma separated values (CSV)</value>+ </data>+ <data name="CopyFormatTSV" xml:space="preserve">+ <value>Tab separated values (TSV)</value>+ </data>+ <data name="CopyFormatFSV" xml:space="preserve">+ <value>Semicolon separated values (CSV French)</value>+ </data>+ <data name="InputOptionDescription" xml:space="preserve">+ <value>Text or path to a file to convert from JSON</value>+ <comment>CLI parameter description</comment>+ </data>+ <data name="OutputFileOptionDescription" xml:space="preserve">+ <value>File where the result should be persisted.</value>+ <comment>CLI parameter description</comment>+ </data>+ <data name="OutputFormatDescription" xml:space="preserve">+ <value>Select which format to output</value>+ <comment>CLI parameter description</comment>+ </data>+</root>\ No newline at end of filediff --git a/src/app/dev/DevToys.Tools/Tools/Converters/JsonTable/JsonTableConverterCommandLineTool.cs b/src/app/dev/DevToys.Tools/Tools/Converters/JsonTable/JsonTableConverterCommandLineTool.csnew file mode 100644index 000000000..69385bf23--- /dev/null+++ b/src/app/dev/DevToys.Tools/Tools/Converters/JsonTable/JsonTableConverterCommandLineTool.cs@@ -0,0 +1,95 @@+using DevToys.Tools.Helpers;+using DevToys.Tools.Tools.Converters.JsonTable;+using Microsoft.Extensions.Logging;+using OneOf;++namespace DevToys.Tools.Tools.Converters.JsonYaml;++[Export(typeof(ICommandLineTool))]+[Name("JsonTableConverter")]+[CommandName(+ Name = "jsonToTable",+ ResourceManagerBaseName = "DevToys.Tools.Tools.Converters.JsonTable.JsonTableConverter",+ DescriptionResourceName = nameof(JsonTableConverter.Description))]+internal sealed class JsonTableConverterCommandLineTool : ICommandLineTool+{+ [CommandLineOption(+ Name = "input",+ Alias = "i",+ IsRequired = true,+ DescriptionResourceName = nameof(JsonTableConverter.InputOptionDescription))]+ internal OneOf<FileInfo, string>? Input { get; set; }++ [CommandLineOption(+ Name = "outputFile",+ Alias = "o",+ DescriptionResourceName = nameof(JsonTableConverter.OutputFileOptionDescription))]+ internal FileInfo? OutputFile { get; set; }++ // Default to comma-separated-values because output-to-file is the most likely use-case.+ [CommandLineOption(+ Name = "format",+ DescriptionResourceName = nameof(JsonTableConverter.ClipboardFormatDescription))]+ internal JsonTableHelper.CopyFormat Format { get; set; } = JsonTableHelper.CopyFormat.CSV;++ public ValueTask<int> InvokeAsync(ILogger logger, CancellationToken cancellationToken)+ {+ if (!Input.HasValue)+ {+ Console.Error.WriteLine(JsonYamlConverter.InvalidInputOrFileCommand);+ return ValueTask.FromResult(-1);+ }++ return Input.Value.Match(+ file =>+ InvokeFileAsync(+ file,+ cancellationToken),+ text =>+ InvokeCliAsync(+ text,+ cancellationToken));+ }++ private async ValueTask<int> InvokeCliAsync(string input, CancellationToken cancellationToken)+ {+ Guard.IsNotNull(Input);++ JsonTableHelper.ConvertResult conversionResult = JsonTableHelper.ConvertFromJson(input, Format, cancellationToken);+ string resultText = conversionResult.Text;++ cancellationToken.ThrowIfCancellationRequested();++ if (conversionResult.Error != JsonTableHelper.ConvertResultError.None)+ {+ Console.Error.WriteLine(conversionResult.Error);+ return -1;+ }++ if (OutputFile == null)+ {+ Console.WriteLine(resultText);+ }+ else+ {+ await File.WriteAllTextAsync(OutputFile.FullName, resultText, cancellationToken);+ }++ return 0;+ }++ private async ValueTask<int> InvokeFileAsync(FileInfo file, CancellationToken cancellationToken)+ {+ Guard.IsNotNull(OutputFile);++ if (!file.Exists)+ {+ Console.Error.WriteLine(JsonYamlConverter.InputFileNotFound);+ return -1;+ }++ string content = await File.ReadAllTextAsync(file.FullName, cancellationToken);++ return await InvokeCliAsync(content, cancellationToken);+ }+}diff --git a/src/app/dev/DevToys.Tools/Tools/Converters/JsonTable/JsonTableConverterGuiTool.cs b/src/app/dev/DevToys.Tools/Tools/Converters/JsonTable/JsonTableConverterGuiTool.csnew file mode 100644index 000000000..9da24bc83--- /dev/null+++ b/src/app/dev/DevToys.Tools/Tools/Converters/JsonTable/JsonTableConverterGuiTool.cs@@ -0,0 +1,210 @@+using System.Data;+using DevToys.Tools.Tools.Converters.JsonTable;+using Microsoft.Extensions.Logging;+using static DevToys.Tools.Helpers.JsonTableHelper;++namespace DevToys.Tools.Tools.Converters.JsonYaml;++[Export(typeof(IGuiTool))]+[Name("JsonTableConverter")]+[ToolDisplayInformation(+ IconFontName = "DevToys-Tools-Icons",+ IconGlyph = '\u0109',+ GroupName = PredefinedCommonToolGroupNames.Converters,+ ResourceManagerAssemblyIdentifier = nameof(DevToysToolsResourceManagerAssemblyIdentifier),+ ResourceManagerBaseName = "DevToys.Tools.Tools.Converters.JsonTable.JsonTableConverter",+ ShortDisplayTitleResourceName = nameof(JsonTableConverter.ShortDisplayTitle),+ LongDisplayTitleResourceName = nameof(JsonTableConverter.LongDisplayTitle),+ DescriptionResourceName = nameof(JsonTableConverter.Description),+ AccessibleNameResourceName = nameof(JsonTableConverter.AccessibleName),+ SearchKeywordsResourceName = nameof(JsonTableConverter.SearchKeywords))]+[AcceptedDataTypeName(PredefinedCommonDataTypeNames.JsonArray)]+internal sealed partial class JsonTableConverterGuiTool : IGuiTool, IDisposable+{+ /// <summary>+ /// Whether the tool should copy to clipboard as CSV or TSV.+ /// </summary>+ /// <remarks>+ /// Default to tab-separated-values because copy-to-Excel is the most likely use-case.+ /// </remarks>+ private static readonly SettingDefinition<CopyFormat> copyFormat+ = new(name: $"{nameof(JsonTableConverterGuiTool)}.{nameof(copyFormat)}", defaultValue: CopyFormat.TSV);++ private enum GridColumn+ {+ Content+ }++ private enum GridRow+ {+ Header,+ Content,+ }++ private readonly ILogger _logger;+ private readonly ISettingsProvider _settingsProvider;+ private readonly IClipboard _clipboard;++ private readonly IUIMultiLineTextInput _inputTextArea = MultilineTextInput("json-input-text-area");+ private readonly IUIDataGrid _outputDataGrid = DataGrid("output-data-grid");+ private readonly IUIButton _copyButton = Button("copy-data-grid");++ private readonly DisposableSemaphore _semaphore = new(); // one convert at a time+ private CancellationTokenSource? _cancellationTokenSource;++ [ImportingConstructor]+ public JsonTableConverterGuiTool(ISettingsProvider settingsProvider, IClipboard clipboard)+ {+ _logger = this.Log();+ _settingsProvider = settingsProvider;+ _clipboard = clipboard;+ }++ internal Task? WorkTask { get; private set; }++ public UIToolView View+ {+ get+ {+ _copyButton+ .Icon("FluentSystemIcons", '\uF32B')+ .OnClick(OnCopyDataGrid)+ .Disable(); // disable until valid JSON is input++ return new(+ isScrollable: false,+ Grid()+ .ColumnLargeSpacing()+ .RowLargeSpacing()+ .Rows(+ (GridRow.Header, Auto),+ (GridRow.Content, new UIGridLength(1, UIGridUnitType.Fraction))+ )+ .Columns(+ (GridColumn.Content, new UIGridLength(1, UIGridUnitType.Fraction))+ )+ .Cells(+ Cell(+ GridRow.Header,+ GridColumn.Content,+ Stack().Vertical().WithChildren(+ Label()+ .Text(JsonTableConverter.Configuration),++ Setting("clipboard-copy-format-setting")+ .Icon("FluentSystemIcons", '\uF7ED')+ .Title(JsonTableConverter.ClipboardFormatTitle)+ .Description(JsonTableConverter.ClipboardFormatDescription)+ .Handle(+ _settingsProvider,+ copyFormat,+ null,+ Item(JsonTableConverter.CopyFormatTSV, CopyFormat.TSV),+ Item(JsonTableConverter.CopyFormatCSV, CopyFormat.CSV),+ Item(JsonTableConverter.CopyFormatFSV, CopyFormat.FSV)+ )+ )+ ),+ Cell(+ GridRow.Content,+ GridColumn.Content,+ SplitGrid()+ .Vertical()+ .WithLeftPaneChild(+ _inputTextArea+ .Title(JsonTableConverter.Input)+ .Language("json")+ .OnTextChanged(OnInputTextChanged))+ .WithRightPaneChild(+ _outputDataGrid+ .Title(JsonTableConverter.Output)+ .CommandBarExtraContent(_copyButton)+ .Extendable())+ )+ )+ );+ }+ }++ // Smart detection handler.+ public void OnDataReceived(string dataTypeName, object? parsedData)+ {+ if (dataTypeName == PredefinedCommonDataTypeNames.JsonArray &&+ parsedData is string jsonStrongTypedParsedData)+ {+ _inputTextArea.Text(jsonStrongTypedParsedData);+ }+ }++ public void Dispose()+ {+ _cancellationTokenSource?.Cancel();+ _cancellationTokenSource?.Dispose();+ _semaphore.Dispose();+ }++ private void OnInputTextChanged(string text)+ {+ StartConvert(ConvertTarget.DataGrid);+ }++ private void StartConvert(ConvertTarget target)+ {+ _cancellationTokenSource?.Cancel();+ _cancellationTokenSource?.Dispose();+ _cancellationTokenSource = new CancellationTokenSource();++ WorkTask = ConvertAsync(target, _cancellationTokenSource.Token);+ }++ private async Task ConvertAsync(ConvertTarget target, CancellationToken cancellationToken)+ {+ using (await _semaphore.WaitAsync(cancellationToken))+ {+ await TaskSchedulerAwaiter.SwitchOffMainThreadAsync(cancellationToken);++ ConvertResult conversionResult = ConvertFromJson(_inputTextArea.Text, _settingsProvider.GetSetting(copyFormat), cancellationToken);++ if (conversionResult.Error == ConvertResultError.None && conversionResult.Data != null)+ {+ _copyButton.Enable();+ switch (target)+ {+ case ConvertTarget.DataGrid:+ SetDataGridData(conversionResult.Data);+ break;++ case ConvertTarget.Clipboard:+ await _clipboard.SetClipboardTextAsync(conversionResult.Text);+ break;+ }+ }+ else+ {+ _copyButton.Disable();+ SetDataGridError();+ }+ }+ }++ private enum ConvertTarget { DataGrid, Clipboard }++ private void SetDataGridData(DataGridContents table)+ {+ IUIDataGridRow[] rows = table.Rows.Select(r => Row(null, r)).ToArray();+ _outputDataGrid.WithColumns(table.Headings);+ _outputDataGrid.WithRows(rows);+ }++ private void SetDataGridError()+ {+ _outputDataGrid.WithColumns(JsonTableConverter.JsonError);+ _outputDataGrid.WithRows();+ }++ private ValueTask OnCopyDataGrid()+ {+ StartConvert(ConvertTarget.Clipboard);+ return ValueTask.CompletedTask;+ }+}diff --git a/src/app/tests/DevToys.UnitTests/Tools/Helpers/JsonTableHelperTests.cs b/src/app/tests/DevToys.UnitTests/Tools/Helpers/JsonTableHelperTests.csnew file mode 100644index 000000000..f98717dba--- /dev/null+++ b/src/app/tests/DevToys.UnitTests/Tools/Helpers/JsonTableHelperTests.cs@@ -0,0 +1,20 @@+using DevToys.Tools.Helpers;+using Newtonsoft.Json;+using Newtonsoft.Json.Linq;++namespace DevToys.UnitTests.Tools.Helpers;++public class JsonTableHelperTests+{+ [Theory]+ [InlineData("{\"a\":{\"b\":{\"c\":1}}}", "{\"a_b_c\":1}")]+ [InlineData("{\"a\":{\"b\":{\"c\":[]}}}", "{}")]+ [InlineData("{\"a\":{\"b\":1,\"c\":{\"d\":2},\"e\":[3,4]},\"f\":[5,6]}", "{\"a_b\":1,\"a_c_d\":2}")]+ public void Flatten(string inputJson, string expectedJson)+ {+ var obj = JsonConvert.DeserializeObject(inputJson) as JObject;+ JObject flattened = JsonTableHelper.FlattenJsonObject(obj);+ string serialized = JsonConvert.SerializeObject(flattened);+ serialized.Should().Be(expectedJson);+ }+}diff --git a/src/app/tests/DevToys.UnitTests/Tools/SmartDetection/JsonArrayDataTypeDetectorTests.cs b/src/app/tests/DevToys.UnitTests/Tools/SmartDetection/JsonArrayDataTypeDetectorTests.csnew file mode 100644index 000000000..cd65406f4--- /dev/null+++ b/src/app/tests/DevToys.UnitTests/Tools/SmartDetection/JsonArrayDataTypeDetectorTests.cs@@ -0,0 +1,35 @@+using System.Diagnostics.CodeAnalysis;+using System.Threading.Tasks;+using DevToys.Tools.SmartDetection;+using Microsoft.Extensions.Logging;++namespace DevToys.UnitTests.Tools.SmartDetection;++[ExcludeFromCodeCoverage]+public class JsonArrayDataTypeDetectorTests+{+ [Theory]+ [InlineData(null, false)]+ [InlineData("\"foo\"", false)]+ [InlineData("123", false)]+ [InlineData("", false)]+ [InlineData(" ", false)]+ [InlineData(" { } ", false)]+ [InlineData(" [ ] ", false)]+ [InlineData(" { \"foo\": 123 } ", false)]+ [InlineData(" [{ \"foo\": 123 }] ", true)]+ [InlineData(" [{ \"foo\": 123 }, { \"bar\": 456 }] ", true)]+ public async Task TryDetectDataAsync(string input, bool expectedResult)+ {+ LoggingExtensions.LoggerFactory ??= LoggerFactory.Create(builder => { });++ DataDetectionResult dataDetectionResult = new(false, input);+ JsonArrayDataTypeDetector sut = new();+ DataDetectionResult result = await sut.TryDetectDataAsync(+ input,+ dataDetectionResult,+ default);++ result.Success.Should().Be(expectedResult);+ }+}