mbraceproject / FsPickler

A fast multi-format message serializer for .NET
http://mbraceproject.github.io/FsPickler/
MIT License
324 stars 52 forks source link

Example doesn't work - Code shown below #64

Closed SoulFireMage closed 8 years ago

SoulFireMage commented 8 years ago

Hi

I've been trying to use this library and had a general issue with the stream being unreadable. So I've had a go at using the simplest case example from this tutorial instead.

The code below is what I've tried (I've omitted the mandatory open statements).

let binearySerializer = FsPickler.CreateBinarySerializer()
let xmlSerializer = FsPickler.CreateXmlSerializer(indent = true)

let stream = Unchecked.defaultof<System.IO.Stream>

xmlSerializer.Serialize(stream, [0. .. 0.1 .. 1.])
xmlSerializer.Deserialize<float list>(stream)

The error I get is the following:

System.ArgumentNullException: Value cannot be null. Parameter name: stream

at System.IO.StreamWriter..ctor(Stream stream, Encoding encoding, Int32 bufferSize, Boolean leaveOpen) at Nessos.FsPickler.XmlPickleFormatProvider.Nessos-FsPickler-IPickleFormatProvider-CreateWriter(Stream stream, Encoding encoding, Boolean _arg1, Boolean leaveOpen) in C:\Users\eirik\Development\nessos\FsPickler\src\FsPickler\Format\XmlFormat.fs:line 382 at Nessos.FsPickler.FsPicklerSerializer.Serialize[T](Stream stream, T value, FSharpOption1 pickler, FSharpOption1 streamingContext, FSharpOption1 encoding, FSharpOption1 leaveOpen) in C:\Users\eirik\Development\nessos\FsPickler\src\FsPickler\FsPickler\Serializer.fs:line 49 at <StartupCode$FSI_0057>.$FSI_0057.main@() in c:\users\richard\documents\visual studio 14\Projects\ConsoleApplication12\ConsoleApplication12\Program.fs:line 31 Stopped due to error

I'm using Visual Studio 2015 Community Edition and F#4

eiriktsarpalis commented 8 years ago

Hi,

you need to pass it an actual stream parameter, like MemoryStream or FileStream. Unchecked.defaultof is a placeholder F# primitive that in this case returns null, hence the error. If you just want to get serialized bytes, try using the Pickle and UnPickle methods.