Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added runtime type serialization #62

Merged
merged 2 commits into from
Dec 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions DEMO/DEMO.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">

<Import Project="..\NonSucking.Framework.Serialization.Advanced\NonSucking.Advanced.props" />
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
Expand All @@ -16,11 +17,12 @@

<ItemGroup>
<ProjectReference Include="..\DEMO.Base\DEMO.Base.csproj" />
<!-- <ProjectReference Include="..\NonSucking.Framework.Serialization.Advanced\NonSucking.Framework.Serialization.Advanced.csproj" />-->

<ProjectReference Include="..\NonSucking.Framework.Serialization.Advanced\NonSucking.Framework.Serialization.Advanced.csproj" />

<ProjectReference Include="..\NonSucking.Framework.Serialization\NonSucking.Framework.Serialization.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="true" />
<ProjectReference Include="..\NonSucking.Framework.Extension\NonSucking.Framework.Extension.csproj" />

</ItemGroup>


</Project>
2 changes: 1 addition & 1 deletion DEMO/DynamicTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public partial class D : B
private int Bab { get; set; }

[NoosonDynamicType(typeof(int), typeof(string))]
public object SomeOther { get; set; }
public object? SomeOther { get; set; }
}
//
// void Test(System.IO.BinaryReader br)
Expand Down
14 changes: 11 additions & 3 deletions DEMO/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using NonSucking.Framework.Serialization;
using NonSucking.Framework.Serialization.Advanced;
using static DEMO.SUTMessage;

namespace DEMO
Expand All @@ -28,6 +32,8 @@ static void Main(string[] args)
bll.NameOfList = "ABC";

var conv = Newtonsoft.Json.JsonConvert.SerializeObject(bll, new JsonSerializerSettings { });
var containerType = new ContainingClass()
{ RuntimeValue = new RuntimeTypeTestA("huhu") { SomeValue = 1234 } };
var sutMessage = new SUTMessage()
{
AlternativUser = new User { Name = "Okay" },
Expand Down Expand Up @@ -58,7 +64,9 @@ static void Main(string[] args)
Type = 897987414,
UsersList = new List<User>() { new User() { Name = "1User" }, new User() { Name = "2User" }, new User() { Name = "3User" }, new User() { Name = "4User" } },
X = 123123,
UnmanagedTypes = new() { SomePos = new Point(1, 2), SomeTime = DateTime.Parse("2022-02-22 12:34")}
UnmanagedTypes = new() { SomePos = new Point(1, 2), SomeTime = DateTime.Parse("2022-02-22 12:34")},
RuntimeValue = containerType,
GenericRuntimeValue = new Generic<ContainingClass>() { Prop = new[] { containerType, new ContainingClass() { RuntimeValue = new RuntimeTypeTestB(12) { SomeValue = 12345} }} }
};

//var sut = new SinglePropTest
Expand All @@ -69,13 +77,13 @@ static void Main(string[] args)
//};
using (var ms = new FileStream("sut.save", FileMode.OpenOrCreate))
{
using var bw = new BinaryWriter(ms);
using var bw = new NoosonBinaryWriter(ms);
sutMessage.Serialize(bw);
}

