12345678910111213141516171819202122232425262728293031323334353637 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Text.Json;
- using System.Text.Json.Serialization;
- namespace Long.Utils.Converter
- {
- public class LongJsonConverter : JsonConverter<long>
- {
- public override long Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
- {
- if (reader.TokenType == JsonTokenType.String)
- {
- string @string = reader.GetString() ?? "";
- if (long.TryParse(@string, out var result))
- {
- return result;
- }
- return 0L;
- }
- if (reader.TokenType != JsonTokenType.Number)
- {
- return 0L;
- }
- return reader.GetInt64();
- }
- public override void Write(Utf8JsonWriter writer, long value, JsonSerializerOptions options)
- {
- writer.WriteStringValue(value.ToString());
- }
- }
- }
|