Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CSHARP

system.text.json DeserializeAsync when to use

/// <Summary>
/// You should used JsonSerializer when you:
///
/// Have a POCO that matches the JSON data, or it’s easy to create one.
/// Need to use most of the properties of the JSON in your application code.
/// </Summary
  
var data = await JsonSerializer.DeserializeAsync<SomeObject>(req.Body);

// It’s much neater to deserialize the request body this way, and it avoids the unnecessary string allocation, since the serializer consumes the stream for you.

// The Deserialize method can also take a ReadOnlySpan of bytes as input. Much like the stream example above, you previously had to read the bytes into a string before deserializing it to JSON. Instead, if you’ve already got the data loaded in memory, this overload saves you a few allocations and parses the JSON directly into a POCO.

// It’s also worth nothing that some of the default options for System.Text.Json are different from Json.NET. System.Text.Json adheres to RFC 8259, so if you’re ever left wondering why a setting is different from Newtonsoft, that’s probably why.
Source by marcroussy.com #
 
PREVIOUS NEXT
Tagged: #DeserializeAsync
ADD COMMENT
Topic
Name
4+7 =