The DataContractJsonSerializer creates JSON for F# record types that includes the '@' character after each property name. Does anyone know if it is possible to get JSON that does not have this trailing at symbol?
{"heart_rate@":20,"latitude@":45.0,"longitude@":108.0,"name@":"Rambo"}
Here is the script I use to output this sample
#r "System.Xml"
#r "System.Runtime.Serialization"
open System.Text
open System.Runtime.Serialization.Json
open System.IO
type Update = {
name: string;
latitude: decimal;
longitude: decimal;
heart_rate: int}
let update = {name = "Rambo"; latitude = 45.0m; longitude = 108.0m; heart_rate = 20}
let serializer = new DataContractJsonSerializer( typeof<Update> )
let stream = new MemoryStream()
let data = serializer.WriteObject(stream, update)
let updateData = stream.ToArray()
let json = (Encoding.UTF8.GetString(updateData))
printfn "%s" json
Serialization is the process of converting an object into a stream of bytes to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed. The reverse process is called deserialization.
To serialize an object means to convert its state to a byte stream so that the byte stream can be reverted back into a copy of the object. A Java object is serializable if its class or any of its superclasses implements either the java. io. Serializable interface or its subinterface, java.
JSON is a format that encodes objects in a string. Serialization means to convert an object into that string, and deserialization is its inverse operation (convert string -> object).
Serialize and Deserialize Binary Tree. Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.
It's using the name of the compiler generated backing fields. You can use DataMemberAttribute
to provide your own names.
[<DataContract>]
type Update = {
[<field: DataMember(Name="name")>]
name: string;
[<field: DataMember(Name="latitude")>]
latitude: decimal;
[<field: DataMember(Name="longitude")>]
longitude: decimal;
[<field: DataMember(Name="heart_rate")>]
heart_rate: int}
Although Daniel's solution will work correctly, it is rather tedious to have to add attributes to every property in the record. It turns out that Json.NET produces more readable JSON out of the box. For my application I do not need to use the DataContractSerializer specifically, so JSON.net it is!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With