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.

JsonReaderExtensions.cs 6.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System;
  2. using System.Text;
  3. using System.Text.Json;
  4. using System.Text.Utf8;
  5. namespace Discord.Serialization
  6. {
  7. internal static class JsonReaderExtensions
  8. {
  9. public static bool GetBool(this JsonReader reader) => GetBool(reader.Value);
  10. public static bool GetBool(this ReadOnlySpan<byte> text)
  11. {
  12. if (PrimitiveParser.TryParseBoolean(text, out bool result, out int ignored, SymbolTable.InvariantUtf8))
  13. return result;
  14. throw new SerializationException("Failed to parse Boolean");
  15. }
  16. public static sbyte GetInt8(this JsonReader reader) => GetInt8(reader.Value);
  17. public static sbyte GetInt8(this ReadOnlySpan<byte> text)
  18. {
  19. if (PrimitiveParser.TryParseSByte(text, out sbyte result, out int ignored, JsonConstants.NumberFormat, SymbolTable.InvariantUtf8))
  20. return result;
  21. throw new SerializationException("Failed to parse Int8");
  22. }
  23. public static short GetInt16(this JsonReader reader) => GetInt16(reader.Value);
  24. public static short GetInt16(this ReadOnlySpan<byte> text)
  25. {
  26. if (PrimitiveParser.TryParseInt16(text, out short result, out int ignored, JsonConstants.NumberFormat, SymbolTable.InvariantUtf8))
  27. return result;
  28. throw new SerializationException("Failed to parse Int16");
  29. }
  30. public static int GetInt32(this JsonReader reader) => GetInt32(reader.Value);
  31. public static int GetInt32(this ReadOnlySpan<byte> text)
  32. {
  33. if (PrimitiveParser.TryParseInt32(text, out int result, out int ignored, JsonConstants.NumberFormat, SymbolTable.InvariantUtf8))
  34. return result;
  35. throw new SerializationException("Failed to parse Int32");
  36. }
  37. public static long GetInt64(this JsonReader reader) => GetInt64(reader.Value);
  38. public static long GetInt64(this ReadOnlySpan<byte> text)
  39. {
  40. if (PrimitiveParser.TryParseInt64(text, out long result, out int ignored, JsonConstants.NumberFormat, SymbolTable.InvariantUtf8))
  41. return result;
  42. throw new SerializationException("Failed to parse Int64");
  43. }
  44. public static byte GetUInt8(this JsonReader reader) => GetUInt8(reader.Value);
  45. public static byte GetUInt8(this ReadOnlySpan<byte> text)
  46. {
  47. if (PrimitiveParser.TryParseByte(text, out byte result, out int ignored, JsonConstants.NumberFormat, SymbolTable.InvariantUtf8))
  48. return result;
  49. throw new SerializationException("Failed to parse UInt8");
  50. }
  51. public static ushort GetUInt16(this JsonReader reader) => GetUInt16(reader.Value);
  52. public static ushort GetUInt16(this ReadOnlySpan<byte> text)
  53. {
  54. if (PrimitiveParser.TryParseUInt16(text, out ushort result, out int ignored, JsonConstants.NumberFormat, SymbolTable.InvariantUtf8))
  55. return result;
  56. throw new SerializationException("Failed to parse UInt16");
  57. }
  58. public static uint GetUInt32(this JsonReader reader) => GetUInt32(reader.Value);
  59. public static uint GetUInt32(this ReadOnlySpan<byte> text)
  60. {
  61. if (PrimitiveParser.TryParseUInt32(text, out uint result, out int ignored, JsonConstants.NumberFormat, SymbolTable.InvariantUtf8))
  62. return result;
  63. throw new SerializationException("Failed to parse UInt32");
  64. }
  65. public static ulong GetUInt64(this JsonReader reader) => GetUInt64(reader.Value);
  66. public static ulong GetUInt64(this ReadOnlySpan<byte> text)
  67. {
  68. if (PrimitiveParser.TryParseUInt64(text, out ulong result, out int ignored, JsonConstants.NumberFormat, SymbolTable.InvariantUtf8))
  69. return result;
  70. throw new SerializationException("Failed to parse UInt64");
  71. }
  72. public static char GetChar(this JsonReader reader) => GetChar(reader.Value);
  73. public static char GetChar(this ReadOnlySpan<byte> text)
  74. {
  75. string str = GetString(text);
  76. if (str.Length == 1)
  77. return str[0];
  78. throw new SerializationException("Failed to parse Char");
  79. }
  80. public static string GetString(this JsonReader reader) => GetString(reader.Value);
  81. public static string GetString(this ReadOnlySpan<byte> text) => new Utf8String(text).ToString();
  82. public static float GetSingle(this JsonReader reader) => GetSingle(reader.Value);
  83. public static float GetSingle(this ReadOnlySpan<byte> text)
  84. {
  85. if (PrimitiveParser.TryParseDecimal(text, out decimal result, out int ignored, SymbolTable.InvariantUtf8))
  86. return (float)result;
  87. throw new SerializationException("Failed to parse Single");
  88. }
  89. public static double GetDouble(this JsonReader reader) => GetDouble(reader.Value);
  90. public static double GetDouble(this ReadOnlySpan<byte> text)
  91. {
  92. if (PrimitiveParser.TryParseDecimal(text, out decimal result, out int ignored, SymbolTable.InvariantUtf8))
  93. return (double)result;
  94. throw new SerializationException("Failed to parse Double");
  95. }
  96. public static decimal GetDecimal(this JsonReader reader) => GetDecimal(reader.Value);
  97. public static decimal GetDecimal(this ReadOnlySpan<byte> text)
  98. {
  99. if (PrimitiveParser.TryParseDecimal(text, out decimal result, out int ignored, SymbolTable.InvariantUtf8))
  100. return result;
  101. throw new SerializationException("Failed to parse Decimal");
  102. }
  103. public static DateTime GetDateTime(this JsonReader reader) => GetDateTime(reader.Value);
  104. public static DateTime GetDateTime(this ReadOnlySpan<byte> text)
  105. {
  106. string str = GetString(text);
  107. if (DateTime.TryParse(str, out var result)) //TODO: Improve perf
  108. return result;
  109. throw new SerializationException("Failed to parse DateTime");
  110. }
  111. public static DateTimeOffset GetDateTimeOffset(this JsonReader reader) => GetDateTimeOffset(reader.Value);
  112. public static DateTimeOffset GetDateTimeOffset(this ReadOnlySpan<byte> text)
  113. {
  114. string str = GetString(text);
  115. if (DateTimeOffset.TryParse(str, out var result)) //TODO: Improve perf
  116. return result;
  117. throw new SerializationException("Failed to parse DateTimeOffset");
  118. }
  119. public static void Skip(this JsonReader reader)
  120. {
  121. int initialDepth = reader._depth;
  122. while (reader.Read() && reader._depth > initialDepth) { }
  123. }
  124. }
  125. }