Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing pickling directory info #106

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/FsPickler/PicklerGeneration/PicklerGenerator.fs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ type PicklerGenerator =
let (|PicklerFactory|_|) (shape : TypeShape) = registry.TryGetPicklerFactory shape.Type

match shape with
| _ when isUnsupportedType shape.Type -> raise <| NonSerializableTypeException shape.Type
| Shape.Bool -> new BooleanPickler() :> _
| Shape.Byte -> new BytePickler() :> _
| Shape.SByte -> new SBytePickler() :> _
Expand Down Expand Up @@ -65,12 +64,17 @@ type PicklerGenerator =
| :? TypeShape<Assembly> -> ReflectionPicklers.CreateAssemblyPickler resolver :> _
| :? TypeShape<MemberInfo> -> ReflectionPicklers.CreateMemberInfoPickler ArrayPickler.Create resolver :> _
| :? TypeShape<System.DBNull> -> new DBNullPickler() :> _
| PicklerFactory factory ->
| PicklerFactory factory ->
let pickler = factory resolver
if pickler.Type <> shape.Type then
raise <| PicklerGenerationException(shape.Type, "unexpected pickler type from custom pickler generator.")
pickler

// The unsupported check is now after the pickler factory case due to the fact that the DirectoryInfo
// type has 'IsMarshalByRef' set if .NET framework is used. This allows someone to specify
// a custom pickler that will serialize a DirectoryInfo.
| _ when isUnsupportedType shape.Type -> raise <| NonSerializableTypeException shape.Type

| Shape.Nullable s ->
s.Accept {
new INullableVisitor<Pickler> with
Expand Down Expand Up @@ -313,4 +317,4 @@ type PicklerGenerator =
shape.Accept {
new ITypeShapeVisitor<Pickler> with
member __.Visit<'T> () = pickler.Cast<'T>() :> Pickler
}
}
25 changes: 24 additions & 1 deletion tests/FsPickler.Tests/SerializerTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,29 @@ type SerializationTests (fixture : ISerializerFixture) =
[<Test; Category("Bytes")>]
member __.``Primitive: byte []`` () = testEquals (null : byte []) ; Check.QuickThrowOnFail<byte []> testEquals

[<Test; Category("DirectoryInfo")>]
member __.``DirectoryInfo`` () =
let makeDirectoryInfoPickler (resolver : IPicklerResolver) =
let stringPickler = resolver.Resolve<string> ()

let writer (w : WriteState) (di : DirectoryInfo) =
stringPickler.Write w "Directory" di.FullName

let reader (r : ReadState) =
stringPickler.Read r "Directory" |> DirectoryInfo

Pickler.FromPrimitives (reader, writer)

let di = DirectoryInfo "testDirectory"
let registry = new CustomPicklerRegistry ()
registry.RegisterFactory makeDirectoryInfoPickler
let cache = PicklerCache.FromCustomPicklerRegistry registry
let ser = FsPickler.CreateXmlSerializer (picklerResolver = cache)
di
|> ser.PickleToString
|> ser.UnPickleOfString<DirectoryInfo>
|> should equal di

//
// Reflection types
//
Expand Down Expand Up @@ -1157,4 +1180,4 @@ type SerializationTests (fixture : ISerializerFixture) =
let msg = sprintf "Too many random object serialization failures (%d out of %d)." failedResults results.Length
raise <| new AssertionException(msg)
else
printfn "Failed Serializations: %d out of %d." failedResults results.Length
printfn "Failed Serializations: %d out of %d." failedResults results.Length