Table of Contents

JsonSerializerExtensions Class

Definition

Namespace
Savvyio.Extensions.Newtonsoft.Json
Assemblies
Savvyio.Extensions.Newtonsoft.Json.dll
Source
src/Savvyio.Extensions.Newtonsoft.Json/JsonSerializerExtensions.cs

Provides extension methods for the Newtonsoft.Json.JsonSerializer class.

public static class JsonSerializerExtensions
Inheritance
object
JsonSerializerExtensions

Examples

Use ResolvePropertyKeyByConvention and ResolveDictionaryKeyByConvention to derive JSON property names and dictionary key names from the serializer's naming strategy.

using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Savvyio.Extensions.Newtonsoft.Json;

namespace ExampleApp;

public static class NamingConventions
{
    public static (string PropertyKey, string DictionaryKey) ResolveKeys()
    {
        var serializer = JsonSerializer.Create(new JsonSerializerSettings
        {
            ContractResolver = new DefaultContractResolver
            {
                NamingStrategy = new SnakeCaseNamingStrategy()
            }
        });

        var propertyKey = serializer.ResolvePropertyKeyByConvention(nameof(OrderProjection.OrderId));
        var dictionaryKey = serializer.ResolveDictionaryKeyByConvention(nameof(OrderProjection.OrderId));
        return (propertyKey, dictionaryKey);
    }
}

public sealed class OrderProjection
{
    public string OrderId { get; init; } = string.Empty;
}

Methods

Name Description
ResolveDictionaryKeyByConvention(JsonSerializer, string)

Resolves the dictionary key by convention using the specified Newtonsoft.Json.JsonSerializer.

ResolvePropertyKeyByConvention(JsonSerializer, string)

Resolves the property key by convention using the specified Newtonsoft.Json.JsonSerializer.