I am using RestSharp (version 104.4 via NuGet) to make calls to a Rest Web Service. I have designed a set of objects (POCO) which matches resources exposed in the API. However, my objects property names does not match those expected by the Rest Service when posting data, so I would like to "transform" them when I make a request to the Rest service to make them match match. I read that adding SerializeAs
attribute (with a Name specified) on my POCO's property will make them serialize correctly, but it won't.
My POCO
Imports RestSharp.Serializers
<Serializable(), SerializeAs(Name:="ApiMember")>
Public Class ApiMember
<SerializeAs(Name:="id")>
Public Property Id As Integer?
<SerializeAs(Name:="email")>
Public Property EmailAddress As String
<SerializeAs(Name:="firstname")>
Public Property Firstname As String
<SerializeAs(Name:="lastname")>
Public Property Lastname As String
End Class
My Call to the API
Dim request As RestRequest = New RestRequest(Method.POST)
Dim member As ApiMember = new ApiMember()
member.EmailAddress = "me@example.com"
request.Resource = "members"
request.RequestFormat = DataFormat.Json
request.AddBody(member)
Dim client As RestClient = New RestClient()
client.BaseUrl = "http://url.com"
client.Authenticator = New HttpBasicAuthenticator("username", "password")
client.Execute(Of ApiGenericResponse)(request)
What ends up being posted
{"Id":null,"EmailAddress":"me@example.com","Firstname":null,"Lastname":null}
Notice the name of the properties does not match thoses I specified in SerializeAs
(uppercases, name of EmailAddress)
Am I missing something ?
I came across this issue, and solved this a slightly different way than above, wanted to note it here.
We have a
factory
class that builds all of our requests. Looks like the followingRather than use the
AddJsonBody
andAddBody
methods against the request, both of which cause serialisation, I usedAddParameter
which will add the object you pass in without serialisation. I created a method calledSerialise
, which usesJSON.net
to serialise our class.This then allows us to use
JSON.net
'sJsonProperty
annotation above your propertys. Here is an example -This is for @MaxiWheat and anyone else interested in how to use JSON.NET as the JSON serializer in a RestSharp request. Basically, I used the approach described in this blog post by Patrick Riley:
and the
RestSharpJsonNetSerializer
is an implementation (less than 100 lines of code) from the JSON.NET guys (John Sheehan) that can be found on their Github pagesWith this setup, my problems went away and I was able to have a proper DTO with nice CamelCase properties, while the serialized JSON uses them in all "lowercase".
RestSharp uses SimpleJson. This library doesn't know or respect the
[SerializeAs]
attribute (which is XML-only anyway), it just outputs the POCO's property name, unless it's compiled with#SIMPLE_JSON_DATACONTRACT
defined, then you can use the[DataContract]
attribute to rename properties.So your options seem to be to recompile the SimpleJson library with that define and decorate your properties with the
[DataContract(Name="lowercasepropertyname")]
attribute, or create a custom serializer that uses a JSON serializer that does respect other attributes as suggested in @Ryan's answer.In RestSharp 104.4, the default
JsonSerializer
doesn't use the[SerializeAs]
attribute, as seen by reviewing the source code.One workaround to this is to create a custom serializer that uses the Json.NET
JsonSerializer
(a good example is here) and then decorate your properties with the[JsonProperty]
attribute, like so: