0

I want to move completely to .NET Core so I need to use System.Text.Json instead of Newtonsoft.Json.

How can I write this code in the System.Text.Json?

private readonly JsonSerializer _serializer;

_serializer = JsonSerializer.Create(new JsonSerializerSettings
{
  DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate
});

private JObject _jsonSettings;

protected override void LoadSection(string sectionName, object section)
{    
  var jsonSection = _jsonSettings[sectionName];
  if (jsonSection != null)
  {
    using (var reader = jsonSection.CreateReader())
    {
      _serializer.Populate(reader, section);
    }
  }
}

protected override void SaveSection(string sectionName, object section)
{        
  var settings = _jsonSettings ?? new JObject();
  settings[sectionName] = JObject.FromObject(section);
  _jsonSettings = settings;
}

protected override void LoadDefaults()
{
  _jsonSettings = new JObject();
}

private void LoadFromJson(string json)
{
  _jsonSettings = JObject.Parse(json);
}
hadi khodabandeh
  • 585
  • 2
  • 7
  • 20

1 Answers1

0

Please refer to the official How to migrate from Newtonsoft.Json to System.Text.Json.

There are 3.1 and 5 versions provided. Please note that in 3.1 you can install the 5.0 package to get the new features (for example deserializing fields).

tymtam
  • 31,798
  • 8
  • 86
  • 126