This repository has been archived on 2022-11-03. You can view files and clone it, but cannot push or open issues or pull requests.
DevOpsOpenHack/support/simulator/DeviceSim/Helpers/CustomContractResolver.cs

51 lines
1.7 KiB
C#
Raw Normal View History

2022-11-03 20:41:13 +00:00
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System.Collections.Generic;
using System.Reflection;
namespace DeviceSim.Model
{
public class CustomContractResolver : DefaultContractResolver
{
public CustomContractResolver()
{
PropertyMappings = new Dictionary<string, string>
{
["Longitude"] = "Lon",
["Latitude"] = "Lat",
["ShortTermFuelBank"] = "ShortTermFuelBank1",
["LongTermFuelBank"] = "LongTermFuelBank1",
["MassFlowRate"] = "MAFFlowRate",
["RPM"] = "EngineRPM",
["Id"] = "TripPointId",
["DistanceWithMalfunctionLight"] = "DistancewithMIL",
["HasSimulatedOBDData"] = "IsSimulated",
};
IgnoreProperties = new List<string> { "HasOBDData" };
}
private Dictionary<string, string> PropertyMappings { get; }
private List<string> IgnoreProperties { get; }
protected override string ResolvePropertyName(string propertyName)
{
string resolvedName;
var resolved = PropertyMappings.TryGetValue(propertyName, out resolvedName);
return resolved ? resolvedName : base.ResolvePropertyName(propertyName);
}
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
JsonProperty property = base.CreateProperty(member, memberSerialization);
if (IgnoreProperties.Contains(property.PropertyName))
{
property.ShouldSerialize = p => false;
}
return property;
}
}
}