Utf8JsonReader.ToString 给出:无法获取令牌类型的值 'None'
Utf8JsonReader.ToString gives: Cannot get the value of a token type 'None'
请注意,这是 System.Text.Json 而不是 Json.Net,因此 不是重复的。
我喜欢对我的自定义 JsonConverter 进行单元测试:
using System.Text.Json;
public class DateTimeShortConverter : JsonConverter<DateTime>
{
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
// support yyyy-MM-dd but also with times
return DateTime.Parse(reader.GetString());
}
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString("yyyy-MM-dd"));
}
我需要一个 Utf8JsonReader
并且我需要用 string
喂那个。
我试过这个:
byte[] bytes = Encoding.UTF8.GetBytes("2019-01-02");
var reader = new Utf8JsonReader(bytes.AsSpan());
这似乎有效,但在执行 reader.GetString()
:
时转换器会崩溃
System.InvalidOperationException : Cannot get the value of a token type 'None' as a string.
at System.Text.Json.Utf8JsonReader.GetString()
reader.GetString()
应该是正确的,see Microsoft's example,所以我想我把Utf8JsonReader
喂错了。
reader.Position
是只读的,因此这不是一个选项。我怎样才能正确地为 Utf8JsonReader 提供字符串?
完整测试:
using System;
using System.Text;
using System.Text.Json;
Using Xunit;
[Fact]
public void ReadDateTimeTests()
{
// Arrange
var input = "2019-01-02";
var dateTimeShortConverter = new DateTimeShortConverter();
byte[] bytes = Encoding.UTF8.GetBytes(input);
var reader = new Utf8JsonReader(bytes.AsSpan());
Type typeToConvert = typeof(DateTime);
JsonSerializerOptions options = new JsonSerializerOptions();
var expected = new DateTime(2019, 01, 02);
// Act
var result = dateTimeShortConverter.Read(ref reader, typeToConvert, options);
// Assert
Assert.Equal(expected, result);
}
更新:这也没有回答我的问题:。我也试过(从那个答案)没有运气:
byte[] bytes = Encoding.UTF8.GetBytes(input);
var reader = new Utf8JsonReader(bytes.AsSpan(), false, new JsonReaderState(new JsonReaderOptions()));
更新
"2019-01-02"
在 C# 中是不正确的 JSON,但是 "\"2019-01-02\""
是并且它仍然给出相同的错误。
找到问题了,
首先 "2019-01-02"
是有效的 JSON,但不是 2019-01-02
,所以我需要在那里逃脱。
之后,我需要读一读字符串的引号:reader.Read();
所以这有效:
using System;
using System.Text;
using System.Text.Json;
Using Xunit;
[Fact]
public void ReadDateTimeTests2()
{
// Arrange
var input = "\"2019-01-02\"";
var dateTimeShortConverter = new DateTimeShortConverter();
byte[] bytes = Encoding.UTF8.GetBytes(input);
var reader = new Utf8JsonReader(bytes.AsSpan());
reader.Read(); // Read the quote
Type typeToConvert = typeof(DateTime);
JsonSerializerOptions options = new JsonSerializerOptions();
var expected = new DateTime(2019, 01, 02);
// Act
var result = dateTimeShortConverter.Read(ref reader, typeToConvert, options);
// Assert
Assert.Equal(expected, result);
}
请注意,这是 System.Text.Json 而不是 Json.Net,因此
我喜欢对我的自定义 JsonConverter 进行单元测试:
using System.Text.Json;
public class DateTimeShortConverter : JsonConverter<DateTime>
{
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
// support yyyy-MM-dd but also with times
return DateTime.Parse(reader.GetString());
}
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString("yyyy-MM-dd"));
}
我需要一个 Utf8JsonReader
并且我需要用 string
喂那个。
我试过这个:
byte[] bytes = Encoding.UTF8.GetBytes("2019-01-02");
var reader = new Utf8JsonReader(bytes.AsSpan());
这似乎有效,但在执行 reader.GetString()
:
System.InvalidOperationException : Cannot get the value of a token type 'None' as a string. at System.Text.Json.Utf8JsonReader.GetString()
reader.GetString()
应该是正确的,see Microsoft's example,所以我想我把Utf8JsonReader
喂错了。
reader.Position
是只读的,因此这不是一个选项。我怎样才能正确地为 Utf8JsonReader 提供字符串?
完整测试:
using System;
using System.Text;
using System.Text.Json;
Using Xunit;
[Fact]
public void ReadDateTimeTests()
{
// Arrange
var input = "2019-01-02";
var dateTimeShortConverter = new DateTimeShortConverter();
byte[] bytes = Encoding.UTF8.GetBytes(input);
var reader = new Utf8JsonReader(bytes.AsSpan());
Type typeToConvert = typeof(DateTime);
JsonSerializerOptions options = new JsonSerializerOptions();
var expected = new DateTime(2019, 01, 02);
// Act
var result = dateTimeShortConverter.Read(ref reader, typeToConvert, options);
// Assert
Assert.Equal(expected, result);
}
更新:这也没有回答我的问题:
byte[] bytes = Encoding.UTF8.GetBytes(input);
var reader = new Utf8JsonReader(bytes.AsSpan(), false, new JsonReaderState(new JsonReaderOptions()));
更新
"2019-01-02"
在 C# 中是不正确的 JSON,但是 "\"2019-01-02\""
是并且它仍然给出相同的错误。
找到问题了,
首先 "2019-01-02"
是有效的 JSON,但不是 2019-01-02
,所以我需要在那里逃脱。
之后,我需要读一读字符串的引号:reader.Read();
所以这有效:
using System;
using System.Text;
using System.Text.Json;
Using Xunit;
[Fact]
public void ReadDateTimeTests2()
{
// Arrange
var input = "\"2019-01-02\"";
var dateTimeShortConverter = new DateTimeShortConverter();
byte[] bytes = Encoding.UTF8.GetBytes(input);
var reader = new Utf8JsonReader(bytes.AsSpan());
reader.Read(); // Read the quote
Type typeToConvert = typeof(DateTime);
JsonSerializerOptions options = new JsonSerializerOptions();
var expected = new DateTime(2019, 01, 02);
// Act
var result = dateTimeShortConverter.Read(ref reader, typeToConvert, options);
// Assert
Assert.Equal(expected, result);
}