You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

38 lines
1.2 KiB

using System;
using System.Text.Json;
using System.Text.Json.Serialization;
using Znyc.Cloudcar.Admin.Commons.Extensions;
namespace Znyc.Cloudcar.Admin.Commons.Helpers
{
public class DecimalJsonConverter : JsonConverter<decimal>
{
/// <summary>
/// </summary>
/// <param name="reader"></param>
/// <param name="typeToConvert"></param>
/// <param name="options"></param>
/// <returns></returns>
public override decimal Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Number)
{
return reader.GetDecimal();
}
return reader.GetString().Equals("")
? "0.00".ToDecimal()
: reader.GetString().ToDecimal();
}
/// <summary>
/// </summary>
/// <param name="writer"></param>
/// <param name="value"></param>
/// <param name="options"></param>
public override void Write(Utf8JsonWriter writer, decimal value, JsonSerializerOptions options)
{
writer.WriteNumberValue(value);
}
}
}