3 people like it.
Like the snippet!
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
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 CopyTo : destination:Stream * bufferSize:int -> unit
member CopyToAsync : destination:Stream * bufferSize:int * cancellationToken:CancellationToken -> Task
member Flush : unit -> unit
member FlushAsync : cancellationToken:CancellationToken -> Task
member GetBuffer : unit -> byte[]
...
--------------------
MemoryStream() : MemoryStream
MemoryStream(capacity: int) : MemoryStream
MemoryStream(buffer: byte []) : MemoryStream
MemoryStream(buffer: byte [], writable: bool) : MemoryStream
MemoryStream(buffer: byte [], index: int, count: int) : MemoryStream
MemoryStream(buffer: byte [], index: int, count: int, writable: bool) : MemoryStream
MemoryStream(buffer: byte [], index: int, count: int, writable: bool, publiclyVisible: bool) : MemoryStream
Multiple items
type DataContractJsonSerializer =
inherit XmlObjectSerializer
new : type:Type -> DataContractJsonSerializer + 6 overloads
member DateTimeFormat : DateTimeFormat
member EmitTypeInformation : EmitTypeInformation
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 SerializeReadOnlyTypes : bool
member UseSimpleDictionaryFormat : bool
...
--------------------
DataContractJsonSerializer(type: System.Type) : DataContractJsonSerializer
DataContractJsonSerializer(type: System.Type, rootName: string) : DataContractJsonSerializer
DataContractJsonSerializer(type: System.Type, rootName: XmlDictionaryString) : DataContractJsonSerializer
DataContractJsonSerializer(type: System.Type, knownTypes: System.Collections.Generic.IEnumerable<System.Type>) : DataContractJsonSerializer
DataContractJsonSerializer(type: System.Type, settings: DataContractJsonSerializerSettings) : DataContractJsonSerializer
DataContractJsonSerializer(type: System.Type, rootName: string, knownTypes: System.Collections.Generic.IEnumerable<System.Type>) : DataContractJsonSerializer
DataContractJsonSerializer(type: System.Type, rootName: XmlDictionaryString, knownTypes: System.Collections.Generic.IEnumerable<System.Type>) : DataContractJsonSerializer
val typeof<'T> : System.Type
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 + 5 overloads
member GetBytes : chars:char[] -> byte[] + 7 overloads
member GetCharCount : bytes:byte[] -> int + 3 overloads
...
property Encoding.Default: Encoding with get
val internal unjson : jsonString:string -> 't
Object from Json
val jsonString : string
Multiple items
val string : value:'T -> string
--------------------
type string = System.String
Multiple items
type ASCIIEncoding =
inherit Encoding
new : unit -> ASCIIEncoding
member GetByteCount : chars:string -> int + 3 overloads
member GetBytes : chars:ReadOnlySpan<char> * bytes:Span<byte> -> int + 3 overloads
member GetCharCount : bytes:ReadOnlySpan<byte> -> int + 2 overloads
member GetChars : bytes:ReadOnlySpan<byte> * chars:Span<char> -> int + 2 overloads
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
...
--------------------
ASCIIEncoding() : ASCIIEncoding
Multiple items
val obj : obj
--------------------
type obj = System.Object
More information