ServiceStack.Text FromJson 和 EmitLowercaseUnderscoreNames
ServiceStack.Text FromJson and EmitLowercaseUnderscoreNames
我调用的第三方 returns 小写并带有下划线的对象,例如
{ "token_type":"bearer", "access_token":"blahblah", "expires_in":3600, "scope":"rsp" }
我想将其反序列化为 Pascal 大小写样式 class,例如
public class OAuthResponse
{
public string TokenType { get; set; }
public string AccessToken { get; set; }
public int ExpiresIn { get; set; }
public string Scope { get; set; }
}
我试过为此设置一个自定义范围,但它不起作用。这是一个失败的测试:
[Fact]
public void ShouldDeserializeUsingScope()
{
// Arrange
using (var scope = JsConfig.BeginScope())
{
scope.EmitLowercaseUnderscoreNames = true;
scope.EmitCamelCaseNames = false;
var response = "{ \"token_type\":\"bearer\", \"access_token\":\"blahblah\", \"expires_in\":3600, \"scope\":\"rsp\" }";
// Act
var oAuthResponse = response.FromJson<OAuthResponse>();
// Assert
Assert.Equal("rsp", oAuthResponse.Scope);
Assert.Equal("blahblah", oAuthResponse.AccessToken); // it fails on this line
}
}
如何自定义反序列化?
尝试使用以下
using (JsConfig.With(emitLowercaseUnderscoreNames: true, propertyConvention: PropertyConvention.Lenient))
{
var OAuthResponseDto = JsonSerializer.DeserializeFromString<OAuthResponse>(JsonResponse);
}
解决方法是使用TextCase.SnakeCase
.
这是我的及格测试:
[Fact]
public void ShouldDeserializeUsingScope()
{
// Arrange
using (var scope = JsConfig.BeginScope())
{
scope.TextCase = TextCase.SnakeCase;
var response = "{ \"token_type\":\"bearer\", \"access_token\":\"blahblah\", \"expires_in\":3600, \"scope\":\"rsp\" }";
// Act
var oAuthResponse = response.FromJson<OAuthResponse>();
// Assert
Assert.Equal("rsp", oAuthResponse.Scope);
Assert.Equal("blahblah", oAuthResponse.Access_Token);
}
}
我调用的第三方 returns 小写并带有下划线的对象,例如
{ "token_type":"bearer", "access_token":"blahblah", "expires_in":3600, "scope":"rsp" }
我想将其反序列化为 Pascal 大小写样式 class,例如
public class OAuthResponse
{
public string TokenType { get; set; }
public string AccessToken { get; set; }
public int ExpiresIn { get; set; }
public string Scope { get; set; }
}
我试过为此设置一个自定义范围,但它不起作用。这是一个失败的测试:
[Fact]
public void ShouldDeserializeUsingScope()
{
// Arrange
using (var scope = JsConfig.BeginScope())
{
scope.EmitLowercaseUnderscoreNames = true;
scope.EmitCamelCaseNames = false;
var response = "{ \"token_type\":\"bearer\", \"access_token\":\"blahblah\", \"expires_in\":3600, \"scope\":\"rsp\" }";
// Act
var oAuthResponse = response.FromJson<OAuthResponse>();
// Assert
Assert.Equal("rsp", oAuthResponse.Scope);
Assert.Equal("blahblah", oAuthResponse.AccessToken); // it fails on this line
}
}
如何自定义反序列化?
尝试使用以下
using (JsConfig.With(emitLowercaseUnderscoreNames: true, propertyConvention: PropertyConvention.Lenient))
{
var OAuthResponseDto = JsonSerializer.DeserializeFromString<OAuthResponse>(JsonResponse);
}
解决方法是使用TextCase.SnakeCase
.
这是我的及格测试:
[Fact]
public void ShouldDeserializeUsingScope()
{
// Arrange
using (var scope = JsConfig.BeginScope())
{
scope.TextCase = TextCase.SnakeCase;
var response = "{ \"token_type\":\"bearer\", \"access_token\":\"blahblah\", \"expires_in\":3600, \"scope\":\"rsp\" }";
// Act
var oAuthResponse = response.FromJson<OAuthResponse>();
// Assert
Assert.Equal("rsp", oAuthResponse.Scope);
Assert.Equal("blahblah", oAuthResponse.Access_Token);
}
}