Skip to content

Deserializing with NullValueHandling.Ignore for a type with constructor parameters doesn't ignore null values #2954

@ker1o

Description

@ker1o

Source/destination types

class A {
	public A(int arg) {
        }
	
	public int field;
}

Source/destination JSON

{"arg": 1, "field": null}

Expected behavior

When new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore } is provided field property is initialized with default value for int type

Actual behavior

Unhandled exception. Newtonsoft.Json.JsonSerializationException: Error converting value {null} to type 'System.Int32'. Path 'field'

Steps to reproduce

using Newtonsoft.Json;
					
public class Program
{
	class A {
		public A(int arg) {
		}
	
		public int field;
	}
	
	public static void Main()
	{
		var result = JsonConvert.DeserializeObject<A>("{\"arg\": 1, \"field\": null}", new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
	}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions