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...
}
}
IObjectSerializer (defined in the Volo.Abp.Serialization package, independently of the JSON system) serializes objects to and from byte[]. The default implementation uses UTF-8 JSON bytes from System.Text.Json:
public interface IObjectSerializer
{
byte[]? Serialize<T>(T? obj);
T? Deserialize<T>(byte[] bytes);
}
Inject IObjectSerializer when a storage or transport API works with bytes instead of strings. To customize serialization for a specific type, implement IObjectSerializer<T>. ABP automatically exposes conventionally registered implementations through the corresponding closed generic interface, and the default serializer uses that implementation for T:
public class ProductSerializer : IObjectSerializer<Product>, ITransientDependency
{
public byte[]? Serialize(Product? obj)
{
return obj is null ? null : JsonSerializer.SerializeToUtf8Bytes(obj);
}
public Product? Deserialize(byte[]? bytes)
{
return bytes is null ? null : JsonSerializer.Deserialize<Product>(bytes);
}
}
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
});