1

Closed

Properties aren't serialized correctly

description

I have class "Response" that is used as a return result for one of my functions:
 
Public Class Response
Private _status As String
Public Property Status() As String
    Get
        Return _status
    End Get
    Set(ByVal value As String)
        _status = value
    End Set
End Property
End Class
 
In webapi, userscontroller has the following function "Login" :
 
<HttpGet()>
Public Function Login(email As String, password As String) As Response
'some code goes here
Dim failedResponse As New Response
failedResponse.Status = "failed"
return failedResponse
End Function
 
Now here is the part that is confusing me,the returned json is:
{
_status: "failed"
}
 
I thought that my response should use the public naming of the property which is "Status" not the private value "_status", Am I missing anything or is it a bug?
 
I have the latest version of json.net which is till today: 4.5.8
I also have the latest version of Webapi : 4.0.2071.0,as far as I know jsonformatter uses json.net by default in RC and later versions/RTM.
 
EDIT: Strange,if I reinitialize SerializerSettings of JsonFormatter it works as I want,but WHY?!!
 
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    RouteTable.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}",
                                   New With {.id = Http.RouteParameter.Optional})
 
    Dim jsonFormatter = GlobalConfiguration.Configuration.Formatters.JsonFormatter

    'Code before last change
    'jsonFormatter.SerializerSettings.NullValueHandling=NullValueHandling.Ignore

    'Code after changes
    Dim jsonSettings As New JsonSerializerSettings
    jsonSettings.Converters.Add(New IsoDateTimeConverter)
    jsonSettings.NullValueHandling = NullValueHandling.Ignore
    jsonFormatter.SerializerSettings = jsonSettings
    'end changes

    GlobalConfiguration.Configuration.Formatters.Clear()
    'Force the Web Api to return JSON to the browser by default
    GlobalConfiguration.Configuration.Formatters.Add(jsonFormatter)

End Sub
Closed Aug 31, 2012 at 10:51 AM by JamesNK
I don't know. Ask the Web API team

comments