mirror of
https://github.com/Tribufu/TribufuDotNet
synced 2026-08-08 20:31:06 +00:00
Add shared packages
This commit is contained in:
parent
6ebfde013a
commit
5b2588b47f
32 changed files with 981 additions and 7 deletions
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue