Is it possible to hide an element if the decimal value is zero?
public class MyData
{
public decimal Val1 { get; set; }
public decimal Val2 { get; set; }
public decimal Val3 { get; set; }
}
I would like to ignore Val3
if it is zero.
var result = new JavaScriptSerializer().Serialize(MyData);
You can use a custom JavaScriptConverter
:
For example:
public class MyJavaScriptSerializer : JavaScriptConverter
{
public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
{
throw new NotImplementedException();
}
public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
{
var myData = obj as MyData;
var result = new Dictionary<string, object>();
if (myData != null)
{
result["val1"] = myData.Val1;
result["val2"] = myData.Val2;
if(myData.Val3 != 0)
result["val3"] = myData.Val3;
return result;
}
return new Dictionary<string, object>();
}
public override IEnumerable<Type> SupportedTypes =>
new ReadOnlyCollection<Type>(new List<Type> { typeof(MyData) });
}
And use it like:
var js = new JavaScriptSerializer();
js.RegisterConverters(new List<JavaScriptConverter> { new MyJavaScriptSerializer() });
string result = js.Serialize(myData);
Or you can create a more powerful solution with reflection and your custom attributes.
just for example:
Make your custom attribute:
public class IgnoreIfValueExactlyAttribute : Attribute
{
public int ValueToIgnore { get; }
public IgnoreIfValueExactlyAttribute(int valueToIgnore)
{
ValueToIgnore = valueToIgnore;
}
}
Declare enumerable of supported types:
public override IEnumerable<Type> SupportedTypes => new ReadOnlyCollection<Type>(new List<Type> { typeof(object), /*your types*/ });
And override Serialize
method like:
public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
{
var result = new Dictionary<string, object>();
if (obj != null)
{
var properties = obj.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
foreach (var propertyInfo in properties)
{
var isIgnorable = propertyInfo.GetCustomAttribute<IgnoreIfValueExactlyAttribute>();
var value = decimal.Parse(propertyInfo.GetValue(obj).ToString());
if (isIgnorable != null && isIgnorable.ValueToIgnore == value)
continue;
result[propertyInfo.Name] = value;
}
return result;
}
return new Dictionary<string, object>();
}
And use attribute in your selializable types:
public class MyData
{
public decimal Val1 { get; set; }
public decimal Val2 { get; set; }
[IgnoreIfValueExactly(0)]
public decimal Val3 { get; set; }
}