I serialize a list of Interfaces into a JSON file that I deserialize later. In one application, I am deserializing all types of JSON objects, but in another application I am wanting to deserialize only one specific implementation of the Interface (Machine1). I want to keep the way I am serializing the data as-is, but change the deserialization code to SKIP any of the incorrect objects (JSON objects of a different type than I am interested)- my plan was to do this by checking if a Member variable is missing (if powerWatts is missing, then the data must be representing Machine2, therefore we should skip deserializing this particular object (Machine2) and instead deserialize the others in the array). How would I implement this using JSON.Net? MissingMemberHandling.Error throws an exception, so I don't think what would work for deserializing the rest of the list after seeing a missing property. MissingMemberHandling.Ignore leaves me with a property equal to 0, which is not correct.
Interface and Classes:
public interface IMachineInfo
{
DateTime windowsTime { get; set; }
DateTime irigTime { get; set; }
string messageTypeFlag { get; }
byte? sequenceNum { get; }
}
public class Machine1 : IMachineInfo
{
// Interface properties omitted for brevity
public double powerWatts { get; set; }
public double radiation { get; set; }
public double current { get; set; }
}
public class Machine2 : IMachineInfo
{
public double dbm { get; set; }
public double magneticField { get; set; }
public double frequency { get; set; }
}
Main:
// Serialization: get the interface and specific data into a collection that will be written to a file
IMachineInfo decoded = data[key].MachineDataDecoded;
dataConverted[key] = decoded;
//... more code
//write JSON data to JSONData.txt
string jsondata = JsonConvert.SerializeObject(dataConverted.Values.ToArray(), Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
File.AppendAllText(Environment.CurrentDirectory + subDirectoryName + jsonFileName, jsondata);
App2 (deserialize the data)
// try to read the json, but I only want Machine1 data, skip anything in the json that is related to machine2
JsonSerializerSettings settings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
MissingMemberHandling = MissingMemberHandling.Ignore
};
List<Machine1> ampData = JsonConvert.DeserializeObject<List<Machine1>>(@"C/Path/jsonfile", settings);
Real JSON Data:
[
{
"windowsTime": "2019-01-14T18:47:55.8390256-06:00",
"irigTime": "0001-01-01T00:00:00",
"messageTypeFlag": "Q",
"sequenceNum": 0,
},
{
"dbm": "66",
"magneticField ": "8967",
"frequency": "34500",
"windowsTime": "2019-01-14T18:47:55.8390256-06:00",
"irigTime": "0001-01-01T00:00:00",
"messageTypeFlag": "Q",
"sequenceNum": 0,
},
{
"powerWatts": "4000",
"radiation": "67",
"current": "2478",
"windowsTime": "2019-01-14T18:47:55.8390256-06:00",
"irigTime": "0001-01-01T00:00:00",
"messageTypeFlag": "Q",
"sequenceNum": 0,
},
{
"powerWatts": "4000",
"radiation": "67",
"current": "2478",
"windowsTime": "2019-01-14T18:47:55.8390258-06:00",
"irigTime": "0001-01-01T00:00:00",
"messageTypeFlag": "Q",
"sequenceNum": 0,
}
]
in my data above, only the 3rd and 4th element is of type Machine1, therefore I only want those object to be added to the list from json deserialization. The problem is, that when I deserialize this now, powerWatts = 0
for all 4 elements (not the behavior I want), even though it is only a valid property of the 3rd and 4th element. This is a problem since i can't just check if powerWatts == 0
and remove it from my list since 0 could be a valid value in a real world situation. I need to only deserialize Machine1 JSON objects