{
Id:
{
Include: true,
Value: "aef7b4c1-98f6-4f53-9be3-2fa72d1e319d"
},
Name:
{
Include: true,
Value: "Hello"
},
Address1_Line1:
{
Include: true,
Value: "Home!"
}
}
Which is how our Excludables map to JSON. Here do we stop this?
Extend the IExcludable interface
To convert our excludables correctly we need to intercept the conversion and handle these properties manually. The first problem we hit is although we know it's an Excludable<> we don't know what the raw type is. The best way around this is to expand the IExcludable interface to allow exposing of a raw value, like this:
public interface IExcludable
{
bool Include { get; set; }
object RawValue { get; set; }
}
The Excludable<> class just implements it on top of the existing value field, like this:
public object RawValue
{
get
{
return value;
}
set { this.value = (T) value; }
}
View Model Json Converter
Now that we can find the raw value without needing to know the underlying generic type we can intercept any excludable and convert it. This is the full converter class that results:
public class ViewModelJsonConverter : JsonConverter
{
public override bool CanRead => false;
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
JObject o = JObject.Parse(JsonConvert.SerializeObject(value, Formatting.Indented,
new JsonSerializerSettings {ReferenceLoopHandling = ReferenceLoopHandling.Ignore}));
foreach (var propertyInfo in value.GetType().GetProperties())
{
if (propertyInfo.CanRead)
{
var currentValue = propertyInfo.GetValue(value);
IExcludable excludable = currentValue as IExcludable;
if (excludable != null)
{
if (excludable.Include)
{
if (excludable.RawValue == null || excludable.RawValue.GetType().IsValueType || excludable.RawValue.GetType().Name == "String")
{
o.Property(propertyInfo.Name).Value = new JValue(excludable.RawValue);
}
else
{
o.Property(propertyInfo.Name).Value = JObject.FromObject(excludable.RawValue);
}
}
else
{
o.Remove(propertyInfo.Name);
}
}
}
}
o.WriteTo(writer);
}
public override bool CanConvert(Type objectType)
{
if (objectType.BaseType == typeof (ViewModel))
{
return true;
}
return false;
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new System.NotImplementedException();
}
}
In the above code our view models code had inherited a ViewModel class. Also worth pointing out is how you handle the both value and object types (with strings needing an extra helping hand as they're a bit different!). For value types and strings you need to create a JValue where as for reference types you need to expose them as a JObject.
Now, just add this converter like so in your global.asax exactly how you added your excludable converter from the previous post:
GlobalConfiguration.Configuration.AddJsonConverter(new ViewModelJsonConverter());
This code effectively flattens the excludable class into the generic types and outside of the world of your REST api nobody is any the wiser.
ReplyDeleteHello All ▲▲▲ I am Dianna Kent and I live in Nevada, U.S .I am a full member of the Illuminati brotherhood, and am among the 1252 lodge. ▲▲▲ Iwas ordained by the Grand-master to go out to the world and bringmore members to the Illuminati, ▲ so I was requested to get 400Recruits for the brotherhood, I have 210 new recruits already. I amlooking for only 190 more to go,▲I am sure you want to be amongthe 190 people that will be earning $500,000 monthly and a brandnew car, ▲so add me up now so we can achieve this great chanceof being rich ▲. You get to live your dream life on earth filled with luxury. so i urge you not to miss thisGreat opportunity becoming what you long wished for life email: illuminatitemple6634@gmail.com or Whats-app +1(854) 220-5771 for guidance
Hello All ▲▲▲ I am Dianna Kent and I live in Nevada, U.S .I am a full member of the Illuminati brotherhood, and am among the 1252 lodge. ▲▲▲ Iwas ordained by the Grand-master to go out to the world and bringmore members to the Illuminati, ▲ so I was requested to get 400Recruits for the brotherhood, I have 210 new recruits already. I amlooking for only 190 more to go,▲I am sure you want to be amongthe 190 people that will be earning $500,000 monthly and a brandnew car, ▲so add me up now so we can achieve this great chanceof being rich ▲. You get to live your dream life on earth filled with luxury. so i urge you not to miss thisGreat opportunity becoming what you long wished for life email: illuminatitemple6634@gmail.com or Whats-app +1(854) 220-5771 for guidance
Hello All ▲▲▲ I am Dianna Kent and I live in Nevada, U.S .I am a full member of the Illuminati brotherhood, and am among the 1252 lodge. ▲▲▲ Iwas ordained by the Grand-master to go out to the world and bringmore members to the Illuminati, ▲ so I was requested to get 400Recruits for the brotherhood, I have 210 new recruits already. I amlooking for only 190 more to go,▲I am sure you want to be amongthe 190 people that will be earning $500,000 monthly and a brandnew car, ▲so add me up now so we can achieve this great chanceof being rich ▲. You get to live your dream life on earth filled with luxury. so i urge you not to miss thisGreat opportunity becoming what you long wished for life email: illuminatitemple6634@gmail.com or Whats-app +1(854) 220-5771 for guidance