public void OpenGenericClass_ShouldNotCreateDefaultSerializer() { var metadataExplorer = MetadataExplorer.CreateFor(typeof(MyOpenGenericType <>)); Action action = () => metadataExplorer.Serializers.ResolveSerializer(typeof(MyOpenGenericType <>)); action.ShouldThrow <ShapeshifterException>().Where(i => i.Id == Exceptions.SerializerResolutionFailedId); }
public void AbstractClass_ShouldNotCreateCustomSerializer() { var metadataExplorer = MetadataExplorer.CreateFor(typeof(MyAbstractTypeWithCustomSerializer)); Action action = () => metadataExplorer.Serializers.ResolveSerializer(typeof(MyAbstractTypeWithCustomSerializer)); action.ShouldThrow <ShapeshifterException>().Where(i => i.Id == Exceptions.SerializerResolutionFailedId); }
private static T Deserialize <T>(string input) { var typeContext = MetadataExplorer.CreateFor(typeof(T)).Deserializers; var engine = new InternalPackformatReader(input, typeContext); return(engine.Unpack <T>()); }
public void CustomDeserializerWithTypeAndNoVersion_UsesTheCurentVersionOfTheType() { var explorer = MetadataExplorer.CreateFor(typeof(MyTypeWithCustomDeserializerWithTypeAndNoVersion)); var deserializer = explorer.Deserializers.First(); deserializer.Version.Should().Be(3948729994); }
/// <summary> /// Returns a <see cref="ShapeshifterSerializer{T}"/> for the specified type. /// </summary> /// <typeparam name="T">Type to be serialized/deserialized</typeparam> /// <returns>A serializer for the given type.</returns> public ShapeshifterSerializer <T> GetSerializer <T>() { return((ShapeshifterSerializer <T>)_cache.GetOrAdd(typeof(T), type => { var metadata = MetadataExplorer.CreateFor(_assembliesToParse, ShapeshifterSerializer.BuiltInKnownTypes); return new ShapeshifterSerializer <T>(metadata); })); }
public static SnapshotDetector CreateFor(IEnumerable <Assembly> assembliesInScope) { var metadataExplorer = MetadataExplorer.CreateFor(assembliesInScope); var serializers = metadataExplorer.Serializers.Select(ToSerializerInfo).ToList(); var deserializers = metadataExplorer.Deserializers.Select(ToDeserializerInfo).ToList(); return(new SnapshotDetector(serializers, deserializers)); }
private static string Serialize <T>(T toPack, Type declaredType = null) { var typeContext = MetadataExplorer.CreateFor(typeof(T)).Serializers; var sb = new StringBuilder(); var engine = new InternalPackformatWriter(new StringWriter(sb), typeContext); engine.Pack(toPack, declaredType); return(sb.ToString()); }
public static SnapshotDetector CreateFor(IEnumerable <Type> rootTypes, IEnumerable <Type> knownTypes, IEnumerable <Assembly> descendantSearchScope = null) { var metadataExplorer = MetadataExplorer.CreateFor( rootTypes ?? Enumerable.Empty <Type>(), knownTypes ?? Enumerable.Empty <Type>(), descendantSearchScope); var serializers = metadataExplorer.Serializers.Select(ToSerializerInfo).ToList(); var deserializers = metadataExplorer.Deserializers.Select(ToDeserializerInfo).ToList(); return(new SnapshotDetector(serializers, deserializers)); }
public void BaseClassWithoutDataContractMultiLevelHierarchy_ShouldThrow() { Action action = () => MetadataExplorer.CreateFor(typeof(MostDerivedClass)); action.ShouldThrow <ShapeshifterException>().Where(i => i.Id == Exceptions.DataContractAttributeMissingFromHierarchyId); }
public void OpenGenericClass_ShouldNotCreateDefaultDeserializer() { var metadataExplorer = MetadataExplorer.CreateFor(typeof(MyOpenGenericType <>)); metadataExplorer.Deserializers.ResolveDeserializer(new DeserializerKey("MyOpenGenericType<T>", 1)).Should().BeNull(); }
public void CustomSerializerForChild_WithNoVersion_ShouldCreateSerializerWithHash() { var metadataExplorer = MetadataExplorer.CreateFor(typeof(MyTypeWithSerializerWithNoVersion), new[] { Assembly.GetExecutingAssembly() }); metadataExplorer.Serializers.Should().ContainSingle(i => i.Type == typeof(MyChildWithSerializerWithNoVersion) && i.Version != 0); }
public void AbstractClass_ShouldNotCreateDefaultDeserializer() { var metadataExplorer = MetadataExplorer.CreateFor(typeof(MyAbstractType)); metadataExplorer.Deserializers.ResolveDeserializer(new DeserializerKey("MyAbstractType", 1)).Should().BeNull(); }
public void CustomSerializer_WithExplicitVersion_ShouldCreateSerializerWithExplicitVersion() { var metadataExplorer = MetadataExplorer.CreateFor(typeof(MyTypeWithSerializerWithExplicitVersion)); metadataExplorer.Serializers.Should().ContainSingle(i => i.Type == typeof(MyTypeWithSerializerWithExplicitVersion) && i.Version == 1); }
public void ShouldDetectItemTypeOfListMembersWithDataMemberAttribute() { var result = MetadataExplorer.CreateFor(typeof(ClassWithList)).Serializers; result.ResolveSerializer(typeof(InnerClass)).Should().NotBeNull(); }
public void ShouldDetectKnownTypesAddedByMethod() { var result = MetadataExplorer.CreateFor(typeof(ClassWithKnownTypesMethod)).Serializers; result.ResolveSerializer(typeof(InnerClass)).Should().NotBeNull(); }
public void SameClassNameInDifferentNamespace_ShouldThrow() { Action action = () => MetadataExplorer.CreateFor(typeof(OuterClass)); action.ShouldThrow <ShapeshifterException>().Where(i => i.Id == Exceptions.PackformatNameCollisionyId); }
public void ShouldDetectNonStaticDeserializerAndFailIfNoPublicDefaultConstructor() { Action action = () => MetadataExplorer.CreateFor(typeof(NonStaticDeserializerClassNoConstructor)); action.ShouldThrow <ShapeshifterException>().Where(i => i.Id == Exceptions.TypeHasNoPublicDefaultConstructorId); }
public void StructWithDataContract_HierarchyCheck_ShouldNotThrow() { Action action = () => MetadataExplorer.CreateFor(typeof(TestStruct)); action.ShouldNotThrow(); }
public void ShouldDetectNonStaticDeserializer() { var result = MetadataExplorer.CreateFor(typeof(NonStaticDeserializerClass)).Deserializers; result.Count().Should().Be(1); }
public void CustomDeserializer_WithExplicitVersion_ShouldCreateDeserializerWithExplicitVersion() { var metadataExplorer = MetadataExplorer.CreateFor(typeof(MyTypeWithSerializerWithExplicitVersion)); metadataExplorer.Deserializers.Should().ContainSingle(i => i.PackformatName == "MyTypeWithSerializerWithExplicitVersion" && i.Version == 1); }
public void CustomDeserializerWithTypeAndNoVersion_ShouldNotThrow() { Action action = () => MetadataExplorer.CreateFor(typeof(MyTypeWithCustomDeserializerWithTypeAndNoVersion)); action.ShouldNotThrow(); }
public void CustomDeserializerForChild_WithExplicitVersion_ShouldCreateDeserializerWithExplicitVersion() { var metadataExplorer = MetadataExplorer.CreateFor(typeof(MyTypeWithSerializerWithExplicitVersion), new[] { Assembly.GetExecutingAssembly() }); metadataExplorer.Deserializers.Should().ContainSingle(i => i.PackformatName == "MyChildWithSerializerWithExplicitVersion" && i.Version == 1); }