mirror of
https://github.com/tribufu/tribufu-dotnet
synced 2025-08-03 22:14:51 +00:00
Add json serialization configs
This commit is contained in:
20
src/Tribufu.Serialization/BaseClassFirstContractResolver.cs
Normal file
20
src/Tribufu.Serialization/BaseClassFirstContractResolver.cs
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
// Copyright (c) Tribufu. All Rights Reserved.
|
||||||
|
// SPDX-License-Identifier: UNLICENSED
|
||||||
|
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Newtonsoft.Json.Serialization;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
namespace Tribufu.Serialization
|
||||||
|
{
|
||||||
|
public class BaseClassFirstContractResolver : DefaultContractResolver
|
||||||
|
{
|
||||||
|
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
|
||||||
|
{
|
||||||
|
var props = base.CreateProperties(type, memberSerialization);
|
||||||
|
return props.OrderBy(p => { return p.DeclaringType == type ? 1 : 0; }).ToList();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
33
src/Tribufu.Serialization/SerializationConfiguration.cs
Normal file
33
src/Tribufu.Serialization/SerializationConfiguration.cs
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
// Copyright (c) Tribufu. All Rights Reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Newtonsoft.Json.Converters;
|
||||||
|
using Newtonsoft.Json.Serialization;
|
||||||
|
|
||||||
|
namespace Tribufu.Serialization
|
||||||
|
{
|
||||||
|
public static class SerializationConfiguration
|
||||||
|
{
|
||||||
|
public static JsonSerializerSettings GetNewtonsoftJsonSerializerSettings()
|
||||||
|
{
|
||||||
|
var settings = new JsonSerializerSettings
|
||||||
|
{
|
||||||
|
ContractResolver = new BaseClassFirstContractResolver
|
||||||
|
{
|
||||||
|
NamingStrategy = new SnakeCaseNamingStrategy()
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
settings.Converters.Add(new DecimalNullableStringConverter());
|
||||||
|
settings.Converters.Add(new DecimalStringConverter());
|
||||||
|
settings.Converters.Add(new ULongNullableStringConverter());
|
||||||
|
settings.Converters.Add(new ULongStringConverter());
|
||||||
|
settings.Converters.Add(new StringEnumConverter(new SnakeCaseNamingStrategy(), false));
|
||||||
|
|
||||||
|
settings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
|
||||||
|
|
||||||
|
return settings;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user