mirror of
https://github.com/nsnail/dot.git
synced 2025-06-17 21:13:21 +08:00
<feat> + json工具
This commit is contained in:
parent
0e2a8237d0
commit
6e995dfdf4
68
src/Json/Main.cs
Normal file
68
src/Json/Main.cs
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
using System.Text.Json;
|
||||||
|
using NSExt.Extensions;
|
||||||
|
using TextCopy;
|
||||||
|
|
||||||
|
namespace Dot.Json;
|
||||||
|
|
||||||
|
public class Main : ToolBase<Option>
|
||||||
|
{
|
||||||
|
private readonly object _inputObj;
|
||||||
|
|
||||||
|
public Main(Option opt) : base(opt)
|
||||||
|
{
|
||||||
|
var inputText = ClipboardService.GetText();
|
||||||
|
if (inputText.NullOrWhiteSpace()) throw new ArgumentException(Str.InputTextIsEmpty);
|
||||||
|
|
||||||
|
try {
|
||||||
|
_inputObj = inputText.Object<object>();
|
||||||
|
}
|
||||||
|
catch (JsonException) {
|
||||||
|
try {
|
||||||
|
inputText = UnescapeString(inputText);
|
||||||
|
_inputObj = inputText.Object<object>();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
catch (JsonException) { }
|
||||||
|
|
||||||
|
throw new ArgumentException(Str.InvalidJsonString);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private async Task<string> ConvertToString()
|
||||||
|
{
|
||||||
|
var ret = await JsonCompress();
|
||||||
|
ret = ret.Replace("\"", "\\\"");
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Task<string> JsonCompress()
|
||||||
|
{
|
||||||
|
var ret = _inputObj.Json();
|
||||||
|
return Task.FromResult(ret);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Task<string> JsonFormat()
|
||||||
|
{
|
||||||
|
var ret = _inputObj.Json(true);
|
||||||
|
return Task.FromResult(ret);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string UnescapeString(string text)
|
||||||
|
{
|
||||||
|
return text.Replace("\\\"", "\"");
|
||||||
|
}
|
||||||
|
|
||||||
|
public override async Task Run()
|
||||||
|
{
|
||||||
|
string result = null;
|
||||||
|
if (Opt.Compress)
|
||||||
|
result = await JsonCompress();
|
||||||
|
else if (Opt.ConvertToString)
|
||||||
|
result = await ConvertToString();
|
||||||
|
else if (Opt.Format) result = await JsonFormat();
|
||||||
|
|
||||||
|
if (result.NullOrWhiteSpace()) return;
|
||||||
|
await ClipboardService.SetTextAsync(result!);
|
||||||
|
}
|
||||||
|
}
|
15
src/Json/Option.cs
Normal file
15
src/Json/Option.cs
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
namespace Dot.Json;
|
||||||
|
|
||||||
|
[Verb("json", HelpText = nameof(Str.Json), ResourceType = typeof(Str))]
|
||||||
|
public class Option : OptionBase
|
||||||
|
{
|
||||||
|
[Option('c', "compress", HelpText = nameof(Str.CompressJson), Default = false, ResourceType = typeof(Str))]
|
||||||
|
public bool Compress { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
[Option('s', "convert-to-string", HelpText = nameof(Str.FormatJson), Default = false, ResourceType = typeof(Str))]
|
||||||
|
public bool ConvertToString { get; set; }
|
||||||
|
|
||||||
|
[Option('f', "format", HelpText = nameof(Str.FormatJson), Default = true, ResourceType = typeof(Str))]
|
||||||
|
public bool Format { get; set; }
|
||||||
|
}
|
@ -155,4 +155,22 @@
|
|||||||
<data name="MaxRecursionDepth" xml:space="preserve">
|
<data name="MaxRecursionDepth" xml:space="preserve">
|
||||||
<value>Directory search depth</value>
|
<value>Directory search depth</value>
|
||||||
</data>
|
</data>
|
||||||
|
<data name="InvalidJsonString" xml:space="preserve">
|
||||||
|
<value>Clipboard does not contain correct Json string</value>
|
||||||
|
</data>
|
||||||
|
<data name="Json" xml:space="preserve">
|
||||||
|
<value>JsonTools</value>
|
||||||
|
</data>
|
||||||
|
<data name="CompressJson" xml:space="preserve">
|
||||||
|
<value>Compress Json text</value>
|
||||||
|
</data>
|
||||||
|
<data name="FormatJson" xml:space="preserve">
|
||||||
|
<value>Format JSON text</value>
|
||||||
|
</data>
|
||||||
|
<data name="GeneratorClass" xml:space="preserve">
|
||||||
|
<value>generate entity classes</value>
|
||||||
|
</data>
|
||||||
|
<data name="JsonToString" xml:space="preserve">
|
||||||
|
<value>Json text escaped into a string</value>
|
||||||
|
</data>
|
||||||
</root>
|
</root>
|
@ -33,6 +33,9 @@
|
|||||||
<data name="PathNotFound" xml:space="preserve">
|
<data name="PathNotFound" xml:space="preserve">
|
||||||
<value>指定的路径“{0}”不存在</value>
|
<value>指定的路径“{0}”不存在</value>
|
||||||
</data>
|
</data>
|
||||||
|
<data name="InvalidJsonString" xml:space="preserve">
|
||||||
|
<value>剪贴板未包含正确的Json字符串</value>
|
||||||
|
</data>
|
||||||
<data name="SearchingFileOK" xml:space="preserve">
|
<data name="SearchingFileOK" xml:space="preserve">
|
||||||
<value>{0} 个文件</value>
|
<value>{0} 个文件</value>
|
||||||
</data>
|
</data>
|
||||||
@ -45,6 +48,9 @@
|
|||||||
<data name="Ip" xml:space="preserve">
|
<data name="Ip" xml:space="preserve">
|
||||||
<value>IP工具</value>
|
<value>IP工具</value>
|
||||||
</data>
|
</data>
|
||||||
|
<data name="Json" xml:space="preserve">
|
||||||
|
<value>Json工具</value>
|
||||||
|
</data>
|
||||||
<data name="ScreenPixelTool" xml:space="preserve">
|
<data name="ScreenPixelTool" xml:space="preserve">
|
||||||
<value>屏幕坐标颜色选取工具</value>
|
<value>屏幕坐标颜色选取工具</value>
|
||||||
</data>
|
</data>
|
||||||
@ -54,6 +60,7 @@
|
|||||||
<data name="TextTobeProcessed" xml:space="preserve">
|
<data name="TextTobeProcessed" xml:space="preserve">
|
||||||
<value>要处理的文本(默认取取剪贴板值)</value>
|
<value>要处理的文本(默认取取剪贴板值)</value>
|
||||||
</data>
|
</data>
|
||||||
|
|
||||||
<data name="Copied" xml:space="preserve">
|
<data name="Copied" xml:space="preserve">
|
||||||
<value>{0}(已复制到剪贴板)</value>
|
<value>{0}(已复制到剪贴板)</value>
|
||||||
</data>
|
</data>
|
||||||
@ -96,6 +103,18 @@
|
|||||||
<data name="GitArgs" xml:space="preserve">
|
<data name="GitArgs" xml:space="preserve">
|
||||||
<value>传递给Git的参数</value>
|
<value>传递给Git的参数</value>
|
||||||
</data>
|
</data>
|
||||||
|
<data name="CompressJson" xml:space="preserve">
|
||||||
|
<value>压缩Json文本</value>
|
||||||
|
</data>
|
||||||
|
<data name="FormatJson" xml:space="preserve">
|
||||||
|
<value>格式化Json文本</value>
|
||||||
|
</data>
|
||||||
|
<data name="GeneratorClass" xml:space="preserve">
|
||||||
|
<value>生成实体类</value>
|
||||||
|
</data>
|
||||||
|
<data name="JsonToString" xml:space="preserve">
|
||||||
|
<value>Json文本转义成字符串</value>
|
||||||
|
</data>
|
||||||
<data name="RandomPasswordGenerator" xml:space="preserve">
|
<data name="RandomPasswordGenerator" xml:space="preserve">
|
||||||
<value>随机密码生成器</value>
|
<value>随机密码生成器</value>
|
||||||
</data>
|
</data>
|
||||||
|
@ -6,10 +6,10 @@ public class Option : OptionBase
|
|||||||
[Flags]
|
[Flags]
|
||||||
public enum GenerateTypes
|
public enum GenerateTypes
|
||||||
{
|
{
|
||||||
Number = 1
|
Number = 0b0001
|
||||||
, LowerCaseLetter = 2
|
, LowerCaseLetter = 0b0010
|
||||||
, UpperCaseLetter = 4
|
, UpperCaseLetter = 0b0100
|
||||||
, SpecialCharacter = 8
|
, SpecialCharacter = 0b1000
|
||||||
}
|
}
|
||||||
|
|
||||||
[Value(1, Required = true, HelpText = nameof(Str.PwdLength), ResourceType = typeof(Str))]
|
[Value(1, Required = true, HelpText = nameof(Str.PwdLength), ResourceType = typeof(Str))]
|
||||||
|
@ -17,6 +17,7 @@ public static class ToolsFactory
|
|||||||
, Color.Option o => new Color.Main(o)
|
, Color.Option o => new Color.Main(o)
|
||||||
, IP.Option o => new IP.Main(o)
|
, IP.Option o => new IP.Main(o)
|
||||||
, Git.Option o => new Git.Main(o)
|
, Git.Option o => new Git.Main(o)
|
||||||
|
, Json.Option o => new Json.Main(o)
|
||||||
, _ => throw new ArgumentOutOfRangeException(nameof(option))
|
, _ => throw new ArgumentOutOfRangeException(nameof(option))
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
<RootNamespace>Dot</RootNamespace>
|
<RootNamespace>Dot</RootNamespace>
|
||||||
<AssemblyName>dot</AssemblyName>
|
<AssemblyName>dot</AssemblyName>
|
||||||
<Version>1.1.4</Version>
|
<Version>1.1.5</Version>
|
||||||
<Authors>nsnail</Authors>
|
<Authors>nsnail</Authors>
|
||||||
<Copyright>Copyright (c) 2022 nsnail</Copyright>
|
<Copyright>Copyright (c) 2022 nsnail</Copyright>
|
||||||
<RepositoryUrl>https://github.com/nsnail/dot.git</RepositoryUrl>
|
<RepositoryUrl>https://github.com/nsnail/dot.git</RepositoryUrl>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user