Browse Source

优化yaml导出测试

dev-lhz
chenzhihang 1 year ago
parent
commit
ff4475c25e
1 changed files with 25 additions and 3 deletions
  1. +25
    -3
      ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/YamlUtils.java

+ 25
- 3
ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/YamlUtils.java View File

@@ -2,11 +2,14 @@ package com.ruoyi.platform.utils;


import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.nodes.Node;
import org.yaml.snakeyaml.representer.Represent;
import org.yaml.snakeyaml.representer.Representer;


import java.io.*; import java.io.*;
import java.util.Iterator; import java.util.Iterator;
import java.util.Map; import java.util.Map;
import org.yaml.snakeyaml.nodes.Tag;
public class YamlUtils { public class YamlUtils {


/** /**
@@ -20,8 +23,27 @@ public class YamlUtils {
DumperOptions options = new DumperOptions(); DumperOptions options = new DumperOptions();
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
options.setDefaultScalarStyle(DumperOptions.ScalarStyle.PLAIN); options.setDefaultScalarStyle(DumperOptions.ScalarStyle.PLAIN);
// options.setPrettyFlow(true);
Yaml yaml = new Yaml(options);
options.setPrettyFlow(false);

Representer representer = new Representer() {
{
// 重写字符串表示方法
representers.put(String.class, new Represent() {
public Node representData(Object data) {
return representScalar(Tag.STR, (String) data, null);
}
});
}
};

// 去掉引号时使用PLAIN样式
representer.setDefaultScalarStyle(DumperOptions.ScalarStyle.PLAIN);

// 创建Yaml实例
Yaml yaml = new Yaml(representer, options);

// Yaml yaml = new Yaml(options);

File directory = new File(path); File directory = new File(path);
if (!directory.exists()) { if (!directory.exists()) {
boolean isCreated = directory.mkdirs(); boolean isCreated = directory.mkdirs();


Loading…
Cancel
Save