/// <summary> /// 根据key获得值 /// </summary> /// <param name="key"></param> /// <returns></returns> public T Get <T>(string key) { //默认值 var value = default(T); if (_Database == null) { return(value); } try { //cache值 var cacheValue = _Database.StringGet(key); if (!cacheValue.IsNull) { byte[] bytes = (byte[])cacheValue; if (bytes != null && bytes.Length > 0) { value = ByteSerializer.Deserialize <T>(bytes); } if (value == null) { Remove(key); } return(value); } } catch (Exception ex) { //new ECFException(ex, "Cache-Redis"); } return(value); }
public static T Deserialize <T>(ContentType contentType, Stream stream) { switch (contentType) { case ContentType.Bytes: { var serializer = new ByteSerializer(true, false, true); return(serializer.Deserialize <T>(stream)); } case ContentType.Json: { using (MemoryStream ms = new MemoryStream()) { stream.CopyTo(ms); return(System.Text.Json.JsonSerializer.Deserialize <T>(ms.ToArray())); } } case ContentType.JsonNameless: { using (MemoryStream ms = new MemoryStream()) { stream.CopyTo(ms); return(JsonSerializer.DeserializeNameless <T>(ms.ToArray())); } } default: throw new NotImplementedException(); } }
public void StringArrayOfArrayThing() { var serializer = new ByteSerializer(); var model1 = new string[][] { new string[] { "a", "b", "c" }, new string[] { "d", "e", "f" } }; var bytes = serializer.Serialize(model1); var model2 = serializer.Deserialize <string[][]>(bytes); }
public static Exception DeserializeException(ContentType contentType, Stream stream) { switch (contentType) { case ContentType.Bytes: { var serializer = new ByteSerializer(); var model = serializer.Deserialize <ExceptionModel>(stream); return(new Exception(model.Message)); } case ContentType.Json: { using (MemoryStream ms = new MemoryStream()) { stream.CopyTo(ms); var model = System.Text.Json.JsonSerializer.Deserialize <ExceptionModel>(ms.ToArray()); return(new Exception(model.Message)); } } case ContentType.JsonNameless: { using (MemoryStream ms = new MemoryStream()) { stream.CopyTo(ms); var model = JsonSerializer.DeserializeNameless <ExceptionModel>(ms.ToArray()); return(new Exception(model.Message)); } } default: throw new NotImplementedException(); } }
public void EmptyObjectsAndNulls() { var serializer = new ByteSerializer(); var model1 = new NoPropertiesModel(); var bytes1 = serializer.Serialize(model1); var model2 = serializer.Deserialize <NoPropertiesModel>(bytes1); Assert.IsNotNull(model2); var model3 = new NoPropertiesModel[] { new NoPropertiesModel(), new NoPropertiesModel() }; var bytes3 = serializer.Serialize(model3); var model4 = serializer.Deserialize <NoPropertiesModel[]>(bytes3); Assert.AreEqual(2, model4.Length); Assert.IsNotNull(model4[0]); Assert.IsNotNull(model4[1]); var model5 = new NoPropertiesModel[] { null, null }; var bytes5 = serializer.Serialize(model5); var model6 = serializer.Deserialize <NoPropertiesModel[]>(bytes5); Assert.AreEqual(2, model6.Length); Assert.IsNull(model6[0]); Assert.IsNull(model6[1]); var bytes7 = serializer.Serialize(null); var model7 = serializer.Deserialize <NoPropertiesModel>(bytes7); Assert.IsNull(model7); var bytes8 = serializer.Serialize(null); var model8 = serializer.Deserialize <NoPropertiesModel[]>(bytes8); Assert.IsNull(model8); var model9 = new NoPropertiesModel[0]; var bytes9 = serializer.Serialize(model9); var model10 = serializer.Deserialize <IEnumerable <NoPropertiesModel> >(bytes9); Assert.AreEqual(model9.GetType(), model10?.GetType()); var model11 = new ArrayChainModel[] { new ArrayChainModel() { ID = Guid.NewGuid(), Children = null } }; var bytes11 = serializer.Serialize(model11); var model12 = serializer.Deserialize <ArrayChainModel[]>(bytes11); Assert.AreEqual(model11[0].Children, model12[0].Children); }
public void SerializeAndDeserializeToTheSameObject(byte originalValue) { Serializer <byte> serializer = new ByteSerializer(); byte[] serializedValue = serializer.Serialize(originalValue); byte deserializedValue = serializer.Deserialize(serializedValue); Assert.Equal(originalValue, deserializedValue); }
public void BySerializerIndex() { var serializer = new ByteSerializer(); var model1 = Factory.GetSerializerIndexModel(); var bytes = serializer.Serialize(model1); var model2 = serializer.Deserialize <TestSerializerIndexModel2>(bytes); Factory.AssertAreEqual(model1, model2); }
public void ByPropertyName() { var serializer = new ByteSerializer(true); var model1 = Factory.GetAllTypesModel(); var bytes = serializer.Serialize(model1); var model2 = serializer.Deserialize <AllTypesReversedModel>(bytes); Factory.AssertAreEqual(model1, model2); }
public void ByIgnoreSerailizerIndex() { var serializer = new ByteSerializer(false, false, true); var model1 = Factory.GetSerializerIndexModel(); var bytes = serializer.Serialize(model1); var model2 = serializer.Deserialize <TestSerializerIndexModel2>(bytes); Factory.AssertAreNotEqual(model1, model2); }
public void ExceptionObject() { var serializer = new ByteSerializer(); var model1 = new Exception("bad things happened"); var bytes = serializer.Serialize(model1); var model2 = serializer.Deserialize <Exception>(bytes); Assert.AreEqual(model1.Message, model2.Message); }
public void ByLargeSerializerIndex() { var serializer = new ByteSerializer(false, false, false, ByteSerializerIndexSize.UInt16); var model1 = Factory.GetSerializerLongIndexModel(); var bytes = serializer.Serialize(model1); var model2 = serializer.Deserialize <TestSerializerLongIndexModel>(bytes); Factory.AssertAreEqual(model1, model2); }
public void Types() { var serializer = new ByteSerializer(); var model1 = Factory.GetAllTypesModel(); var bytes = serializer.Serialize(model1); var model2 = serializer.Deserialize <AllTypesModel>(bytes); Factory.AssertAreEqual(model1, model2); }
public void Boxing() { var serializer = new ByteSerializer(false, true); var model1 = Factory.GetBoxingModel(); var bytes = serializer.Serialize(model1); var model2 = serializer.Deserialize <TestBoxingModel>(bytes); Factory.AssertAreEqual(model1, model2); }
public void Arrays() { var serializer = new ByteSerializer(); var model1 = Factory.GetArrayModel(); var bytes = serializer.Serialize(model1); var model2 = serializer.Deserialize <BasicModel[]>(bytes); Factory.AssertAreEqual(model1, model2); }
public void CookieObject() { var serializer = new ByteSerializer(); var model1 = new Cookie("tester", "stuff", null, null); var bytes = serializer.Serialize(model1); var model2 = serializer.Deserialize <Cookie>(bytes); Assert.AreEqual(model1.Name, model2.Name); Assert.AreEqual(model1.Value, model2.Value); Assert.AreEqual(model1.Path, model2.Path); Assert.AreEqual(model1.Domain, model2.Domain); }
public void Interface() { var serializer = new ByteSerializer(); ITestInterface model1 = new TestInterfaceImplemented() { Property1 = 5, Property2 = 6, Property3 = 7 }; var bytes = serializer.Serialize(model1); var model2 = serializer.Deserialize <ITestInterface>(bytes); Assert.AreEqual(5, model2.Property1); Assert.AreEqual(6, model2.Property2); }
public void StreamSerializeArray() { var serializer = new ByteSerializer(); var model1 = Factory.GetArrayModel(); byte[] bytes; using (var ms = new MemoryStream()) { serializer.SerializeAsync(ms, model1).GetAwaiter().GetResult(); bytes = ms.ToArray(); } var model2 = serializer.Deserialize <BasicModel[]>(bytes); Factory.AssertAreEqual(model1, model2); }
public void StreamSerializeArrayByPropertyName() { var serializer = new ByteSerializer(true); var model1 = Factory.GetAllTypesModel(); byte[] bytes; using (var ms = new MemoryStream()) { serializer.SerializeAsync(ms, model1).GetAwaiter().GetResult(); bytes = ms.ToArray(); } var model2 = serializer.Deserialize <AllTypesReversedModel>(bytes); Factory.AssertAreEqual(model1, model2); }
private IEnumerator LoadOfficialLevels() { officialLevels = new List <LevelFile>(); // TODO: Heavy, think of something else. var levelAssets = Resources.LoadAll <TextAsset>(officialLevelsDir); foreach (var levelAsset in levelAssets) { LevelFile level = ByteSerializer.Deserialize <LevelFile>(levelAsset.bytes); level.fileName = levelAsset.name; level.isOfficial = true; officialLevels.Add(level); } yield return(null); }
private IEnumerator LoadCustomLevels() { customLevels = new List <LevelFile>(); DirectoryInfo dir = new DirectoryInfo(customLevelsDir); FileInfo[] files = dir.GetFiles("*.lvl"); foreach (var file in files) { LevelFile level = ByteSerializer.Deserialize <LevelFile>(File.ReadAllBytes(file.FullName)); level.fileName = Path.GetFileNameWithoutExtension(file.FullName); level.isOfficial = false; customLevels.Add(level); } yield return(null); }
/// <summary> /// 订阅 /// </summary> /// <param name="subChannael"></param> /// <param name="action"></param> public void RedisSub <T>(string subChannael, Action <T> action) { if (_Subscriber == null) { return; } _Subscriber.Subscribe(subChannael, (channel, message) => { byte[] bytes = (byte[])message; if (bytes != null && bytes.Length > 0) { var value = ByteSerializer.Deserialize <T>(bytes); action(value); } }); }
public static T Deserialize <T>(ContentType contentType, byte[] bytes) { switch (contentType) { case ContentType.Bytes: { var serializer = new ByteSerializer(true, false, true); return(serializer.Deserialize <T>(bytes)); } case ContentType.Json: { return(System.Text.Json.JsonSerializer.Deserialize <T>(bytes)); } case ContentType.JsonNameless: { return(JsonSerializer.DeserializeNameless <T>(bytes)); } default: throw new NotImplementedException(); } }
public static T Deserialize <T>(ReadOnlySpan <byte> bytes) { var serializer = new ByteSerializer(true, true); return(serializer.Deserialize <T>(bytes)); }
public static void TestSpeed() { var items = GetTestStuff(); var timer0 = Stopwatch.StartNew(); foreach (var item in items) { var w1 = new ByteSerializer().Serialize(item); var w2 = new ByteSerializer(true, false).Serialize(item); var w3 = new ByteSerializer(false, true).Serialize(item); var w4 = new ByteSerializer(true, true).Serialize(item); var w5 = Serialize(item); var w6 = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(item)); var v1 = new ByteSerializer().Deserialize <Test>(w1); var v2 = new ByteSerializer(true, false).Deserialize <Test>(w2); var v3 = new ByteSerializer(false, true).Deserialize <Test>(w3); var v4 = new ByteSerializer(true, true).Deserialize <Test>(w4); var v5 = Deserialize(w5); var v6 = JsonConvert.DeserializeObject <Test>(Encoding.UTF8.GetString(w6)); } Console.WriteLine($"Warmup {timer0.ElapsedMilliseconds:n0}ms"); Console.WriteLine(); Console.WriteLine($"Serialize"); GC.Collect(); var timer1 = Stopwatch.StartNew(); var s1 = new List <byte[]>(); long totalSize1 = 0; foreach (var item in items) { var s = new ByteSerializer(); var lapDataBytes = s.Serialize(item); totalSize1 += lapDataBytes.Length; s1.Add(lapDataBytes); } timer1.Stop(); Console.WriteLine($"ByteSerializer {timer1.ElapsedMilliseconds:n0}ms {totalSize1 / 1000:n0}kb"); GC.Collect(); var timer1a = Stopwatch.StartNew(); var s1a = new List <byte[]>(); long totalSize1a = 0; foreach (var item in items) { var s = new ByteSerializer(true, false); var lapDataBytes = s.Serialize(item); totalSize1a += lapDataBytes.Length; s1a.Add(lapDataBytes); } timer1a.Stop(); Console.WriteLine($"ByteSerializerNames {timer1a.ElapsedMilliseconds:n0}ms {totalSize1a / 1000:n0}kb"); GC.Collect(); var timer1b = Stopwatch.StartNew(); var s1b = new List <byte[]>(); long totalSize1b = 0; foreach (var item in items) { var s = new ByteSerializer(false, true); var lapDataBytes = s.Serialize(item); totalSize1b += lapDataBytes.Length; s1b.Add(lapDataBytes); } timer1b.Stop(); Console.WriteLine($"ByteSerializerTypes {timer1b.ElapsedMilliseconds:n0}ms {totalSize1b / 1000:n0}kb"); GC.Collect(); var timer1c = Stopwatch.StartNew(); var s1c = new List <byte[]>(); long totalSize1c = 0; foreach (var item in items) { var s = new ByteSerializer(true, true); var lapDataBytes = s.Serialize(item); totalSize1c += lapDataBytes.Length; s1c.Add(lapDataBytes); } timer1c.Stop(); Console.WriteLine($"ByteSerializerNamesTypes {timer1c.ElapsedMilliseconds:n0}ms {totalSize1c / 1000:n0}kb"); GC.Collect(); var timer2 = Stopwatch.StartNew(); var s2 = new List <byte[]>(); long totalSize2 = 0; foreach (var item in items) { var lapDataBytes = Serialize(item); totalSize2 += lapDataBytes.Length; s2.Add(lapDataBytes); } timer2.Stop(); Console.WriteLine($"BinaryFormatter {timer2.ElapsedMilliseconds:n0}ms {totalSize2 / 1000:n0}kb"); GC.Collect(); var timer3 = Stopwatch.StartNew(); var s3 = new List <byte[]>(); long totalSize3 = 0; foreach (var item in items) { var lapDataBytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(item)); totalSize3 += lapDataBytes.Length; s3.Add(lapDataBytes); } timer3.Stop(); Console.WriteLine($"JsonConvert {timer3.ElapsedMilliseconds:n0}ms {totalSize3 / 1000:n0}kb"); Console.WriteLine(); Console.WriteLine($"Deserialize"); GC.Collect(); var timer4 = Stopwatch.StartNew(); foreach (var lapDataBytes in s1) { var s = new ByteSerializer(); var item = s.Deserialize <Test>(lapDataBytes); } timer4.Stop(); Console.WriteLine($"ByteSerializer {timer4.ElapsedMilliseconds:n0}ms"); //GC.Collect(); //var timerz = Stopwatch.StartNew(); //foreach (var lapDataBytes in s1) //{ // var s = new ByteSerializer(); // var item = s.NewDeserialize<Test>(lapDataBytes); //} //timerz.Stop(); //Console.WriteLine($"ByteSerializerNew {timerz.ElapsedMilliseconds:n0}ms"); GC.Collect(); var timer4a = Stopwatch.StartNew(); foreach (var lapDataBytes in s1a) { var s = new ByteSerializer(true, false); var item = s.Deserialize <Test>(lapDataBytes); } timer4a.Stop(); Console.WriteLine($"ByteSerializerNames {timer4a.ElapsedMilliseconds:n0}ms"); GC.Collect(); var timer4b = Stopwatch.StartNew(); foreach (var lapDataBytes in s1b) { var s = new ByteSerializer(false, true); var item = s.Deserialize <Test>(lapDataBytes); } timer4b.Stop(); Console.WriteLine($"ByteSerializerTypes {timer4b.ElapsedMilliseconds:n0}ms"); GC.Collect(); var timer4c = Stopwatch.StartNew(); foreach (var lapDataBytes in s1c) { var s = new ByteSerializer(true, true); var item = s.Deserialize <Test>(lapDataBytes); } timer4c.Stop(); Console.WriteLine($"ByteSerializerNamesTypes {timer4c.ElapsedMilliseconds:n0}ms"); //GC.Collect(); //var timerz4 = Stopwatch.StartNew(); //foreach (var lapDataBytes in s1c) //{ // var s = new ByteSerializer(true, true); // var item = s.NewDeserialize<Test>(lapDataBytes); //} //timerz4.Stop(); //Console.WriteLine($"ByteSerializerNamesTypesNew {timerz4.ElapsedMilliseconds:n0}ms"); GC.Collect(); var timer5 = Stopwatch.StartNew(); foreach (var lapDataBytes in s2) { var item = Deserialize(lapDataBytes); } timer5.Stop(); Console.WriteLine($"BinaryFormatter {timer5.ElapsedMilliseconds:n0}ms"); GC.Collect(); var timer6 = Stopwatch.StartNew(); foreach (var lapDataBytes in s3) { var item = JsonConvert.DeserializeObject <Test>(Encoding.UTF8.GetString(lapDataBytes)); } timer6.Stop(); Console.WriteLine($"JsonConvert {timer6.ElapsedMilliseconds:n0}"); Console.WriteLine(); }
public void ThrowIfDeserializingLessThanOneByte() { Serializer <byte> serializer = new ByteSerializer(); Assert.Throws <ArgumentOutOfRangeException>(() => serializer.Deserialize(new byte[] { })); }
public static T Deserialize <T>(byte[] bytes) { var serializer = new ByteSerializer(true, true, true); return(serializer.Deserialize <T>(bytes)); }
public void ThrowIfDeserializingNull() { Serializer <byte> serializer = new ByteSerializer(); Assert.Throws <ArgumentNullException>(() => serializer.Deserialize(null)); }