Version
Google Translate

JSON

The VCP 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 VCP without worrying about the underlying library's internal details.

The JSON serialization system is implemented with the Verto.Vcp.Json NuGet package(Verto.Vcp.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

VcpJsonOptions

VcpJsonOptions type provides options for the JSON operations in the VCP.

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

VcpSystemTextJsonSerializerOptions

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

VcpSystemTextJsonSerializerModifiersOptions

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

Newtonsoft

Add Verto.Vcp.Json.Newtonsoft package and depends on VcpJsonNewtonsoftModule to replace the System Text Json.

VcpNewtonsoftJsonSerializerOptions

  • 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)

In this document