public static byte[] SerializeNonGeneric(object?obj, ABSaveMap map, Dictionary <Type, uint>?targetVersions = null) { var stream = new MemoryStream(); // Use pooling for "MemoryStream"s? SerializeNonGeneric(obj, map, stream, targetVersions); return(stream.ToArray()); }
public void Setup() { ABSaveResult = new MemoryStream(); //WhoaResult = new MemoryStream(); //NewtonsoftJsonResult = new MemoryStream(); Utf8JsonResult = new MemoryStream(); TextJsonResult = new MemoryStream(); //BinaryFormatterResult = new MemoryStream(); //XMLResult = new MemoryStream(); MessagePackResult = new MemoryStream(); BinaryPackResult = new MemoryStream(); Map = ABSaveMap.Get <JsonResponseModel>(ABSaveSettings.ForSpeed); Serializer = Map.GetSerializer(ABSaveResult); var str = File.ReadAllText($@"{Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)}\model.txt"); JsonBytes = Encoding.UTF8.GetBytes(str); TestObj = JsonSerializer.Deserialize <JsonResponseModel>(str); // Serialize everyone ABSave(); UTF8Json(); TextJson(); MessagePack(); BinaryPack(); }
public void Serialize_ByteArray() { var map = ABSaveMap.Get <string>(ABSaveSettings.ForSize); byte[] arr = ABSaveConvert.Serialize("A", map); Assert.AreNotEqual(0, arr.Length); }
public void Setup() { Settings = ABSaveSettings.ForSize; CurrentMap = new ABSaveMap(Settings); CurrentGenerator = new MapGenerator(); CurrentGenerator.Initialize(CurrentMap); }
public async Task GetOrAddNull_WaitsOnNull() { Setup(); // This thread will make an "Allocating" item, the "waiter" should wait for that to change. var secondGenerator = Map.GetGenerator(); Converter retrieved = null; var waiter = new Task(() => { retrieved = Generator.GetExistingOrAddNull(typeof(int)); }); // Make an "Allocating" item. Generator.GetExistingOrAddNull(typeof(int)); waiter.Start(); // Wait a second - this should be more than enough time for the waiter to be stuck in the waiting cycle. await Task.Delay(1000); // Now, we will finish the item, and see if the thread finishes accordingly. var newMapItem = new EmptyConverter(); Generator.ApplyItem(newMapItem, typeof(int)); await Task.Delay(1000); Assert.AreEqual(newMapItem, retrieved); ABSaveMap.ReleaseGenerator(secondGenerator); }
internal ABSaveSerializer(ABSaveMap map) { Map = map; Settings = map.Settings; ShouldReverseEndian = map.Settings.UseLittleEndian != BitConverter.IsLittleEndian; _currentVersionInfos = new VersionInfo[map._highestConverterInstanceId]; }
public void Setup() { var settings = ABSaveSettings.ForSpeed.Customize(b => b.AddConverter <SubTypeConverter>()); Map = new ABSaveMap(settings); Generator = new MapGenerator(); Generator.Initialize(Map); }
public void Deserialize_ByteArray() { var map = ABSaveMap.Get <string>(ABSaveSettings.ForSize); byte[] arr = ABSaveConvert.Serialize("A", map); Assert.AreEqual("A", ABSaveConvert.Deserialize <string>(arr, map)); }
public void ResetStateWithMapFor(Type type) { ResetState(); var gen = CurrentMap.GetGenerator(); CurrentMapItem = gen.GetMap(type); ABSaveMap.ReleaseGenerator(gen); }
public void Initialize(ABSaveSettings template, Dictionary <Type, uint> targetVersions = null, bool lazyWriteCompressed = false) { var settings = template.Customize(b => b .SetLazyWriteCompressed(lazyWriteCompressed) .AddConverter <BaseTypeConverter>() .AddConverter <SubTypeConverter>() .AddConverter <OtherTypeConverter>() ); CurrentMap = ABSaveMap.Get <EmptyClass>(settings); Stream = new MemoryStream(); Serializer = CurrentMap.GetSerializer(Stream, targetVersions); Deserializer = CurrentMap.GetDeserializer(Stream); }
public void GetOrAddNull_TwoThreads_GenerateNew() { Setup(); var secondGenerator = Map.GetGenerator(); Converter first = null; Converter second = null; // Trigger both threads at exactly the same time. Thread tsk = new Thread(() => { first = Generator.GetExistingOrAddNull(typeof(int)); if (first == null) { Generator.ApplyItem(new EmptyConverter(), typeof(int)); } }); Thread tsk2 = new Thread(() => { second = Generator.GetExistingOrAddNull(typeof(int)); if (second == null) { Generator.ApplyItem(new EmptyConverter(), typeof(int)); } }); tsk.Start(); tsk2.Start(); tsk.Join(); tsk2.Join(); // Whichever raced to get the generation done doesn't matter, // if one is null, the other should be not be null. Assert.IsNotNull(first ?? second); // Check that the item was created successfully. Assert.IsInstanceOfType(Map._allTypes[typeof(int)], typeof(EmptyConverter)); ABSaveMap.ReleaseGenerator(secondGenerator); }
public static byte[] Serialize <T>(T obj, ABSaveMap map, Dictionary <Type, uint>?targetVersions = null) => SerializeNonGeneric(obj, map, targetVersions);
public static T Deserialize <T>(Stream stream, ABSaveMap map) where T : class? => (T)DeserializeNonGeneric(stream, map) !;
public static object?DeserializeNonGeneric(byte[] arr, ABSaveMap map) { var stream = new MemoryStream(arr); return(DeserializeNonGeneric(stream, map)); }
public static T Deserialize <T>(byte[] arr, ABSaveMap map) => (T)DeserializeNonGeneric(arr, map) !;
public static void SerializeNonGeneric(object?obj, ABSaveMap map, Stream stream, Dictionary <Type, uint>?targetVersions = null) { using ABSaveSerializer serializer = map.GetSerializer(stream, targetVersions); serializer.SerializeRoot(obj); }
public static void Serialize <T>(T obj, ABSaveMap map, Stream stream, Dictionary <Type, uint>?targetVersions = null) => SerializeNonGeneric(obj, map, stream, targetVersions);
public void Cleanup() { ABSaveMap.ReleaseGenerator(CurrentGenerator); }
internal void Initialize(ABSaveMap map) { _map = map; _converterCache = new Converter[map.Settings.ConverterCount]; }