25 Nov’19

Dot NetHow to use System.Text.Json APIs in Asp.net

Initially for parsing object to Json or json to object in Asp.net an additional Newtonsoft.Json Api was required, but now Microsoft has developed their own Apis “System.Test.Json”. Below Steps will guide you on how to use this api.

  1. Install the System.Text.Json NuGet package.
  2. To use the api make sure you import the following two namespaces:
    1. using System.Text.Json;
    2. using System.Text.Json.Serialization;
  3. Using the serializer as follows:
    1. The System.Text.Json serializer can read and write JSON asynchronously and is optimized for UTF-8 text, making it ideal for REST API and back-end applications.

class WeatherForecast

{

public DateTimeOffset Date {get; set;}

public int TemperatureC {get; set;}

public string Summary {get; set;}

}

 

string Serialize (WeatherForecast value)

{

return JsonSerializer.ToString<WeatherForecast>(value);

}

  1. Api also support asynchronous serialization and deserialization.

async Task SerializeAsync(WeatherForecast value, Stream stream)

{

await JsonSerializer.WriteAsync<WeatherForecast> (value, stream);

}

  1. You can also use custom attributes to control serialization behavior, for example, ignoring properties and specifying the name of the property in the JSON:

class WeatherForecast

{

public DateTimeOffset Date {get; set;}

 

// Always in Celsius.

[JsonPropertyName(“temp”)]

public int TemperatureC {get; set;}

 

public string Summary {get; set;}

 

// Don’t serialize this property.

[JsonIgnore]

public bool IsHot => TemperatureC >= 30;

}

Note: In the above point isHot property will not get parsed to json and json will look like below:

     {

“date”: “2013-01-07T00:00:00Z”,

“temp”: 23,

}

Written by

Tejesh Sharma

D365 CRM Consultant

Leave a Reply

Your email address will not be published. Required fields are marked *

Want to streamline your business processes?

  • This field is for validation purposes and should be left unchanged.

Recent Articles

  • How to create Dynamics 365 Finance & Operations (formerly Dynamics AX) connection in SSIS

    12 December’ 2019

    Introduction: Creating a Connection is one of the first Steps during the integration of data. While performing Integrati...

    Read more
  • How to create AX (D365 F&0) Connection in SSIS

    12 December’ 2019

    Introduction: Creating a Connection is one of the first Steps during integration of data. While performing Integration w...

    Read more
  • First cut review of new D365 Project Service

    12 December’ 2019

    The new Project Service coming up is a much-advanced version of PSA. Even more, than it will be called as Project Servic...

    Read more
  • Business Central Wave 2 Features – Section 4

    11 December’ 2019

    Introduction: In this blog, I will be attempting to provide information about the new Modern Client features of Business...

    Read more
  • Business Central Wave 2 Features – Section 5

    11 December’ 2019

    Introduction: In this blog, I will be attempting to provide information about the new Modern Client features of Business...

    Read more

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close