[英]groovy YAML delete few lines
Using groovy yaml parser I need delete below lines and write into the file. 使用groovy yaml解析器,我需要删除以下几行并写入文件。
lines to remove. 要删除的行。
- name: hostname
required: true
secure: false
valueExposed: true
When I try to load the yaml data to map. 当我尝试加载Yaml数据以进行映射时。 its failing with 'org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object ' exception.
失败,出现“ org.codehaus.groovy.runtime.typehandling.GroovyCastException:无法转换对象”异常。
I am looking help on. 我正在寻求帮助。 How to load this yaml data and remove 4 lines from it.
如何加载此Yaml数据并从中删除4行。
import org.yaml.snakeyaml.DumperOptions
import org.yaml.snakeyaml.Yaml
class Test {
def static main(args) {
DumperOptions options = new DumperOptions()
options.setPrettyFlow(true)
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK)
Yaml yaml = new Yaml(options)
def Map map = (Map) yaml.load(data)
println yaml.dump(map)
}
def static String data = '''
- description: checkc the disk spacce
executionEnabled: true
loglevel: INFO
name: disk spacce
options:
- description: file system name
name: filesystem
required: true
- name: hostname
required: true
secure: false
valueExposed: true
scheduleEnabled: true
sequence:
commands:
- exec: df -h
keepgoing: false
'''
}
If you have looked into exception message carefully, you would have noticed that you are trying to convert a List
into a Map
, which would throw a ClassCastException
. 如果仔细查看了异常消息,您会发现您正在尝试将
List
转换为Map
,这将引发ClassCastException
。 You surely need to read and understand the YAML structure as you made the same mistake in your previous question . 您肯定需要阅读和理解YAML结构,因为您在上一个问题中犯了同样的错误。
So to load the data into a list: List list = (List) yaml.load(data)
因此要将数据加载到列表中:
List list = (List) yaml.load(data)
Now if you are sure about the structure of the yaml data, then you can remove the data in an ugly but direct way using list.first().options.remove(1)
. 现在,如果您确定yaml数据的结构,则可以使用
list.first().options.remove(1)
以一种难看但直接的方式删除数据。
Or you can iterate over the data and find the data that you need to remove and then remove it. 或者,您可以遍历数据并找到需要删除的数据,然后将其删除。
static Map dataToBeRemoved = [
name : 'hostname',
required : true,
secure : false,
valueExposed: true
]
public static findAndRemoveMap(List list) {
Object o
ListIterator iterator = list.listIterator()
while (iterator.hasNext()) {
o = iterator.next()
if (o instanceof Map) {
if (compareJsonObjects(dataToBeRemoved, o)) {
iterator.remove()
} else {
o.findAll { it.value instanceof List }.each {
findAndRemoveMap(it.value)
}
}
} else if (o instanceof List) {
findAndRemoveMap(o)
}
}
}
compareJsonObjects
is a method that compares two maps and return whether they are equal or not. compareJsonObjects
是一种比较两个映射并返回是否相等的方法。 You can create your own implementation or can use an external library like jsonassert
from skyscreamer
. 您可以创建自己的实现,也可以使用
jsonassert
的外部库(如skyscreamer
。 Using jsonassert
, the implementation for compareJsonObjects
would be: 使用
jsonassert
,为实现compareJsonObjects
将是:
public static boolean compareJsonObjects(Map<String, Object> obj_1, Map<String, Object> obj_2) {
String one = new groovy.json.JsonBuilder(obj_1).toPrettyString()
String two = new groovy.json.JsonBuilder(obj_2).toPrettyString()
JSONCompareResult r = JSONCompare.compareJSON(one, two, JSONCompareMode.NON_EXTENSIBLE)
return r.passed()
}
as already mentioned by @sandeep-poonia that cast exception was because of Map data type, you can refer to this code for parsing and removing those lines. 正如@ sandeep-poonia已经提到的,强制转换异常是由于Map数据类型引起的,您可以参考此代码来解析和删除这些行。
static Map dataToBeRemoved = [
name : 'hostname',
required : true,
secure : false,
valueExposed: true
]
static def removeEntries
removeEntries = {def yaml ->
if(yaml.getClass() == ArrayList){
yaml = yaml.collect {
removeEntries(it)
}
yaml = yaml.findAll{it} // remove empty items (list or map) from list.
} else if(yaml.getClass() == LinkedHashMap){
if(!(dataToBeRemoved - yaml)){
yaml = yaml - dataToBeRemoved
}
yaml.each{
yaml[it.key] = removeEntries(it.value)
}
}
yaml
}
def static main(args){
DumperOptions options = new DumperOptions()
options.setPrettyFlow(true)
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK)
Yaml yaml = new Yaml(options)
List yamlData = yaml.load(data)
yamlData = removeEntries(yamlData)
println yaml.dump(yamlData)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.