Hey all,
I'm implementing a lo-REST API and looking to return either XML or JSON. Building it in .NET WCF.
I like the design of both Flickr and Last FM APIs which wrap their variable complex types in a simple response ala:
<lfm status="ok">
<user>
<name>RJ</name>
<realname>Richard Jones </realname>
<country>UK</country>
<age>27</age>
</user>
</lfm>
or
<lfm status="ok">
<track>
<name>Best track ever</name>
<album>Thriller</album>
</user>
</lfm>
or
<lfm status="fail">
... error details
</lfm>
Serialization of the complex types is simple as you'd expect (using DataContract, DataMember etc). But wrapping it in some kind of custom response is tripping me up because of the variable complex types that may be contained inside. Giving the response a member of type object which is filled by the complex type does not serialize:
[DataContract]
public class Response
{
public enum ResponseStatus
{
ok,
fail
}
[DataMember]
public ResponseStatus Status { get; set; }
[DataMember]
public object Data { get; set; }
}
Any ideas or help is greatly appreciated.
Many thanks, Alex
EDIT: Tim Roberts provides an interesting solution although it doesn't serialise nicely. An option is to have all potential complex types as properties with [DataMember(EmitDefaultValue = false)] specified so nulls do not serialise. Not sure this is the best solution though.