Ignore a property when deserializing using Json.Net with ItemRequired = Required.Always

27,300

Evidently JsonIgnore will only control the serialization in this case. JsonIgnore is required to specify that the FullName property should not be serialized to the json representation.

To ignore the property during deserialization we need to add the JsonProperty annotation with Required = Required.Default (which means not required).

So, this is how to avoid the JsonSerializationException:

[JsonObject(ItemRequired = Required.Always)]
public class Hamster
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    [JsonIgnore]
    [JsonProperty(Required = Required.Default)]
    public string FullName { get { return FirstName + LastName; }}
}
Share:
27,300

Related videos on Youtube

i3arnon
Author by

i3arnon

I'm Bar Arnon, a performance driven developer at Microsoft. Working on Azure Data Explorer. Worked on Microsoft ATA & Azure ATP. Blog: i3arnon.com Twitter: @i3arnon GitHub: @i3arnon CV: http://stackoverflow.com/cv/i3arnon Blog posts: Introducing AsyncUtilities ConcurrentDictionary Is Not Always Thread-Safe Duck Typing And Async/Await The Issue With Scoped Async Synchronization Constructs Return Any (Task-Like) Type From An Async Method Not All Beginnings Must Have An End TPL Dataflow Is The Best Library You're Not Using Async LINQ To Objects Over MongoDB On The Efficiency Of ValueTask Protobuf-net is broken around DateTime Surprising Contention in System.Threading.Timer LongRunning is useless for Task.Run with async-await LogicalOperationStack Is Broken With async-await

Updated on July 01, 2020

Comments

  • i3arnon
    i3arnon almost 4 years

    I'm using Json.Net to serialize and deserialize classes to json and back.

    I added to a class marked with [JsonObject(ItemRequired = Required.Always)] (or Required.Always) a new get-only property. That results in the following JsonSerializationException:

    Newtonsoft.Json.JsonSerializationException: Required property '<PropertyName>' not found in JSON

    I thought marking that property with JsonIgnore would solve the issue, but that doesn't work.

    How can I tell Json.Net that this property should be ignored?

    Here's a minimal example reproducing the issue:

    [JsonObject(ItemRequired = Required.Always)]
    public class Hamster
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        [JsonIgnore]
        public string FullName { get { return FirstName + LastName; }}
    }
    
    private static void Main()
    {
        var hamster = new Hamster {FirstName = "Bar", LastName = "Arnon"};
        var serializeObject = JsonConvert.SerializeObject(hamster);
        var deserializeObject = JsonConvert.DeserializeObject<Hamster>(serializeObject);
    }
    
  • nAviD
    nAviD over 3 years
    why there is no JsonPropertyAttribute in Json.Net (.net core 5)?
  • i3arnon
    i3arnon over 3 years
    @nAviD there is. Just checked. I assume you're not actually using Json.NET.