Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# WebAPI default deserialization producing @ sign

I'm working on an F# Web API application - https://github.com/odytrice/Dumia

When I try to send an Array of the following records,

[<CLIMutable>]
type Product = 
   { ProductID : int
     Code : string
     Name : string
     Price : decimal
     ImageUrl : string }

[<CLIMutable>]
type Inventory =
   { Product: Product
     Quantity: int }

Here is my current WebAPI Configuration

let registerWebApi (app:IAppBuilder) = 

    let config = new HttpConfiguration()
    // Configure routing
    config.MapHttpAttributeRoutes()

    // Remove XML Formatter
    config.Formatters.Clear()

    let formatter = new JsonMediaTypeFormatter()
    formatter.UseDataContractJsonSerializer <- false
    config.Formatters.Add(formatter)

    config.Services.Replace(typeof<IHttpControllerActivator>, CompositionRoot())

    app.UseWebApi(config)

My Web API is producing the following output

{
   Product@: {
       ProductID@: 1,
       Code@: "Bag-01",
       Name@: "Ladies Bag",
       Price@: 120,
       ImageUrl@: "/content/images/bag.jpg"
   },
   Quantity@: 15
}

Does anyone have an idea how to get rid of the @ sign?

like image 223
Ody Avatar asked Jun 17 '26 19:06

Ody


1 Answers

The Problem is because of the Default DataContract used by WebAPI.

I had to change it to

config.Formatters
      .JsonFormatter
      .SerializerSettings
      .ContractResolver 
           <- Serialization.DefaultContractResolver()

or better yet

config.Formatters
      .JsonFormatter
      .SerializerSettings
      .ContractResolver 
           <- Serialization.CamelCasePropertyNamesContractResolver()
like image 154
Ody Avatar answered Jun 19 '26 23:06

Ody



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!