Skip to content

A Swiss Army knife for developers.

Added Json to Yaml tool (#16)

Etienne Baudouxf77e44f8dcb6
diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md
index 41240acbc..3db75d1bb 100644
--- a/.github/PULL_REQUEST_TEMPLATE.md
+++ b/.github/PULL_REQUEST_TEMPLATE.md
@@ -38,4 +38,5 @@ Issue Number: N/A
Before creating this PR, have you:
- [ ] Followed the code style guideline as described in [CONTRIBUTING.md](https://github.com/veler/DevToys/blob/main/CONTRIBUTING.md)
-- [ ] Verified that the change work in Release build configuration
\ No newline at end of file
+- [ ] Verified that the change work in Release build configuration
+- [ ] Checked all unit tests pass
\ No newline at end of file
diff --git a/THIRD-PARTY-NOTICES.md b/THIRD-PARTY-NOTICES.md
index 4cb755c76..9bb291e1d 100644
--- a/THIRD-PARTY-NOTICES.md
+++ b/THIRD-PARTY-NOTICES.md
@@ -8,6 +8,7 @@ This project incorporates components from the projects listed below. The origina
4. GitInfo (https://github.com/devlooped/GitInfo)
5. Windows Community Toolkit (https://github.com/CommunityToolkit/WindowsCommunityToolkit)
6. Notepads App (https://github.com/JasonStein/Notepads)
+7. YamlDotNet (https://github.com/aaubry/YamlDotNet)
Newtonsoft.Json NOTICES AND INFORMATION BEGIN HERE
=========================================
@@ -192,6 +193,28 @@ furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+
+YamlDotNet NOTICES AND INFORMATION BEGIN HERE
+=========================================
+Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014 Antoine Aubry and contributors
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+of the Software, and to permit persons to whom the Software is furnished to do
+so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
diff --git a/src/Directory.Build.props b/src/Directory.Build.props
index 32f71782b..3b90dc2d2 100644
--- a/src/Directory.Build.props
+++ b/src/Directory.Build.props
@@ -34,5 +34,6 @@
<NuGet_MSTestTestFramework>2.1.1</NuGet_MSTestTestFramework>
<NuGet_NewtonsoftJson>12.0.3</NuGet_NewtonsoftJson>
<NuGet_SystemComposition>5.0.0</NuGet_SystemComposition>
+ <NuGet_YamlDotNet>11.2.1</NuGet_YamlDotNet>
</PropertyGroup>
</Project>
\ No newline at end of file
diff --git a/src/dev/impl/DevToys/DevToys.csproj b/src/dev/impl/DevToys/DevToys.csproj
index d1bd7eb78..310400df1 100644
--- a/src/dev/impl/DevToys/DevToys.csproj
+++ b/src/dev/impl/DevToys/DevToys.csproj
@@ -197,6 +197,11 @@
<DependentUpon>Notification.xaml</DependentUpon>
</Compile>
<Compile Include="UI\Converters\NullToVisibilityConverter.cs" />
+ <Compile Include="Helpers\JsonHelper.cs" />
+ <Compile Include="Helpers\YamlHelper.cs" />
+ <Compile Include="UI\Controls\FormattedTextBlock\Languages\Yaml.cs" />
+ <Compile Include="ViewModels\Tools\JsonYaml\JsonYamlToolProvider.cs" />
+ <Compile Include="ViewModels\Tools\JsonYaml\JsonYamlToolViewModel.cs" />
<Compile Include="ViewModels\Tools\ToolProviderBase.cs" />
<Compile Include="Core\UriActivationProtocolService.cs" />
<Compile Include="LanguageDefinition.cs" />
@@ -211,7 +216,6 @@
<Compile Include="App.xaml.cs">
<DependentUpon>App.xaml</DependentUpon>
</Compile>
- <Compile Include="Tools\ToolProviderBase.cs" />
<Compile Include="UI\Controls\CustomPathIcon.cs" />
<Compile Include="UI\Controls\CustomTextBox.xaml.cs">
<DependentUpon>CustomTextBox.xaml</DependentUpon>
@@ -278,6 +282,9 @@
<Compile Include="Views\Tools\HashGenerator\HashGeneratorToolPage.xaml.cs">
<DependentUpon>HashGeneratorToolPage.xaml</DependentUpon>
</Compile>
+ <Compile Include="Views\Tools\JsonYaml\JsonYamlToolPage.xaml.cs">
+ <DependentUpon>JsonYamlToolPage.xaml</DependentUpon>
+ </Compile>
<Compile Include="Views\Tools\JsonFormatter\JsonFormatterToolPage.xaml.cs">
<DependentUpon>JsonFormatterToolPage.xaml</DependentUpon>
</Compile>
@@ -323,6 +330,8 @@
<Link>Package.appxmanifest</Link>
<SubType>Designer</SubType>
</AppxManifest>
+ <PRIResource Include="Strings\fr\JsonYaml.resw" />
+ <PRIResource Include="Strings\en\JsonYaml.resw" />
<Page Include="Themes\Custom.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
@@ -367,6 +376,10 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
+ <Page Include="Views\Tools\JsonYaml\JsonYamlToolPage.xaml">
+ <Generator>MSBuild:Compile</Generator>
+ <SubType>Designer</SubType>
+ </Page>
<Page Include="Views\Tools\JsonFormatter\JsonFormatterToolPage.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
@@ -416,6 +429,7 @@
<PackageReference Include="Microsoft.UI.Xaml" Version="$(NuGet_MicrosoftUIXaml)" />
<PackageReference Include="Newtonsoft.Json" Version="$(NuGet_NewtonsoftJson)" />
<PackageReference Include="System.Composition" Version="$(NuGet_SystemComposition)" />
+ <PackageReference Include="YamlDotNet" Version="$(NuGet_YamlDotNet)" />
</ItemGroup>
<ItemGroup>
<Service Include="{508349B6-6B84-4DF5-91F0-309BEEBAD82D}" />
diff --git a/src/dev/impl/DevToys/Helpers/JsonHelper.cs b/src/dev/impl/DevToys/Helpers/JsonHelper.cs
new file mode 100644
index 000000000..9d557eea1
--- /dev/null
+++ b/src/dev/impl/DevToys/Helpers/JsonHelper.cs
@@ -0,0 +1,49 @@
+#nullable enable
+
+using DevToys.Core;
+using Newtonsoft.Json;
+using Newtonsoft.Json.Linq;
+using System;
+
+namespace DevToys.Helpers
+{
+ internal static class JsonHelper
+ {
+ /// <summary>
+ /// Detects whether the given string is a valid JSON or not.
+ /// </summary>
+ internal static bool IsValidJson(string? input)
+ {
+ if (string.IsNullOrWhiteSpace(input))
+ {
+ return false;
+ }
+
+ input = input!.Trim();
+
+ if ((input.StartsWith("{") && input.EndsWith("}")) //For object
+ || (input.StartsWith("[") && input.EndsWith("]"))) //For array
+ {
+ try
+ {
+ JToken? jtoken = JToken.Parse(input);
+ return jtoken is not null;
+ }
+ catch (JsonReaderException)
+ {
+ // Exception in parsing json. It likely mean the text isn't a JSON.
+ return false;
+ }
+ catch (Exception ex) //some other exception
+ {
+ Logger.LogFault("Check is string if JSON", ex);
+ return false;
+ }
+ }
+ else
+ {
+ return false;
+ }
+ }
+ }
+}
diff --git a/src/dev/impl/DevToys/Helpers/YamlHelper.cs b/src/dev/impl/DevToys/Helpers/YamlHelper.cs
new file mode 100644
index 000000000..a12f4468b
--- /dev/null
+++ b/src/dev/impl/DevToys/Helpers/YamlHelper.cs
@@ -0,0 +1,33 @@
+#nullable enable
+
+using System;
+using YamlDotNet.Serialization;
+
+namespace DevToys.Helpers
+{
+ internal static class YamlHelper
+ {
+ /// <summary>
+ /// Detects whether the given string is a valid YAML or not.
+ /// </summary>
+ internal static bool IsValidYaml(string? input)
+ {
+ if (string.IsNullOrWhiteSpace(input))
+ {
+ return false;
+ }
+
+ input = input!.Trim();
+
+ try
+ {
+ object result = new DeserializerBuilder().Build().Deserialize<object>(input);
+ return result is not null && result is not string;
+ }
+ catch (Exception)
+ {
+ return false;
+ }
+ }
+ }
+}
diff --git a/src/dev/impl/DevToys/LanguageManager.cs b/src/dev/impl/DevToys/LanguageManager.cs
index 9cf47293d..00d3cc608 100644
--- a/src/dev/impl/DevToys/LanguageManager.cs
+++ b/src/dev/impl/DevToys/LanguageManager.cs
@@ -28,6 +28,7 @@ namespace DevToys
private readonly CommonStrings _common = new CommonStrings();
private readonly HashGeneratorStrings _hashgenerator = new HashGeneratorStrings();
private readonly JsonFormatterStrings _jsonformatter = new JsonFormatterStrings();
+ private readonly JsonYamlStrings _jsonyaml = new JsonYamlStrings();
private readonly MainPageStrings _mainpage = new MainPageStrings();
private readonly RegExStrings _regex = new RegExStrings();
private readonly SettingsStrings _settings = new SettingsStrings();
@@ -63,6 +64,11 @@ namespace DevToys
/// </summary>
public JsonFormatterStrings JsonFormatter => _jsonformatter;
+ /// <summary>
+ /// Gets the <see cref="JsonYamlStrings"/>.
+ /// </summary>
+ public JsonYamlStrings JsonYaml => _jsonyaml;
+
/// <summary>
/// Gets the <see cref="MainPageStrings"/>.
/// </summary>
@@ -360,6 +366,71 @@ namespace DevToys
public string TwoSpaces => _resources.GetString("TwoSpaces");
}
+ public class JsonYamlStrings : ObservableObject
+ {
+ private readonly ResourceLoader _resources = ResourceLoader.GetForViewIndependentUse("JsonYaml");
+
+ /// <summary>
+ /// Gets the resource Configuration.
+ /// </summary>
+ public string Configuration => _resources.GetString("Configuration");
+
+ /// <summary>
+ /// Gets the resource ConversionDescription.
+ /// </summary>
+ public string ConversionDescription => _resources.GetString("ConversionDescription");
+
+ /// <summary>
+ /// Gets the resource ConversionTitle.
+ /// </summary>
+ public string ConversionTitle => _resources.GetString("ConversionTitle");
+
+ /// <summary>
+ /// Gets the resource DisplayName.
+ /// </summary>
+ public string DisplayName => _resources.GetString("DisplayName");
+
+ /// <summary>
+ /// Gets the resource FourSpaces.
+ /// </summary>
+ public string FourSpaces => _resources.GetString("FourSpaces");
+
+ /// <summary>
+ /// Gets the resource Indentation.
+ /// </summary>
+ public string Indentation => _resources.GetString("Indentation");
+
+ /// <summary>
+ /// Gets the resource Input.
+ /// </summary>
+ public string Input => _resources.GetString("Input");
+
+ /// <summary>
+ /// Gets the resource InvalidYaml.
+ /// </summary>
+ public string InvalidYaml => _resources.GetString("InvalidYaml");
+
+ /// <summary>
+ /// Gets the resource JsonToYaml.
+ /// </summary>
+ public string JsonToYaml => _resources.GetString("JsonToYaml");
+
+ /// <summary>
+ /// Gets the resource Output.
+ /// </summary>
+ public string Output => _resources.GetString("Output");
+
+ /// <summary>
+ /// Gets the resource TwoSpaces.
+ /// </summary>
+ public string TwoSpaces => _resources.GetString("TwoSpaces");
+
+ /// <summary>
+ /// Gets the resource YamlToJson.
+ /// </summary>
+ public string YamlToJson => _resources.GetString("YamlToJson");
+ }
+
public class MainPageStrings : ObservableObject
{
private readonly ResourceLoader _resources = ResourceLoader.GetForViewIndependentUse("MainPage");
diff --git a/src/dev/impl/DevToys/Strings/en/JsonYaml.resw b/src/dev/impl/DevToys/Strings/en/JsonYaml.resw
new file mode 100644
index 000000000..6af3eac6c
--- /dev/null
+++ b/src/dev/impl/DevToys/Strings/en/JsonYaml.resw
@@ -0,0 +1,156 @@
+<?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="Configuration" xml:space="preserve">
+ <value>Configuration</value>
+ </data>
+ <data name="ConversionDescription" xml:space="preserve">
+ <value>Select which conversion mode you want to use</value>
+ </data>
+ <data name="ConversionTitle" xml:space="preserve">
+ <value>Conversion</value>
+ </data>
+ <data name="DisplayName" xml:space="preserve">
+ <value>Json &lt;&gt; Yaml</value>
+ </data>
+ <data name="FourSpaces" xml:space="preserve">
+ <value>4 spaces</value>
+ </data>
+ <data name="Indentation" xml:space="preserve">
+ <value>Indentation</value>
+ </data>
+ <data name="Input" xml:space="preserve">
+ <value>Input</value>
+ </data>
+ <data name="InvalidYaml" xml:space="preserve">
+ <value>The entered YAML is invalid.</value>
+ </data>
+ <data name="JsonToYaml" xml:space="preserve">
+ <value>Json to Yaml</value>
+ </data>
+ <data name="Output" xml:space="preserve">
+ <value>Output</value>
+ </data>
+ <data name="TwoSpaces" xml:space="preserve">
+ <value>2 spaces</value>
+ </data>
+ <data name="YamlToJson" xml:space="preserve">
+ <value>Yaml to Json</value>
+ </data>
+</root>
\ No newline at end of file
diff --git a/src/dev/impl/DevToys/Strings/fr/JsonYaml.resw b/src/dev/impl/DevToys/Strings/fr/JsonYaml.resw
new file mode 100644
index 000000000..c13bae637
--- /dev/null
+++ b/src/dev/impl/DevToys/Strings/fr/JsonYaml.resw
@@ -0,0 +1,156 @@
+<?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="Configuration" xml:space="preserve">
+ <value>Configuration</value>
+ </data>
+ <data name="ConversionDescription" xml:space="preserve">
+ <value>Sélectionner le mode de conversion désiré</value>
+ </data>
+ <data name="ConversionTitle" xml:space="preserve">
+ <value>Conversion</value>
+ </data>
+ <data name="DisplayName" xml:space="preserve">
+ <value>Json &lt;&gt; Yaml</value>
+ </data>
+ <data name="FourSpaces" xml:space="preserve">
+ <value>4 espaces</value>
+ </data>
+ <data name="Indentation" xml:space="preserve">
+ <value>Indentation</value>
+ </data>
+ <data name="Input" xml:space="preserve">
+ <value>Entrée</value>
+ </data>
+ <data name="InvalidYaml" xml:space="preserve">
+ <value>Le YAML saisie est invalide.</value>
+ </data>
+ <data name="JsonToYaml" xml:space="preserve">
+ <value>Json vers Yaml</value>
+ </data>
+ <data name="Output" xml:space="preserve">
+ <value>Sortie</value>
+ </data>
+ <data name="TwoSpaces" xml:space="preserve">
+ <value>2 espaces</value>
+ </data>
+ <data name="YamlToJson" xml:space="preserve">
+ <value>Yaml vers Json</value>
+ </data>
+</root>
\ No newline at end of file
diff --git a/src/dev/impl/DevToys/Themes/Custom.xaml b/src/dev/impl/DevToys/Themes/Custom.xaml
index ab9ef51d6..76e2496d8 100644
--- a/src/dev/impl/DevToys/Themes/Custom.xaml
+++ b/src/dev/impl/DevToys/Themes/Custom.xaml
@@ -16,6 +16,7 @@
<x:String x:Key="JsonFormatterToolProvider">M 51.371 33.762 L 51.371 45.772 C 46.391 45.772 42.641 46.799 40.121 48.852 C 37.608 50.899 35.814 54.655 34.741 60.122 C 33.768 63.395 32.974 67.035 32.361 71.042 C 31.754 75.042 31.291 78.435 30.971 81.222 C 30.651 84.002 30.174 86.762 29.541 89.502 C 28.908 92.235 27.881 94.552 26.461 96.452 C 25.048 98.359 23.144 99.825 20.751 100.852 C 23.098 101.779 25.028 103.329 26.541 105.502 C 28.054 107.675 29.164 110.262 29.871 113.262 C 30.578 116.269 31.104 119.212 31.451 122.092 C 31.791 124.972 32.144 128.195 32.511 131.762 C 32.878 135.322 33.281 138.129 33.721 140.182 C 34.694 146.529 36.524 151.155 39.211 154.062 C 41.898 156.969 45.974 158.422 51.441 158.422 L 51.441 170.432 C 43.041 170.432 36.364 168.819 31.411 165.592 C 26.451 162.372 22.948 157.245 20.901 150.212 C 20.414 148.505 20.048 145.785 19.801 142.052 C 19.554 138.312 19.298 134.685 19.031 131.172 C 18.764 127.659 18.314 124.179 17.681 120.732 C 17.041 117.292 15.891 114.509 14.231 112.382 C 12.571 110.262 10.398 109.202 7.711 109.202 L 7.711 94.992 C 11.818 94.992 14.724 92.112 16.431 86.352 C 17.211 83.859 18.091 78.682 19.071 70.822 C 19.071 70.629 19.094 70.432 19.141 70.232 C 19.434 68.039 19.678 66.259 19.871 64.892 C 20.071 63.519 20.401 61.712 20.861 59.472 C 21.328 57.225 21.818 55.355 22.331 53.862 C 22.844 52.375 23.551 50.655 24.451 48.702 C 25.358 46.749 26.348 45.125 27.421 43.832 C 28.494 42.539 29.811 41.195 31.371 39.802 C 32.938 38.409 34.671 37.309 36.571 36.502 C 38.478 35.702 40.688 35.045 43.201 34.532 C 45.714 34.019 48.438 33.762 51.371 33.762 ZM 110.175 78.802 C 110.175 82.122 109.259 84.832 107.425 86.932 C 105.599 89.032 103.099 90.082 99.925 90.082 C 96.899 90.082 94.432 88.995 92.525 86.822 C 90.619 84.649 89.665 81.975 89.665 78.802 C 89.665 75.529 90.582 72.832 92.415 70.712 C 94.249 68.585 96.752 67.522 99.925 67.522 C 102.905 67.522 105.359 68.622 107.285 70.822 C 109.212 73.015 110.175 75.675 110.175 78.802 Z M 87.325 164.932 L 87.325 159.952 C 90.599 159.952 93.515 158.805 96.075 156.512 C 98.642 154.219 99.925 151.485 99.925 148.312 C 96.899 148.312 94.372 147.212 92.345 145.012 C 90.319 142.819 89.305 140.135 89.305 136.962 C 89.305 133.789 90.282 131.115 92.235 128.942 C 94.189 126.769 96.799 125.682 100.065 125.682 C 103.145 125.682 105.649 126.975 107.575 129.562 C 109.502 132.149 110.465 135.225 110.465 138.792 C 110.465 142.939 109.625 147.039 107.945 151.092 C 106.259 155.145 103.939 158.515 100.985 161.202 C 98.032 163.889 94.919 165.232 91.645 165.232 C 89.499 165.232 88.059 165.132 87.325 164.932 ZM 147.598 170.432 L 147.598 158.422 C 152.338 158.422 155.928 157.482 158.368 155.602 C 160.808 153.722 162.662 150.292 163.928 145.312 C 164.808 142.572 165.492 138.725 165.978 133.772 C 166.472 128.819 166.875 124.425 167.188 120.592 C 167.508 116.759 168.402 113.059 169.868 109.492 C 171.328 105.925 173.402 103.532 176.088 102.312 C 174.428 101.485 173.002 100.252 171.808 98.612 C 170.608 96.979 169.678 94.952 169.018 92.532 C 168.365 90.119 167.865 87.849 167.518 85.722 C 167.178 83.602 166.862 81.002 166.568 77.922 C 166.322 74.362 166.065 71.359 165.798 68.912 C 165.532 66.472 165.178 64.045 164.738 61.632 C 164.298 59.212 163.775 57.232 163.168 55.692 C 162.555 54.159 161.795 52.695 160.888 51.302 C 159.988 49.909 158.915 48.845 157.668 48.112 C 156.422 47.379 154.982 46.805 153.348 46.392 C 151.715 45.979 149.822 45.772 147.668 45.772 L 147.668 33.762 C 152.162 33.762 156.082 34.299 159.428 35.372 C 162.775 36.445 165.558 37.899 167.778 39.732 C 169.998 41.559 171.892 44.022 173.458 47.122 C 175.018 50.229 176.248 53.489 177.148 56.902 C 178.055 60.322 178.875 64.499 179.608 69.432 L 180.998 78.582 C 181.878 84.442 183.025 88.642 184.438 91.182 C 185.858 93.722 188.155 94.992 191.328 94.992 L 191.328 109.202 C 187.908 109.202 185.465 110.482 183.998 113.042 C 182.538 115.609 181.488 119.749 180.848 125.462 L 179.968 135.562 C 179.628 138.595 179.288 141.172 178.948 143.292 C 178.608 145.419 178.082 147.799 177.368 150.432 C 176.662 153.072 175.832 155.269 174.878 157.022 C 173.932 158.782 172.652 160.579 171.038 162.412 C 169.425 164.239 167.568 165.692 165.468 166.772 C 163.368 167.845 160.805 168.722 157.778 169.402 C 154.752 170.089 151.358 170.432 147.598 170.432 Z</x:String>
<x:String x:Key="TextDiffToolProvider">M 6 3 L 6 29 L 13 29 L 13 27 L 8 27 L 8 5 L 13 5 L 13 3 L 6 3 z M 15 3 L 15 5 L 17 5 L 17 3 L 15 3 z M 19 3 L 19 5 L 24 5 L 24 27 L 19 27 L 19 29 L 26 29 L 26 3 L 19 3 z M 15 7 L 15 9 L 17 9 L 17 7 L 15 7 z M 15 11 L 15 13 L 17 13 L 17 11 L 15 11 z M 15 15 L 15 17 L 17 17 L 17 15 L 15 15 z M 15 19 L 15 21 L 17 21 L 17 19 L 15 19 z M 15 23 L 15 25 L 17 25 L 17 23 L 15 23 z M 15 27 L 15 29 L 17 29 L 17 27 L 15 27 z</x:String>
<x:String x:Key="RegExToolProvider">M1.62 10a13.63 13.63 0 0 0 .45 3.51A13.39 13.39 0 0 0 3.4 16.7a.91.91 0 0 1 .1.27.41.41 0 0 1 0 .21.38.38 0 0 1-.1.15l-.14.11-.83.5a14.89 14.89 0 0 1-1.11-2 13.62 13.62 0 0 1-.74-2 13.22 13.22 0 0 1-.42-2 16.4 16.4 0 0 1 0-4.14 13.22 13.22 0 0 1 .42-2 13.84 13.84 0 0 1 .74-2A14.94 14.94 0 0 1 2.4 2l.83.51.14.11a.4.4 0 0 1 .1.15.41.41 0 0 1 0 .21.93.93 0 0 1-.1.27A13.6 13.6 0 0 0 1.62 10zM5 13.51a1.53 1.53 0 0 1 .11-.59 1.5 1.5 0 0 1 .31-.48 1.5 1.5 0 0 1 1.65-.32 1.51 1.51 0 0 1 .8.8 1.47 1.47 0 0 1 .12.59 1.46 1.46 0 0 1-.12.59 1.56 1.56 0 0 1-.32.48 1.46 1.46 0 0 1-.48.32 1.57 1.57 0 0 1-1.18 0 1.4 1.4 0 0 1-.47-.32A1.5 1.5 0 0 1 5 13.51zm10.8-4.72l-.54.94-1.75-1-.34-.23a1.38 1.38 0 0 1-.27-.26A1.84 1.84 0 0 1 13 9v2h-1V9a2.16 2.16 0 0 1 .12-.76 1.82 1.82 0 0 1-.58.48l-1.74 1-.54-.94 1.73-1a2.25 2.25 0 0 1 .75-.29 1.77 1.77 0 0 1-.75-.28L9.2 6.2l.54-.94 1.75 1 .33.24a1.64 1.64 0 0 1 .27.27A2 2 0 0 1 12 6V4h1v2a2.93 2.93 0 0 1 0 .4 1.36 1.36 0 0 1-.1.36 2.24 2.24 0 0 1 .59-.49l1.74-1 .54.94-1.73 1-.36.18a1.29 1.29 0 0 1-.36.1 2.11 2.11 0 0 1 .36.1 2 2 0 0 1 .36.19zM18.37 10a13.65 13.65 0 0 0-.45-3.51 13.81 13.81 0 0 0-1.32-3.27.93.93 0 0 1-.1-.27.45.45 0 0 1 0-.21.36.36 0 0 1 .1-.15l.14-.11.86-.48a15.54 15.54 0 0 1 1.1 2 13.79 13.79 0 0 1 .74 2 13.18 13.18 0 0 1 .42 2 16.16 16.16 0 0 1 .14 2 16.21 16.21 0 0 1-.13 2 13.18 13.18 0 0 1-.42 2 13.57 13.57 0 0 1-.74 2 15.49 15.49 0 0 1-1.1 2l-.84-.5-.14-.11a.35.35 0 0 1-.1-.15.44.44 0 0 1 0-.21.91.91 0 0 1 .1-.27 13.62 13.62 0 0 0 1.31-3.23 13.69 13.69 0 0 0 .43-3.53z</x:String>
+ <x:String x:Key="JsonYamlToolProvider">M 5 5 L 5 7 L 25 7 L 25 18.585938 L 23.707031 17.292969 L 22.292969 18.707031 L 26 22.414062 L 29.707031 18.707031 L 28.292969 17.292969 L 27 18.585938 L 27 5 L 5 5 z M 6 9.5859375 L 2.2929688 13.292969 L 3.7070312 14.707031 L 5 13.414062 L 5 27 L 27 27 L 27 25 L 7 25 L 7 13.414062 L 8.2929688 14.707031 L 9.7070312 13.292969 L 6 9.5859375 z M 11 11 L 11 13 L 21 13 L 21 11 L 11 11 z M 11 15 L 11 17 L 21 17 L 21 15 L 11 15 z M 11 19 L 11 21 L 18 21 L 18 19 L 11 19 z</x:String>
<!-- From https://github.com/morning4coffe-dev/storylines -->
<Style x:Key="RightAlignedToggleSwitchStyle" TargetType="ToggleSwitch">
diff --git a/src/dev/impl/DevToys/Tools/ToolProviderBase.cs b/src/dev/impl/DevToys/Tools/ToolProviderBase.cs
deleted file mode 100644
index 0280e8f19..000000000
--- a/src/dev/impl/DevToys/Tools/ToolProviderBase.cs
+++ /dev/null
@@ -1,34 +0,0 @@
-#nullable enable
-
-using DevToys.Core;
-using DevToys.Core.Threading;
-using Microsoft.Toolkit.Mvvm.ComponentModel;
-using System.Threading.Tasks;
-using Windows.UI.Xaml;
-using Windows.UI.Xaml.Controls;
-using Windows.UI.Xaml.Markup;
-using Windows.UI.Xaml.Media;
-
-namespace DevToys.Tools
-{
- internal abstract class ToolProviderBase : ObservableRecipient
- {
- protected const string AssetsFolderPath = "ms-appx:///DevToys/Assets/";
-
- protected TaskCompletionNotifier<IconElement> CreatePathIconFromPath(string resourceName)
- {
- Arguments.NotNullOrWhiteSpace(resourceName, nameof(resourceName));
-
- return new TaskCompletionNotifier<IconElement>(
- ThreadHelper.RunOnUIThreadAsync(() =>
- {
- string? pathMarkup = Application.Current.Resources[resourceName] as string;
- Assumes.NotNullOrWhiteSpace(pathMarkup, nameof(pathMarkup));
- return Task.FromResult<IconElement>(new PathIcon
- {
- Data = (Geometry)XamlBindingHelper.ConvertValue(typeof(Geometry), pathMarkup)
- });
- }));
- }
- }
-}
diff --git a/src/dev/impl/DevToys/UI/Controls/FormattedTextBlock/Languages/Yaml.cs b/src/dev/impl/DevToys/UI/Controls/FormattedTextBlock/Languages/Yaml.cs
new file mode 100644
index 000000000..6328d00e5
--- /dev/null
+++ b/src/dev/impl/DevToys/UI/Controls/FormattedTextBlock/Languages/Yaml.cs
@@ -0,0 +1,78 @@
+#nullable enable
+
+using ColorCode;
+using ColorCode.Common;
+using System.Collections.Generic;
+
+namespace DevToys.UI.Controls.FormattedTextBlock.Languages
+{
+ internal sealed class Yaml : ILanguage
+ {
+ public string Id
+ {
+ get { return "yaml"; }
+ }
+
+ public string Name
+ {
+ get { return "YAML"; }
+ }
+
+ public string CssClassName
+ {
+ get { return "yaml"; }
+ }
+
+ public string FirstLinePattern
+ {
+ get
+ {
+ return null;
+ }
+ }
+
+ public IList<LanguageRule> Rules
+ {
+ get
+ {
+ return new List<LanguageRule>
+ {
+ new LanguageRule(
+ @"(#.*?)\r?$",
+ new Dictionary<int, string>
+ {
+ { 1, ScopeName.Comment },
+ }),
+ new LanguageRule(
+ @"'(?>[^\']*)'",
+ new Dictionary<int, string>
+ {
+ { 0, ScopeName.String },
+ }),
+ new LanguageRule(
+ @"""(?>[^\""]*)""",
+ new Dictionary<int, string>
+ {
+ { 0, ScopeName.String },
+ }),
+ new LanguageRule(
+ @"^[^:]+",
+ new Dictionary<int, string>
+ {
+ { 0, ScopeName.Keyword },
+ })
+ };
+ }
+ }
+
+ public bool HasAlias(string lang)
+ {
+ return false;
+ }
+
+ public override string ToString()
+ {
+ return Name;
+ }
+ }
+}
diff --git a/src/dev/impl/DevToys/ViewModels/Tools/JsonFormatter/JsonFormatterToolProvider.cs b/src/dev/impl/DevToys/ViewModels/Tools/JsonFormatter/JsonFormatterToolProvider.cs
index c5f4573c5..0e224ed44 100644
--- a/src/dev/impl/DevToys/ViewModels/Tools/JsonFormatter/JsonFormatterToolProvider.cs
+++ b/src/dev/impl/DevToys/ViewModels/Tools/JsonFormatter/JsonFormatterToolProvider.cs
@@ -3,6 +3,7 @@
using DevToys.Api.Core.Injection;
using DevToys.Api.Tools;
using DevToys.Core;
+using DevToys.Helpers;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
@@ -31,46 +32,12 @@ namespace DevToys.ViewModels.Tools.JsonFormatter
public bool CanBeTreatedByTool(string data)
{
- return IsValidJson(data);
+ return JsonHelper.IsValidJson(data);
}
public IToolViewModel CreateTool()
{
return _mefProvider.Import<JsonFormatterToolViewModel>();
}
-
- private bool IsValidJson(string input)
- {
- if (string.IsNullOrWhiteSpace(input))
- {
- return false;
- }
-
- input = input.Trim();
-
- if ((input.StartsWith("{") && input.EndsWith("}")) //For object
- || (input.StartsWith("[") && input.EndsWith("]"))) //For array
- {
- try
- {
- JToken? jtoken = JToken.Parse(input);
- return jtoken is not null;
- }
- catch (JsonReaderException)
- {
- // Exception in parsing json. It likely mean the text isn't a JSON.
- return false;
- }
- catch (Exception ex) //some other exception
- {
- Logger.LogFault("Check is string if JSON", ex);
- return false;
- }
- }
- else
- {
- return false;
- }
- }
}
}
diff --git a/src/dev/impl/DevToys/ViewModels/Tools/JsonYaml/JsonYamlToolProvider.cs b/src/dev/impl/DevToys/ViewModels/Tools/JsonYaml/JsonYamlToolProvider.cs
new file mode 100644
index 000000000..4041c6b33
--- /dev/null
+++ b/src/dev/impl/DevToys/ViewModels/Tools/JsonYaml/JsonYamlToolProvider.cs
@@ -0,0 +1,39 @@
+#nullable enable
+
+using DevToys.Api.Core.Injection;
+using DevToys.Api.Tools;
+using DevToys.Helpers;
+using System.Composition;
+
+namespace DevToys.ViewModels.Tools.JsonYaml
+{
+ [Export(typeof(IToolProvider))]
+ [Name("Json <> Yaml")]
+ [ProtocolName("jsonyaml")]
+ [Order(0)]
+ [NotScrollable]
+ internal sealed class JsonYamlToolProvider : ToolProviderBase, IToolProvider
+ {
+ private readonly IMefProvider _mefProvider;
+
+ public string DisplayName => LanguageManager.Instance.JsonYaml.DisplayName;
+
+ public object IconSource => CreatePathIconFromPath(nameof(JsonYamlToolProvider));
+
+ [ImportingConstructor]
+ public JsonYamlToolProvider(IMefProvider mefProvider)
+ {
+ _mefProvider = mefProvider;
+ }
+
+ public bool CanBeTreatedByTool(string data)
+ {
+ return JsonHelper.IsValidJson(data) || YamlHelper.IsValidYaml(data);
+ }
+
+ public IToolViewModel CreateTool()
+ {
+ return _mefProvider.Import<JsonYamlToolViewModel>();
+ }
+ }
+}
diff --git a/src/dev/impl/DevToys/ViewModels/Tools/JsonYaml/JsonYamlToolViewModel.cs b/src/dev/impl/DevToys/ViewModels/Tools/JsonYaml/JsonYamlToolViewModel.cs
new file mode 100644
index 000000000..763cba6d9
--- /dev/null
+++ b/src/dev/impl/DevToys/ViewModels/Tools/JsonYaml/JsonYamlToolViewModel.cs
@@ -0,0 +1,287 @@
+#nullable enable
+
+using ColorCode;
+using DevToys.Api.Core.Settings;
+using DevToys.Api.Tools;
+using DevToys.Core;
+using DevToys.Core.Threading;
+using DevToys.Helpers;
+using DevToys.UI.Controls.FormattedTextBlock;
+using DevToys.UI.Controls.FormattedTextBlock.Languages;
+using DevToys.Views.Tools.JsonYaml;
+using Microsoft.Toolkit.Mvvm.ComponentModel;
+using Newtonsoft.Json;
+using System;
+using System.Collections.Generic;
+using System.Composition;
+using System.Dynamic;
+using System.IO;
+using System.Text;
+using System.Threading.Tasks;
+using YamlDotNet.Core;
+using YamlDotNet.Serialization;
+
+namespace DevToys.ViewModels.Tools.JsonYaml
+{
+ [Export(typeof(JsonYamlToolViewModel))]
+ public sealed class JsonYamlToolViewModel : ObservableRecipient, IToolViewModel
+ {
+ internal const string JsonToYaml = nameof(JsonToYaml);
+ internal const string YamlToJson = nameof(YamlToJson);
+ private const string TwoSpaceIndentation = "TwoSpaces";
+ private const string FourSpaceIndentation = "FourSpaces";
+
+ private readonly Queue<string> _conversionQueue = new();
+
+ private bool _conversionInProgress;
+ private bool _setPropertyInProgress;
+ private string? _inputValue;
+ private string? _outputValue;
+ private string _indentation = TwoSpaceIndentation;
+ private string _conversionMode = JsonToYaml;
+
+ public Type View { get; } = typeof(JsonYamlToolPage);
+
+ internal JsonYamlStrings Strings => LanguageManager.Instance.JsonYaml;
+
+ /// <summary>
+ /// Gets or sets the desired conversion mode.
+ /// </summary>
+ internal string ConversionMode
+ {
+ get => _conversionMode;
+ set
+ {
+ if (!_setPropertyInProgress)
+ {
+ _setPropertyInProgress = true;
+ ThreadHelper.ThrowIfNotOnUIThread();
+ SetProperty(ref _conversionMode, value);
+
+ if (string.Equals(value, JsonToYaml))
+ {
+ if (JsonHelper.IsValidJson(_outputValue))
+ {
+ InputValue = _outputValue;
+ }
+ }
+ else
+ {
+ if (YamlHelper.IsValidYaml(_outputValue))
+ {
+ InputValue = _outputValue;
+ }
+ }
+
+ _setPropertyInProgress = false;
+ }
+ }
+ }
+
+ /// <summary>
+ /// Gets or sets the desired indentation.
+ /// </summary>
+ internal string Indentation
+ {
+ get => _indentation;
+ set
+ {
+ SetProperty(ref _indentation, value);
+ QueueConversion();
+ }
+ }
+
+ /// <summary>
+ /// Gets or sets the input text.
+ /// </summary>
+ internal string? InputValue
+ {
+ get => _inputValue;
+ set
+ {
+ SetProperty(ref _inputValue, value);
+ QueueConversion();
+ }
+ }
+
+ internal ISettingsProvider SettingsProvider { get; }
+
+ internal IFormattedTextBlock? OutputTextBlock { private get; set; }
+
+ [ImportingConstructor]
+ public JsonYamlToolViewModel(ISettingsProvider settingsProvider)
+ {
+ SettingsProvider = settingsProvider;
+
+ Languages.Load(new Yaml());
+ }
+
+ private void QueueConversion()
+ {
+ _conversionQueue.Enqueue(InputValue ?? string.Empty);
+ TreatQueueAsync().Forget();
+ }
+
+ private async Task TreatQueueAsync()
+ {
+ if (_conversionInProgress)
+ {
+ return;
+ }
+
+ Assumes.NotNull(OutputTextBlock, nameof(OutputTextBlock));
+
+ _conversionInProgress = true;
+
+ await TaskScheduler.Default;
+
+ while (_conversionQueue.TryDequeue(out string text))
+ {
+ bool success;
+ string result;
+ ILanguage language;
+ if (string.Equals(ConversionMode, JsonToYaml, StringComparison.Ordinal))
+ {
+ success = ConvertJsonToYaml(text, out result);
+ language = Languages.FindById("yaml");
+ }
+ else
+ {
+ success = ConvertYamlToJson(text, out result);
+ language = Languages.JavaScript;
+ }
+
+ ThreadHelper.RunOnUIThreadAsync(ThreadPriority.Low, () =>
+ {
+ _outputValue = result;
+ if (success)
+ {
+ OutputTextBlock!.SetText(result, language);
+ }
+ else
+ {
+ OutputTextBlock!.SetText(result);
+ }
+ }).ForgetSafely();
+ }
+
+ _conversionInProgress = false;
+ }
+
+ private bool ConvertJsonToYaml(string input, out string output)
+ {
+ if (string.IsNullOrWhiteSpace(input))
+ {
+ output = string.Empty;
+ return false;
+ }
+
+ try
+ {
+ dynamic jsonObject = JsonConvert.DeserializeObject<ExpandoObject>(input);
+ if (jsonObject is not null && jsonObject is not string)
+ {
+ int indent = 0;
+ switch (Indentation)
+ {
+ case TwoSpaceIndentation:
+ indent = 2;
+ break;
+
+ case FourSpaceIndentation:
+ indent = 4;
+ break;
+
+ default:
+ throw new NotSupportedException();
+ }
+
+ var serializer
+ = Serializer.FromValueSerializer(
+ new SerializerBuilder().BuildValueSerializer(),
+ EmitterSettings.Default.WithBestIndent(indent).WithIndentedSequences());
+
+ string? yaml = serializer.Serialize(jsonObject);
+ output = yaml ?? string.Empty;
+ return true;
+ }
+
+ output = string.Empty;
+ }
+ catch (JsonReaderException ex)
+ {
+ output = ex.Message;
+ }
+ catch (Exception ex)
+ {
+ Logger.LogFault("Yaml to Json Converter", ex);
+ output = string.Empty;
+ }
+
+ return false;
+ }
+
+ private bool ConvertYamlToJson(string input, out string output)
+ {
+ if (string.IsNullOrWhiteSpace(input))
+ {
+ output = string.Empty;
+ return false;
+ }
+
+ try
+ {
+ using var stringReader = new StringReader(input);
+
+ var deserializer = new Deserializer();
+ object? yamlObject = deserializer.Deserialize(stringReader);
+
+ if (yamlObject is null || yamlObject is string)
+ {
+ output = Strings.InvalidYaml;
+ return false;
+ }
+
+ var stringBuilder = new StringBuilder();
+ using (var stringWriter = new StringWriter(stringBuilder))
+ using (var jsonTextWriter = new JsonTextWriter(stringWriter))
+ {
+ switch (Indentation)
+ {
+ case TwoSpaceIndentation:
+ jsonTextWriter.Formatting = Formatting.Indented;
+ jsonTextWriter.IndentChar = ' ';
+ jsonTextWriter.Indentation = 2;
+ break;
+
+ case FourSpaceIndentation:
+ jsonTextWriter.Formatting = Formatting.Indented;
+ jsonTextWriter.IndentChar = ' ';
+ jsonTextWriter.Indentation = 4;
+ break;
+
+ default:
+ throw new NotSupportedException();
+ }
+
+ var jsonSerializer = new JsonSerializer();
+ jsonSerializer.Serialize(jsonTextWriter, yamlObject);
+ }
+
+ output = stringBuilder.ToString();
+ return true;
+ }
+ catch (SemanticErrorException ex)
+ {
+ output = ex.Message;
+ }
+ catch (Exception ex)
+ {
+ Logger.LogFault("Yaml to Json Converter", ex);
+ output = string.Empty;
+ }
+
+ return false;
+ }
+ }
+}
diff --git a/src/dev/impl/DevToys/Views/Tools/Base64EncoderDecoder/Base64EncoderDecoderToolPage.xaml b/src/dev/impl/DevToys/Views/Tools/Base64EncoderDecoder/Base64EncoderDecoderToolPage.xaml
index 10ee4b718..2b2f1f49c 100644
--- a/src/dev/impl/DevToys/Views/Tools/Base64EncoderDecoder/Base64EncoderDecoderToolPage.xaml
+++ b/src/dev/impl/DevToys/Views/Tools/Base64EncoderDecoder/Base64EncoderDecoderToolPage.xaml
@@ -36,7 +36,7 @@
Title="{x:Bind ViewModel.Strings.ConversionTitle}"
Description="{x:Bind ViewModel.Strings.ConversionDescription}">
<controls:ExpandableSettingControl.Icon>
- <FontIcon Glyph="&#x00CA;" />
+ <FontIcon Glyph="&#xF18D;" />
</controls:ExpandableSettingControl.Icon>
<ComboBox
SelectedValuePath="Tag"
diff --git a/src/dev/impl/DevToys/Views/Tools/JsonYaml/JsonYamlToolPage.xaml b/src/dev/impl/DevToys/Views/Tools/JsonYaml/JsonYamlToolPage.xaml
new file mode 100644
index 000000000..2d7870679
--- /dev/null
+++ b/src/dev/impl/DevToys/Views/Tools/JsonYaml/JsonYamlToolPage.xaml
@@ -0,0 +1,104 @@
+<Page
+ x:Class="DevToys.Views.Tools.JsonYaml.JsonYamlToolPage"
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+ xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
+ xmlns:controls="using:DevToys.UI.Controls"
+ xmlns:formattedtextblock="using:DevToys.UI.Controls.FormattedTextBlock"
+ xmlns:texteditor="using:DevToys.UI.Controls.TextEditor"
+ xmlns:ex="using:DevToys.UI.Extensions"
+ xmlns:toolkit="using:Microsoft.Toolkit.Uwp.UI.Controls"
+ mc:Ignorable="d"
+ NavigationCacheMode="Required">
+
+ <Grid Margin="0,0,0,16" RowSpacing="12">
+ <VisualStateManager.VisualStateGroups>
+ <VisualStateGroup>
+ <VisualState>
+ <VisualState.StateTriggers>
+ <ex:IsCompactOverlayModeTrigger TargetElement="{x:Bind}" />
+ </VisualState.StateTriggers>
+ <VisualState.Setters>
+ <Setter Target="ConfigurationStackPanel.Visibility" Value="Collapsed" />
+ </VisualState.Setters>
+ </VisualState>
+ </VisualStateGroup>
+ </VisualStateManager.VisualStateGroups>
+
+ <Grid.ChildrenTransitions>
+ <TransitionCollection>
+ <EntranceThemeTransition IsStaggeringEnabled="True" FromVerticalOffset="50"/>
+ </TransitionCollection>
+ </Grid.ChildrenTransitions>
+
+ <Grid.RowDefinitions>
+ <RowDefinition Height="Auto"/>
+ <RowDefinition Height="*"/>
+ </Grid.RowDefinitions>
+
+ <StackPanel x:Name="ConfigurationStackPanel" Grid.Row="0" Spacing="4" >
+ <TextBlock Style="{StaticResource SubTitleTextBlock}" Text="{x:Bind ViewModel.Strings.Configuration}" />
+
+ <controls:ExpandableSettingControl
+ Title="{x:Bind ViewModel.Strings.ConversionTitle}"
+ Description="{x:Bind ViewModel.Strings.ConversionDescription}">
+ <controls:ExpandableSettingControl.Icon>
+ <FontIcon Glyph="&#xF18D;" />
+ </controls:ExpandableSettingControl.Icon>
+ <ComboBox
+ SelectedValuePath="Tag"
+ SelectedValue="{x:Bind ViewModel.ConversionMode, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
+ <ComboBoxItem Tag="JsonToYaml" Content="{x:Bind ViewModel.Strings.JsonToYaml}"/>
+ <ComboBoxItem Tag="YamlToJson" Content="{x:Bind ViewModel.Strings.YamlToJson}"/>
+ </ComboBox>
+ </controls:ExpandableSettingControl>
+
+ <controls:ExpandableSettingControl
+ Title="{x:Bind ViewModel.Strings.Indentation}">
+ <controls:ExpandableSettingControl.Icon>
+ <FontIcon Glyph="&#xF6F8;" />
+ </controls:ExpandableSettingControl.Icon>
+ <ComboBox
+ SelectedValuePath="Tag"
+ SelectedValue="{x:Bind ViewModel.Indentation, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
+ <ComboBoxItem Tag="TwoSpaces" Content="{x:Bind ViewModel.Strings.TwoSpaces}"/>
+ <ComboBoxItem Tag="FourSpaces" Content="{x:Bind ViewModel.Strings.FourSpaces}"/>
+ </ComboBox>
+ </controls:ExpandableSettingControl>
+ </StackPanel>
+
+ <Grid Grid.Row="1">
+ <Grid.ColumnDefinitions>
+ <ColumnDefinition MinWidth="100"/>
+ <ColumnDefinition Width="16"/>
+ <ColumnDefinition MinWidth="100"/>
+ </Grid.ColumnDefinitions>
+
+ <texteditor:TextEditor
+ Grid.Column="0"
+ Header="{x:Bind ViewModel.Strings.Input}"
+ SettingsProvider="{x:Bind ViewModel.SettingsProvider}"
+ Text="{x:Bind ViewModel.InputValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
+
+ <toolkit:GridSplitter
+ Grid.Column="1"
+ Width="16"
+ Margin="0,42,0,0"
+ ResizeBehavior="BasedOnAlignment"
+ ResizeDirection="Auto">
+ <toolkit:GridSplitter.Element>
+ <FontIcon
+ Glyph="&#xFD55;"
+ FontSize="13"/>
+ </toolkit:GridSplitter.Element>
+ </toolkit:GridSplitter>
+
+ <formattedtextblock:FormattedTextBlock
+ x:Name="OutputTextBlock"
+ Grid.Column="2"
+ Header="{x:Bind ViewModel.Strings.Output}"
+ SettingsProvider="{x:Bind ViewModel.SettingsProvider}"/>
+ </Grid>
+ </Grid>
+</Page>
diff --git a/src/dev/impl/DevToys/Views/Tools/JsonYaml/JsonYamlToolPage.xaml.cs b/src/dev/impl/DevToys/Views/Tools/JsonYaml/JsonYamlToolPage.xaml.cs
new file mode 100644
index 000000000..dbdae29f9
--- /dev/null
+++ b/src/dev/impl/DevToys/Views/Tools/JsonYaml/JsonYamlToolPage.xaml.cs
@@ -0,0 +1,67 @@
+#nullable enable
+
+using DevToys.Api.Core.Navigation;
+using DevToys.Core;
+using DevToys.Helpers;
+using DevToys.ViewModels.Tools.JsonYaml;
+using Windows.UI.Xaml;
+using Windows.UI.Xaml.Controls;
+using Windows.UI.Xaml.Navigation;
+
+namespace DevToys.Views.Tools.JsonYaml
+{
+ public sealed partial class JsonYamlToolPage : Page
+ {
+ public static readonly DependencyProperty ViewModelProperty
+ = DependencyProperty.Register(
+ nameof(ViewModel),
+ typeof(JsonYamlToolViewModel),
+ typeof(JsonYamlToolPage),
+ new PropertyMetadata(null));
+
+ /// <summary>
+ /// Gets the page's view model.
+ /// </summary>
+ public JsonYamlToolViewModel ViewModel
+ {
+ get => (JsonYamlToolViewModel)GetValue(ViewModelProperty);
+ set => SetValue(ViewModelProperty, value);
+ }
+
+ public JsonYamlToolPage()
+ {
+ InitializeComponent();
+ }
+
+ protected override void OnNavigatedTo(NavigationEventArgs e)
+ {
+ var parameters = (NavigationParameter)e.Parameter;
+
+ if (ViewModel is null)
+ {
+ // Set the view model
+ Assumes.NotNull(parameters.ViewModel, nameof(parameters.ViewModel));
+ ViewModel = (JsonYamlToolViewModel)parameters.ViewModel!;
+ DataContext = ViewModel;
+
+ ViewModel.OutputTextBlock = OutputTextBlock;
+ }
+
+ if (!string.IsNullOrWhiteSpace(parameters.ClipBoardContent))
+ {
+ if (JsonHelper.IsValidJson(parameters.ClipBoardContent!))
+ {
+ ViewModel.ConversionMode = JsonYamlToolViewModel.JsonToYaml;
+ }
+ else
+ {
+ ViewModel.ConversionMode = JsonYamlToolViewModel.YamlToJson;
+ }
+
+ ViewModel.InputValue = parameters.ClipBoardContent;
+ }
+
+ base.OnNavigatedTo(e);
+ }
+ }
+}
diff --git a/src/tests/DevToys.Tests/DevToys.Tests.csproj b/src/tests/DevToys.Tests/DevToys.Tests.csproj
index 3f707d2a2..321a5f463 100644
--- a/src/tests/DevToys.Tests/DevToys.Tests.csproj
+++ b/src/tests/DevToys.Tests/DevToys.Tests.csproj
@@ -20,35 +20,32 @@
<IsTestProject>true</IsTestProject>
<AppxBundle>Never</AppxBundle>
</PropertyGroup>
- <PropertyGroup Condition="'$(Configuration)' == 'Debug'">
- <DebugSymbols>true</DebugSymbols>
- <DefineConstants>DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
- <DebugType>full</DebugType>
- </PropertyGroup>
-
- <PropertyGroup Condition="'$(Configuration)' == 'Release'">
- <DefineConstants>TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
- <Optimize>true</Optimize>
- <DebugType>pdbonly</DebugType>
- <UseDotNetNativeToolchain>true</UseDotNetNativeToolchain>
- </PropertyGroup>
-
- <PropertyGroup Condition="'$(Platform)' == 'x86'">
- <PlatformTarget>x86</PlatformTarget>
- </PropertyGroup>
-
- <PropertyGroup Condition="'$(Platform)' == 'x64'">
- <PlatformTarget>x64</PlatformTarget>
- </PropertyGroup>
-
- <PropertyGroup Condition="'$(Platform)' == 'ARM'">
- <PlatformTarget>ARM</PlatformTarget>
- </PropertyGroup>
-
+ <PropertyGroup Condition="'$(Configuration)' == 'Debug'">
+ <DebugSymbols>true</DebugSymbols>
+ <DefineConstants>DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
+ <DebugType>full</DebugType>
+ </PropertyGroup>
+ <PropertyGroup Condition="'$(Configuration)' == 'Release'">
+ <DefineConstants>TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
+ <Optimize>true</Optimize>
+ <DebugType>pdbonly</DebugType>
+ <UseDotNetNativeToolchain>true</UseDotNetNativeToolchain>
+ </PropertyGroup>
+ <PropertyGroup Condition="'$(Platform)' == 'x86'">
+ <PlatformTarget>x86</PlatformTarget>
+ </PropertyGroup>
+ <PropertyGroup Condition="'$(Platform)' == 'x64'">
+ <PlatformTarget>x64</PlatformTarget>
+ </PropertyGroup>
+ <PropertyGroup Condition="'$(Platform)' == 'ARM'">
+ <PlatformTarget>ARM</PlatformTarget>
+ </PropertyGroup>
<ItemGroup>
<SDKReference Include="TestPlatform.Universal, Version=$(UnitTestPlatformVersion)" />
</ItemGroup>
<ItemGroup>
+ <Compile Include="Helpers\YamlHelperTests.cs" />
+ <Compile Include="Helpers\JsonHelperTests.cs" />
<Compile Include="MefBaseTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Providers\Tools\HashGeneratorTests.cs" />
diff --git a/src/tests/DevToys.Tests/Helpers/JsonHelperTests.cs b/src/tests/DevToys.Tests/Helpers/JsonHelperTests.cs
new file mode 100644
index 000000000..c8a4805c4
--- /dev/null
+++ b/src/tests/DevToys.Tests/Helpers/JsonHelperTests.cs
@@ -0,0 +1,21 @@
+using DevToys.Helpers;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+namespace DevToys.Tests.Helpers
+{
+ [TestClass]
+ public class JsonHelperTests
+ {
+ [DataTestMethod]
+ [DataRow(null, false)]
+ [DataRow("", false)]
+ [DataRow(" ", false)]
+ [DataRow(" { } ", true)]
+ [DataRow(" { \"foo\": 123 } ", true)]
+ [DataRow(" bar { \"foo\": 123 } ", false)]
+ public void IsValidJson(string input, bool expectedResult)
+ {
+ Assert.AreEqual(expectedResult, JsonHelper.IsValidJson(input));
+ }
+ }
+}
diff --git a/src/tests/DevToys.Tests/Helpers/YamlHelperTests.cs b/src/tests/DevToys.Tests/Helpers/YamlHelperTests.cs
new file mode 100644
index 000000000..0bea4039f
--- /dev/null
+++ b/src/tests/DevToys.Tests/Helpers/YamlHelperTests.cs
@@ -0,0 +1,20 @@
+using DevToys.Helpers;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+namespace DevToys.Tests.Helpers
+{
+ [TestClass]
+ public class YamlHelperTests
+ {
+ [DataTestMethod]
+ [DataRow(null, false)]
+ [DataRow("", false)]
+ [DataRow(" ", false)]
+ [DataRow("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.", false)]
+ [DataRow("foo :\n bar :\n - boo: 1\n - rab: 2\n - plop: 3", true)]
+ public void IsValidYaml(string input, bool expectedResult)
+ {
+ Assert.AreEqual(expectedResult, YamlHelper.IsValidYaml(input));
+ }
+ }
+}