using (var ms = new FileStream("sut.save", FileMode.Open))
{
using var br = new BinaryReader(ms);
using var br = new NoosonBinaryReader(ms);
var sutMessageDes = SUTMessage.Deserialize(br);

if (sutMessageDes == sutMessage)
Expand Down
269 changes: 269 additions & 0 deletions DEMO/RuntimeTypeTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,269 @@
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.InteropServices;
using NonSucking.Framework.Serialization;

namespace DEMO;

[Nooson]
public partial class RuntimeTypeTestBase : IEquatable<RuntimeTypeTestBase>
{
public int SomeValue { get; set; }

public bool Equals(RuntimeTypeTestBase? other)
{
if (ReferenceEquals(null, other))
{
return false;
}

if (ReferenceEquals(this, other))
{
return true;
}

if (other.GetType() != GetType())
return false;

return SomeValue == other.SomeValue;
}

public override bool Equals(object? obj)
{
if (ReferenceEquals(null, obj))
{
return false;
}

if (ReferenceEquals(this, obj))
{
return true;
}

if (obj.GetType() != this.GetType())
{
return false;
}

return Equals((RuntimeTypeTestBase)obj);
}

public override int GetHashCode()
{
return SomeValue;
}
}

[Nooson]
public partial class RuntimeTypeTestA : RuntimeTypeTestBase, IEquatable<RuntimeTypeTestA>
{
public RuntimeTypeTestA(string someOtherValue)
{
SomeOtherValue = someOtherValue;
}

public string SomeOtherValue { get; }

public bool Equals(RuntimeTypeTestA? other)
{
if (ReferenceEquals(null, other))
{
return false;
}
if (ReferenceEquals(this, other))
{
return true;
}
return SomeOtherValue == other.SomeOtherValue && SomeValue == other.SomeValue;
}

public override bool Equals(object? obj)
{
if (ReferenceEquals(null, obj))
{
return false;
}

if (ReferenceEquals(this, obj))
{
return true;
}

if (obj.GetType() != this.GetType())
{
return false;
}

return Equals((RuntimeTypeTestA)obj);
}

public override int GetHashCode()
{
return SomeOtherValue.GetHashCode();
}
}

[Nooson]
public partial class Generic<T> : IEquatable<Generic<T>>
{
[NoosonDynamicType(Resolver = typeof(UnknownTypeResolver))]
public T[] Prop { get; set; }


public bool Equals(Generic<T>? other)
{
if (other is null)
return false;
return Prop.SequenceEqual(other.Prop);
}
}

[Nooson]
public partial class RuntimeTypeTestB : RuntimeTypeTestBase, IEquatable<RuntimeTypeTestB>
{
public RuntimeTypeTestB(int someOtherValue)
{
SomeOtherValue = someOtherValue;
}

public int SomeOtherValue { get; }

public bool Equals(RuntimeTypeTestB? other)
{
if (ReferenceEquals(null, other))
{
return false;
}

if (ReferenceEquals(this, other))
{
return true;
}

return SomeOtherValue == other.SomeOtherValue && SomeValue == other.SomeValue;
}

public override bool Equals(object? obj)
{
if (ReferenceEquals(null, obj))
{
return false;
}

if (ReferenceEquals(this, obj))
{
return true;
}

if (obj.GetType() != this.GetType())
{
return false;
}

return Equals((RuntimeTypeTestB)obj);
}

public override int GetHashCode()
{
return SomeOtherValue;
}
}

[Nooson]
public partial class RuntimeTypeTestC : RuntimeTypeTestBase
{
public RuntimeTypeTestC(string someOtherValue)
{
SomeOtherValue = someOtherValue;
}

public string SomeOtherValue { get; }
}

[Nooson]
public partial class ContainingClass : IEquatable<ContainingClass>
{
[NoosonDynamicType(typeof(RuntimeTypeTestC), Resolver = typeof(Resolver<string>))]
public RuntimeTypeTestBase RuntimeValue { get; set; }

public bool Equals(ContainingClass? other)
{
if (ReferenceEquals(null, other))
{
return false;
}

if (ReferenceEquals(this, other))
{
return true;
}

return RuntimeValue.Equals(other.RuntimeValue);
}

public override bool Equals(object? obj)
{
if (ReferenceEquals(null, obj))
{
return false;
}

if (ReferenceEquals(this, obj))
{
return true;
}

if (obj.GetType() != this.GetType())
{
return false;
}

return Equals((ContainingClass)obj);
}

public override int GetHashCode()
{
return RuntimeValue.GetHashCode();
}
}

internal class UnknownTypeResolver : NoosonRuntimeTypeResolver<string>
{
public static UnknownTypeResolver Instance = new();
protected override Type ResolveType<TReader>(string identifier)
{
return Type.GetType(identifier) ?? throw new ArgumentException();
}

protected override string SolveType<TWriter>(Type type)
{
return type.AssemblyQualifiedName ?? throw new ArgumentException();
}
}


partial struct SomeWrapperStruct<T>
{
public SomeWrapperStruct(T identifier)
{
Identifier = identifier;
}

public T Identifier { get; }
}
internal class Resolver<T> : NoosonRuntimeTypeResolver<SomeWrapperStruct<T>>
{
public static Resolver<T> Instance = new();
protected override Type ResolveType<TReader>(SomeWrapperStruct<T> identifier)
{
return Type.GetType(identifier.Identifier?.ToString() ?? "") ?? throw new ArgumentOutOfRangeException();
}

protected override SomeWrapperStruct<T> SolveType<TWriter>(Type type)
{
var val = type.AssemblyQualifiedName
?? type.FullName
?? throw new ArgumentOutOfRangeException();
return new SomeWrapperStruct<T>((T)(object)val);
}
}
Loading
Loading