0

I have used Newtonsoft.Json for converting JSON object to string and vice versa. Recently I read about System.Text.Json in https://learn.microsoft.com/en-gb/dotnet/api/system.text.json?view=netcore-3.0.

I read the article and it is faster than Newtonsoft.Json.

using Newtonsoft.Json;

using Newtonsoft.Json;
Product product = new Product();    
product.Name = "Apple";
product.ExpiryDate = new DateTime(2008, 12, 28);
product.Sizes = new string[] { "Small", "Medium", "Large" };    
string output = JsonConvert.SerializeObject(product);
//{
//  "Name": "Apple",
//  "ExpiryDate": "2008-12-28T00:00:00",
//  "Sizes": [
//    "Small",
//    "Medium",
//    "Large"
//  ]
//}

Product deserializedProduct = JsonConvert.DeserializeObject<Product>(output);

using System.Text;

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

Product product = new Product();    
product.Name = "Apple";
product.ExpiryDate = new DateTime(2008, 12, 28);
product.Sizes = new string[] { "Small", "Medium", "Large" }; 
string jsonString = JsonSerializer.Serialize(product);

var objproduct = JsonSerializer.Deserialize<Product>(jsonString);
dbc
  • 104,963
  • 20
  • 228
  • 340
Jayakumar Thangavel
  • 1,884
  • 1
  • 22
  • 29

2 Answers2

0

Yes, you can. Nobody is blocking you, unless you are getting some issues. But in a project there will be some guidelines, so it is better to follow the guidelines, if you are implementing something new, its better to discuss with the team and then implement.

Indranil
  • 2,229
  • 2
  • 27
  • 40
0

Html escaping by default in System.Text.Json is one example of a difference.

Null values and circular references handling are some other differences.

System.Text.Json does not serialised fields as of .NET Core 3.0.

System.Json.Text is not a one to one replacement for Json.NET.

tymtam
  • 31,798
  • 8
  • 86
  • 126