Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serializing F# Record type to JSON includes '@' character after each property

Tags:

json

f#

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
like image 845
John Atwood Avatar asked Oct 23 '12 19:10

John Atwood


People also ask

What does serializing mean in coding?

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.

What does serializing mean in Java?

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.

What is serializing in JSON?

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

What is serializing a binary tree?

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.


2 Answers

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}
like image 154
Daniel Avatar answered Oct 16 '22 00:10

Daniel


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!

like image 31
John Atwood Avatar answered Oct 15 '22 23:10

John Atwood