public void RoundTrip_TimeParty() { var input = new TimeParty { UnixDateTimeSeconds = 2, UnitDateTimeSecondsOrNull = 2, DateTimeUtc = new DateTime(2002, 2, 2, 12, 1, 2, 123, DateTimeKind.Utc), DateTimeUtcOrNull = new DateTime(2002, 2, 2, 12, 1, 2, 123, DateTimeKind.Utc), DateTimeLocal = new DateTime(2002, 2, 2, 12, 1, 2, 123, DateTimeKind.Local), DateTimeLocalOrNull = new DateTime(2002, 2, 2, 12, 1, 2, 123, DateTimeKind.Local), DateTimeUnspecified = new DateTime(2002, 2, 2, 12, 1, 2, 123, DateTimeKind.Unspecified), DateTimeUnspecifiedOrNull = new DateTime(2002, 2, 2, 12, 1, 2, 123, DateTimeKind.Unspecified), DateTimeOffsetUtc = new DateTimeOffset(2002, 2, 2, 12, 1, 2, TimeSpan.FromHours(0)), DateTimeOffsetUtcOrNull = new DateTimeOffset(2002, 2, 2, 12, 1, 2, TimeSpan.FromHours(0)), DateTimeOffsetLocal = new DateTimeOffset(2002, 2, 2, 12, 1, 2, TimeSpan.FromHours(-5)), DateTimeOffsetLocalOrNull = new DateTimeOffset(2002, 2, 2, 12, 1, 2, TimeSpan.FromHours(-5)), }; var serialized = BinaryConvert.Serialize(input); var deserialized = BinaryConvert.Deserialize <TimeParty>(serialized); ObjectTreeAssert.AreEqual(input, deserialized); Assert.Equal(DateTimeKind.Utc, deserialized.DateTimeUtc.Kind); Assert.Equal(DateTimeKind.Utc, deserialized.DateTimeUtcOrNull.Value.Kind); Assert.Equal(DateTimeKind.Local, deserialized.DateTimeLocal.Kind); Assert.Equal(DateTimeKind.Local, deserialized.DateTimeLocalOrNull.Value.Kind); Assert.Equal(DateTimeKind.Unspecified, deserialized.DateTimeUnspecified.Kind); Assert.Equal(DateTimeKind.Unspecified, deserialized.DateTimeUnspecifiedOrNull.Value.Kind); }
public void Serialize_SelfReferencingClass() { var input = new SelfReferencingClass(); input.Self = input; Assert.Throws <FormatException>(() => BinaryConvert.Serialize(input)); }
public void Serialize_EmptyArray() { var input = new string[0]; var actual = BinaryConvert.Serialize(input); var expected = new byte[] { 5, 0, 0, 0, 0 }; Assert.Equal(expected, actual); }
public void RoundTrip_ArrayOfIntegers() { int[] input = new int[] { 1, 2, 3 }; var serialized = BinaryConvert.Serialize(input); var deserialized = BinaryConvert.Deserialize <int[]>(serialized); Assert.Equal(input, deserialized); }
public void Serialize_EmptyString() { var input = string.Empty; var actual = BinaryConvert.Serialize(input); var expected = new byte[0]; Assert.Equal(expected, actual); }
public void RoundTrip_ImmutableIdNamePair_ToIdNamePair() { var input = new ImmutableIdNamePair(2, "hello"); var serialized = BinaryConvert.Serialize(input); var deserialized = BinaryConvert.Deserialize <IdNamePair>(serialized); ObjectTreeAssert.AreEqual(new IdNamePair(), deserialized); }
public void RoundTrip_Primitives(object input) { var inputType = input.GetType(); var serialized = BinaryConvert.Serialize(input); var deserialized = BinaryConvert.Deserialize(serialized, inputType); Assert.Equal(input, deserialized); }
public void Serialize_CircularParentChildClass() { var input = new CircularParentClass(); input.Child = new CircularChildClass(); input.Child.Parent = input; Assert.Throws <FormatException>(() => BinaryConvert.Serialize(input)); }
public void RoundTrip_CancellationToken() { var input = new CancellationToken(true); var serialized = BinaryConvert.Serialize(input); var deserialized = BinaryConvert.Deserialize <CancellationToken>(serialized); Assert.Equal(default(CancellationToken), deserialized); }
public void RoundTrip_TimeParty_Defaults() { var input = new TimeParty(); var serialized = BinaryConvert.Serialize(input); var deserialized = BinaryConvert.Deserialize <TimeParty>(serialized); ObjectTreeAssert.AreEqual(input, deserialized); }
public void RoundTrip_EmptyArray() { int[] input = new int[0]; var serialized = BinaryConvert.Serialize(input); var deserialized = BinaryConvert.Deserialize <int[]>(serialized); Assert.NotNull(deserialized); Assert.Empty(deserialized); }
public void RoundTrip_EmptyList() { List <int> input = new List <int>(0); var serialized = BinaryConvert.Serialize(input); var deserialized = BinaryConvert.Deserialize <List <int> >(serialized); Assert.NotNull(deserialized); Assert.Empty(deserialized); }
public void RoundTrip_ListToIList() { List <int> input = new List <int> { 1, 2, 3 }; var serialized = BinaryConvert.Serialize(input); var deserialized = BinaryConvert.Deserialize <IList <int> >(serialized); Assert.Equal(input, deserialized); }
public void RoundTrip_StringComparisonMetadata() { var input = new StringComparisonMetadata { StringComparisonId = (int)StringComparison.OrdinalIgnoreCase, }; var serialized = BinaryConvert.Serialize(input); var deserialized = BinaryConvert.Deserialize <StringComparisonMetadata>(serialized); ObjectTreeAssert.AreEqual(input, deserialized); }
public void Serialize_StringComparisonMetadata() { var input = new StringComparisonMetadata { StringComparisonId = (int)StringComparison.OrdinalIgnoreCase, }; var serialized = BinaryConvert.Serialize(input); var actual = Convert.ToBase64String(serialized); const string expected = @"HQAAABBTdHJpbmdDb21wYXJpc29uSWQABQAAAAA="; Assert.Equal(expected, actual); }
public void RoundTrip_CustomDictionary() { IDictionary <string, int> input = new CustomDictionary <string, int> { ["one"] = 1, ["two"] = 2, }; var serialized = BinaryConvert.Serialize(input); var deserialized = BinaryConvert.Deserialize <CustomDictionary <string, int> >(serialized); Assert.Equal(input.OrderBy(x => x.Key), deserialized.OrderBy(x => x.Key)); }
public void RoundTrip_IdNamePair() { var input = new IdNamePair { Id = 2, Name = "hello", }; var serialized = BinaryConvert.Serialize(input); var deserialized = BinaryConvert.Deserialize <IdNamePair>(serialized); ObjectTreeAssert.AreEqual(input, deserialized); }
public void RoundTrip_DictionaryToIDictionary() { Dictionary <string, int> input = new Dictionary <string, int> { ["one"] = 1, ["two"] = 2, }; var serialized = BinaryConvert.Serialize(input); var deserialized = BinaryConvert.Deserialize <IDictionary>(serialized); Assert.Equal(input.Keys.OrderBy(x => x), deserialized.Keys.Cast <string>().OrderBy(x => x)); }
public void RoundTrip_DictionaryOfStrings() { Dictionary <string, string> input = new Dictionary <string, string> { ["one"] = "1", ["two"] = "2", }; var serialized = BinaryConvert.Serialize(input); var deserialized = BinaryConvert.Deserialize <Dictionary <string, string> >(serialized); Assert.Equal(input.OrderBy(x => x.Key), deserialized.OrderBy(x => x.Key)); }
public void RoundTrip_NoIdGetterIdNamePair_ToIdNamePair() { var input = new NoIdGetterIdNamePair { Id = 2, Name = "hello", }; var serialized = BinaryConvert.Serialize(input); var deserialized = BinaryConvert.Deserialize <IdNamePair>(serialized); var expected = new IdNamePair { Name = "hello" }; ObjectTreeAssert.AreEqual(expected, deserialized); }
public void RoundTrip_CustomJsonAttributeClass() { var input = new CustomJsonAttributeClass { Id = 1, AlternateId = 2, Ignored = 3, Name = "4", }; var serialized = BinaryConvert.Serialize(input); var deserialized = BinaryConvert.Deserialize <CustomJsonAttributeClass>(serialized); ObjectTreeAssert.AreEqual(input, deserialized); }
public void RoundTrip_CustomDataContractClass() { var input = new CustomDataContractClass { Id = 1, AlternateId = 2, NotMember = 3, Ignored = 4, Name = "5", }; var serialized = BinaryConvert.Serialize(input); var deserialized = BinaryConvert.Deserialize <CustomDataContractClass>(serialized); ObjectTreeAssert.AreEqual(input, deserialized); }
/// <summary> /// Gets a <typeparamref name="TItem"/> value from the specified cache with the specified key. /// </summary> /// <typeparam name="TItem">The type of value to get.</typeparam> /// <param name="cache">The cache in which to store the data.</param> /// <param name="key">The key to get the stored data for.</param> /// <returns>The <typeparamref name="TItem"/> value from the stored cache key.</returns> public static TItem Get <TItem>(this IDistributedCache cache, string key) { var valueBytes = cache.Get(key); if (valueBytes == null) { return(default(TItem)); } try { return(BinaryConvert.Deserialize <TItem>(valueBytes)); } catch (Exception) { return(default(TItem)); } }
/// <summary> /// Asynchronously gets a <typeparamref name="TItem"/> value from the specified cache with the specified key. /// </summary> /// <typeparam name="TItem">The type of value to get.</typeparam> /// <param name="cache">The cache in which to store the data.</param> /// <param name="key">The key to get the stored data for.</param> /// <param name="token">Optional. A <see cref="CancellationToken" /> to cancel the operation.</param> /// <returns>A task that gets the <typeparamref name="TItem"/> value from the stored cache key.</returns> public static async Task <TItem> GetAsync <TItem>(this IDistributedCache cache, string key, CancellationToken token = default(CancellationToken)) { var valueBytes = await cache.GetAsync(key, token); if (valueBytes == null) { return(default(TItem)); } try { return(BinaryConvert.Deserialize <TItem>(valueBytes)); } catch (Exception) { return(default(TItem)); } }
public void RoundTrip_ListOfObjects() { TimeParty[] input = new TimeParty[] { new TimeParty { UnixDateTimeSeconds = 2, DateTimeUtc = new DateTime(2002, 2, 2, 12, 1, 2, 123, DateTimeKind.Utc), DateTimeLocal = new DateTime(2002, 2, 2, 12, 1, 2, 123, DateTimeKind.Local), DateTimeUnspecified = new DateTime(2002, 2, 2, 12, 1, 2, 123, DateTimeKind.Unspecified), DateTimeOffsetUtc = new DateTimeOffset(2002, 2, 2, 12, 1, 2, TimeSpan.FromHours(0)), DateTimeOffsetLocal = new DateTimeOffset(2002, 2, 2, 12, 1, 2, TimeSpan.FromHours(-5)), }, new TimeParty(), }; var serialized = BinaryConvert.Serialize(input); var deserialized = BinaryConvert.Deserialize <List <TimeParty> >(serialized); ObjectTreeAssert.AreEqual(input, deserialized); }
/// <summary> /// Gets a <typeparamref name="TItem"/> value from the specified cache with the specified key if present. /// </summary> /// <remarks> /// This method is primarily intended for non-nullable value types. /// For nullable types, it's probably easier to use the normal <see cref="Get"/> method and check the result for null. /// </remarks> /// <typeparam name="TItem">The type of value to get.</typeparam> /// <param name="cache">The cache in which to store the data.</param> /// <param name="key">The key to get the stored data for.</param> /// <param name="value">The <typeparamref name="TItem"/> value from the stored cache key.</param> /// <returns>True if the key was found.</returns> public static bool TryGet <TItem>(this IDistributedCache cache, string key, out TItem value) { var valueBytes = cache.Get(key); if (valueBytes == null) { value = default(TItem); return(false); } try { value = BinaryConvert.Deserialize <TItem>(valueBytes); return(true); } catch (Exception) { value = default(TItem); return(false); } }
public void RoundTrip_DictionaryOfObjects() { Dictionary <string, TimeParty> input = new Dictionary <string, TimeParty> { ["one"] = new TimeParty(), ["two"] = new TimeParty { UnixDateTimeSeconds = 2, DateTimeUtc = new DateTime(2002, 2, 2, 12, 1, 2, 123, DateTimeKind.Utc), DateTimeLocal = new DateTime(2002, 2, 2, 12, 1, 2, 123, DateTimeKind.Local), DateTimeUnspecified = new DateTime(2002, 2, 2, 12, 1, 2, 123, DateTimeKind.Unspecified), DateTimeOffsetUtc = new DateTimeOffset(2002, 2, 2, 12, 1, 2, TimeSpan.FromHours(0)), DateTimeOffsetLocal = new DateTimeOffset(2002, 2, 2, 12, 1, 2, TimeSpan.FromHours(-5)), }, ["three"] = null }; var serialized = BinaryConvert.Serialize(input); var deserialized = BinaryConvert.Deserialize <Dictionary <string, TimeParty> >(serialized); Assert.Equal(input.Keys.OrderBy(x => x), deserialized.Keys.OrderBy(x => x)); ObjectTreeAssert.AreEqual(input["one"], deserialized["one"]); ObjectTreeAssert.AreEqual(input["two"], deserialized["two"]); Assert.Null(deserialized["three"]); }
public void RoundTrip_Task() { Task <int> input = Task.FromResult(2); Assert.Throws <FormatException>(() => BinaryConvert.Serialize(input)); }
public void Serialize_Null() { Assert.Throws <ArgumentNullException>(() => BinaryConvert.Serialize(null)); }
public void Serialize_Type() { var input = typeof(string); Assert.Throws <FormatException>(() => BinaryConvert.Serialize(input)); }