我有以下代码来解析 JSON 文件:
@Override
Map<String, Configuration> parseJson() {
Object configurationFile = readConfigurationFile()
configurationFile.schemas.each { schemaProtectionInformation ->
processing(schemaProtectionInformation)
}
}
private Object readConfigurationFile() {
InputStream is = getClass().getClassLoader().getResourceAsStream("test.json")
BufferedReader reader = new BufferedReader(new InputStreamReader(is))
return new JsonSlurper().parse(reader)
}
处理以下 JSON 文件:
{
"schemas": [
{
"name": "plan_pm_test",
"protectedDimensions": [
{
"name": "dActivityWbs",
"usedToSecureFactTable": true,
"aliasInFactTable": "PLAN_WBS",
"levels" : ["LEVEL_1_ID","LEVEL_2_ID","LEVEL_3_ID","LEVEL_4_ID","LEVEL_5_ID","LEVEL_6_ID","LEVEL_7_ID","LEVEL_8_ID","LEVEL_9_ID"]
},
{
"name": "dResponsibleOrganicUnit",
"usedToSecureFactTable": true,
"aliasInFactTable": "RES_ORG_UNIT",
"levels" : ["ID","LEVEL_1_ID","LEVEL_2_ID"]
},
{
"name": "dContributionOrganicUnit",
"usedToSecureFactTable": true,
"aliasInFactTable": "CON_ORG_UNIT",
"levels" : ["ID","LEVEL_1_ID","LEVEL_2_ID"]
}
]
}
]
}
如果我执行此代码,我将收到以下错误:
Cannot cast object '[{name=plan_pm_test, protectedDimensions=[{aliasInFactTable=PLAN_WBS, levels=[LEVEL_1_ID, LEVEL_2_ID, LEVEL_3_ID, LEVEL_4_ID, LEVEL_5_ID, LEVEL_6_ID, LEVEL_7_ID, LEVEL_8_ID, LEVEL_9_ID], name=dActivityWbs, usedToSecureFactTable=true}, {aliasInFactTable=RES_ORG_UNIT, levels=[ID, LEVEL_1_ID, LEVEL_2_ID], name=dResponsibleOrganicUnit, usedToSecureFactTable=true}, {aliasInFactTable=CON_ORG_UNIT, levels=[ID, LEVEL_1_ID, LEVEL_2_ID], name=dContributionOrganicUnit, usedToSecureFactTable=true}]}]' with class 'java.util.ArrayList' to class 'java.util.Map' due to: groovy.lang.GroovyRuntimeException: Could not find matching constructor for: java.util.Map(groovy.json.internal.LazyMap)
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '[{name=plan_pm_test, protectedDimensions=[{aliasInFactTable=PLAN_WBS, levels=[LEVEL_1_ID, LEVEL_2_ID, LEVEL_3_ID, LEVEL_4_ID, LEVEL_5_ID, LEVEL_6_ID, LEVEL_7_ID, LEVEL_8_ID, LEVEL_9_ID], name=dActivityWbs, usedToSecureFactTable=true}, {aliasInFactTable=RES_ORG_UNIT, levels=[ID, LEVEL_1_ID, LEVEL_2_ID], name=dResponsibleOrganicUnit, usedToSecureFactTable=true}, {aliasInFactTable=CON_ORG_UNIT, levels=[ID, LEVEL_1_ID, LEVEL_2_ID], name=dContributionOrganicUnit, usedToSecureFactTable=true}]}]' with class 'java.util.ArrayList' to class 'java.util.Map' due to: groovy.lang.GroovyRuntimeException: Could not find matching constructor for: java.util.Map(groovy.json.internal.LazyMap)
at cern.ais.datawarehouse.baserver.mondriansecurity.common.schemaprotectionconfiguration.JsonResourceFileConfigurationRepositoryPopulator.readConfiguration(JsonResourceFileConfigurationRepositoryPopulator.groovy:23)
at cern.ais.datawarehouse.baserver.mondriansecurity.common.schemaprotectionconfiguration.JsonResourceFileConfigurationRepositoryPopulatorTest.tes(JsonResourceFileConfigurationRepositoryPopulatorTest.groovy:12)
所以当然我开始一步步调试应用程序,看看部分处理()中的哪部分代码抛出了这个异常。令人惊讶的是,那里的所有代码都正常执行:没有抛出异常并返回结果我除外。
更让我吃惊的是,当我稍微更改第一种方法的代码时,它可以正常工作而不会产生异常。
@Override
Map<String, Configuration> readConfiguration() {
Object configurationFile = readConfigurationFile()
configurationFile.schemas.each { schemaProtectionInformation ->
processing(schemaProtectionInformation)
}
println "test 2"
}
我不知道println方法如何改变那里的任何东西。当然,它不一定是println方法可以解决问题。所以如果我做这样的事情:
@Override
Map<String, Configuration> readConfiguration() {
Object configurationFile = readConfigurationFile()
configurationFile.schemas.each { schemaProtectionInformation ->
processing(schemaProtectionInformation)
}
test()
}
void test() {
}
它也可以正常工作(不会抛出任何异常)。我不知道为什么在处理 json 文件后有一些额外的代码应该在此处进行任何更改。
刚才我其实已经把处理方法注释掉了,所以方法体如下图。
@Override
Map<String, Configuration> readConfiguration() {
Object configurationFile = readConfigurationFile()
configurationFile.schemas.each { schemaProtectionInformation ->
//processing(schemaProtectionInformation)
}
}
即使我收到了同样的例外。因此,该错误与处理方法的实现无关。
非常感谢您的意见。