Anyone know if this can easily be achieved? I'm sure there are plenty of ways to do it (custom MediaTypeFormatters or something), but I'm really looking for something less timeconsuming. Something with attributes perhaps?
You can use the DataContract attributes to control casing for JSON output, as long as you're using the default serializer.
Put the DataContract attribute on the class, then decorate the properties like this:
/// <summary>
/// Gets or sets the ID of the product.
/// </summary>
[DataMember(Name = "id")]
public int Id { get; set; }
Can you use Xml serializer attributes for the Xml and DataContractAttributes for the JSON?
e.g.
XmlElement("name")
DataMember("Name")
If you need to, you can swap the serializer using a custom MediaTypeFormatter (I know you said you didn't want to but it is relatively straightforward - especially plugging in JSON.NET - careful not to close the stream though!). Using an XmlSerializer will
lose the ability to easily handle dictionaries though, so you may want to use JSON.NET attributes for the JSON and DataContract attributes for the Xml.
Can you use Xml serializer attributes for the Xml and DataContractAttributes for the JSON?
Probably yeah, but thats where this breaks down and exposes some design flaws, I dont want to decorate my resources classes with one attribute per supported formatter! I would love to have attributes that aren't bound to a specific serializer, but can be
used by them all.
Dunno if the Xml formatter can use the datacontract attributes? Tbh I've never tried :) It shouldn't be too difficult to implement one if it doesnt?
inmykingdom
Member
105 Points
34 Posts
PascalCase for Xml output and camelCase for Json output?
Mar 05, 2012 11:26 AM|LINK
Anyone know if this can easily be achieved? I'm sure there are plenty of ways to do it (custom MediaTypeFormatters or something), but I'm really looking for something less timeconsuming. Something with attributes perhaps?
SiggiGG
Member
265 Points
105 Posts
Re: PascalCase for Xml output and camelCase for Json output?
Mar 05, 2012 12:38 PM|LINK
You can use the DataContract attributes to control casing for JSON output, as long as you're using the default serializer.
Put the DataContract attribute on the class, then decorate the properties like this:
/// <summary> /// Gets or sets the ID of the product. /// </summary> [DataMember(Name = "id")] public int Id { get; set; }LittleClive
Member
91 Points
65 Posts
Re: PascalCase for Xml output and camelCase for Json output?
Mar 05, 2012 12:48 PM|LINK
Can you use Xml serializer attributes for the Xml and DataContractAttributes for the JSON?
e.g.
XmlElement("name")
DataMember("Name")
If you need to, you can swap the serializer using a custom MediaTypeFormatter (I know you said you didn't want to but it is relatively straightforward - especially plugging in JSON.NET - careful not to close the stream though!). Using an XmlSerializer will lose the ability to easily handle dictionaries though, so you may want to use JSON.NET attributes for the JSON and DataContract attributes for the Xml.
SiggiGG
Member
265 Points
105 Posts
Re: PascalCase for Xml output and camelCase for Json output?
Mar 05, 2012 12:51 PM|LINK
Probably yeah, but thats where this breaks down and exposes some design flaws, I dont want to decorate my resources classes with one attribute per supported formatter! I would love to have attributes that aren't bound to a specific serializer, but can be used by them all.
Dunno if the Xml formatter can use the datacontract attributes? Tbh I've never tried :) It shouldn't be too difficult to implement one if it doesnt?
inmykingdom
Member
105 Points
34 Posts
Re: PascalCase for Xml output and camelCase for Json output?
May 09, 2012 08:21 PM|LINK
Here we have the solution:
http://richarddingwall.name/2012/03/10/returning-camelcase-json-from-asp-net-web-api/