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: 
[<JsonObject(MemberSerialization.OptIn)>]
type Host [<JsonConstructor>] (address: string, port: int, ipAddress : string) =
    new (address : string) = Host(address, 0, "")
    new (address : string, port : int) = Host(address, port, "")


    [<JsonProperty>]
    member this.Address : string = address
    [<JsonProperty>]
    member this.Port : int = port
    [<JsonProperty>]
    member this.IpAddress : string = ipAddress

    member this.ConnectionString = match this.Port with
                                    | 0 -> this.Address
                                    | _ -> sprintf "%s:%d" this.Address this.Port
    static member val Empty = new Host("")
    override this.ToString() = this.ConnectionString
Multiple items
type Host =
  new : address:string -> Host
  new : address:string * port:int -> Host
  new : address:string * port:int * ipAddress:string -> Host
  override ToString : unit -> string
  member Address : string
  member ConnectionString : string
  member IpAddress : string
  member Port : int
  static member Empty : Host

Full name: Script.Host

--------------------
new : address:string -> Host
new : address:string * port:int -> Host
new : address:string * port:int * ipAddress:string -> Host
val address : 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
val port : int
Multiple items
val int : value:'T -> int (requires member op_Explicit)

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

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
val ipAddress : string
val this : Host
member Host.Address : string

Full name: Script.Host.Address
member Host.Port : int

Full name: Script.Host.Port
member Host.IpAddress : string

Full name: Script.Host.IpAddress
member Host.ConnectionString : string

Full name: Script.Host.ConnectionString
property Host.Port: int
property Host.Address: string
val sprintf : format:Printf.StringFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.sprintf
override Host.ToString : unit -> string

Full name: Script.Host.ToString
property Host.ConnectionString: string

More information

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