-1

I find it difficult to force the deserialize operation to actually fail if the data doesn't match exactly what's expected for the output class.

class ContainerClass {
   string SomeString { get; set; } // <-- not nullable
}

Json file :

[
  {
    "SomeString": null, // <-- null
  }
]

Deserialize function :

using JsonTextReader reader = new JsonTextReader(file); // <-- the file I got from my controller.

var serializer = Newtonsoft.Json.JsonSerializer.Create(); // or new Serializer, whatever
serializer.MissingMemberHandling = MissingMemberHandling.Error;

return serializer.Deserialize<Collection<RegisterImportItem>>(reader);

I want the deserialize to fail if the string has a null value. The code above succeeds silently and places a null value in the non-nullable field. The horror!

I'd like to achive that by configuring the serializer (as in : I don't want to add a decorator above the field itself ).

Long things short: I want all non-nullable fields to fail if the value is null, no matter what.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
jeancallisti
  • 1,046
  • 1
  • 11
  • 21

4 Answers4

0

JSON.NET was written well before nullable reference types were a thing. The best way to do this is to use the JsonProperty attribute.

Annotate the property with JsonProperty

[JsonProperty(Required = Required.DisallowNull)]
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
  • did you see the sentence "I'd like to achive that by configuring the serializer (as in : I don't want to add a decorator above the field itself )." ? Is there a way to apply this setting on the whole deserializer instead of each field one by one? – jeancallisti Nov 15 '22 at 08:07
-1

your json is array but you are trying to deserialize object, try this code

    var json=@"[
  {
    ""SomeString"": null, // <-- null
    ""JustString"": ""test"" 
  }
]";
    var test = JsonConvert.DeserializeObject<ContainerClass[]>(json);

and fix the class by adding an accessor

public class ContainerClass {
  public  string SomeString { get; set; } 
 public string JustString { get; set; }
}
Serge
  • 40,935
  • 4
  • 18
  • 45
  • Incorrect. You missed the "Collection" word. It works and the array gets deserialized fine. The array becomes a collection and the fields get populated. – jeancallisti Nov 15 '22 at 08:05
-1

I think I found the answer here : Json.NET require all properties on deserialization

public class RequireObjectPropertiesContractResolver : DefaultContractResolver
{
    protected override JsonObjectContract CreateObjectContract(Type objectType)
    {
        var contract = base.CreateObjectContract(objectType);
        contract.ItemRequired = Required.Always;
        return contract;
    }
}

var settings = new JsonSerializerSettings { ContractResolver = new RequireObjectPropertiesContractResolver() };

var serializer = Newtonsoft.Json.JsonSerializer.Create(settings);

EDIT: That's NOT the right answer. Now it forbids all the null fields, even the ones that are nullable in the Container class.

jeancallisti
  • 1,046
  • 1
  • 11
  • 21
-1

There is no way of achieving this on all fields at once. The only way is with decorators on individual fields.

jeancallisti
  • 1,046
  • 1
  • 11
  • 21