Is there any way to tell the JSON.Net formatter to write the names of object properties without the leading underscores when serializing an object for return from a Web API call?
I have the following VB.NET class:
Public Class Account
<JsonProperty(PropertyName:="ID")>
Public Property ID() As Guid
<JsonProperty(PropertyName:="Name")>
Public Property Name() As String = String.Empty
<JsonProperty(PropertyName:="Type")>
Public Property [Type]() As String = String.Empty
<JsonProperty(PropertyName:="Properties")>
Public Property Properties() As Dictionary(Of String, String) = New Dictionary(Of String, String)()
<JsonProperty(PropertyName:="DateCreated")>
Public Property DateCreated() As DateTime = DateTime.UtcNow
<JsonProperty(PropertyName:="LastUpdatedDate")>
Public Property LastUpdatedDate() As DateTime = DateTime.UtcNow
<JsonProperty(PropertyName:="IsActive")>
Public Property IsActive() As Boolean = True
End Class
I return a new Account object in the body of my response after a POST like this:
<HttpPost()>
Public Function Post(ByVal newAccount As PostAccount) As HttpResponseMessage
Try
Dim accountToAdd As Account = New Account()
With accountToAdd
.ID = System.Guid.NewGuid()
.IsActive = newAccount.IsActive
.Name = newAccount.Name
.Type = "GROUP"
For Each key As String In newAccount.Properties.Keys
.Properties.Add(key, newAccount.Properties(key))
Next
End With
_accounts.Add(accountToAdd)
Dim resp As HttpResponseMessage = Request.CreateResponse(Of Account)(HttpStatusCode.Created, accountToAdd)
resp.Headers.Location = New Uri(Url.Link("DefaultAPI", New With {.id = accountToAdd.ID}))
Return resp
Catch ex As Exception
Dim errorMessages As List(Of String) = New List(Of String)()
errorMessages.Add(ex.Message)
Dim resp As HttpResponseMessage = Request.CreateResponse(Of List(Of String))(HttpStatusCode.BadRequest, errorMessages)
Return resp
End Try
End Function
And I wind up getting the following JSON as a response to my POST:
Found the solution: I was missing the JsonObject attribute on my class. Without it, the property names are preceded by underscores. After adding the attribute, the underscores went away.
richmiller
Member
40 Points
51 Posts
Underscores in JSON Property Names When Returning Serialized Object
Aug 17, 2012 06:03 PM|LINK
Is there any way to tell the JSON.Net formatter to write the names of object properties without the leading underscores when serializing an object for return from a Web API call?
I have the following VB.NET class:
Public Class Account <JsonProperty(PropertyName:="ID")> Public Property ID() As Guid <JsonProperty(PropertyName:="Name")> Public Property Name() As String = String.Empty <JsonProperty(PropertyName:="Type")> Public Property [Type]() As String = String.Empty <JsonProperty(PropertyName:="Properties")> Public Property Properties() As Dictionary(Of String, String) = New Dictionary(Of String, String)() <JsonProperty(PropertyName:="DateCreated")> Public Property DateCreated() As DateTime = DateTime.UtcNow <JsonProperty(PropertyName:="LastUpdatedDate")> Public Property LastUpdatedDate() As DateTime = DateTime.UtcNow <JsonProperty(PropertyName:="IsActive")> Public Property IsActive() As Boolean = True End ClassI return a new Account object in the body of my response after a POST like this:
<HttpPost()> Public Function Post(ByVal newAccount As PostAccount) As HttpResponseMessage Try Dim accountToAdd As Account = New Account() With accountToAdd .ID = System.Guid.NewGuid() .IsActive = newAccount.IsActive .Name = newAccount.Name .Type = "GROUP" For Each key As String In newAccount.Properties.Keys .Properties.Add(key, newAccount.Properties(key)) Next End With _accounts.Add(accountToAdd) Dim resp As HttpResponseMessage = Request.CreateResponse(Of Account)(HttpStatusCode.Created, accountToAdd) resp.Headers.Location = New Uri(Url.Link("DefaultAPI", New With {.id = accountToAdd.ID})) Return resp Catch ex As Exception Dim errorMessages As List(Of String) = New List(Of String)() errorMessages.Add(ex.Message) Dim resp As HttpResponseMessage = Request.CreateResponse(Of List(Of String))(HttpStatusCode.BadRequest, errorMessages) Return resp End Try End FunctionAnd I wind up getting the following JSON as a response to my POST:
HTTP/1.1 201 Created Content-Length: 251 Content-Type: application/json; charset=utf-8 Location: http://localhost:8080/accounts/ca6cd110-a35b-4e15-ae2e-4da3f56f6a7b Server: Microsoft-HTTPAPI/2.0 Date: Fri, 17 Aug 2012 17:42:45 GMT { "_ID": "ca6cd110-a35b-4e15-ae2e-4da3f56f6a7b", "_Name": "New Account", "_Type": "GROUP", "_Properties": {}, "_DateCreated": "2012-08-17T17:42:40.5615627Z", "_LastUpdatedDate": "2012-08-17T17:42:40.5615627Z", "_IsActive": true }I'd rather that my properties were named "ID" and "Name" instead of "_ID" and "_Name". Does anyone have any suggestions about how to make that happen?
Thanks,
Rich
Json
richmiller
Member
40 Points
51 Posts
Re: Underscores in JSON Property Names When Returning Serialized Object
Aug 17, 2012 06:34 PM|LINK
Found the solution: I was missing the JsonObject attribute on my class. Without it, the property names are preceded by underscores. After adding the attribute, the underscores went away.
Rich
Json