using System; using System.IO; using System.Runtime.Serialization.Formatters.Binary; using System.Security.Cryptography; using System.Text; using Newtonsoft.Json; namespace _2DGAMELIB { //serialization stuff public static class Ser { private static SerializableAttribute s = new SerializableAttribute(); public static T DeepCopy(this T Object) { BinaryFormatter binaryFormatter = new BinaryFormatter(); using MemoryStream memoryStream = new MemoryStream(); binaryFormatter.Serialize(memoryStream, Object); memoryStream.Position = 0L; return (T)binaryFormatter.Deserialize(memoryStream); } public static byte[] ToSerialBytes(this T Object) { using MemoryStream memoryStream = new MemoryStream(); new BinaryFormatter().Serialize(memoryStream, Object); return memoryStream.ToArray(); } public static T ToDeserialObject(this byte[] Bytes) { using MemoryStream serializationStream = new MemoryStream(Bytes); return (T)new BinaryFormatter().Deserialize(serializationStream); } public static void Save(this T Object, string Path) { using FileStream serializationStream = new FileStream(Path, FileMode.Create, FileAccess.Write); new BinaryFormatter().Serialize(serializationStream, Object); } public static T Load(this string Path) { using FileStream serializationStream = new FileStream(Path, FileMode.Open, FileAccess.Read); return (T)new BinaryFormatter().Deserialize(serializationStream); } public static T Load(this byte[] bd) { using MemoryStream serializationStream = new MemoryStream(bd); return (T)new BinaryFormatter().Deserialize(serializationStream); } /* public static void ToXml(this T Object, string Path) { using FileStream output = new FileStream(Path, FileMode.Create, FileAccess.Write); using XmlWriter writer = XmlWriter.Create(output, new XmlWriterSettings { Indent = true }); new DataContractSerializer(typeof(T)).WriteObject(writer, Object); } public static T FromXml(this string Path) { using FileStream input = new FileStream(Path, FileMode.Open, FileAccess.Read); using XmlReader reader = XmlReader.Create(input); return (T)new DataContractSerializer(typeof(T)).ReadObject(reader); } */ static Ser(){} public static T JsonDeepCopy(this T Object) { using MemoryStream memoryStream = new MemoryStream(); JsonSerializer jsonSerializer = new JsonSerializer { PreserveReferencesHandling = PreserveReferencesHandling.All, TypeNameHandling = TypeNameHandling.All, Formatting = Newtonsoft.Json.Formatting.Indented, NullValueHandling = NullValueHandling.Ignore }; jsonSerializer.Serialize(new StreamWriter(memoryStream, Encoding.UTF8), Object); return (T)new JsonSerializer { PreserveReferencesHandling = PreserveReferencesHandling.All, TypeNameHandling = TypeNameHandling.All, Formatting = Newtonsoft.Json.Formatting.Indented, NullValueHandling = NullValueHandling.Ignore }.Deserialize(new StreamReader(memoryStream), typeof(T)); } public static byte[] ToJsonBytes(this T Object) { MemoryStream textWriter = new MemoryStream(); JsonSerializer jsonSerializer = new JsonSerializer { PreserveReferencesHandling = PreserveReferencesHandling.Objects, TypeNameHandling = TypeNameHandling.All, Formatting = Newtonsoft.Json.Formatting.Indented, NullValueHandling = NullValueHandling.Ignore }; jsonSerializer.Serialize(new StreamWriter(textWriter, Encoding.UTF8), Object); return textWriter.ToArray(); } public static T ToUnJsonObject(this byte[] Bytes) { using StreamReader reader = new StreamReader(new MemoryStream(Bytes), Encoding.UTF8); return (T)new JsonSerializer { PreserveReferencesHandling = PreserveReferencesHandling.All, TypeNameHandling = TypeNameHandling.All, Formatting = Newtonsoft.Json.Formatting.Indented, NullValueHandling = NullValueHandling.Ignore }.Deserialize(reader, typeof(T)); } public static void ToJson(this T Object, string Path) { using StreamWriter textWriter = File.CreateText(Path); JsonSerializer jsonSerializer = new JsonSerializer { PreserveReferencesHandling = PreserveReferencesHandling.All, TypeNameHandling = TypeNameHandling.All, Formatting = Newtonsoft.Json.Formatting.Indented, NullValueHandling = NullValueHandling.Ignore }; jsonSerializer.Serialize(textWriter, Object); } public static T UnJson(string Path) { using StreamReader reader = File.OpenText(Path); return (T)new JsonSerializer { PreserveReferencesHandling = PreserveReferencesHandling.All, TypeNameHandling = TypeNameHandling.All, Formatting = Newtonsoft.Json.Formatting.Indented, NullValueHandling = NullValueHandling.Ignore }.Deserialize(reader, typeof(T)); } } }