diff --git a/SabreTools.Serialization/ConcreteAbstractSerializer.cs b/SabreTools.Serialization/ConcreteAbstractSerializer.cs new file mode 100644 index 00000000..9b2229b0 --- /dev/null +++ b/SabreTools.Serialization/ConcreteAbstractSerializer.cs @@ -0,0 +1,48 @@ +#if !NETFRAMEWORK + +using System; +using System.Reflection; +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace SabreTools.Serialization +{ + /// + /// Serializer class for abstract classes + /// + /// + internal class ConcreteAbstractSerializer : JsonConverterFactory + { + public override bool CanConvert(Type typeToConvert) => typeToConvert.IsAbstract; + + class ConcreteAbstractSerializerOfType : JsonConverter + { + static ConcreteAbstractSerializerOfType() + { + if (!typeof(TAbstract).IsAbstract && !typeof(TAbstract).IsInterface) + throw new NotImplementedException(string.Format("Concrete class {0} is not supported", typeof(TAbstract))); + } + +#if NETCOREAPP3_1 + public override TAbstract Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => + throw new NotImplementedException(); +#else + public override TAbstract? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => + throw new NotImplementedException(); +#endif + + public override void Write(Utf8JsonWriter writer, TAbstract value, JsonSerializerOptions options) => + JsonSerializer.Serialize(writer, value!, options); + } + + public override JsonConverter CreateConverter(Type type, JsonSerializerOptions options) => + (JsonConverter)Activator.CreateInstance( + typeof(ConcreteAbstractSerializerOfType<>).MakeGenericType(new Type[] { type }), + BindingFlags.Instance | BindingFlags.Public, + binder: null, + args: Array.Empty(), + culture: null).ThrowOnNull(); + } +} + +#endif \ No newline at end of file diff --git a/SabreTools.Serialization/ConcreteInterfaceSerializer.cs b/SabreTools.Serialization/ConcreteInterfaceSerializer.cs new file mode 100644 index 00000000..31b51825 --- /dev/null +++ b/SabreTools.Serialization/ConcreteInterfaceSerializer.cs @@ -0,0 +1,48 @@ +#if !NETFRAMEWORK + +using System; +using System.Reflection; +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace SabreTools.Serialization +{ + /// + /// Serializer class for interfaces + /// + /// + internal class ConcreteInterfaceSerializer : JsonConverterFactory + { + public override bool CanConvert(Type typeToConvert) => typeToConvert.IsInterface; + + class ConcreteInterfaceSerializerOfType : JsonConverter + { + static ConcreteInterfaceSerializerOfType() + { + if (!typeof(TInterface).IsAbstract && !typeof(TInterface).IsInterface) + throw new NotImplementedException(string.Format("Concrete class {0} is not supported", typeof(TInterface))); + } + +#if NETCOREAPP3_1 + public override TInterface Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => + throw new NotImplementedException(); +#else + public override TInterface? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => + throw new NotImplementedException(); +#endif + + public override void Write(System.Text.Json.Utf8JsonWriter writer, TInterface value, JsonSerializerOptions options) => + JsonSerializer.Serialize(writer, value!, options); + } + + public override JsonConverter CreateConverter(Type type, JsonSerializerOptions options) => + (JsonConverter)Activator.CreateInstance( + typeof(ConcreteInterfaceSerializerOfType<>).MakeGenericType(new Type[] { type }), + BindingFlags.Instance | BindingFlags.Public, + binder: null, + args: Array.Empty(), + culture: null).ThrowOnNull(); + } +} + +#endif \ No newline at end of file diff --git a/SabreTools.Serialization/ObjectExtensions.cs b/SabreTools.Serialization/ObjectExtensions.cs new file mode 100644 index 00000000..518c8f0b --- /dev/null +++ b/SabreTools.Serialization/ObjectExtensions.cs @@ -0,0 +1,17 @@ +#if !NETFRAMEWORK + +using System; + +namespace SabreTools.Serialization +{ + /// + /// Extensions for generic object types + /// + /// + internal static class ObjectExtensions + { + public static T ThrowOnNull(this T? value) where T : class => value ?? throw new ArgumentNullException(); + } +} + +#endif \ No newline at end of file diff --git a/SabreTools.Serialization/Wrappers/ConcreteInterfaceSerializer.cs b/SabreTools.Serialization/Wrappers/ConcreteInterfaceSerializer.cs deleted file mode 100644 index 97a84a0f..00000000 --- a/SabreTools.Serialization/Wrappers/ConcreteInterfaceSerializer.cs +++ /dev/null @@ -1,94 +0,0 @@ -#if !NETFRAMEWORK - -using System; -using System.Reflection; -using System.Text.Json; -using System.Text.Json.Serialization; - -namespace SabreTools.Serialization.Wrappers -{ - /// - /// Serializer class for abstract classes - /// - /// - internal class ConcreteAbstractSerializer : JsonConverterFactory - { - public override bool CanConvert(Type typeToConvert) => typeToConvert.IsAbstract; - - class ConcreteAbstractSerializerOfType : JsonConverter - { - static ConcreteAbstractSerializerOfType() - { - if (!typeof(TAbstract).IsAbstract && !typeof(TAbstract).IsInterface) - throw new NotImplementedException(string.Format("Concrete class {0} is not supported", typeof(TAbstract))); - } - -#if NETCOREAPP3_1 - public override TAbstract Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => - throw new NotImplementedException(); -#else - public override TAbstract? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => - throw new NotImplementedException(); -#endif - - public override void Write(Utf8JsonWriter writer, TAbstract value, JsonSerializerOptions options) => - JsonSerializer.Serialize(writer, value!, options); - } - - public override JsonConverter CreateConverter(Type type, JsonSerializerOptions options) => - (JsonConverter)Activator.CreateInstance( - typeof(ConcreteAbstractSerializerOfType<>).MakeGenericType(new Type[] { type }), - BindingFlags.Instance | BindingFlags.Public, - binder: null, - args: Array.Empty(), - culture: null).ThrowOnNull(); - } - - /// - /// Serializer class for interfaces - /// - /// - internal class ConcreteInterfaceSerializer : JsonConverterFactory - { - public override bool CanConvert(Type typeToConvert) => typeToConvert.IsInterface; - - class ConcreteInterfaceSerializerOfType : JsonConverter - { - static ConcreteInterfaceSerializerOfType() - { - if (!typeof(TInterface).IsAbstract && !typeof(TInterface).IsInterface) - throw new NotImplementedException(string.Format("Concrete class {0} is not supported", typeof(TInterface))); - } - -#if NETCOREAPP3_1 - public override TInterface Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => - throw new NotImplementedException(); -#else - public override TInterface? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => - throw new NotImplementedException(); -#endif - - public override void Write(System.Text.Json.Utf8JsonWriter writer, TInterface value, JsonSerializerOptions options) => - JsonSerializer.Serialize(writer, value!, options); - } - - public override JsonConverter CreateConverter(Type type, JsonSerializerOptions options) => - (JsonConverter)Activator.CreateInstance( - typeof(ConcreteInterfaceSerializerOfType<>).MakeGenericType(new Type[] { type }), - BindingFlags.Instance | BindingFlags.Public, - binder: null, - args: Array.Empty(), - culture: null).ThrowOnNull(); - } - - /// - /// Extensions for generic object types - /// - /// - internal static class ObjectExtensions - { - public static T ThrowOnNull(this T? value) where T : class => value ?? throw new ArgumentNullException(); - } -} - -#endif \ No newline at end of file