/// <summary> /// Ensures that an entry has a key. /// </summary> /// <param name="entry">The entry.</param> private void EnsureKey(ReadOnlyLazyEntry <TKey, TValue> entry) { if (entry.HasKey) { return; } entry.Key = _serializationService.ToObject <TKey>(entry.KeyData); }
private void AssertAggregator <TResult>(AggregatorBase <TResult> aggregator, int classId) { var aggregatorType = aggregator.GetType(); Assert.That(aggregator.FactoryId, Is.EqualTo(FactoryIds.AggregatorDsFactoryId)); Assert.That(aggregator.ClassId, Is.EqualTo(classId)); Assert.Throws <ArgumentException>(() => _ = Aggregators.Count("")); Assert.Throws <ArgumentException>(() => _ = Aggregators.Count(null)); Assert.Throws <ArgumentNullException>(() => aggregator.WriteData(null)); Assert.Throws <ArgumentNullException>(() => aggregator.ReadData(null)); using var output = new ObjectDataOutput(1024, _serializationService, Endianness.BigEndian); aggregator.WriteData(output); using var input = new ObjectDataInput(output.Buffer, _serializationService, Endianness.BigEndian); var a = (AggregatorBase <TResult>)Activator.CreateInstance(aggregatorType); a.ReadData(input); Assert.That(a.AttributePath, Is.EqualTo(aggregator.AttributePath)); var data = _serializationService.ToData(aggregator); if (aggregatorType.IsGenericType) { // doh - cannot deserialize generic types? IAggregator <object> x = null; if (aggregatorType.GetGenericTypeDefinition() == typeof(MaxAggregator <>)) { x = _serializationService.ToObject <MaxAggregator <object> >(data); } else if (aggregatorType.GetGenericTypeDefinition() == typeof(MinAggregator <>)) { x = _serializationService.ToObject <MinAggregator <object> >(data); } else { Assert.Fail("Unsupported generic aggregator type."); } Assert.That(x.AttributePath, Is.EqualTo(aggregator.AttributePath)); } else { var x = _serializationService.ToObject <IAggregator <TResult> >(data); Assert.That(x.AttributePath, Is.EqualTo(aggregator.AttributePath)); } }
internal static void TestPreDefinedDifferentVersions(SerializationService serializationService, SerializationService serializationService2, MainPortable mainPortable) { var data = serializationService.ToData(mainPortable); Assert.AreEqual(mainPortable, serializationService2.ToObject <MainPortable>(data)); }
internal IEnumerable <KeyValuePair <int, KeyValuePair <object, object> > > AsAnchorIterator(SerializationService serializationService) { using var dataEntryIterator = AnchorDataList.GetEnumerator(); foreach (var pageNumber in AnchorPageList) { dataEntryIterator.MoveNext(); var(keyData, valueData) = dataEntryIterator.Current; var key = serializationService.ToObject(keyData); var value = serializationService.ToObject(valueData); var entry = new KeyValuePair <object, object>(key, value); yield return(new KeyValuePair <int, KeyValuePair <object, object> >(pageNumber, entry)); } }
public void TestSerializeDeserializeJsonValue() { var jsonValue = new HazelcastJsonValue("{ \"key\": \"value\" }"); var jsonData = _serializationService.ToData(jsonValue); var jsonDeserialized = _serializationService.ToObject <HazelcastJsonValue>(jsonData); Assert.AreEqual(jsonValue, jsonDeserialized); }
/// <summary> /// Ensures that a cache entry has a value. /// </summary> /// <param name="entry">The cache entry.</param> private void EnsureValue(ReadOnlyLazyEntry <TValue> entry) { if (entry.HasValue) { return; } // accepted race-condition here entry.Value = _serializationService.ToObject <TValue>(entry.ValueData); }
private T AssertPredicate <T>(T predicate, int classId) where T : IIdentifiedDataSerializable { Assert.That(predicate.FactoryId, Is.EqualTo(FactoryIds.PredicateFactoryId)); Assert.That(predicate.ClassId, Is.EqualTo(classId)); // Assert.Throws<ArgumentNullException>(() => predicate.WriteData(null)); // Assert.Throws<ArgumentNullException>(() => predicate.ReadData(null)); using var output = new ObjectDataOutput(1024, _serializationService, Endianness.BigEndian); predicate.WriteData(output); T p = default; if (typeof(T) != typeof(PagingPredicate) && typeof(T) != typeof(PartitionPredicate)) { using var input = new ObjectDataInput(output.Buffer, _serializationService, Endianness.BigEndian); p = (T)Activator.CreateInstance(typeof(T)); p.ReadData(input); Assert.That(predicate.Equals(p)); Assert.That(predicate.Equals(predicate)); Assert.That(predicate.Equals(null), Is.False); Assert.That(Equals(predicate, p)); Assert.That(Equals(predicate, predicate)); Assert.That(Equals(predicate, null), Is.False); var type = typeof(T); MethodInfo staticEquals; do { staticEquals = type.GetMethod("Equals", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy); type = type.BaseType; } while (staticEquals == null && type != typeof(object)); Assert.That(staticEquals, Is.Not.Null); Assert.That((bool)staticEquals.Invoke(null, new object[] { predicate, p })); Assert.That((bool)staticEquals.Invoke(null, new object[] { predicate, predicate })); Assert.That((bool)staticEquals.Invoke(null, new object[] { predicate, null }), Is.False); var data = _serializationService.ToData(predicate); p = _serializationService.ToObject <T>(data); Assert.That(predicate.Equals(p)); } _ = predicate.GetHashCode(); Console.WriteLine($"{typeof(T)}: {predicate}"); return(p); }
public void TestLargeStringEncodeDecode() { var sb = new StringBuilder(); var i = 0; var j = 0; while (j < TestStrSize) { var ch = i++ % char.MaxValue; if (char.IsLetter((char)ch)) { sb.Append(ch); j++; } } var actualStr = sb.ToString(); var strBytes = Encoding.UTF8.GetBytes(actualStr); var actualDataBytes = _serializationService.ToData(actualStr).ToByteArray(); var expectedDataByte = ToDataByte(strBytes); var decodedStr = (string)_serializationService.ToObject <object>(new HeapData(expectedDataByte)); Assert.AreEqual(decodedStr, actualStr); Assert.AreEqual(expectedDataByte, actualDataBytes, "Deserialized byte array do not match utf-8 encoding"); }
/// <exception cref="System.IO.IOException" /> internal static void TestDifferentClassVersionsUsingDataWriteAndRead(SerializationService serializationService, SerializationService serializationService2) { var portableV1 = new NamedPortable("portable-v1", 111); var dataV1 = serializationService.ToData(portableV1); // serialize new portable version var portableV2 = new NamedPortableV2("portable-v2", 123, 500); var dataV2 = serializationService2.ToData(portableV2); NamedPortable v1FromV2 = serializationService.ToObject <NamedPortable>(dataV2); Assert.AreEqual(portableV2.name, v1FromV2.name); Assert.AreEqual(portableV2.k, v1FromV2.k); NamedPortableV2 v2FromV1 = serializationService2.ToObject <NamedPortableV2>(dataV1); Assert.AreEqual(portableV1.name, v2FromV1.name); Assert.AreEqual(portableV1.k, v2FromV1.k); Assert.AreEqual(v2FromV1.v, 0); }
internal static void TestDifferentClassVersions(SerializationService serializationService, SerializationService serializationService2) { NamedPortable portableV1 = new NamedPortable("named-portable", 123); IData dataV1 = serializationService.ToData(portableV1); NamedPortableV2 portableV2 = new NamedPortableV2("named-portable", 123, 500); IData dataV2 = serializationService2.ToData(portableV2); NamedPortable v1FromV2 = serializationService.ToObject <NamedPortable>(dataV2); Assert.AreEqual(portableV2.name, v1FromV2.name); Assert.AreEqual(portableV2.k, v1FromV2.k); NamedPortableV2 v2FromV1 = serializationService2.ToObject <NamedPortableV2>(dataV1); Assert.AreEqual(portableV1.name, v2FromV1.name); Assert.AreEqual(portableV1.k, v2FromV1.k); Assert.AreEqual(v2FromV1.v, 0); //Assert.IsNull(v2FromV1.v); }
public virtual void TestNestedPortableVersionedSerializer() { SerializationServiceBuilder builder1 = new SerializationServiceBuilder(new NullLoggerFactory()); builder1.SetPortableVersion(6); builder1.AddPortableFactory(1, new MyPortableFactory()); SerializationService ss1 = builder1.Build(); SerializationServiceBuilder builder2 = new SerializationServiceBuilder(new NullLoggerFactory()); builder2.SetPortableVersion(6); builder2.AddPortableFactory(1, new MyPortableFactory()); SerializationService ss2 = builder2.Build(); //make sure ss2 cached class definition of Child ss2.ToData(new Child("ubeyd")); //serialized parent from ss1 Parent parent = new Parent(new Child("ubeyd")); IData data = ss1.ToData(parent); // cached class definition of Child and the class definition from data coming from ss1 should be compatible Assert.AreEqual(parent, ss2.ToObject <Parent>(data)); }
/// <summary> /// Converts a value <see cref="IData"/> to the internal cached value format. /// </summary> /// <param name="valueData">Value data.</param> /// <returns>Internal cached value.</returns> protected virtual object ToCachedValue(IData valueData) { return(InMemoryFormat.Equals(InMemoryFormat.Binary) ? valueData : SerializationService.ToObject <object>(valueData)); }