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

Primitives.Unsigned.cs 2.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System.Text.Json;
  2. namespace Discord.Serialization.Json.Converters
  3. {
  4. internal class UInt8PropertyConverter : IJsonPropertyConverter<byte>
  5. {
  6. public byte Read(JsonReader reader, bool read = true)
  7. {
  8. if (read)
  9. reader.Read();
  10. if (reader.ValueType != JsonValueType.Number)
  11. throw new SerializationException("Bad input, expected Number");
  12. return reader.ParseUInt8();
  13. }
  14. public void Write(JsonWriter writer, byte value)
  15. => writer.WriteValue(value);
  16. }
  17. internal class UInt16PropertyConverter : IJsonPropertyConverter<ushort>
  18. {
  19. public ushort Read(JsonReader reader, bool read = true)
  20. {
  21. if (read)
  22. reader.Read();
  23. if (reader.ValueType != JsonValueType.Number)
  24. throw new SerializationException("Bad input, expected Number");
  25. return reader.ParseUInt16();
  26. }
  27. public void Write(JsonWriter writer, ushort value)
  28. => writer.WriteValue(value);
  29. }
  30. internal class UInt32PropertyConverter : IJsonPropertyConverter<uint>
  31. {
  32. public uint Read(JsonReader reader, bool read = true)
  33. {
  34. if (read)
  35. reader.Read();
  36. if (reader.ValueType != JsonValueType.Number)
  37. throw new SerializationException("Bad input, expected Number");
  38. return reader.ParseUInt32();
  39. }
  40. public void Write(JsonWriter writer, uint value)
  41. => writer.WriteValue(value);
  42. }
  43. internal class UInt64PropertyConverter : IJsonPropertyConverter<ulong>
  44. {
  45. public ulong Read(JsonReader reader, bool read = true)
  46. {
  47. if (read)
  48. reader.Read();
  49. if (reader.ValueType != JsonValueType.String)
  50. throw new SerializationException("Bad input, expected String");
  51. return reader.ParseUInt64();
  52. }
  53. public void Write(JsonWriter writer, ulong value)
  54. => writer.WriteValue(value.ToString());
  55. }
  56. }