英文:
Exclude whole object when dumping yaml using snakeyaml
问题
I understand your request. Here is the translated code portion:
我想在将对象序列化为YAML时,将其```disabled```为```true```的对象设为不可见,但是我不能将它们删除,而是得到一些```null```。
阅读了snake文档后,我构建了自己的Representer:
```java
public class MyYamlRepresenter extends Representer {
public MyYamlRepresenter() {
super();
this.representers.put(Norwich.class,new NorwichRepresenter());
}
private class NorwichRepresenter implements Represent {
@Override
public Node representData(Object o) {
Norwich norwich = (Norwich) o;
if(norwich.getDisabled()!=null && norwich.getDisabled()){
//return representScalar(Tag.NULL,"");
return nullRepresenter.representData((Object) null);
}
return representJavaBean(getProperties(Norwich.class),norwich);
}
}
// 过滤掉空字段和我不需要的字段
@Override
protected NodeTuple representJavaBeanProperty(Object javaBean, Property property, Object propertyValue, Tag customTag) {
if(javaBean==null){
return null;
}else if(propertyValue==null){
return null;
}else if(propertyValue instanceof List && ((List<?>) propertyValue).size()==0 && !property.getName().equals("children")){
return null;
}else if(property.getName().equals("disabled")){
return null;
}
else{
return super.representJavaBeanProperty(javaBean, property, propertyValue, customTag);
}
}
}
和类Norwich
:
public class Norwich{
private String type;
private String name;
private Integer id;
private Integer interval;
private Integer count;
private Boolean disabled;
private List<Integer> conditions = new ArrayList<>();
private Integer condition;
// 递归子节点
private List<Norwich> children = new ArrayList<>();
// 获取器和设置器
}
然后,我尝试将另一个包含Norwich
列表的对象转储为YAML:
DumperOptions options = new DumperOptions();
MyYamlRepresenter myYamlRepresenter = new MyYamlRepresenter();
myYamlRepresenter.addClassTag(foo.bar.NorwichParent.class, Tag.MAP);
Yaml yaml = new Yaml(myYamlRepresenter, options);
NorwichParent parent = new NorwichParent();
List<Norwich> norwiches = mysql.select();// 从数据库获取它们
parent.setChildren(norwiches);
yaml.dump(parent, fileWriter);
然后,我得到了一个类似这样的文件:
type: main
name: main
id: 1
children:
- null
- type: logic_if
name: if
id: 5
condition: 1
children:
- type: logic_if_true
name: true
id: 6
children: [null]
- type: logic_if_false
name: false
id: 7
children: []
我切换了MyYamlRepresenter
中使用representScalar
的注释,结果只改变为:
type: main
name: main
id: 1
children:
-
- type: logic_if
name: if
id: 5
condition: 1
children:
- type: logic_if_true
name: true
id: 6
children: [!!null '']
- type: logic_if_false
name: false
id: 7
children: []
那么,我该如何跳过整个对象并删除main
的第一个子节点和{id:6}
的第一个子节点:
type: main
name: main
id: 1
children:
- type: logic_if
name: if
id: 5
condition: 1
children:
- type: logic_if_true
name: true
id: 6
children: []
- type: logic_if_false
name: false
id: 7
children: []
<details>
<summary>英文:</summary>
I want to make objects invisible whose ```disabled``` are ```true``` when serializing them to the yaml,but I can't remove them but get some ```null```s.
I construct my own Representer after reading docs of snake:
```java
public class MyYamlRepresenter extends Representer {
public MyYamlRepresenter() {
super();
this.representers.put(Norwich.class,new NorwichRepresenter());
}
private class NorwichRepresenter implements Represent {
@Override
public Node representData(Object o) {
Norwich norwich = (Norwich) o;
if(norwich.getDisabled()!=null && norwich.getDisabled()){
//return representScalar(Tag.NULL,"");
return nullRepresenter.representData((Object) null);
}
return representJavaBean(getProperties(Norwich.class),norwich);
}
}
// filter null fields and those I don't need
@Override
protected NodeTuple representJavaBeanProperty(Object javaBean, Property property, Object propertyValue, Tag customTag) {
if(javaBean==null){
return null;
}else if(propertyValue==null){
return null;
}else if(propertyValue instanceof List && ((List<?>) propertyValue).size()==0 && !property.getName().equals("children")){
return null;
}else if(property.getName().equals("disabled")){
return null;
}
else{
return super.representJavaBeanProperty(javaBean, property, propertyValue, customTag);
}
}
and class Norwich
:
public class Norwich{
private String type;
private String name;
private Integer id;
private Integer interval;
private Integer count;
private Boolean disabled;
private List<Integer> conditions = new ArrayList<>();
private Integer condition;
// recursive children
private List<Norwich> children = new ArrayList<>();
// getters and setters
}
Then I tried to dump another object who keeps a list of Norwich
as its children
:
DumperOptions options = new DumperOptions();
MyYamlRepresenter myYamlRepresenter = new MyYamlRepresenter();
myYamlRepresenter.addClassTag(foo.bar.NorwichParent.class, Tag.MAP);
Yaml yaml=new Yaml(myYamlRepresenter,options);
NorwichParent parent = new NorwichParent();
List<Norwich> norwiches = mysql.select();// I got them from database
parent.setChildren(norwiches);
yaml.dump(parent,fileWriter);
Then I got a file like this:
type: main
name: main
id: 1
children:
- null
- type: logic_if
name: if
id: 5
condition: 1
children:
- type: logic_if_true
name: true
id: 6
children: [null]
- type: logic_if_false
name: false
id: 7
children: []
and I switch the comment in MyYamlRepresenter
which using representScalar
instead, the result just changed to:
type: main
name: main
id: 1
children:
-
- type: logic_if
name: if
id: 5
condition: 1
children:
- type: logic_if_true
name: true
id: 6
children: [!!null '']
- type: logic_if_false
name: false
id: 7
children: []
So how do I just skip the whole obj and remove the first child of main
and the first child of {id:6}
:
type: main
name: main
id: 1
children:
- type: logic_if
name: if
id: 5
condition: 1
children:
- type: logic_if_true
name: true
id: 6
children: []
- type: logic_if_false
name: false
id: 7
children: []
答案1
得分: 0
public MyYamlRepresenter() {
super();
// 记得注释掉这部分,以防出现NullPointerException: null.getDisabled();
// this.representers.put(Norwich.class,new NorwichRepresenter());
// 具体的类应该是 ArrayList 而不是 List
this.representers.put(ArrayList.class,new ListRepresenter());
}
private class ListRepresenter implements Represent{
@Override
public Node representData(Object o) {
List<Norwich> newList = (List<Norwich>)o;
List<Norwich> enabled = newList.stream().filter(pre -> pre.getDisabled() == null || !pre.getDisabled()).collect(Collectors.toList());
return representSequence(getTag(enabled.getClass(), Tag.SEQ), enabled, DumperOptions.FlowStyle.AUTO);
}
}
英文:
Thanks to @Alex R.
public MyYamlRepresenter() {
super();
// remember comment this out, in case of NullPointerException: null.getDisabled();
// this.representers.put(Norwich.class,new NorwichRepresenter());
// concrete class ArrayList rather List
this.representers.put(ArrayList.class,new ListRepresenter());
}
private class ListRepresenter implements Represent{
@Override
public Node representData(Object o) {
List<Norwich> newList = (List<Norwich>)o;
List<Norwich> enabled = newList.stream().filter(pre -> pre.getDisabled() == null || !pre.getDisabled()).collect(Collectors.toList());
return representSequence(getTag(enabled.getClass(), Tag.SEQ), enabled, DumperOptions.FlowStyle.AUTO);
}
}
Thanks again.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论