示例#1
0
        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());
        }
示例#2
0
        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();
        }
示例#3
0
        public void Serialize_ByteArray()
        {
            var map = ABSaveMap.Get <string>(ABSaveSettings.ForSize);

            byte[] arr = ABSaveConvert.Serialize("A", map);
            Assert.AreNotEqual(0, arr.Length);
        }
示例#4
0
 public void Setup()
 {
     Settings         = ABSaveSettings.ForSize;
     CurrentMap       = new ABSaveMap(Settings);
     CurrentGenerator = new MapGenerator();
     CurrentGenerator.Initialize(CurrentMap);
 }
示例#5
0
        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);
        }
示例#6
0
 internal ABSaveSerializer(ABSaveMap map)
 {
     Map                  = map;
     Settings             = map.Settings;
     ShouldReverseEndian  = map.Settings.UseLittleEndian != BitConverter.IsLittleEndian;
     _currentVersionInfos = new VersionInfo[map._highestConverterInstanceId];
 }
示例#7
0
        public void Setup()
        {
            var settings = ABSaveSettings.ForSpeed.Customize(b => b.AddConverter <SubTypeConverter>());

            Map       = new ABSaveMap(settings);
            Generator = new MapGenerator();
            Generator.Initialize(Map);
        }
示例#8
0
        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));
        }
示例#9
0
        public void ResetStateWithMapFor(Type type)
        {
            ResetState();

            var gen = CurrentMap.GetGenerator();

            CurrentMapItem = gen.GetMap(type);
            ABSaveMap.ReleaseGenerator(gen);
        }
示例#10
0
        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);
        }
示例#11
0
        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);
        }
示例#12
0
 public static byte[] Serialize <T>(T obj, ABSaveMap map, Dictionary <Type, uint>?targetVersions = null) => SerializeNonGeneric(obj, map, targetVersions);
示例#13
0
 public static T Deserialize <T>(Stream stream, ABSaveMap map) where T : class? =>
 (T)DeserializeNonGeneric(stream, map) !;
示例#14
0
        public static object?DeserializeNonGeneric(byte[] arr, ABSaveMap map)
        {
            var stream = new MemoryStream(arr);

            return(DeserializeNonGeneric(stream, map));
        }
示例#15
0
 public static T Deserialize <T>(byte[] arr, ABSaveMap map) =>
 (T)DeserializeNonGeneric(arr, map) !;
示例#16
0
 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);
 }
示例#17
0
 public static void Serialize <T>(T obj, ABSaveMap map, Stream stream, Dictionary <Type, uint>?targetVersions = null) =>
 SerializeNonGeneric(obj, map, stream, targetVersions);
示例#18
0
 public void Cleanup()
 {
     ABSaveMap.ReleaseGenerator(CurrentGenerator);
 }
示例#19
0
 internal void Initialize(ABSaveMap map)
 {
     _map            = map;
     _converterCache = new Converter[map.Settings.ConverterCount];
 }