Search code examples
c#json.net-corejson.netjson-deserialization

Deserialize json object that is sometimes an int value


I have an API call that returns the attached JSON response, when deserializing I get the following error "Error converting value 0 to type '....Models.TripMaps.Alarms'. Path 'map[2].Alarm', line 1, position 1354."

So the 2nd map array item has a value of 0 for alarms (rather than the expected Alarm object).

How do I handle this? Any help appreciated

{
   "metrics":"1",
   "map":[
      {
         "GPRSPoints":{
            "ID":"349709508",
            "DataReceived":"2019-12-16 11:30:20",
            "IgnitionOn":"2019-12-16 11:30:07",
            "Direction":0,
            "Latitude":-37.81026166666667,
            "Liters":0,
            "Longitude":144.962755,
            "RPM":-1,
            "Speed":0
         },
         "Alarm":{
            "ID":"349709508",
            "Alarm":"Ignition On",
            "AlarmId":"16",
            "AlarmAddress":"Melbourne Central, Elizabeth Street, Melbourne City, City of Melbourne, Victoria, 3000, Australia",
            "AlarmThreshold":"0",
            "DataReceived":"2019-12-16 11:30:20",
            "IgnitionOn":"2019-12-16 11:30:07",
            "Latitude":-37.81026166666667,
            "Longitude":144.962755,
            "Liters":0,
            "RPM":-1,
            "Speed":0
         }
      },
      {
         "GPRSPoints":{
            "ID":"349709519",
            "DataReceived":"2019-12-16 11:30:27",
            "IgnitionOn":"2019-12-16 11:30:07",
            "Direction":0,
            "Latitude":-37.81026166666667,
            "Liters":0,
            "Longitude":144.962755,
            "RPM":-1,
            "Speed":0
         },
         "Alarm":{
            "ID":"349709519",
            "Alarm":"Fatigue driving",
            "AlarmId":"0D",
            "AlarmAddress":"Melbourne Central, Elizabeth Street, Melbourne City, City of Melbourne, Victoria, 3000, Australia",
            "AlarmThreshold":"240",
            "DataReceived":"2019-12-16 11:30:27",
            "IgnitionOn":"2019-12-16 11:30:07",
            "Latitude":-37.81026166666667,
            "Longitude":144.962755,
            "Liters":0,
            "RPM":-1,
            "Speed":0
         }
      },
      {
         "GPRSPoints":{
            "ID":"349709550",
            "DataReceived":"2019-12-16 11:31:20",
            "IgnitionOn":"2019-12-16 11:30:07",
            "Direction":0,
            "Latitude":-37.81026166666667,
            "Liters":0,
            "Longitude":144.962755,
            "RPM":0,
            "Speed":0
         },
         "Alarm":0
      },
      {
         "GPRSPoints":{
            "ID":"349709551",
            "DataReceived":"2019-12-16 11:31:20",
            "IgnitionOn":"2019-12-16 11:30:07",
            "Direction":0,
            "Latitude":-37.81026166666667,
            "Liters":0,
            "Longitude":144.962755,
            "RPM":0,
            "Speed":0
         },
         "Alarm":0
      },
      {
         "GPRSPoints":{
            "ID":"349709552",
            "DataReceived":"2019-12-16 11:31:20",
            "IgnitionOn":"2019-12-16 11:30:07",
            "Direction":0,
            "Latitude":-37.81026166666667,
            "Liters":0,
            "Longitude":144.962755,
            "RPM":0,
            "Speed":0
         },
         "Alarm":0
      },
      {
         "GPRSPoints":{
            "ID":"349709554",
            "DataReceived":"2019-12-16 11:31:20",
            "IgnitionOn":"2019-12-16 11:30:07",
            "Direction":163.9,
            "Latitude":-37.81124166666667,
            "Liters":0,
            "Longitude":144.96304833333335,
            "RPM":0,
            "Speed":1.0799999999999998
         },
         "Alarm":0
      },
      {
         "GPRSPoints":{
            "ID":"349709555",
            "DataReceived":"2019-12-16 11:31:20",
            "IgnitionOn":"2019-12-16 11:30:07",
            "Direction":153.8,
            "Latitude":-37.80994333333334,
            "Liters":0,
            "Longitude":144.962625,
            "RPM":0,
            "Speed":0.612
         },
         **"Alarm":0**
      },
      {
         "GPRSPoints":{
            "ID":"349709556",
            "DataReceived":"2019-12-16 11:31:20",
            "IgnitionOn":"2019-12-16 11:30:07",
            "Direction":6.4,
            "Latitude":-37.809958333333334,
            "Liters":0,
            "Longitude":144.96257666666668,
            "RPM":0,
            "Speed":1.5479999999999998
         },
         "Alarm":0
      },
      {
         "GPRSPoints":{
            "ID":"349709557",
            "DataReceived":"2019-12-16 11:31:20",
            "IgnitionOn":"2019-12-16 11:30:07",
            "Direction":343.9,
            "Latitude":-37.80993,
            "Liters":0,
            "Longitude":144.96251333333333,
            "RPM":0,
            "Speed":1.188
         },
         "Alarm":0
      },
      {
         "GPRSPoints":{
            "ID":"349709558",
            "DataReceived":"2019-12-16 11:31:20",
            "IgnitionOn":"2019-12-16 11:30:07",
            "Direction":343.9,
            "Latitude":-37.80992666666667,
            "Liters":0,
            "Longitude":144.96246666666667,
            "RPM":0,
            "Speed":0.39599999999999996
         },
         "Alarm":0
      },
      {
         "GPRSPoints":{
            "ID":"349709559",
            "DataReceived":"2019-12-16 11:31:20",
            "IgnitionOn":"2019-12-16 11:30:07",
            "Direction":343.9,
            "Latitude":-37.809938333333335,
            "Liters":0,
            "Longitude":144.96243333333334,
            "RPM":0,
            "Speed":0.36
         },
         "Alarm":0
      },
      {
         "GPRSPoints":{
            "ID":"349709560",
            "DataReceived":"2019-12-16 11:31:20",
            "IgnitionOn":"2019-12-16 11:30:07",
            "Direction":343.9,
            "Latitude":-37.80994666666667,
            "Liters":0,
            "Longitude":144.962395,
            "RPM":0,
            "Speed":0.7559999999999999
         },
         "Alarm":0
      },
      {
         "GPRSPoints":{
            "ID":"349709561",
            "DataReceived":"2019-12-16 11:31:20",
            "IgnitionOn":"2019-12-16 11:30:07",
            "Direction":156.1,
            "Latitude":-37.80976666666667,
            "Liters":0,
            "Longitude":144.96213,
            "RPM":0,
            "Speed":1.404
         },
         "Alarm":0
      },
      {
         "GPRSPoints":{
            "ID":"349709562",
            "DataReceived":"2019-12-16 11:31:20",
            "IgnitionOn":"2019-12-16 11:30:07",
            "Direction":291.4,
            "Latitude":-37.809868333333334,
            "Liters":0,
            "Longitude":144.96237,
            "RPM":0,
            "Speed":0.5399999999999999
         },
         "Alarm":0
      },
      {
         "GPRSPoints":{
            "ID":"349709563",
            "DataReceived":"2019-12-16 11:31:20",
            "IgnitionOn":"2019-12-16 11:30:07",
            "Direction":291.4,
            "Latitude":-37.809875,
            "Liters":0,
            "Longitude":144.96233833333332,
            "RPM":0,
            "Speed":0
         },
         "Alarm":0
      },
      {
         "GPRSPoints":{
            "ID":"349709564",
            "DataReceived":"2019-12-16 11:31:20",
            "IgnitionOn":"2019-12-16 11:30:07",
            "Direction":291.4,
            "Latitude":-37.809873333333336,
            "Liters":0,
            "Longitude":144.96232666666666,
            "RPM":0,
            "Speed":0
         },
         "Alarm":0
      },
      {
         "GPRSPoints":{
            "ID":"349709565",
            "DataReceived":"2019-12-16 11:31:20",
            "IgnitionOn":"2019-12-16 11:30:07",
            "Direction":291.4,
            "Latitude":-37.809873333333336,
            "Liters":0,
            "Longitude":144.96232666666666,
            "RPM":0,
            "Speed":0
         },
         "Alarm":0
      },
      {
         "GPRSPoints":{
            "ID":"349709566",
            "DataReceived":"2019-12-16 11:31:20",
            "IgnitionOn":"2019-12-16 11:30:07",
            "Direction":291.4,
            "Latitude":-37.809873333333336,
            "Liters":0,
            "Longitude":144.96232666666666,
            "RPM":0,
            "Speed":0
         },
         "Alarm":0
      },
      {
         "GPRSPoints":{
            "ID":"349709567",
            "DataReceived":"2019-12-16 11:31:20",
            "IgnitionOn":"2019-12-16 11:30:07",
            "Direction":291.4,
            "Latitude":-37.809873333333336,
            "Liters":0,
            "Longitude":144.96232666666666,
            "RPM":0,
            "Speed":0
         },
         "Alarm":0
      },
      {
         "GPRSPoints":{
            "ID":"349709568",
            "DataReceived":"2019-12-16 11:31:20",
            "IgnitionOn":"2019-12-16 11:30:07",
            "Direction":291.4,
            "Latitude":-37.809873333333336,
            "Liters":0,
            "Longitude":144.96232666666666,
            "RPM":0,
            "Speed":0
         },
         "Alarm":0
      },
      {
         "GPRSPoints":{
            "ID":"349709569",
            "DataReceived":"2019-12-16 11:31:20",
            "IgnitionOn":"2019-12-16 11:30:07",
            "Direction":291.4,
            "Latitude":-37.809873333333336,
            "Liters":0,
            "Longitude":144.96232666666666,
            "RPM":0,
            "Speed":0
         },
         "Alarm":0
      },
      {
         "GPRSPoints":{
            "ID":"349709570",
            "DataReceived":"2019-12-16 11:31:20",
            "IgnitionOn":"2019-12-16 11:30:07",
            "Direction":291.4,
            "Latitude":-37.809873333333336,
            "Liters":0,
            "Longitude":144.96232666666666,
            "RPM":0,
            "Speed":0
         },
         "Alarm":0
      },
      {
         "GPRSPoints":{
            "ID":"349710609",
            "DataReceived":"2019-12-16 11:31:44",
            "IgnitionOn":"2019-12-16 11:30:07",
            "Direction":291.4,
            "Latitude":-37.809873333333336,
            "Liters":0,
            "Longitude":144.96232666666666,
            "RPM":0,
            "Speed":0
         },
         "Alarm":0
      },
      {
         "GPRSPoints":{
            "ID":"349710610",
            "DataReceived":"2019-12-16 11:31:44",
            "IgnitionOn":"2019-12-16 11:30:07",
            "Direction":291.4,
            "Latitude":-37.809873333333336,
            "Liters":0,
            "Longitude":144.96232666666666,
            "RPM":0,
            "Speed":0
         },
         "Alarm":0
      },
      {
         "GPRSPoints":{
            "ID":"349710612",
            "DataReceived":"2019-12-16 11:31:44",
            "IgnitionOn":"2019-12-16 11:30:07",
            "Direction":291.4,
            "Latitude":-37.809873333333336,
            "Liters":0,
            "Longitude":144.96232666666666,
            "RPM":0,
            "Speed":0
         },
         "Alarm":0
      },
      {
         "GPRSPoints":{
            "ID":"349710613",
            "DataReceived":"2019-12-16 11:31:44",
            "IgnitionOn":"2019-12-16 11:30:07",
            "Direction":291.4,
            "Latitude":-37.809873333333336,
            "Liters":0,
            "Longitude":144.96232666666666,
            "RPM":0,
            "Speed":0
         },
         "Alarm":0
      },
      {
         "GPRSPoints":{
            "ID":"349710614",
            "DataReceived":"2019-12-16 11:31:44",
            "IgnitionOn":"2019-12-16 11:30:07",
            "Direction":291.4,
            "Latitude":-37.809873333333336,
            "Liters":0,
            "Longitude":144.96232666666666,
            "RPM":0,
            "Speed":0
         },
         "Alarm":0
      },
      {
         "GPRSPoints":{
            "ID":"349710615",
            "DataReceived":"2019-12-16 11:31:44",
            "IgnitionOn":"2019-12-16 11:30:07",
            "Direction":291.4,
            "Latitude":-37.809873333333336,
            "Liters":0,
            "Longitude":144.96232666666666,
            "RPM":0,
            "Speed":0
         },
         "Alarm":0
      },
      {
         "GPRSPoints":{
            "ID":"349710616",
            "DataReceived":"2019-12-16 11:31:44",
            "IgnitionOn":"2019-12-16 11:30:07",
            "Direction":291.4,
            "Latitude":-37.809873333333336,
            "Liters":0,
            "Longitude":144.96232666666666,
            "RPM":0,
            "Speed":0
         },
         "Alarm":0
      },
      {
         "GPRSPoints":{
            "ID":"349710617",
            "DataReceived":"2019-12-16 11:31:44",
            "IgnitionOn":"2019-12-16 11:30:07",
            "Direction":291.4,
            "Latitude":-37.809873333333336,
            "Liters":0,
            "Longitude":144.96232666666666,
            "RPM":0,
            "Speed":0
         },
         "Alarm":0
      },
      {
         "GPRSPoints":{
            "ID":"349709573",
            "DataReceived":"2019-12-16 11:31:49",
            "IgnitionOn":"2019-12-16 11:30:07",
            "Direction":291.4,
            "Latitude":-37.809873333333336,
            "Liters":0,
            "Longitude":144.96232666666666,
            "RPM":0,
            "Speed":0
         },
         "Alarm":0
      },
      {
         "GPRSPoints":{
            "ID":"349709574",
            "DataReceived":"2019-12-16 11:31:51",
            "IgnitionOn":"2019-12-16 11:30:07",
            "Direction":291.4,
            "Latitude":-37.809873333333336,
            "Liters":0,
            "Longitude":144.96232666666666,
            "RPM":-1,
            "Speed":0
         },
         "Alarm":{
            "ID":"349709574",
            "Alarm":"Ignition Off",
            "AlarmId":"17",
            "AlarmAddress":"Aurora Melbourne Central, Little La Trobe Street, Melbourne City, City of Melbourne, Victoria, 3000, Australia",
            "AlarmThreshold":"0",
            "DataReceived":"2019-12-16 11:31:51",
            "IgnitionOn":"2019-12-16 11:30:07",
            "Latitude":-37.809873333333336,
            "Longitude":144.96232666666666,
            "Liters":0,
            "RPM":-1,
            "Speed":0
         }
      },
      {
         "GPRSPoints":{
            "ID":"349709576",
            "DataReceived":"2019-12-16 11:31:51",
            "IgnitionOn":"2019-12-16 11:30:07",
            "Direction":291.4,
            "Latitude":-37.809873333333336,
            "Liters":0,
            "Longitude":144.96232666666666,
            "RPM":-1,
            "Speed":0
         },
         "Alarm":{
            "ID":"349709576",
            "Alarm":"Fatigue driving",
            "AlarmId":"0D",
            "AlarmAddress":"Aurora Melbourne Central, Little La Trobe Street, Melbourne City, City of Melbourne, Victoria, 3000, Australia",
            "AlarmThreshold":"240",
            "DataReceived":"2019-12-16 11:31:51",
            "IgnitionOn":"2019-12-16 11:30:07",
            "Latitude":-37.809873333333336,
            "Longitude":144.96232666666666,
            "Liters":0,
            "RPM":-1,
            "Speed":0
         }
      }
   ]
}

