JSON

The ABP provides an abstraction to work with JSON. Having such an abstraction has some benefits;

  • You can write library independent code. Therefore, you can change the underlying library with the minimum effort and code change.
  • You can use the predefined converters defined in the ABP without worrying about the underlying library's internal details.

The JSON serialization system is implemented with the Volo.Abp.Json NuGet package(Volo.Abp.Json.SystemTextJson is the default implementation). Most of the time, you don't need to manually install it since it comes pre-installed with the application startup template.

IJsonSerializer

You can inject IJsonSerializer and use it for JSON operations. Here is the available operations in the IJsonSerializer interface.

public interface IJsonSerializer
{
    string Serialize(object obj, bool camelCase = true, bool indented = false);
    T Deserialize<T>(string jsonString, bool camelCase = true);
    object Deserialize(Type type, string jsonString, bool camelCase = true);
}

Usage Example:

public class ProductManager
{
    public IJsonSerializer JsonSerializer { get; }

    public ProductManager(IJsonSerializer jsonSerializer)
    {
        JsonSerializer = jsonSerializer;
    }

    public void SendRequest(Product product)
    {
        var json=  JsonSerializer.Serialize(product);
        // Left blank intentionally for demo purposes...
    }
}

Configuration

AbpJsonOptions

AbpJsonOptions type provides options for the JSON operations in the ABP.

Properties:

  • InputDateTimeFormats(List<string>): Formats of input JSON date, Empty string means default format. You can provide multiple formats to parse the date.
  • OutputDateTimeFormat(string): Format of output json date, Null or empty string means default format.

System Text Json

AbpSystemTextJsonSerializerOptions

  • JsonSerializerOptions(System.Text.Json.JsonSerializerOptions): Global options for System.Text.Json library operations. See here for reference.

AbpSystemTextJsonSerializerModifiersOptions

  • Modifiers(List<Action<JsonTypeInfo>>): Configure Modifiers of DefaultJsonTypeInfoResolver. See here for reference.

Newtonsoft

Add Volo.Abp.Json.Newtonsoft package and depends on AbpJsonNewtonsoftModule to replace the System Text Json.

AbpNewtonsoftJsonSerializerOptions

  • JsonSerializerSettings(Newtonsoft.Json.JsonSerializerSettings): Global options for Newtonsoft library operations. See here for reference.

Configuring JSON options in ASP.NET Core

You can change the JSON behavior in ASP.NET Core by configuring JsonOptions or MvcNewtonsoftJsonOptions(if you use Newtonsoft.Json)

Contributors


Last updated: October 10, 2024 Edit this page on GitHub

Was this page helpful?

Please make a selection.

To help us improve, please share your reason for the negative feedback in the field below.

Please enter a note.

Thank you for your valuable feedback!

Please note that although we cannot respond to feedback, our team will use your comments to improve the experience.

In this document
Community Talks

What’s New with .NET 9 & ABP 9?

21 Nov, 17:00
Online
Register Now
Mastering ABP Framework Book
Mastering ABP Framework

This book will help you gain a complete understanding of the framework and modern web application development techniques.

Learn More