Convert an object to json, and json to object

There is a namespace System.Runtime.Serialization.Json To serialize generic object you can do like this...

Copy Source
Copy Link
Tools:
 1: #r "System.Runtime.Serialization" // for interactive
 2: // Reference to assembly System.Runtime.Serialization and System.Xml
 3: 
 4: open System.IO
 5: open System.Runtime.Serialization.Json
 6: open System.Xml
 7: open System.Text
 8: 
 9: /// Object to Json 
10: let internal json<'t> (myObj:'t) =   
11:         use ms = new MemoryStream() 
12:         (new DataContractJsonSerializer(typeof<'t>)).WriteObject(ms, myObj) 
13:         Encoding.Default.GetString(ms.ToArray()) 
14: 
15: 
16: /// Object from Json 
17: let internal unjson<'t> (jsonString:string)  : 't =  
18:         use ms = new MemoryStream(ASCIIEncoding.Default.GetBytes(jsonString)) 
19:         let obj = (new DataContractJsonSerializer(typeof<'t>)).ReadObject(ms) 
20:         obj :?> 't
21: 
22: 
namespace System
namespace System.IO
namespace System.Runtime
namespace System.Runtime.Serialization
namespace System.Xml
namespace System.Text
val internal json : 't -> string

Full name: Test.json

Object to Json
val myObj : 't
val ms : MemoryStream

  type: MemoryStream
  implements: System.IDisposable
  inherits: Stream
  inherits: System.MarshalByRefObject
type MemoryStream =
  class
    inherit System.IO.Stream
    new : unit -> System.IO.MemoryStream
    new : int -> System.IO.MemoryStream
    new : System.Byte [] -> System.IO.MemoryStream
    new : System.Byte [] * bool -> System.IO.MemoryStream
    new : System.Byte [] * int * int -> System.IO.MemoryStream
    new : System.Byte [] * int * int * bool -> System.IO.MemoryStream
    new : System.Byte [] * int * int * bool * bool -> System.IO.MemoryStream
    member CanRead : bool
    member CanSeek : bool
    member CanWrite : bool
    member Capacity : int with get, set
    member Flush : unit -> unit
    member GetBuffer : unit -> System.Byte []
    member Length : int64
    member Position : int64 with get, set
    member Read : System.Byte [] * int * int -> int
    member ReadByte : unit -> int
    member Seek : int64 * System.IO.SeekOrigin -> int64
    member SetLength : int64 -> unit
    member ToArray : unit -> System.Byte []
    member Write : System.Byte [] * int * int -> unit
    member WriteByte : System.Byte -> unit
    member WriteTo : System.IO.Stream -> unit
  end

Full name: System.IO.MemoryStream

  type: MemoryStream
  implements: System.IDisposable
  inherits: Stream
  inherits: System.MarshalByRefObject
val typeof<'T> : System.Type

Full name: Microsoft.FSharp.Core.Operators.typeof
type Encoding =
  class
    member BodyName : string
    member Clone : unit -> obj
    member CodePage : int
    member DecoderFallback : System.Text.DecoderFallback with get, set
    member EncoderFallback : System.Text.EncoderFallback with get, set
    member EncodingName : string
    member Equals : obj -> bool
    member GetByteCount : char [] -> int
    member GetByteCount : string -> int
    member GetByteCount : char * int -> int
    member GetByteCount : char [] * int * int -> int
    member GetBytes : char [] -> System.Byte []
    member GetBytes : string -> System.Byte []
    member GetBytes : char [] * int * int -> System.Byte []
    member GetBytes : char * int * System.Byte * int -> int
    member GetBytes : char [] * int * int * System.Byte [] * int -> int
    member GetBytes : string * int * int * System.Byte [] * int -> int
    member GetCharCount : System.Byte [] -> int
    member GetCharCount : System.Byte * int -> int
    member GetCharCount : System.Byte [] * int * int -> int
    member GetChars : System.Byte [] -> char []
    member GetChars : System.Byte [] * int * int -> char []
    member GetChars : System.Byte * int * char * int -> int
    member GetChars : System.Byte [] * int * int * char [] * int -> int
    member GetDecoder : unit -> System.Text.Decoder
    member GetEncoder : unit -> System.Text.Encoder
    member GetHashCode : unit -> int
    member GetMaxByteCount : int -> int
    member GetMaxCharCount : int -> int
    member GetPreamble : unit -> System.Byte []
    member GetString : System.Byte [] -> string
    member GetString : System.Byte [] * int * int -> string
    member HeaderName : string
    member IsAlwaysNormalized : unit -> bool
    member IsAlwaysNormalized : System.Text.NormalizationForm -> bool
    member IsBrowserDisplay : bool
    member IsBrowserSave : bool
    member IsMailNewsDisplay : bool
    member IsMailNewsSave : bool
    member IsReadOnly : bool
    member IsSingleByte : bool
    member WebName : string
    member WindowsCodePage : int
    static member ASCII : System.Text.Encoding
    static member BigEndianUnicode : System.Text.Encoding
    static member Convert : System.Text.Encoding * System.Text.Encoding * System.Byte [] -> System.Byte []
    static member Convert : System.Text.Encoding * System.Text.Encoding * System.Byte [] * int * int -> System.Byte []
    static member Default : System.Text.Encoding
    static member GetEncoding : int -> System.Text.Encoding
    static member GetEncoding : string -> System.Text.Encoding
    static member GetEncoding : int * System.Text.EncoderFallback * System.Text.DecoderFallback -> System.Text.Encoding
    static member GetEncoding : string * System.Text.EncoderFallback * System.Text.DecoderFallback -> System.Text.Encoding
    static member GetEncodings : unit -> System.Text.EncodingInfo []
    static member UTF32 : System.Text.Encoding
    static member UTF7 : System.Text.Encoding
    static member UTF8 : System.Text.Encoding
    static member Unicode : System.Text.Encoding
  end

Full name: System.Text.Encoding

  type: Encoding
  implements: System.ICloneable
property Encoding.Default: Encoding
Multiple overloads
Encoding.GetString(bytes: byte []) : string
Encoding.GetString(bytes: byte [], index: int, count: int) : string
MemoryStream.ToArray() : byte []
val internal unjson : string -> 't

Full name: Test.unjson

Object from Json
val jsonString : string

  type: string
  implements: System.IComparable
  implements: System.ICloneable
  implements: System.IConvertible
  implements: System.IComparable<string>
  implements: seq<char>
  implements: System.Collections.IEnumerable
  implements: System.IEquatable<string>
Multiple items
val string : 'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------

type string = System.String

Full name: Microsoft.FSharp.Core.string

  type: string
  implements: System.IComparable
  implements: System.ICloneable
  implements: System.IConvertible
  implements: System.IComparable<string>
  implements: seq<char>
  implements: System.Collections.IEnumerable
  implements: System.IEquatable<string>
type ASCIIEncoding =
  class
    inherit System.Text.Encoding
    new : unit -> System.Text.ASCIIEncoding
    member GetByteCount : string -> int
    member GetByteCount : char * int -> int
    member GetByteCount : char [] * int * int -> int
    member GetBytes : char * int * System.Byte * int -> int
    member GetBytes : string * int * int * System.Byte [] * int -> int
    member GetBytes : char [] * int * int * System.Byte [] * int -> int
    member GetCharCount : System.Byte * int -> int
    member GetCharCount : System.Byte [] * int * int -> int
    member GetChars : System.Byte * int * char * int -> int
    member GetChars : System.Byte [] * int * int * char [] * int -> int
    member GetDecoder : unit -> System.Text.Decoder
    member GetEncoder : unit -> System.Text.Encoder
    member GetMaxByteCount : int -> int
    member GetMaxCharCount : int -> int
    member GetString : System.Byte [] * int * int -> string
    member IsSingleByte : bool
  end

Full name: System.Text.ASCIIEncoding

  type: ASCIIEncoding
  implements: System.ICloneable
  inherits: Encoding
Multiple overloads
Encoding.GetBytes(s: string) : byte []
Encoding.GetBytes(chars: char []) : byte []
Encoding.GetBytes(chars: char [], index: int, count: int) : byte []
Encoding.GetBytes(chars: nativeptr<char>, charCount: int, bytes: nativeptr<byte>, byteCount: int) : int
Encoding.GetBytes(s: string, charIndex: int, charCount: int, bytes: byte [], byteIndex: int) : int
Encoding.GetBytes(chars: char [], charIndex: int, charCount: int, bytes: byte [], byteIndex: int) : int
Multiple items
val obj : 'a

--------------------

type obj = System.Object

Full name: Microsoft.FSharp.Core.obj

More information

Link: http://fssnip.net/1l
Posted: 3 years ago
Author: Tuomas Hietanen (website)
Tags: JSON, Serialization