mirror of
https://github.com/tribufu/tribufu-dotnet
synced 2026-02-04 18:59:06 +00:00
Add shared packages
This commit is contained in:
46
src/Tribufu.Serialization/DecimalNullableStringConverter.cs
Normal file
46
src/Tribufu.Serialization/DecimalNullableStringConverter.cs
Normal file
@@ -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
src/Tribufu.Serialization/DecimalStringConverter.cs
Normal file
32
src/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));
|
||||
}
|
||||
}
|
||||
}
|
||||
1
src/Tribufu.Serialization/README.md
Normal file
1
src/Tribufu.Serialization/README.md
Normal file
@@ -0,0 +1 @@
|
||||
# Tribufu
|
||||
19
src/Tribufu.Serialization/Tribufu.Serialization.csproj
Normal file
19
src/Tribufu.Serialization/Tribufu.Serialization.csproj
Normal file
@@ -0,0 +1,19 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<PackageId>Tribufu.Serialization</PackageId>
|
||||
<Description>Tribufu Serialization Extensions</Description>
|
||||
<PackageReadmeFile>README.md</PackageReadmeFile>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<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
src/Tribufu.Serialization/ULongNullableStringConverter.cs
Normal file
45
src/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
src/Tribufu.Serialization/ULongStringConverter.cs
Normal file
31
src/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());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user