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.1 KiB

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