in DarabonbaUnitTests/Utils/JSONUtilsTest.cs [70:173]
public void Test_ReadPath()
{
var context = new Context
{
Str = "test",
TestBool = true,
ContextInteger = 123,
ContextLong = 123L,
ContextDouble = 1.123d,
ContextFloat = 3.456f,
ContextListLong = new List<long?> { 123L, 456L },
ListList = new List<List<int?>>
{
new List<int?> { 789, 123 },
new List<int?> { 8, 9 }
},
IntegerListMap = new Dictionary<string, List<int?>>
{
{ "integerList", new List<int?> { 123, 456 } }
}
};
Assert.Null(JSONUtils.ReadPath(context, "$.notExist"));
Assert.True(JSONUtils.ReadPath(context, "$.testBool") is bool);
Assert.True(JSONUtils.ReadPath(context, "$.listList") is List<object>);
Assert.True(JSONUtils.ReadPath(context, "$.contextInteger") is long);
Assert.True(JSONUtils.ReadPath(context, "$.contextLong") is long);
Assert.True(JSONUtils.ReadPath(context, "$.contextDouble") is double);
Assert.True(JSONUtils.ReadPath(context, "$.contextFloat") is double);
Assert.True(JSONUtils.ReadPath(context, "$.contextListLong") is List<object>);
Assert.True(JSONUtils.ReadPath(context, "$.integerListMap") is Dictionary<string, object>);
Assert.Equal(true, JSONUtils.ReadPath(context, "$.testBool"));
Assert.Equal("test", JSONUtils.ReadPath(context, "$.testStr"));
Assert.Equal(123L, JSONUtils.ReadPath(context, "$.contextLong"));
var listLong = JSONUtils.ReadPath(context, "$.contextListLong") as List<object>;
Assert.Equal(123L, listLong[0]);
var listList = JSONUtils.ReadPath(context, "$.listList") as List<object>;
Assert.Equal(789L, (listList[0] as List<object>)[0]);
var map = JSONUtils.ReadPath(context, "$.integerListMap") as Dictionary<string, object>;
Assert.Equal(123L, (map["integerList"] as List<object>)[0]);
var realListList = new List<List<int?>>();
foreach (var itemList in listList)
{
var intList = itemList as List<object>;
var nullableIntList = new List<int?>();
if (intList != null)
{
foreach (var item in intList)
{
var intValue = (int?)(item as long?);
nullableIntList.Add(intValue);
}
}
realListList.Add(nullableIntList);
}
var realIntegerListMap = new Dictionary<string, List<int?>>();
foreach (var kvp in map)
{
string key = kvp.Key;
object value = kvp.Value;
var intList = value as List<object>;
var nullableIntList = new List<int?>();
if (intList != null)
{
foreach (var item in intList)
{
nullableIntList.Add((int?)(item as long?));
}
}
realIntegerListMap[key] = nullableIntList;
}
var context1 = new Context
{
ContextLong = JSONUtils.ReadPath(context, "$.contextLong") as long?,
ContextInteger = (int?)(JSONUtils.ReadPath(context, "$.contextInteger") as long?),
ContextFloat = (float?)(JSONUtils.ReadPath(context, "$.contextFloat") as double?),
ContextDouble = JSONUtils.ReadPath(context, "$.contextDouble") as double?,
ContextListLong = (JSONUtils.ReadPath(context, "$.contextListLong") as List<object>)
.Select(item => item is long longValue ? longValue : (long?)null)
.ToList(),
ListList = realListList,
IntegerListMap = realIntegerListMap
};
Assert.Equal(123L, context1.ContextLong);
Assert.Equal(123, context1.ContextInteger);
Assert.Equal(3.456f, context1.ContextFloat);
Assert.Equal(1.123d, context1.ContextDouble);
Assert.Equal(new List<long?> { 123L, 456L }, context1.ContextListLong);
Assert.Equal(new List<List<int?>>
{
new List<int?> { 789, 123 },
new List<int?> { 8, 9 }
}, context1.ListList);
Assert.Equal(123, (context1.IntegerListMap["integerList"] as List<int?>)[0]);
}