mirror of
https://github.com/Tribufu/TribufuDotNet
synced 2026-08-10 05:11:07 +00:00
Migrate to new tribufu project structure (#2)
* Migrate to new tribufu project structure * Update .gitignore * Remove old api files * Update Directory.Build.props * Rename solution
This commit is contained in:
parent
be25ee8fe3
commit
7e8e192dc3
115 changed files with 41 additions and 262 deletions
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
35
Source/Tribufu.Serialization/CustomSerializerSettings.cs
Normal file
35
Source/Tribufu.Serialization/CustomSerializerSettings.cs
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
// 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 CustomSerializerSettings
|
||||
{
|
||||
public static JsonSerializerSettings GetNewtonsoftJson()
|
||||
{
|
||||
var settings = new JsonSerializerSettings();
|
||||
SetNewtonsoftJson(settings);
|
||||
return settings;
|
||||
}
|
||||
|
||||
public static void SetNewtonsoftJson(JsonSerializerSettings settings)
|
||||
{
|
||||
settings.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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,46 @@
|
|||
// Copyright (c) Tribufu. All Rights Reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Globalization;
|
||||
|
||||
namespace Tribufu.Serialization
|
||||
{
|
||||
public class DecimalNullableStringConverter : JsonConverter<decimal?>
|
||||
{
|
||||
public override decimal? ReadJson(JsonReader reader, Type objectType, decimal? existingValue, bool hasExistingValue, JsonSerializer serializer)
|
||||
{
|
||||
if (reader.TokenType == JsonToken.Null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (reader.TokenType == JsonToken.String || reader.TokenType == JsonToken.Integer)
|
||||
{
|
||||
string value = reader.Value?.ToString();
|
||||
|
||||
if (string.IsNullOrWhiteSpace(value))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return decimal.Parse(value);
|
||||
}
|
||||
|
||||
throw new JsonSerializationException($"Unexpected token {reader.TokenType} when parsing decimal?.");
|
||||
}
|
||||
|
||||
public override void WriteJson(JsonWriter writer, decimal? value, JsonSerializer serializer)
|
||||
{
|
||||
if (value.HasValue)
|
||||
{
|
||||
writer.WriteValue(value.Value.ToString(CultureInfo.InvariantCulture));
|
||||
}
|
||||
else
|
||||
{
|
||||
writer.WriteNull();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
32
Source/Tribufu.Serialization/DecimalStringConverter.cs
Normal file
32
Source/Tribufu.Serialization/DecimalStringConverter.cs
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
// Copyright (c) Tribufu. All Rights Reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Globalization;
|
||||
|
||||
namespace Tribufu.Serialization
|
||||
{
|
||||
public class DecimalStringConverter : JsonConverter<decimal>
|
||||
{
|
||||
public override decimal ReadJson(JsonReader reader, Type objectType, decimal existingValue, bool hasExistingValue, JsonSerializer serializer)
|
||||
{
|
||||
if (reader.TokenType == JsonToken.String && decimal.TryParse(reader.Value?.ToString(), out var result))
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
if (reader.TokenType == JsonToken.Integer)
|
||||
{
|
||||
return Convert.ToUInt64(reader.Value);
|
||||
}
|
||||
|
||||
throw new JsonSerializationException($"Unexpected token {reader.TokenType} when parsing decimal.");
|
||||
}
|
||||
|
||||
public override void WriteJson(JsonWriter writer, decimal value, JsonSerializer serializer)
|
||||
{
|
||||
writer.WriteValue(value.ToString(CultureInfo.InvariantCulture));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
// Copyright (c) Tribufu. All Rights Reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Tribufu.Serialization
|
||||
{
|
||||
public class NetworkAwareSnakeCaseNamingStrategy : SnakeCaseNamingStrategy
|
||||
{
|
||||
private static readonly string[] KnownAcronyms = new string[] { "IPv4", "IPv6" };
|
||||
|
||||
protected override string ResolvePropertyName(string name)
|
||||
{
|
||||
if (string.IsNullOrEmpty(name))
|
||||
{
|
||||
return name;
|
||||
}
|
||||
|
||||
foreach (var acr in KnownAcronyms)
|
||||
{
|
||||
name = Regex.Replace(name, acr, acr.ToLower());
|
||||
}
|
||||
|
||||
var snake = base.ResolvePropertyName(name);
|
||||
foreach (var acr in KnownAcronyms)
|
||||
{
|
||||
var lower = acr.ToLower();
|
||||
snake = snake.Replace(lower.Replace("_", ""), lower);
|
||||
}
|
||||
|
||||
return snake;
|
||||
}
|
||||
}
|
||||
}
|
||||
1
Source/Tribufu.Serialization/README.md
Normal file
1
Source/Tribufu.Serialization/README.md
Normal file
|
|
@ -0,0 +1 @@
|
|||
# Tribufu Serialization
|
||||
20
Source/Tribufu.Serialization/Tribufu.Serialization.csproj
Normal file
20
Source/Tribufu.Serialization/Tribufu.Serialization.csproj
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<PackageId>Tribufu.Serialization</PackageId>
|
||||
<Description>Tribufu Serialization</Description>
|
||||
<PackageReadmeFile>README.md</PackageReadmeFile>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<IsPublishable>false</IsPublishable>
|
||||
<OutputType>Library</OutputType>
|
||||
<TargetFrameworks>netstandard2.0;net45;net5.0</TargetFrameworks>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<None Include="README.md" Pack="true" PackagePath="\" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Newtonsoft.Json" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
45
Source/Tribufu.Serialization/ULongNullableStringConverter.cs
Normal file
45
Source/Tribufu.Serialization/ULongNullableStringConverter.cs
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
// Copyright (c) Tribufu. All Rights Reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
|
||||
namespace Tribufu.Serialization
|
||||
{
|
||||
public class ULongNullableStringConverter : JsonConverter<ulong?>
|
||||
{
|
||||
public override ulong? ReadJson(JsonReader reader, Type objectType, ulong? existingValue, bool hasExistingValue, JsonSerializer serializer)
|
||||
{
|
||||
if (reader.TokenType == JsonToken.Null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (reader.TokenType == JsonToken.String || reader.TokenType == JsonToken.Integer)
|
||||
{
|
||||
string value = reader.Value?.ToString();
|
||||
|
||||
if (string.IsNullOrWhiteSpace(value))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return ulong.Parse(value);
|
||||
}
|
||||
|
||||
throw new JsonSerializationException($"Unexpected token {reader.TokenType} when parsing ulong?.");
|
||||
}
|
||||
|
||||
public override void WriteJson(JsonWriter writer, ulong? value, JsonSerializer serializer)
|
||||
{
|
||||
if (value.HasValue)
|
||||
{
|
||||
writer.WriteValue(value.Value.ToString());
|
||||
}
|
||||
else
|
||||
{
|
||||
writer.WriteNull();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
31
Source/Tribufu.Serialization/ULongStringConverter.cs
Normal file
31
Source/Tribufu.Serialization/ULongStringConverter.cs
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
// Copyright (c) Tribufu. All Rights Reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
|
||||
namespace Tribufu.Serialization
|
||||
{
|
||||
public class ULongStringConverter : JsonConverter<ulong>
|
||||
{
|
||||
public override ulong ReadJson(JsonReader reader, Type objectType, ulong existingValue, bool hasExistingValue, JsonSerializer serializer)
|
||||
{
|
||||
if (reader.TokenType == JsonToken.String && ulong.TryParse(reader.Value?.ToString(), out var result))
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
if (reader.TokenType == JsonToken.Integer)
|
||||
{
|
||||
return Convert.ToUInt64(reader.Value);
|
||||
}
|
||||
|
||||
throw new JsonSerializationException($"Unexpected token {reader.TokenType} when parsing ulong.");
|
||||
}
|
||||
|
||||
public override void WriteJson(JsonWriter writer, ulong value, JsonSerializer serializer)
|
||||
{
|
||||
writer.WriteValue(value.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue