Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I would suggest to go different way. Keep your C# object model as much standard as possible <em>(I wouldn't use <code>@</code> sign and <code>C# keywords</code> as property name)</em>.</p> <p>We can separate the serialized (JSON) world and C# objects - just by using the Json.NET features.</p> <p>One of the simpliest to use is decoration with Attribute:</p> <pre><code>[JsonProperty(PropertyName = "default")] public string DefaultValue { get; set; } </code></pre> <p>In this case we have to reference Newtonsoft.Json in the project. If it must be POCO, we can introduce <code>CustomResolver</code> derrived from <code>DefaultContractResolver</code> and define these conversions there...</p> <p>But separation of concern in this case is a bit more pure solution, I would say</p> <p><strong>EDIT: JSON Contract Resolver draft</strong> <em>(see comments)</em></p> <p>Important NOTE: <a href="http://james.newtonking.com/projects/json/help/" rel="nofollow noreferrer">Newtonsoft.Json</a> is part of the Web API. Not only it is an open source, but even MS team bet on that as a core JSON serializer.</p> <p>1) Newtonsoft.Json (as a part of the Web.API) is already installed in your solution. So you do not have to downloaded (nuget) separately. It would always be in your <code>packages</code> folder. So, to use the attribute is just adding the reference. It is there...</p> <p>2) There is a small draft how to <strong>do the attribute stuff, while keeping the POCO</strong>. As I've tried explain here: <a href="https://stackoverflow.com/a/13632872/1679310">POCO's, behavior and Peristance Igorance</a>, to keep POCO (e.g. we do profit from layered Architecture with NHibernate on a data layer), we can <em>replace</em> attributes with a <code>Contract Resolver</code>. Our POCO library does not have to reference anything</p> <p>We just have to do extend the service layer:</p> <pre><code>public class MyResolver : DefaultContractResolver { protected override JsonProperty CreateProperty( MemberInfo member, MemberSerialization memberSerialization) { var jProperty = base.CreateProperty(member, memberSerialization); var propertyInfo = member as PropertyInfo; if (propertyInfo == null) { return jProperty; } // just adjust in case if Property name is DefaultValue var isDefaultValueProeprty = propertyInfo.Name.Equals("DefaultValue"); if(isDefaultValueProeprty) { jProperty.PropertyName = "default"; } return jProperty; } ... </code></pre> <p>This way we've provided the same information to <em>serailizer</em> as with the <code>[JsonPropertyAttribute]</code>.</p> <p>Now, we just have to use it. There are many ways (e.g. global) but we can do it for a controller only:</p> <pre><code>protected override void Initialize(HttpControllerContext context) { base.Initialize(context); var jSettings = context.Configuration.Formatters.JsonFormatter.SerializerSettings; jSettings.ContractResolver = MyResolver; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload