Scenario:
We have a Web Service which talks to a 3rd party service and receives json where the property names that are not standard to our organization. We don't want the overhead of created multiple DTO objects to support transformations.
Example:
{"office_code" : "ABC1234"} (office_code needs to be "Code")
We have to take the incoming json and deserialize to a organization standard POCO and run rules to it and serialize it again back out to the organization so it would now look like:
Example: {"Code" : "ABC1234"}
The workflow:
3rd Party Json => Our Web Service => Run Rules => Serialize to Internal consumer using company standard naming
so:
{"office_code" : "ABC1234"} => POCO Deserialzied to {"Code" : "ABC1234"} => Run Rules => POCO Serialized {"Code" : "ABC1234"}
example:
Our Original POCO looks like: If I Serialize it as is, it will have
Code serialized to
office_code which what I don't want. I want to stay as
Code [DataContract]
public class Test
{
[DataMember]
public int Id { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public string Description { get; set; }
[DataMember]
public string Whatever { get; set; }
[DataMember]
[JsonProperty("office_code")]
public string Code { get; set; }
}
Updated Test Object by adding a private method called _code with Attributes and method
[DataMember]
[JsonProperty("office_code")]
private string _code { get; set; }
Then updated the public string Code property to use the private property _code
[DataMember]
public string Code
{
get
{
return _code;
}
set
{
_code = value;
}
}
Microsoft Serializer and Newtonsoft both support a method called ShouldSerialize{Property} that is not heavily documented. We'll create a method for our _code property
public bool ShouldSerialize_code()
{
return false;
}
You can deserialize Json office_code to C# _code but when you Serialize Test, it will not serialize _code
[DataContract]
public class Test
{
[DataMember]
public int Id { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public string Description { get; set; }
[DataMember]
public string Whatever { get; set; }
[DataMember]
public string Code
{
get
{
return _code;
}
set
{
_code = value;
}
}
[DataMember]
[JsonProperty("office_code")]
private string _code { get; set; }
public bool ShouldSerialize_code()
{
return false;
}
public bool ShouldSerializeWhatever()
{
return false;
}
}
Running the Test
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Practices.Unity;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Runtime.Serialization;
using System.Threading.Tasks;
using System.Web.Http;
namespace Generic
{
public class BlahController : ApiController
{
[HttpGet]
public Test Get()
{
Test test1 = null;
//JSON Deserialization string {"Id":0,"Name":"Robert","Description":"Cool","office_code":"BlahBlahBlah"}
//office_code will be deserialized to a private property called _code
string deserializeFrom = "{\"Id\":0,\"Name\":\"Robert\",\"Description\":\"Cool\",\"office_code\":\"BlahBlahBlah\"}";
test1 = JsonConvert.DeserializeObject(deserializeFrom);
string serializeTo = JsonConvert.SerializeObject(test1);
//Results
// Json string to Serialized Object {"Id":0,"Name":"Robert","Description":"Cool","office_code":"BlahBlahBlah"}
// Serialized objec to json string {"Id":0,"Name":"Robert","Description":"Cool","Code":"BlahBlahBlah"}
//
Test test2 = new Test();
test2.Id = 0;
test2.Name = "Robert";
test2.Description = "Cool";
test2.Whatever = "How Annoying";
test2.Code = "BlahBlahBlah";
//test._code = "BlahBlahBlah" Inaccessible
return test2;
}
}
}