elsa-core/src/scripting/Elsa.Scripting.JavaScript/Converters/TruncatingNumberJsonConverter.cs

33 lines
1.5 KiB
C#
Raw Normal View History

2020-10-25 20:37:15 +00:00
using System;
using Newtonsoft.Json;
namespace Elsa.Scripting.JavaScript.Converters
{
/// <summary>
/// Ensures that whole numeric values are serialized without any decimal (e.g. 2 instead of 2.0) to ensure deserialization to models having int properties works.
/// </summary>
public class TruncatingNumberJsonConverter : JsonConverter
{
public override bool CanRead => false;
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) => throw new NotImplementedException("Unnecessary because CanRead is false. The type will skip the converter.");
2020-10-25 20:37:15 +00:00
public override bool CanConvert(Type objectType) => objectType == typeof(decimal) || objectType == typeof(float) || objectType == typeof(double);
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) => writer.WriteRawValue(IsWholeValue(value) ? JsonConvert.ToString(Convert.ToInt64(value)) : JsonConvert.ToString(value));
private static bool IsWholeValue(object value)
{
if (value is decimal decimalValue)
{
var precision = (decimal.GetBits(decimalValue)[3] >> 16) & 0x000000FF;
return precision == 0;
}
if (value is float || value is double)
{
2020-10-25 20:37:15 +00:00
var doubleValue = Convert.ToDouble(value);
return doubleValue == Math.Truncate(doubleValue);
}
return false;
}
}
}