排除整个对象在使用SnakeYAML转储YAML时。

huangapple go评论61阅读模式
英文:

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&#39;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 &amp;&amp; norwich.getDisabled()){
//return representScalar(Tag.NULL,&quot;&quot;);
return nullRepresenter.representData((Object) null);
}
return representJavaBean(getProperties(Norwich.class),norwich);
}
}
// filter null fields and those I don&#39;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 &amp;&amp; ((List&lt;?&gt;) propertyValue).size()==0 &amp;&amp; !property.getName().equals(&quot;children&quot;)){
return null;
}else if(property.getName().equals(&quot;disabled&quot;)){
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&lt;Integer&gt; conditions = new ArrayList&lt;&gt;();
    private Integer condition;

    // recursive children
    private List&lt;Norwich&gt; children = new ArrayList&lt;&gt;();
    // 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&lt;Norwich&gt; 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 &#39;&#39;]
  - 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&lt;Norwich&gt; newList = (List&lt;Norwich&gt;)o;
    List&lt;Norwich&gt; enabled = newList.stream().filter(pre -&gt; 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&lt;Norwich&gt; newList = (List&lt;Norwich&gt;)o;
    List&lt;Norwich&gt; enabled = newList.stream().filter(pre -&gt; pre.getDisabled() == null || !pre.getDisabled()).collect(Collectors.toList());
    return representSequence(getTag(enabled.getClass(), Tag.SEQ), enabled, DumperOptions.FlowStyle.AUTO);
  }
}

Thanks again.

huangapple
  • 本文由 发表于 2023年6月12日 09:14:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76453141.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定