These are my model classes

public class MapResponse
{
    [JsonProperty("metrics")]

    public long metrics { get; set; }

    [JsonProperty("map")]
    public Map[] map { get; set; }
}

and

public class Map
{
    [JsonProperty("GPRSPoints")]
    public GprsPoints GprsPoints { get; set; }

    [JsonProperty("Alarm")]
    public Alarms Alarm { get; set; }
}

I am deserializing as follows:

MapResponse result = JsonConvert.DeserializeObject<MapResponse>(res); 

I guess I want Alarm to be null when the JSON value is 0.


Solution

  • You can introduce a custom JsonConverter that returns null in the event that the value of "Alarm" is an integer:

    public class AlarmsConverter : JsonConverter<Alarms>
    {
        public override Alarms ReadJson(JsonReader reader, Type objectType, Alarms existingValue, bool hasExistingValue, JsonSerializer serializer)
        {
            switch (reader.MoveToContentAndAssert().TokenType)
            {
                case JsonToken.Null:
                case JsonToken.Integer:
                    return null;
    
                default:
                    var alarm = hasExistingValue ? existingValue : (Alarms)serializer.ContractResolver.ResolveContract(objectType).DefaultCreator();
                    serializer.Populate(reader, alarm);
                    return alarm;
            }
        }
    
        public override bool CanWrite => false;
    
        public override void WriteJson(JsonWriter writer, Alarms value, JsonSerializer serializer) => throw new NotImplementedException();
    }
    
    public static partial class JsonExtensions
    {
        public static JsonReader MoveToContentAndAssert(this JsonReader reader)
        {
            if (reader == null)
                throw new ArgumentNullException();
            if (reader.TokenType == JsonToken.None)       // Skip past beginning of stream.
                reader.ReadAndAssert();
            while (reader.TokenType == JsonToken.Comment) // Skip past comments.
                reader.ReadAndAssert();
            return reader;
        }
    
        public static JsonReader ReadAndAssert(this JsonReader reader)
        {
            if (reader == null)
                throw new ArgumentNullException();
            if (!reader.Read())
                throw new JsonReaderException("Unexpected end of JSON stream.");
            return reader;
        }
    }
    

    And apply it to the Alarm property as follows:

    public class Map
    {
        [JsonProperty("Alarm")]
        [JsonConverter(typeof(AlarmsConverter))]
        public Alarms Alarm { get; set; }
    
        // Remainder unchanged
    

    Demo fiddle here.