mirror of
https://github.com/Tribufu/TribufuDotNet
synced 2026-08-07 20:06:22 +00:00
* Migrate to new tribufu project structure * Update .gitignore * Remove old api files * Update Directory.Build.props * Rename solution
31 lines
984 B
C#
31 lines
984 B
C#
// Copyright (c) Tribufu. All Rights Reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
using Newtonsoft.Json;
|
|
using System;
|
|
|
|
namespace Tribufu.Serialization
|
|
{
|
|
public class ULongStringConverter : JsonConverter<ulong>
|
|
{
|
|
public override ulong ReadJson(JsonReader reader, Type objectType, ulong existingValue, bool hasExistingValue, JsonSerializer serializer)
|
|
{
|
|
if (reader.TokenType == JsonToken.String && ulong.TryParse(reader.Value?.ToString(), out var result))
|
|
{
|
|
return result;
|
|
}
|
|
|
|
if (reader.TokenType == JsonToken.Integer)
|
|
{
|
|
return Convert.ToUInt64(reader.Value);
|
|
}
|
|
|
|
throw new JsonSerializationException($"Unexpected token {reader.TokenType} when parsing ulong.");
|
|
}
|
|
|
|
public override void WriteJson(JsonWriter writer, ulong value, JsonSerializer serializer)
|
|
{
|
|
writer.WriteValue(value.ToString());
|
|
}
|
|
}
|
|
}
|