using System;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Myshipping.Core;
///
/// Long 类型Json返回处理
///
public class LongJsonConverter : JsonConverter
{
/// Reads and converts the JSON to type .
/// The reader.
/// The type to convert.
/// An object that specifies serialization options to use.
/// The converted value.
public override long Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
// 这里做处理,前端传入的Long类型可能为String类型,或者Number类型。
return reader.TokenType == JsonTokenType.String ? long.Parse(reader.GetString()) : reader.GetInt64();
}
/// Writes a specified value as JSON.
/// The writer to write to.
/// The value to convert to JSON.
/// An object that specifies serialization options to use.
public override void Write(Utf8JsonWriter writer, long value, JsonSerializerOptions options)
{
writer.WriteStringValue($"{value}");
}
}