Search code examples
c#.net-corejson.netsystem.text.json

Does the new `System.Text.Json` have a required property attribute?


I've combed through the MS docs but cannot find an attribute equivalent to the NewtonSoft JsonPropertyRequired.

What I'm looking for is this:

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

Am I just missing something or does this level of validation not exist in the Microsoft library?


Solution

  • Prependment:

    The question was specifically mentioned Required.Always, which in Newtonsoft.Json would require the property and disallow nulls (for instance "firstName": null wouldn't be allowed).

    In System.Text.Json that is equivalent to [JsonRequired] (see this article) because it must have a value, and null is not permitted.

    The official migration doc is currently very weak on the other related Newtonsoft features. For instance, in Newtonsoft.Json you have the following four options for the Required enumeration (which is the attribute used on JsonProperty):

    Default        The property is not required. The default state.
    AllowNull      The property must be defined in JSON but can be a null value.
    Always         The property must be defined in JSON and cannot be a null value.
    DisallowNull   The property is not required but it cannot be a null value.
    

    It's very important to note that this is NOT the [JsonRequired] attribute (from Newtonsoft), whiich means '...always serialize the member, and to require that the member has a value.'.

    I'm not going to attempt to make a mapping table for the above because I will almost certainly get it wrong - and I actually don't think it's even possible.

    However there is one other related attribute [JsonIgnore(Condition = JsonIgnoreCondition.XXXX])[attribute][3] inSystem.Text.Json` which affects the serialization and is likely to be more useful.

    Using [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] will NOT output the value to the JSON if null. So that's sort of similar to Required.AllowNull (but you wouldn't use [JsonRequired] anymore because it isn't actually required!).

    The bottom line is there are not exact parallels between the two libraries and the way you use it will influence greatly the ease of your migration. Plan carefully!


    .NET 7 (November 2022) now has its very own [JsonRequired] which many will discover for the first time when seeing errors like this:

    'JsonRequired' is an ambiguous reference between 'Newtonsoft.Json.JsonRequiredAttribute' and 'System.Text.Json.Serialization.JsonRequiredAttribute'

    This will probably be as a result of having the following two using statements in a file:

    using System.Text.Json.Serialization;
    using Newtonsoft.Json;
    

    The simplest and safest quick solution (if you want to keep with Newtonsoft) is to search and replace and make these two replacements:

    [JsonRequired] => [Newtonsoft.Json.JsonRequired]
    [JsonRequired( => [Newtonsoft.Json.JsonRequired(
    

    This will only be guaranteed to work if all your code is currently .NET 6 (where usage of this attribute must have been from Newtonsoft). Be careful if you're combining code from a .NET 6 and .NET 7 project. This is what I'm planning on doing to avoid future confusion if I switch (which realistically I don't plan on).

    There is a more general article about migrating fully to System.Text.Json if you are able to do that.