0

I have the following json which is malformed, which has missing property value, I want JSON.Convert to throw an error while deserializing, but instead phoneNumber is replaced with null when deserialize to strongly typed Object Person, and with dynamic type the value of phoneNumber is replaced with {}, I have tried various JsonSerializerSettings but doesn't seem to help throw an error, is there a way to do this

var json=    {
      "firstName": "joe",
      "lastName": "doe",
      "phoneNumber": ,
      "email": "joe.doe@test.com"
    }
JsonConvert.DeserializeObject<Person>(json);
JsonConvert.DeserializeObject<Dynamic>(json);
inan
  • 178
  • 11
  • Really?? That sounds like a parser bug. – Ry- Dec 08 '17 at 15:48
  • @Ryan yeah if tried in other libraries in javascript or python, it just throws an error which is good – inan Dec 08 '17 at 15:49
  • Check this out: https://stackoverflow.com/questions/22683040/how-to-check-all-properties-of-an-object-whether-null-or-empty (check after deserialization) – LocEngineer Dec 08 '17 at 15:51

1 Answers1

0

This might be a bit of a work around, but you can mark it with an attribute that a value is required. Which migh or might not work for you depending on your exact case:

public class Videogame
{
    [JsonProperty(Required = Required.Always)]
    public string Name { get; set; }

    [JsonProperty(Required = Required.AllowNull)]
    public DateTime? ReleaseDate { get; set; }
}
Gerrie Pretorius
  • 3,381
  • 2
  • 31
  • 34