1

I've had a good google but can't seem to find a replacement for Json.Net's annotations in System.Text.Json (specifically JsonRequired).

Is the new Microsoft framework not really a replacement for Newtonsoft?

Ahmad Ibrahim
  • 1,915
  • 2
  • 15
  • 32
user2883072
  • 217
  • 3
  • 12
  • Not yet, see [Does the new `System.Text.Json` have a required property attribute?](https://stackoverflow.com/q/58443181/3744182), for which the accepted answer is *Not as of .NET core 3.0.* – dbc Nov 27 '19 at 08:04
  • Possible duplicate of [Does the new \`System.Text.Json\` have a required property attribute?](https://stackoverflow.com/questions/58443181/does-the-new-system-text-json-have-a-required-property-attribute) – dbc Nov 28 '19 at 17:54

1 Answers1

0

Please try this library I wrote as an extension to System.Text.Json to offer missing features: https://github.com/dahomey-technologies/Dahomey.Json.

You will find support for JsonRequiredAttribute.

public class A
{
    [JsonRequired]
    public int Id { get;set; }
}

Setup json extensions by calling on JsonSerializerOptions the extension method SetupExtensions defined in the namespace Dahomey.Json. Then deserialize your class with the regular Sytem.Text.Json API.

JsonSerializerOptions options = new JsonSerializerOptions();
options.SetupExtensions();

const string json = @"{""Id"":12}";
A obj = JsonSerializer.Deserialize<A>(json, options);