Skip to content

Commit 0202eed

Browse files
committed
Merge branch 'master' of github.com:aaubry/YamlDotNet into ec-1018-nullablefixes
2 parents 0e3bbac + 4f93e42 commit 0202eed

File tree

7 files changed

+448
-13
lines changed

7 files changed

+448
-13
lines changed

YamlDotNet.Analyzers.StaticGenerator/ObjectAccessorFileGenerator.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,17 @@ public override void Write(SerializableSyntaxReceiver syntaxReceiver)
4242

4343
Write("public void Set(string propertyName, object target, object value)");
4444
Write("{"); Indent();
45+
var typeName = classObject.FullName.Replace("?", string.Empty);
4546
if (classObject.FieldSymbols.Count > 0 || classObject.PropertySymbols.Count > 0)
4647
{
47-
Write($"var v = ({classObject.FullName.Replace("?", string.Empty)})target;");
48+
if (classObject.ModuleSymbol.TypeKind is TypeKind.Struct)
49+
{
50+
Write($"ref var v = ref System.Runtime.CompilerServices.Unsafe.Unbox<{typeName}>(target);");
51+
}
52+
else
53+
{
54+
Write($"var v = ({typeName})target;");
55+
}
4856
Write("switch (propertyName)");
4957
Write("{"); Indent();
5058
foreach (var field in classObject.FieldSymbols)
@@ -68,7 +76,7 @@ public override void Write(SerializableSyntaxReceiver syntaxReceiver)
6876

6977
Write("public object Read(string propertyName, object target)");
7078
Write("{"); Indent();
71-
Write($"var v = ({classObject.FullName.Replace("?", string.Empty)})target;");
79+
Write($"var v = ({typeName})target;");
7280
if (classObject.FieldSymbols.Count > 0 || classObject.PropertySymbols.Count > 0)
7381
{
7482
Write("switch (propertyName)");

YamlDotNet.Analyzers.StaticGenerator/SerializableSyntaxReceiver.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
4444
HandleEnum(enumSymbol);
4545
}
4646
}
47-
else if (context.Node is ClassDeclarationSyntax classDeclarationSyntax)
47+
else if (context.Node is BaseTypeDeclarationSyntax classDeclarationSyntax)
4848
{
4949
var classSymbol = context.SemanticModel.GetDeclaredSymbol(classDeclarationSyntax)!;
5050
if (classSymbol.GetAttributes().Any())
@@ -62,7 +62,7 @@ public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
6262

6363
foreach (var type in types.OfType<INamedTypeSymbol>())
6464
{
65-
if (type.TypeKind == TypeKind.Class)
65+
if (type.TypeKind is TypeKind.Class or TypeKind.Struct)
6666
{
6767
AddSerializableClass(type);
6868
}

YamlDotNet.Core7AoTCompileTest/Program.cs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,18 @@
8888
SomeDictionary:
8989
a: 1
9090
b: 2
91+
StructField:
92+
X: 1
93+
Y: 2
94+
Nested:
95+
X: 3
96+
Y: 4
97+
StructProperty:
98+
X: 5
99+
Y: 6
100+
Nested:
101+
X: 7
102+
Y: 8
91103
");
92104

93105
var input = new StringReader(yaml);
@@ -167,6 +179,12 @@
167179
{
168180
Console.WriteLine(" {0}", s);
169181
}
182+
Console.WriteLine("Structs:");
183+
Console.WriteLine(" StructField: <{0},{1}>", x.StructField.X, x.StructField.Y);
184+
Console.WriteLine(" Nested: <{0},{1}>", x.StructField.Nested.X, x.StructField.Nested.Y);
185+
Console.WriteLine(" StructProperty: <{0},{1}>", x.StructProperty.X, x.StructProperty.Y);
186+
Console.WriteLine(" Nested: <{0},{1}>", x.StructProperty.Nested.X, x.StructProperty.Nested.Y);
187+
170188
Console.WriteLine("==============");
171189
Console.WriteLine("Serialized:");
172190

@@ -275,6 +293,8 @@ public class PrimitiveTypes
275293
public ICollection<string> SomeCollectionStrings { get; set; }
276294
public object SomeObject { get; set; }
277295
public object SomeDictionary { get; set; }
296+
public MyTestStruct StructField;
297+
public MyTestStruct StructProperty { get; set; }
278298
}
279299

280300
public class InheritedBase
@@ -329,6 +349,45 @@ public enum EnumMemberedEnum
329349
Hello = 1
330350
}
331351

352+
[YamlSerializable]
353+
public struct MyTestStruct
354+
{
355+
public float X;
356+
public float Y;
357+
public MyTestNestedStruct Nested;
358+
359+
[OnSerializing]
360+
public void Serializing()
361+
{
362+
Console.WriteLine("MyTestStruct: Serializing");
363+
}
364+
365+
[OnSerialized]
366+
public void Serialized()
367+
{
368+
Console.WriteLine("MyTestStruct: Serialized");
369+
}
370+
371+
[OnDeserialized]
372+
public void Deserialized()
373+
{
374+
Console.WriteLine("MyTestStruct: Deserialized");
375+
}
376+
377+
[OnDeserializing]
378+
public void Deserializing()
379+
{
380+
Console.WriteLine("MyTestStruct: Deserializing");
381+
}
382+
}
383+
384+
[YamlSerializable]
385+
public struct MyTestNestedStruct
386+
{
387+
public float X;
388+
public float Y;
389+
}
390+
332391
#pragma warning restore CS8604 // Possible null reference argument.
333392
#pragma warning restore CS8618 // Possible null reference argument.
334393
#pragma warning restore CS8602 // Possible null reference argument.

0 commit comments

Comments
 (0)