【使用apache snakeyaml 管理yml文件】

使用apache snakeyaml 管理yml文件

1. 一个Yaml工厂

public class YamlFactory {
   
    public static YamlUtil get(){
   
        return new YamlUtil();
    }
}

2.Yaml工具类

@Slf4j
public class YamlUtil {
   

    private Yaml yaml;


    public YamlUtil() {
   
        yaml = new Yaml(new Representer() {
   
            @Override
            protected NodeTuple representJavaBeanProperty(
                    Object javaBean, Property property, Object propertyValue,
                    Tag customTag) {
   
                return propertyValue == null ? null
                        : super.representJavaBeanProperty(javaBean, property, 
                        propertyValue, customTag);
            }
        });
    }

    public <T> T loadAs(String yaml, Class<T> type) {
   
        return this.yaml.loadAs(yaml, type);
    }

    public boolean write(Object obj, String path) {
   
        try (Writer writer = this.writer(path)) {
   
            this.dump(obj, writer);
        } catch (IOException e) {
   
            log.error("写入文件【{}】失败!", path);
            return false;
        }
        return true;
    }

    public boolean yamlFile(@NonNull Object obj, @NonNull String path) {
   
        try (Writer writer = this.writer(path)) {
   
            String yamlStr = this.yaml.dumpAsMap(obj);
            writer.write(yamlStr);
            writer.flush();
            log.info("写入文件: success, 文件路径:【{}】 ", path);
        } catch (IOException e) {
   
            log.error("写入文件: failed, 文件路径:【{}】 ", path);
            return false;
        }
        return true;
    }

    private void dump(Object obj, Writer writer) {
   
        this.yaml.dump(obj, writer);
    }

    private Writer writer(String path) {
   
        try {
   
            File file = new File(path);
            if (!file.exists()) {
   
                file.createNewFile();
            }
            return new PrintWriter(file);
        } catch (IOException e) {
   
            throw new RuntimeException(e);
        }
    }
}

3. 测试类

public class Test {
   
    public static void main1(String[] args) throws IOException {
   
        //String yamlStr = 
        		"name: John\nhobby:\n  - A\n  - B\n  - C\nscores:\n  chinese: 66\n  math: 61";
        //Me me = yaml.loadAs(yamlStr, Me.class);
        //System.out.println(me);
        ArrayList<String> hobby = new ArrayList<>();
        hobby.add("唱歌");
        hobby.add("跳舞");
        hobby.add("跑步");
        hobby.add("画画");
        HashMap<String, String> scores = new HashMap<>();
        scores.put("语文","88");
        scores.put("数学","91");
        scores.put("英语","56");
        ArrayList<Parent> parents = new ArrayList<>();
        parents.add(new Parent("111","mather"));
        parents.add(new Parent("222","father"));
        Me me_ = new Me("John", hobby, scores,parents);

        String filePath = "D:/test.yml";
        YamlFactory.get().yamlFile(me_,filePath);
    }

    public static void main(String[] args) {
   
        String path = "";
        //YamlFactory.get().yamlFile()
    }
}
@Data
@AllArgsConstructor
class Me{
   
    public String name;
    public List<String> hobby;
    public Map<String ,String> scores;
    public List<Parent> parents;
}

@Data
@AllArgsConstructor
class Parent {
   
    public String name;
    public String role;
}

4. 完成 ! OK

相关推荐

  1. 使用apache snakeyaml 管理yml文件

    2023-12-09 06:06:07       44 阅读
  2. Python中使用YAML文件进行配置文件管理

    2023-12-09 06:06:07       42 阅读
  3. k8s使用yml文件部署

    2023-12-09 06:06:07       8 阅读
  4. .gitlab-ci.yml文件参数配置和使用

    2023-12-09 06:06:07       33 阅读
  5. SpringBoot之YAML文件使用

    2023-12-09 06:06:07       22 阅读
  6. rust - 使用serde_yaml读取配置文件

    2023-12-09 06:06:07       18 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-09 06:06:07       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-09 06:06:07       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-09 06:06:07       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-09 06:06:07       20 阅读

热门阅读

  1. 《C++新经典设计模式》之第20章 访问者模式

    2023-12-09 06:06:07       29 阅读
  2. 寄存器、缓存、内存、硬盘、存储器的理解

    2023-12-09 06:06:07       45 阅读
  3. KEIL-MDK5安装包下载以及keil-kill文件分享

    2023-12-09 06:06:07       42 阅读