Jim Bennett

Senior Cloud Advocate at Microsoft, Xamarin Certified Developer, author of Xamarin In Action, blogger, speaker, father and lover of beer, whisky and Thai food. Opinions are my own.

  Redmond, WA, USA

My book:

Xamarin In Action

Contact me:

  Xamarin In Action
  Twitter
  GitHub
  LinkedIn
  YouTube
  Email
  CV

I've been playing a lot with F# recently, both to build Xamarin apps using Elmish.XamarinForms and for some Azure Functions. Whilst building an HTTP trigger I came across some weirdness when serializing a record type to JSON.

This is the relevant parts of my code:

type Output = { TotalBalance : float }

let Run(req: HttpRequestMessage, boundTable: IQueryable<Transaction>, log: TraceWriter) =
    async {
        // stuff
        return req.CreateResponse(HttpStatusCode.OK, { TotalBalance = sum })
    } |> Async.RunSynchronously

Now it's normal to assume the Output type would be serialized to JSON correctly, leading to something like:

{
  "balance" : 100
}

Unfortunately not - what I actually get is:

{
  "Balance@" : 100
}

Note the weird @ symbol on the property name.

So I went spelunking around Google and SO for a fix. The first suggestion was to add the [<CLIMutable>] attribute to my Output type, but this didn't actually work, the @ symbol was still there.

In the end I found the fix here: https://stackoverflow.com/questions/43118406/return-an-f-record-type-as-json-in-azure-functions/48718297#48718297. You can pass a different JSON formatter to the CreateResponse call, and configure that to return the correct property names:

let jsonFormatter = Formatting.JsonMediaTypeFormatter()
jsonFormatter.SerializerSettings.ContractResolver <- CamelCasePropertyNamesContractResolver()

return req.CreateResponse(HttpStatusCode.OK, { TotalBalance = sum }, jsonFormatter)

Done!




About the Author

Jim Bennett
Jim Bennett

Senior Cloud Advocate at Microsoft, Xamarin Certified Developer, blogger, author of Xamarin in Action, speaker, father and lover of beer, whisky and Thai food. Opinions are mine

 

I've been playing a lot with F# recently, both to build Xamarin apps using Elmish.XamarinForms and for some Azure Functions. Whilst building an HTTP trigger I came across some weirdness when serializing a record type to JSON.

This is the relevant parts of my code:

type Output = { TotalBalance : float }

let Run(req: HttpRequestMessage, boundTable: IQueryable<Transaction>, log: TraceWriter) =
    async {
        // stuff
        return req.CreateResponse(HttpStatusCode.OK, { TotalBalance = sum })
    } |> Async.RunSynchronously

Now it's normal to assume the Output type would be serialized to JSON correctly, leading to something like:

{
  "balance" : 100
}

Unfortunately not - what I actually get is:

{
  "Balance@" : 100
}

Note the weird @ symbol on the property name.

So I went spelunking around Google and SO for a fix. The first suggestion was to add the [<CLIMutable>] attribute to my Output type, but this didn't actually work, the @ symbol was still there.

In the end I found the fix here: https://stackoverflow.com/questions/43118406/return-an-f-record-type-as-json-in-azure-functions/48718297#48718297. You can pass a different JSON formatter to the CreateResponse call, and configure that to return the correct property names:

let jsonFormatter = Formatting.JsonMediaTypeFormatter()
jsonFormatter.SerializerSettings.ContractResolver <- CamelCasePropertyNamesContractResolver()

return req.CreateResponse(HttpStatusCode.OK, { TotalBalance = sum }, jsonFormatter)

Done!