Json to struct error 388
This commit is contained in:
parent
64d7091b2b
commit
25cf73ed80
|
|
@ -1430,7 +1430,7 @@ namespace SimpleJson
|
||||||
obj = value;
|
obj = value;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
obj = ConstructorCache[type]();
|
obj = ConstructorCache[type]?.Invoke() ?? Activator.CreateInstance(type);
|
||||||
foreach (KeyValuePair<string, KeyValuePair<Type, ReflectionUtils.SetDelegate>> setter in SetCache[type])
|
foreach (KeyValuePair<string, KeyValuePair<Type, ReflectionUtils.SetDelegate>> setter in SetCache[type])
|
||||||
{
|
{
|
||||||
object jsonValue;
|
object jsonValue;
|
||||||
|
|
|
||||||
|
|
@ -80,6 +80,46 @@ namespace Microsoft.AspNetCore.Blazor.Test
|
||||||
Assert.Equal(new TimeSpan(7665, 1, 30, 0), person.Age);
|
Assert.Equal(new TimeSpan(7665, 1, 30, 0), person.Age);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void CanSerializeStructToJson()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var commandResult = new SimpleError
|
||||||
|
{
|
||||||
|
Message = "Test",
|
||||||
|
ContainsError = true,
|
||||||
|
ErrorId = 1
|
||||||
|
};
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = JsonUtil.Serialize(commandResult);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal("{\"Message\":\"Test\",\"ContainsError\":true,\"ErrorId\":1}", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void CanDeserializeStructFromJson()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var json = "{\"Message\":\"Test\",\"ContainsError\":true,\"ErrorId\":1}";
|
||||||
|
|
||||||
|
//Act
|
||||||
|
var simpleError = JsonUtil.Deserialize<SimpleError>(json);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal("Test", simpleError.Message);
|
||||||
|
Assert.True(simpleError.ContainsError);
|
||||||
|
Assert.Equal(1, simpleError.ErrorId);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct SimpleError
|
||||||
|
{
|
||||||
|
public string Message { get; set; }
|
||||||
|
public bool ContainsError { get; set; }
|
||||||
|
public int? ErrorId { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
class Person
|
class Person
|
||||||
{
|
{
|
||||||
public int Id { get; set; }
|
public int Id { get; set; }
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue