3 people like it.

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...

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
#r "System.Runtime.Serialization" // for interactive
// Reference to assembly System.Runtime.Serialization and System.Xml

open System.IO
open System.Runtime.Serialization.Json
open System.Xml
open System.Text

/// Object to Json 
let internal json<'t> (myObj:'t) =   
        use ms = new MemoryStream() 
        (new DataContractJsonSerializer(typeof<'t>)).WriteObject(ms, myObj) 
        Encoding.Default.GetString(ms.ToArray()) 


/// Object from Json 
let internal unjson<'t> (jsonString:string)  : 't =  
        use ms = new MemoryStream(ASCIIEncoding.Default.GetBytes(jsonString)) 
        let obj = (new DataContractJsonSerializer(typeof<'t>)).ReadObject(ms) 
        obj :?> 't
namespace System
namespace System.IO
namespace System.Runtime
namespace System.Runtime.Serialization
namespace System.Runtime.Serialization.Json
namespace System.Xml
namespace System.Text
val internal json : myObj:'t -> string

Full name: Script.json


 Object to Json
val myObj : 't
val ms : MemoryStream
Multiple items
type MemoryStream =
  inherit Stream
  new : unit -> MemoryStream + 6 overloads
  member CanRead : bool
  member CanSeek : bool
  member CanWrite : bool
  member Capacity : int with get, set
  member Flush : unit -> unit
  member GetBuffer : unit -> byte[]
  member Length : int64
  member Position : int64 with get, set
  member Read : buffer:byte[] * offset:int * count:int -> int
  ...

Full name: System.IO.MemoryStream

--------------------
MemoryStream() : unit
MemoryStream(capacity: int) : unit
MemoryStream(buffer: byte []) : unit
MemoryStream(buffer: byte [], writable: bool) : unit
MemoryStream(buffer: byte [], index: int, count: int) : unit
MemoryStream(buffer: byte [], index: int, count: int, writable: bool) : unit
MemoryStream(buffer: byte [], index: int, count: int, writable: bool, publiclyVisible: bool) : unit
Multiple items
type DataContractJsonSerializer =
  inherit XmlObjectSerializer
  new : type:Type -> DataContractJsonSerializer + 8 overloads
  member DataContractSurrogate : IDataContractSurrogate
  member IgnoreExtensionDataObject : bool
  member IsStartObject : reader:XmlReader -> bool + 1 overload
  member KnownTypes : ReadOnlyCollection<Type>
  member MaxItemsInObjectGraph : int
  member ReadObject : stream:Stream -> obj + 4 overloads
  member WriteEndObject : writer:XmlWriter -> unit + 1 overload
  member WriteObject : stream:Stream * graph:obj -> unit + 2 overloads
  member WriteObjectContent : writer:XmlWriter * graph:obj -> unit + 1 overload
  ...

Full name: System.Runtime.Serialization.Json.DataContractJsonSerializer

--------------------
DataContractJsonSerializer(type: System.Type) : unit
DataContractJsonSerializer(type: System.Type, rootName: string) : unit
DataContractJsonSerializer(type: System.Type, rootName: XmlDictionaryString) : unit
DataContractJsonSerializer(type: System.Type, knownTypes: System.Collections.Generic.IEnumerable<System.Type>) : unit
DataContractJsonSerializer(type: System.Type, rootName: string, knownTypes: System.Collections.Generic.IEnumerable<System.Type>) : unit
DataContractJsonSerializer(type: System.Type, rootName: XmlDictionaryString, knownTypes: System.Collections.Generic.IEnumerable<System.Type>) : unit
DataContractJsonSerializer(type: System.Type, knownTypes: System.Collections.Generic.IEnumerable<System.Type>, maxItemsInObjectGraph: int, ignoreExtensionDataObject: bool, dataContractSurrogate: System.Runtime.Serialization.IDataContractSurrogate, alwaysEmitTypeInformation: bool) : unit
DataContractJsonSerializer(type: System.Type, rootName: string, knownTypes: System.Collections.Generic.IEnumerable<System.Type>, maxItemsInObjectGraph: int, ignoreExtensionDataObject: bool, dataContractSurrogate: System.Runtime.Serialization.IDataContractSurrogate, alwaysEmitTypeInformation: bool) : unit
DataContractJsonSerializer(type: System.Type, rootName: XmlDictionaryString, knownTypes: System.Collections.Generic.IEnumerable<System.Type>, maxItemsInObjectGraph: int, ignoreExtensionDataObject: bool, dataContractSurrogate: System.Runtime.Serialization.IDataContractSurrogate, alwaysEmitTypeInformation: bool) : unit
val typeof<'T> : System.Type

Full name: Microsoft.FSharp.Core.Operators.typeof
type Encoding =
  member BodyName : string
  member Clone : unit -> obj
  member CodePage : int
  member DecoderFallback : DecoderFallback with get, set
  member EncoderFallback : EncoderFallback with get, set
  member EncodingName : string
  member Equals : value:obj -> bool
  member GetByteCount : chars:char[] -> int + 3 overloads
  member GetBytes : chars:char[] -> byte[] + 5 overloads
  member GetCharCount : bytes:byte[] -> int + 2 overloads
  ...

Full name: System.Text.Encoding
property Encoding.Default: Encoding
Encoding.GetString(bytes: byte []) : string
Encoding.GetString(bytes: byte [], index: int, count: int) : string
MemoryStream.ToArray() : byte []
val internal unjson : jsonString:string -> 't

Full name: Script.unjson


 Object from Json
val jsonString : string
Multiple items
val string : value:'T -> string

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

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
Multiple items
type ASCIIEncoding =
  inherit Encoding
  new : unit -> ASCIIEncoding
  member GetByteCount : chars:string -> int + 2 overloads
  member GetBytes : chars:char * charCount:int * bytes:byte * byteCount:int -> int + 2 overloads
  member GetCharCount : bytes:byte * count:int -> int + 1 overload
  member GetChars : bytes:byte * byteCount:int * chars:char * charCount:int -> int + 1 overload
  member GetDecoder : unit -> Decoder
  member GetEncoder : unit -> Encoder
  member GetMaxByteCount : charCount:int -> int
  member GetMaxCharCount : byteCount:int -> int
  member GetString : bytes:byte[] * byteIndex:int * byteCount:int -> string
  ...

Full name: System.Text.ASCIIEncoding

--------------------
ASCIIEncoding() : unit
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 : obj

--------------------
type obj = System.Object

Full name: Microsoft.FSharp.Core.obj

More information

Link:http://fssnip.net/1l
Posted:3 years ago
Author:Tuomas Hietanen
Tags: json , serialization