Reputation: 1381
I'm hoping to use an OData endpoint hosted by WCF DataServices in my project calling it from a javascript front-end. It is important that the property names on the JSON objects follow Javascript conventions rather than c# conventions .i.e:
ThisIsAProperty
should end up: thisIsAProperty
Conversely, the c# objects must retain idiomatic c# naming conventions.
It is also important that accomplishing this goal doesn't cause any duplication of intent in my c# code. For example, adding attributes to each property that simply restate the property name in camelCase is not acceptable.
I'm able to accomplish this fairly easily when using ASP.NET MVC and the Newtonsoft JSON serializer by simply flipping a switch while serializing.
Is there such a way to ensure that the data always serializes to JSON with camelCase attribute names?
Upvotes: 3
Views: 1511
Reputation: 20924
WCF Data Services is case sensitive, so changing the case between the server and a javascript client is not really an option.
However WCF DS simply binds to the underlying data model, so if you can control that you can make everything camelCase in the data model, very easily. I.e. go into the EF designer and set all give all the EntitySets, Properties and Relationships to camelCase names.
Probably not what you are looking for though seeing as this will effect any C# code too... -Alex
Upvotes: 0
Reputation: 82634
Yes, Implement ISerializable
and define you values in camel case:
[Serializable]
public class MyObject : ISerializable
{
public int n1;
public int n2;
public String str;
public MyObject()
{
}
protected MyObject(SerializationInfo info, StreamingContext context)
{
n1 = info.GetInt32("camelCase1");
n2 = info.GetInt32("propertyValue2");
str = info.GetString("kK");
}
[SecurityPermissionAttribute(SecurityAction.Demand,SerializationFormatter=true)]
public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("camelCase1", n1);
info.AddValue("propertyValue2", n2);
info.AddValue("kK", str);
}
}
Upvotes: 1