From 01f70b8166370b1bf078da26b10904b2f926dfb3 Mon Sep 17 00:00:00 2001 From: Yoshifumi Kawai Date: Wed, 23 Feb 2022 22:20:25 +0900 Subject: [PATCH] JsonConverterDictionaryKeySupport --- README.md | 13 +++- sandbox/ConsoleApp/AllPrimitives.cs | 24 ++++---- sandbox/ConsoleApp/ConsoleApp.csproj | 2 +- sandbox/ConsoleApp/Others.cs | 2 +- sandbox/ConsoleApp/Program.cs | 46 ++++---------- src/UnitGenerator/CodeTemplate.Partial.cs | 30 +++++++++- src/UnitGenerator/CodeTemplate.cs | 63 +++++++++++++++++++- src/UnitGenerator/CodeTemplate.tt | 43 +++++++++++++ src/UnitGenerator/UnitGenerateOptions.cs | 3 +- src/UnitGenerator/UnitOfAttributeTemplate.cs | 11 ++-- src/UnitGenerator/UnitOfAttributeTemplate.tt | 3 +- 11 files changed, 184 insertions(+), 56 deletions(-) diff --git a/README.md b/README.md index eb21058..7646d0d 100644 --- a/README.md +++ b/README.md @@ -125,10 +125,12 @@ enum UnitGenerateOptions MessagePackFormatter = 256, DapperTypeHandler = 512, EntityFrameworkValueConverter = 1024, + WithoutComparisonOperator = 2048, + JsonConverterDictionaryKeySupport = 4096 } ``` -UnitGenerateOptions has some serializer support. For example, a result like `Serialize(userId) => { Value = 1111 }` is awful. The value-object should be serialized natively, i.e. `Serialzie(useId) => 1111`, and should be able to be added directly to a database, etc. +UnitGenerateOptions has some serializer support. For example, a result like `Serialize(userId) => { Value = 1111 }` is awful. The value-object should be serialized natively, i.e. `Serialize(useId) => 1111`, and should be able to be added directly to a database, etc. Currently UnitGenerator supports [MessagePack for C#](https://github.com/neuecc/MessagePack-CSharp), System.Text.Json(JsonSerializer), [Dapper](https://github.com/StackExchange/Dapper) and EntityFrameworkCore. @@ -391,6 +393,15 @@ public readonly partial struct UserId } ``` +### JsonConverterDictionaryKeySupport + +Implements `JsonConverter`'s `WriteAsPropertyName/ReadAsPropertyName`. It supports from .NET 6, supports Dictionary's Key. + +```csharp +var dict = Dictionary +JsonSerializer.Serialize(dict); +```` + ### MessagePackFormatter Implements MessagePack for C#'s `MessagePackFormatter`. It will be used `MessagePackSerializer` automatically. diff --git a/sandbox/ConsoleApp/AllPrimitives.cs b/sandbox/ConsoleApp/AllPrimitives.cs index 2699f2e..9443141 100644 --- a/sandbox/ConsoleApp/AllPrimitives.cs +++ b/sandbox/ConsoleApp/AllPrimitives.cs @@ -2,12 +2,14 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using System.Text.Json; +using System.Text.Json.Serialization; using System.Threading.Tasks; using UnitGenerator; namespace ConsoleApp { - [UnitOf(typeof(int), UnitGenerateOptions.ParseMethod | UnitGenerateOptions.MinMaxMethod | UnitGenerateOptions.ArithmeticOperator | UnitGenerateOptions.ValueArithmeticOperator | UnitGenerateOptions.Comparable | UnitGenerateOptions.Validate | UnitGenerateOptions.JsonConverter | UnitGenerateOptions.MessagePackFormatter | UnitGenerateOptions.DapperTypeHandler | UnitGenerateOptions.EntityFrameworkValueConverter)] + [UnitOf(typeof(int), UnitGenerateOptions.ParseMethod | UnitGenerateOptions.MinMaxMethod | UnitGenerateOptions.ArithmeticOperator | UnitGenerateOptions.ValueArithmeticOperator | UnitGenerateOptions.Comparable | UnitGenerateOptions.Validate | UnitGenerateOptions.JsonConverter | UnitGenerateOptions.MessagePackFormatter | UnitGenerateOptions.DapperTypeHandler | UnitGenerateOptions.EntityFrameworkValueConverter | UnitGenerateOptions.JsonConverterDictionaryKeySupport)] public readonly partial struct A { private partial void Validate() @@ -16,7 +18,7 @@ private partial void Validate() } } - [UnitOf(typeof(int), UnitGenerateOptions.ParseMethod | UnitGenerateOptions.MinMaxMethod | UnitGenerateOptions.ArithmeticOperator | UnitGenerateOptions.ValueArithmeticOperator | UnitGenerateOptions.Comparable | UnitGenerateOptions.Validate | UnitGenerateOptions.JsonConverter | UnitGenerateOptions.MessagePackFormatter | UnitGenerateOptions.DapperTypeHandler | UnitGenerateOptions.EntityFrameworkValueConverter)] + [UnitOf(typeof(int), UnitGenerateOptions.ParseMethod | UnitGenerateOptions.MinMaxMethod | UnitGenerateOptions.ArithmeticOperator | UnitGenerateOptions.ValueArithmeticOperator | UnitGenerateOptions.Comparable | UnitGenerateOptions.Validate | UnitGenerateOptions.JsonConverter | UnitGenerateOptions.MessagePackFormatter | UnitGenerateOptions.DapperTypeHandler | UnitGenerateOptions.EntityFrameworkValueConverter | UnitGenerateOptions.JsonConverterDictionaryKeySupport)] public readonly partial struct T { private partial void Validate() @@ -25,7 +27,7 @@ private partial void Validate() } } - [UnitOf(typeof(uint), UnitGenerateOptions.ParseMethod | UnitGenerateOptions.MinMaxMethod | UnitGenerateOptions.ArithmeticOperator | UnitGenerateOptions.ValueArithmeticOperator | UnitGenerateOptions.Comparable | UnitGenerateOptions.Validate | UnitGenerateOptions.JsonConverter | UnitGenerateOptions.MessagePackFormatter | UnitGenerateOptions.DapperTypeHandler | UnitGenerateOptions.EntityFrameworkValueConverter)] + [UnitOf(typeof(uint), UnitGenerateOptions.ParseMethod | UnitGenerateOptions.MinMaxMethod | UnitGenerateOptions.ArithmeticOperator | UnitGenerateOptions.ValueArithmeticOperator | UnitGenerateOptions.Comparable | UnitGenerateOptions.Validate | UnitGenerateOptions.JsonConverter | UnitGenerateOptions.MessagePackFormatter | UnitGenerateOptions.DapperTypeHandler | UnitGenerateOptions.EntityFrameworkValueConverter | UnitGenerateOptions.JsonConverterDictionaryKeySupport)] public readonly partial struct B { private partial void Validate() @@ -34,7 +36,7 @@ private partial void Validate() } } - [UnitOf(typeof(short), UnitGenerateOptions.ParseMethod | UnitGenerateOptions.MinMaxMethod | UnitGenerateOptions.ArithmeticOperator | UnitGenerateOptions.ValueArithmeticOperator | UnitGenerateOptions.Comparable | UnitGenerateOptions.Validate | UnitGenerateOptions.JsonConverter | UnitGenerateOptions.MessagePackFormatter | UnitGenerateOptions.DapperTypeHandler | UnitGenerateOptions.EntityFrameworkValueConverter)] + [UnitOf(typeof(short), UnitGenerateOptions.ParseMethod | UnitGenerateOptions.MinMaxMethod | UnitGenerateOptions.ArithmeticOperator | UnitGenerateOptions.ValueArithmeticOperator | UnitGenerateOptions.Comparable | UnitGenerateOptions.Validate | UnitGenerateOptions.JsonConverter | UnitGenerateOptions.MessagePackFormatter | UnitGenerateOptions.DapperTypeHandler | UnitGenerateOptions.EntityFrameworkValueConverter | UnitGenerateOptions.JsonConverterDictionaryKeySupport)] public readonly partial struct C { private partial void Validate() @@ -43,7 +45,7 @@ private partial void Validate() } } - [UnitOf(typeof(ushort), UnitGenerateOptions.ParseMethod | UnitGenerateOptions.MinMaxMethod | UnitGenerateOptions.ArithmeticOperator | UnitGenerateOptions.ValueArithmeticOperator | UnitGenerateOptions.Comparable | UnitGenerateOptions.Validate | UnitGenerateOptions.JsonConverter | UnitGenerateOptions.MessagePackFormatter | UnitGenerateOptions.DapperTypeHandler | UnitGenerateOptions.EntityFrameworkValueConverter)] + [UnitOf(typeof(ushort), UnitGenerateOptions.ParseMethod | UnitGenerateOptions.MinMaxMethod | UnitGenerateOptions.ArithmeticOperator | UnitGenerateOptions.ValueArithmeticOperator | UnitGenerateOptions.Comparable | UnitGenerateOptions.Validate | UnitGenerateOptions.JsonConverter | UnitGenerateOptions.MessagePackFormatter | UnitGenerateOptions.DapperTypeHandler | UnitGenerateOptions.EntityFrameworkValueConverter | UnitGenerateOptions.JsonConverterDictionaryKeySupport)] public readonly partial struct D { private partial void Validate() @@ -52,7 +54,7 @@ private partial void Validate() } } - [UnitOf(typeof(byte), UnitGenerateOptions.ParseMethod | UnitGenerateOptions.MinMaxMethod | UnitGenerateOptions.ArithmeticOperator | UnitGenerateOptions.ValueArithmeticOperator | UnitGenerateOptions.Comparable | UnitGenerateOptions.Validate | UnitGenerateOptions.JsonConverter | UnitGenerateOptions.MessagePackFormatter | UnitGenerateOptions.DapperTypeHandler | UnitGenerateOptions.EntityFrameworkValueConverter)] + [UnitOf(typeof(byte), UnitGenerateOptions.ParseMethod | UnitGenerateOptions.MinMaxMethod | UnitGenerateOptions.ArithmeticOperator | UnitGenerateOptions.ValueArithmeticOperator | UnitGenerateOptions.Comparable | UnitGenerateOptions.Validate | UnitGenerateOptions.JsonConverter | UnitGenerateOptions.MessagePackFormatter | UnitGenerateOptions.DapperTypeHandler | UnitGenerateOptions.EntityFrameworkValueConverter | UnitGenerateOptions.JsonConverterDictionaryKeySupport)] public readonly partial struct E { private partial void Validate() @@ -61,7 +63,7 @@ private partial void Validate() } } - [UnitOf(typeof(sbyte), UnitGenerateOptions.ParseMethod | UnitGenerateOptions.MinMaxMethod | UnitGenerateOptions.ArithmeticOperator | UnitGenerateOptions.ValueArithmeticOperator | UnitGenerateOptions.Comparable | UnitGenerateOptions.Validate | UnitGenerateOptions.JsonConverter | UnitGenerateOptions.MessagePackFormatter | UnitGenerateOptions.DapperTypeHandler | UnitGenerateOptions.EntityFrameworkValueConverter)] + [UnitOf(typeof(sbyte), UnitGenerateOptions.ParseMethod | UnitGenerateOptions.MinMaxMethod | UnitGenerateOptions.ArithmeticOperator | UnitGenerateOptions.ValueArithmeticOperator | UnitGenerateOptions.Comparable | UnitGenerateOptions.Validate | UnitGenerateOptions.JsonConverter | UnitGenerateOptions.MessagePackFormatter | UnitGenerateOptions.DapperTypeHandler | UnitGenerateOptions.EntityFrameworkValueConverter | UnitGenerateOptions.JsonConverterDictionaryKeySupport)] public readonly partial struct F { private partial void Validate() @@ -70,7 +72,7 @@ private partial void Validate() } } - [UnitOf(typeof(float), UnitGenerateOptions.ParseMethod | UnitGenerateOptions.MinMaxMethod | UnitGenerateOptions.ArithmeticOperator | UnitGenerateOptions.ValueArithmeticOperator | UnitGenerateOptions.Comparable | UnitGenerateOptions.Validate | UnitGenerateOptions.JsonConverter | UnitGenerateOptions.MessagePackFormatter | UnitGenerateOptions.DapperTypeHandler | UnitGenerateOptions.EntityFrameworkValueConverter)] + [UnitOf(typeof(float), UnitGenerateOptions.ParseMethod | UnitGenerateOptions.MinMaxMethod | UnitGenerateOptions.ArithmeticOperator | UnitGenerateOptions.ValueArithmeticOperator | UnitGenerateOptions.Comparable | UnitGenerateOptions.Validate | UnitGenerateOptions.JsonConverter | UnitGenerateOptions.MessagePackFormatter | UnitGenerateOptions.DapperTypeHandler | UnitGenerateOptions.EntityFrameworkValueConverter | UnitGenerateOptions.JsonConverterDictionaryKeySupport)] public readonly partial struct G { private partial void Validate() @@ -79,7 +81,7 @@ private partial void Validate() } } - [UnitOf(typeof(double), UnitGenerateOptions.ParseMethod | UnitGenerateOptions.MinMaxMethod | UnitGenerateOptions.ArithmeticOperator | UnitGenerateOptions.ValueArithmeticOperator | UnitGenerateOptions.Comparable | UnitGenerateOptions.Validate | UnitGenerateOptions.JsonConverter | UnitGenerateOptions.MessagePackFormatter | UnitGenerateOptions.DapperTypeHandler | UnitGenerateOptions.EntityFrameworkValueConverter)] + [UnitOf(typeof(double), UnitGenerateOptions.ParseMethod | UnitGenerateOptions.MinMaxMethod | UnitGenerateOptions.ArithmeticOperator | UnitGenerateOptions.ValueArithmeticOperator | UnitGenerateOptions.Comparable | UnitGenerateOptions.Validate | UnitGenerateOptions.JsonConverter | UnitGenerateOptions.MessagePackFormatter | UnitGenerateOptions.DapperTypeHandler | UnitGenerateOptions.EntityFrameworkValueConverter | UnitGenerateOptions.JsonConverterDictionaryKeySupport)] public readonly partial struct H { private partial void Validate() @@ -88,7 +90,7 @@ private partial void Validate() } } - [UnitOf(typeof(decimal), UnitGenerateOptions.ParseMethod | UnitGenerateOptions.MinMaxMethod | UnitGenerateOptions.ArithmeticOperator | UnitGenerateOptions.ValueArithmeticOperator | UnitGenerateOptions.Comparable | UnitGenerateOptions.Validate | UnitGenerateOptions.JsonConverter | UnitGenerateOptions.MessagePackFormatter | UnitGenerateOptions.DapperTypeHandler | UnitGenerateOptions.EntityFrameworkValueConverter)] + [UnitOf(typeof(decimal), UnitGenerateOptions.ParseMethod | UnitGenerateOptions.MinMaxMethod | UnitGenerateOptions.ArithmeticOperator | UnitGenerateOptions.ValueArithmeticOperator | UnitGenerateOptions.Comparable | UnitGenerateOptions.Validate | UnitGenerateOptions.JsonConverter | UnitGenerateOptions.MessagePackFormatter | UnitGenerateOptions.DapperTypeHandler | UnitGenerateOptions.EntityFrameworkValueConverter | UnitGenerateOptions.JsonConverterDictionaryKeySupport)] public readonly partial struct I { private partial void Validate() @@ -97,7 +99,7 @@ private partial void Validate() } } - [UnitOf(typeof(float), UnitGenerateOptions.ParseMethod | UnitGenerateOptions.MinMaxMethod | UnitGenerateOptions.ArithmeticOperator | UnitGenerateOptions.ValueArithmeticOperator | UnitGenerateOptions.Comparable | UnitGenerateOptions.Validate | UnitGenerateOptions.JsonConverter | UnitGenerateOptions.MessagePackFormatter | UnitGenerateOptions.DapperTypeHandler | UnitGenerateOptions.EntityFrameworkValueConverter)] + [UnitOf(typeof(float), UnitGenerateOptions.ParseMethod | UnitGenerateOptions.MinMaxMethod | UnitGenerateOptions.ArithmeticOperator | UnitGenerateOptions.ValueArithmeticOperator | UnitGenerateOptions.Comparable | UnitGenerateOptions.Validate | UnitGenerateOptions.JsonConverter | UnitGenerateOptions.MessagePackFormatter | UnitGenerateOptions.DapperTypeHandler | UnitGenerateOptions.EntityFrameworkValueConverter | UnitGenerateOptions.JsonConverterDictionaryKeySupport)] public readonly partial struct J { private partial void Validate() diff --git a/sandbox/ConsoleApp/ConsoleApp.csproj b/sandbox/ConsoleApp/ConsoleApp.csproj index 2391fc3..97af75f 100644 --- a/sandbox/ConsoleApp/ConsoleApp.csproj +++ b/sandbox/ConsoleApp/ConsoleApp.csproj @@ -2,7 +2,7 @@ Exe - net5.0 + net6.0 enable diff --git a/sandbox/ConsoleApp/Others.cs b/sandbox/ConsoleApp/Others.cs index abff6fe..92eb317 100644 --- a/sandbox/ConsoleApp/Others.cs +++ b/sandbox/ConsoleApp/Others.cs @@ -19,7 +19,7 @@ private partial void Validate() _ = AsPrimitive(); } } - [UnitOf(typeof(string), UnitGenerateOptions.Validate | UnitGenerateOptions.DapperTypeHandler | UnitGenerateOptions.EntityFrameworkValueConverter)] + [UnitOf(typeof(string), UnitGenerateOptions.Validate | UnitGenerateOptions.JsonConverter | UnitGenerateOptions.DapperTypeHandler | UnitGenerateOptions.EntityFrameworkValueConverter)] public readonly partial struct ST { private partial void Validate() diff --git a/sandbox/ConsoleApp/Program.cs b/sandbox/ConsoleApp/Program.cs index 13ae8ad..a79eeb9 100644 --- a/sandbox/ConsoleApp/Program.cs +++ b/sandbox/ConsoleApp/Program.cs @@ -1,18 +1,26 @@ using Sample; using System; +using System.Collections.Generic; using System.ComponentModel; using System.Globalization; +using System.Text; using System.Text.Json; using System.Text.Json.Serialization; using UnitGenerator; +//var a = UnitGenerateOptions.JsonConverterDictionaryKeySupport; +//var has = UnitGenerateOptions.JsonConverterDictionaryKeySupport.HasFlag(UnitGenerateOptions.Validate); +//Console.WriteLine(has); + +var json = JsonSerializer.Serialize(new Dictionary { { Guid.NewGuid(), "hogemoge" } }); + + + +Console.WriteLine(json); -var id1 = FooId.Empty; -var id2 = new FooId(Guid.Empty); -Console.WriteLine(id1 == id2); [UnitOf(typeof(int))] public readonly partial struct NoNamespace @@ -22,13 +30,13 @@ public readonly partial struct NoNamespace [UnitOf(typeof(Guid), UnitGenerateOptions.Comparable | UnitGenerateOptions.WithoutComparisonOperator)] public readonly partial struct FooId { } -[UnitOf(typeof(Ulid), UnitGenerateOptions.Comparable | UnitGenerateOptions.WithoutComparisonOperator | UnitGenerateOptions.MessagePackFormatter)] +[UnitOf(typeof(Ulid), UnitGenerateOptions.Comparable | UnitGenerateOptions.WithoutComparisonOperator | UnitGenerateOptions.MessagePackFormatter | UnitGenerateOptions.JsonConverter | UnitGenerateOptions.JsonConverterDictionaryKeySupport)] public readonly partial struct BarId { } namespace Sample { - [UnitOf(typeof(int), UnitGenerateOptions.ArithmeticOperator | UnitGenerateOptions.ValueArithmeticOperator | UnitGenerateOptions.Comparable | UnitGenerateOptions.MinMaxMethod)] + [UnitOf(typeof(int), UnitGenerateOptions.ArithmeticOperator | UnitGenerateOptions.ValueArithmeticOperator | UnitGenerateOptions.Comparable | UnitGenerateOptions.MinMaxMethod | UnitGenerateOptions.JsonConverter | UnitGenerateOptions.JsonConverterDictionaryKeySupport)] public readonly partial struct Hp { // public static Hp operator +(in Hp x, in Hp y) => new Hp(checked((int)(x.value + y.value))); @@ -77,31 +85,3 @@ public void Foo() } } - - -namespace ConsoleApp -{ - - - [UnitOf(typeof((string street, string city)))] - public readonly partial struct StreetAddress { } - - class Program - { - static void Foo(short x) - { - } - - static void Main(string[] args) - { - - - } - } - -} - - - - - diff --git a/src/UnitGenerator/CodeTemplate.Partial.cs b/src/UnitGenerator/CodeTemplate.Partial.cs index d752e02..093500c 100644 --- a/src/UnitGenerator/CodeTemplate.Partial.cs +++ b/src/UnitGenerator/CodeTemplate.Partial.cs @@ -40,5 +40,33 @@ internal DbType GetDbType() _ => DbType.Object }; } + + internal bool IsSupportUtf8Formatter() + { + return Type switch + { + "short" => true, + "int" => true, + "long" => true, + "ushort" => true, + "uint" => true, + "ulong" => true, + "bool" => true, + "byte" => true, + "sbyte" => true, + "float" => true, + "double" => true, + "System.DateTime" => true, + "System.DateTimeOffset" => true, + "System.TimeSpan" => true, + "System.Guid" => true, + _ => false + }; + } + + internal bool IsUlid() + { + return Type == "Ulid" || Type == "System.Ulid"; + } } -} +} \ No newline at end of file diff --git a/src/UnitGenerator/CodeTemplate.cs b/src/UnitGenerator/CodeTemplate.cs index 6cbb215..f63a74e 100644 --- a/src/UnitGenerator/CodeTemplate.cs +++ b/src/UnitGenerator/CodeTemplate.cs @@ -1,5 +1,10 @@ // ------------------------------------------------------------------------------ // +// このコードはツールによって生成されました。 +// ランタイム バージョン: 17.0.0.0 +// +// このファイルへの変更は、正しくない動作の原因になる可能性があり、 +// コードが再生成されると失われます。 // // ------------------------------------------------------------------------------ namespace UnitGenerator @@ -377,7 +382,63 @@ public virtual string TransformText() " else\r\n {\r\n throw new JsonException($\"" + "{typeof("); this.Write(this.ToStringHelper.ToStringWithCulture(Type)); - this.Write(")} converter does not found.\");\r\n }\r\n }\r\n }\r\n\r\n"); + this.Write(")} converter does not found.\");\r\n }\r\n }\r\n\r\n"); +if (HasFlag(UnitGenerateOptions.JsonConverterDictionaryKeySupport)) { + this.Write(" public override void WriteAsPropertyName(Utf8JsonWriter writer, "); + this.Write(this.ToStringHelper.ToStringWithCulture(Name)); + this.Write(" value, JsonSerializerOptions options)\r\n {\r\n"); +if (IsSupportUtf8Formatter()) { + this.Write(@" Span buffer = stackalloc byte[36]; + if (System.Buffers.Text.Utf8Formatter.TryFormat(value.value, buffer, out var written)) + { + writer.WritePropertyName(buffer.Slice(0, written)); + } + else + { + writer.WritePropertyName(value.value.ToString()); + } +"); + } else if (IsUlid()) { + this.Write(" writer.WritePropertyName(value.value.ToString());\r\n"); + } else { + this.Write(" writer.WritePropertyName(value.value.ToString());\r\n"); + } + this.Write(" }\r\n\r\n public override "); + this.Write(this.ToStringHelper.ToStringWithCulture(Name)); + this.Write(" ReadAsPropertyName(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializer" + + "Options options)\r\n {\r\n"); +if (IsSupportUtf8Formatter()) { + this.Write(" if (System.Buffers.Text.Utf8Parser.TryParse(reader.ValueSpan, out" + + " "); + this.Write(this.ToStringHelper.ToStringWithCulture(Type)); + this.Write(" value, out var consumed))\r\n {\r\n return new "); + this.Write(this.ToStringHelper.ToStringWithCulture(Name)); + this.Write("(value);\r\n }\r\n else\r\n {\r\n " + + " return new "); + this.Write(this.ToStringHelper.ToStringWithCulture(Name)); + this.Write("("); + this.Write(this.ToStringHelper.ToStringWithCulture(Type)); + this.Write(".Parse(reader.GetString()));\r\n }\r\n"); + } else if (IsUlid()) { + this.Write(" return new "); + this.Write(this.ToStringHelper.ToStringWithCulture(Name)); + this.Write("("); + this.Write(this.ToStringHelper.ToStringWithCulture(Type)); + this.Write(".Parse(reader.GetString()));\r\n"); + } else if(Type == "string") { + this.Write(" return new "); + this.Write(this.ToStringHelper.ToStringWithCulture(Name)); + this.Write("(reader.GetString());\r\n"); + } else { + this.Write(" return new "); + this.Write(this.ToStringHelper.ToStringWithCulture(Name)); + this.Write("("); + this.Write(this.ToStringHelper.ToStringWithCulture(Type)); + this.Write(".Parse(reader.GetString()));\r\n"); + } + this.Write(" }\r\n\r\n"); + } + this.Write("\r\n }\r\n\r\n"); } this.Write("\r\n"); if (HasFlag(UnitGenerateOptions.MessagePackFormatter)) { diff --git a/src/UnitGenerator/CodeTemplate.tt b/src/UnitGenerator/CodeTemplate.tt index f0a64c2..6199c65 100644 --- a/src/UnitGenerator/CodeTemplate.tt +++ b/src/UnitGenerator/CodeTemplate.tt @@ -342,6 +342,49 @@ namespace <#= Namespace #> throw new JsonException($"{typeof(<#= Type #>)} converter does not found."); } } + +<#if (HasFlag(UnitGenerateOptions.JsonConverterDictionaryKeySupport)) { #> + public override void WriteAsPropertyName(Utf8JsonWriter writer, <#= Name #> value, JsonSerializerOptions options) + { +<#if (IsSupportUtf8Formatter()) { #> + Span buffer = stackalloc byte[36]; + if (System.Buffers.Text.Utf8Formatter.TryFormat(value.value, buffer, out var written)) + { + writer.WritePropertyName(buffer.Slice(0, written)); + } + else + { + writer.WritePropertyName(value.value.ToString()); + } +<# } else if (IsUlid()) { #> + writer.WritePropertyName(value.value.ToString()); +<# } else { #> + writer.WritePropertyName(value.value.ToString()); +<# } #> + } + + public override <#= Name #> ReadAsPropertyName(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { +<#if (IsSupportUtf8Formatter()) { #> + if (System.Buffers.Text.Utf8Parser.TryParse(reader.ValueSpan, out <#= Type #> value, out var consumed)) + { + return new <#= Name #>(value); + } + else + { + return new <#= Name #>(<#= Type #>.Parse(reader.GetString())); + } +<# } else if (IsUlid()) { #> + return new <#= Name #>(<#= Type #>.Parse(reader.GetString())); +<# } else if(Type == "string") { #> + return new <#= Name #>(reader.GetString()); +<# } else { #> + return new <#= Name #>(<#= Type #>.Parse(reader.GetString())); +<# } #> + } + +<# } #> + } <# } #> diff --git a/src/UnitGenerator/UnitGenerateOptions.cs b/src/UnitGenerator/UnitGenerateOptions.cs index 4c504ce..69e8297 100644 --- a/src/UnitGenerator/UnitGenerateOptions.cs +++ b/src/UnitGenerator/UnitGenerateOptions.cs @@ -18,6 +18,7 @@ internal enum UnitGenerateOptions MessagePackFormatter = 256, DapperTypeHandler = 512, EntityFrameworkValueConverter = 1024, - WithoutComparisonOperator = 2048 + WithoutComparisonOperator = 2048, + JsonConverterDictionaryKeySupport = 4096 } } \ No newline at end of file diff --git a/src/UnitGenerator/UnitOfAttributeTemplate.cs b/src/UnitGenerator/UnitOfAttributeTemplate.cs index 9d21d09..0d942d7 100644 --- a/src/UnitGenerator/UnitOfAttributeTemplate.cs +++ b/src/UnitGenerator/UnitOfAttributeTemplate.cs @@ -1,10 +1,10 @@ // ------------------------------------------------------------------------------ // -// This code was generated by a tool. -// Runtime Version: 17.0.0.0 +// このコードはツールによって生成されました。 +// ランタイム バージョン: 17.0.0.0 // -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. +// このファイルへの変更は、正しくない動作の原因になる可能性があり、 +// コードが再生成されると失われます。 // // ------------------------------------------------------------------------------ namespace UnitGenerator @@ -64,7 +64,8 @@ internal enum UnitGenerateOptions MessagePackFormatter = 256, DapperTypeHandler = 512, EntityFrameworkValueConverter = 1024, - WithoutComparisonOperator = 2048 + WithoutComparisonOperator = 2048, + JsonConverterDictionaryKeySupport = 4096 } }"); return this.GenerationEnvironment.ToString(); diff --git a/src/UnitGenerator/UnitOfAttributeTemplate.tt b/src/UnitGenerator/UnitOfAttributeTemplate.tt index 0e39402..7478c64 100644 --- a/src/UnitGenerator/UnitOfAttributeTemplate.tt +++ b/src/UnitGenerator/UnitOfAttributeTemplate.tt @@ -42,6 +42,7 @@ namespace UnitGenerator MessagePackFormatter = 256, DapperTypeHandler = 512, EntityFrameworkValueConverter = 1024, - WithoutComparisonOperator = 2048 + WithoutComparisonOperator = 2048, + JsonConverterDictionaryKeySupport = 4096 } } \ No newline at end of file