DataMember(Name = "x")

1,189 views
Skip to first unread message

ean

unread,
Mar 18, 2011, 12:24:57 AM3/18/11
to ServiceStack .NET Open Source REST Web Services Framework
Hello !

[DataMember(Name = "r")]
public string Result { get; set; }

"Name" attribute is not supported by serialization?

Demis Bellot

unread,
Mar 18, 2011, 4:44:11 AM3/18/11
to servic...@googlegroups.com
Nope sorry, its ignored for the moment. It's more an XML thing.

- Demis

Rui Lopes

unread,
Mar 27, 2011, 9:21:09 AM3/27/11
to ServiceStack .NET Open Source REST Web Services Framework
I was also bitten by this. I was trying to use JsonServiceClient (for
the first time) on the following data (from
http://gdata.youtube.com/feeds/api/users/ruigodinholopes/playlists?v=2&alt=jsonc):

{
apiVersion: "2.0"
data: {
...

and the first thing I've written:

[DataContract]
public class PlayListsResponse
{
[DataMember(Name = "apiVersion")]
public string ApiVersion { get; set; }
...

Which does not work unless I also camelCase the properties, which IMHO
is an odd thing to do in C# code.

Is there other way to define the mapping of the JSON property?

Best regards,
Rui Lopes

Demis Bellot

unread,
Mar 27, 2011, 9:44:13 AM3/27/11
to servic...@googlegroups.com, Rui Lopes
Hi Rui,

Unfortunately the JSON serializer doesn't support the [DataMember(Name=)] attribute (it's more an XML thing). At the moment, there is no dependency on the [DataMember] attribute and I don't particularly want to encourage an artificial mismatch between the wire representation and the DTO itself which I believe just encourages unnecessary friction and more runtime issues/bug reports. So my recommendations for own your services is to just leave the PascalCase names, i.e. allow your C# objects to project itself.

So it looks like you're trying to use ServiceStack's JsonServiceClient's to call 3rd party services, I wouldn't. It's designed to best work with ServiceStack services and won't work if there is a mis-match between your DTO's and the external 3rd party API as you've found out. I don't intend to make changes to support 3rd party differences as it will impact on the succinctness of the API and there are already plenty of C# REST clients that already do this. (see: RestSharp, Hammock, EasyHttp, etc).

Note: if you download the external JSON as a string, the ServiceStack.Text serializer provides a few ways to parse 3rd party JSON payloads, all of which I've included examples of in the 3rd party api tests below:


Cheers,


Reply all
Reply to author
Forward
0 new messages