private static T Deserialize <T>(string input)
        {
            var typeContext = MetadataExplorer.CreateFor(typeof(T)).Deserializers;
            var engine      = new InternalPackformatReader(input, typeContext);

            return(engine.Unpack <T>());
        }
Example #2
0
        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);
        }
Example #3
0
        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);
        }
Example #4
0
        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);
     }));
 }
Example #6
0
        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());
        }
Example #8
0
        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));
        }
Example #9
0
        public void OpenGenericClass_ShouldNotCreateDefaultDeserializer()
        {
            var metadataExplorer = MetadataExplorer.CreateFor(typeof(MyOpenGenericType <>));

            metadataExplorer.Deserializers.ResolveDeserializer(new DeserializerKey("MyOpenGenericType<T>", 1)).Should().BeNull();
        }
Example #10
0
        public void AbstractClass_ShouldNotCreateDefaultDeserializer()
        {
            var metadataExplorer = MetadataExplorer.CreateFor(typeof(MyAbstractType));

            metadataExplorer.Deserializers.ResolveDeserializer(new DeserializerKey("MyAbstractType", 1)).Should().BeNull();
        }
Example #11
0
        public void ShouldDetectKnownTypesAddedByMethod()
        {
            var result = MetadataExplorer.CreateFor(typeof(ClassWithKnownTypesMethod)).Serializers;

            result.ResolveSerializer(typeof(InnerClass)).Should().NotBeNull();
        }
Example #12
0
        public void ShouldDetectItemTypeOfListMembersWithDataMemberAttribute()
        {
            var result = MetadataExplorer.CreateFor(typeof(ClassWithList)).Serializers;

            result.ResolveSerializer(typeof(InnerClass)).Should().NotBeNull();
        }
Example #13
0
        public void ShouldDetectNonStaticDeserializerAndFailIfNoPublicDefaultConstructor()
        {
            Action action = () => MetadataExplorer.CreateFor(typeof(NonStaticDeserializerClassNoConstructor));

            action.ShouldThrow <ShapeshifterException>().Where(i => i.Id == Exceptions.TypeHasNoPublicDefaultConstructorId);
        }
Example #14
0
        public void CustomSerializer_WithExplicitVersion_ShouldCreateSerializerWithExplicitVersion()
        {
            var metadataExplorer = MetadataExplorer.CreateFor(typeof(MyTypeWithSerializerWithExplicitVersion));

            metadataExplorer.Serializers.Should().ContainSingle(i => i.Type == typeof(MyTypeWithSerializerWithExplicitVersion) && i.Version == 1);
        }
Example #15
0
        public void StructWithDataContract_HierarchyCheck_ShouldNotThrow()
        {
            Action action = () => MetadataExplorer.CreateFor(typeof(TestStruct));

            action.ShouldNotThrow();
        }
Example #16
0
        public void BaseClassWithoutDataContractMultiLevelHierarchy_ShouldThrow()
        {
            Action action = () => MetadataExplorer.CreateFor(typeof(MostDerivedClass));

            action.ShouldThrow <ShapeshifterException>().Where(i => i.Id == Exceptions.DataContractAttributeMissingFromHierarchyId);
        }
 internal ShapeshifterSerializer(MetadataExplorer metadata) : base(typeof(T), metadata)
 {
 }
Example #18
0
        public void CustomDeserializerWithTypeAndNoVersion_ShouldNotThrow()
        {
            Action action = () => MetadataExplorer.CreateFor(typeof(MyTypeWithCustomDeserializerWithTypeAndNoVersion));

            action.ShouldNotThrow();
        }
Example #19
0
        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);
        }
Example #20
0
        public void CustomDeserializer_WithExplicitVersion_ShouldCreateDeserializerWithExplicitVersion()
        {
            var metadataExplorer = MetadataExplorer.CreateFor(typeof(MyTypeWithSerializerWithExplicitVersion));

            metadataExplorer.Deserializers.Should().ContainSingle(i => i.PackformatName == "MyTypeWithSerializerWithExplicitVersion" && i.Version == 1);
        }
Example #21
0
        public void ShouldDetectNonStaticDeserializer()
        {
            var result = MetadataExplorer.CreateFor(typeof(NonStaticDeserializerClass)).Deserializers;

            result.Count().Should().Be(1);
        }
Example #22
0
        public void SameClassNameInDifferentNamespace_ShouldThrow()
        {
            Action action = () => MetadataExplorer.CreateFor(typeof(OuterClass));

            action.ShouldThrow <ShapeshifterException>().Where(i => i.Id == Exceptions.PackformatNameCollisionyId);
        }
Example #23
0
        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);
        }