docs/en/framework/infrastructure/json.md
//[doc-seo]
{
"Description": "Learn how to use ABP's JSON abstraction for flexible, library-independent serialization with predefined converters for seamless integration."
}
The ABP provides an abstraction to work with JSON. Having such an abstraction has some benefits;
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.
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...
}
}
AbpJsonOptions type provides options for the JSON operations in the ABP.
Properties:
List<string>): Formats of input JSON date, Empty string means default format. You can provide multiple formats to parse the date.string): Format of output json date, Null or empty string means default format.System.Text.Json.JsonSerializerOptions): Global options for System.Text.Json library operations. See here for reference.List<Action<JsonTypeInfo>>): Configure Modifiers of DefaultJsonTypeInfoResolver. See here for reference.Add Volo.Abp.Json.Newtonsoft package and depends on AbpJsonNewtonsoftModule to replace the System Text Json.
Newtonsoft.Json.JsonSerializerSettings): Global options for Newtonsoft library operations. See here for reference.Configuring JSON options in ABP does not affect ASP.NET Core's JSON settings. To modify ASP.NET Core JSON behavior, configure JsonOptions or MvcNewtonsoftJsonOptions (if you're using Newtonsoft.Json) separately.
Example:
Configure<JsonOptions>(options =>
{
//options.SerializerOptions
});
// If you use Newtonsoft.Json
Configure<MvcNewtonsoftJsonOptions>(options =>
{
//options.SerializerSettings
});