mirror of https://github.com/abpframework/abp
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
3.0 KiB
3.0 KiB
JSON
The ABP Framework 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. 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 Framework.
Properties:
- DefaultDateTimeFormat(
string): DefaultDateTimeformat. - UseHybridSerializer(
bool): True by default. Boolean field indicating whether the ABP Framework uses the hybrid approach or not. If the field is true, it will try to useSystem.Json.Textto handle JSON if it can otherwise use theNewtonsoft.Json. - Providers(
ITypeList<IJsonSerializerProvider>): List of JSON serializer providers implementing theIJsonSerializerProviderinterface. You can create and add custom serializers to the list, and the ABP Framework uses them automatically. When theSerializeorDeserializemethod is called on theIJsonSerializerinterface, the ABP Framework calls theCanHandlemethods of the given providers in reverse order and uses the first provider that returnstrueto do the JSON operation.
AbpSystemTextJsonSerializerOptions
AbpSystemTextJsonSerializerOptions provides options for System.Text.Json usage.
Properties:
- JsonSerializerOptions(
System.Text.Json.JsonSerializerOptions): Global options for System.Text.Json library operations. See here for reference. - UnsupportedTypes(
ITypeList): List of the unsupported types. You can add types of the unsupported types to the list and, the hybrid JSON serializer automatically uses theNewtonsoft.Jsonlibrary instead ofSystem.Text.Json.