SnakeYAML序列化时去掉!!类路径信息

编程入门 行业动态 更新时间:2024-10-25 18:26:36

SnakeYAML序列化时去掉!!类<a href=https://www.elefans.com/category/jswz/34/1771438.html style=路径信息"/>

SnakeYAML序列化时去掉!!类路径信息

        将一个对象生成yaml文件时,如何去掉自定义对象后面自带的!! 类路径信息,是此次分享的内容,举例以下内容。

1.首先定义一个yaml类,用来配置类信息等内容(核心配置)
private static Yaml getYamlRepresenter(){Constructor constructor = new Constructor(User.class);//主类对象TypeDescription carDescription = new TypeDescription(User.class);carDescription.putListPropertyType("Address", Address.class);    //属性类对象carDescription.putListPropertyType("Model", Model.class);    //属性类对象constructor.addTypeDescription(carDescription);Representer representer = new Representer();// 好像是设置空属性填充//representer.getPropertyUtils().setSkipMissingProperties(true);//会使用 User 对map的类型进行解析,不会生成!!和包路径representer.addClassTag(User.class, Tag.MAP);    //配合上面使用,将map值解析为对应的类representer.addClassTag(Address.class, Tag.MAP);    representer.addClassTag(Model.class, Tag.MAP);final DumperOptions options = new DumperOptions();options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);    //块状格式options.setExplicitStart(false);options.setIndicatorIndent(2);options.setSplitLines(true);options.setIndent(4);Yaml yaml = new Yaml(constructor,representer,options);return yaml;}
2.构造主要的dump方法(根据需要可选其他方法,这里使用dump)
public static void dumpFileRepresenter(File file, Map data) throws IOException {OutputStreamWriter writer = new FileWriter(file);getYamlRepresenter().dump(data, writer);}
3.创建具体的yaml文件方法
     /*** 创建yaml的配置文件* @param filePath 路径* @param fileName 文件名* @param mapData  生成yaml的数据* @throws IOException*/public File makeYamlFileGetPath(String filePath, String fileName, Map<String,Object> mapData) throws IOException {File file = new File(filePath, fileName);FileUtils.forceMkdir(file.getParentFile());YamlUtil.dumpFileRepresenter(file,mapData);logger.info("yaml dump path {}",file.getAbsolutePath());    //打印了一下本地文件生成的地址信息return file;}
4.简单列出主类信息作参考
public class User {private String name;private Address address;private List<Model> mode;       private Integer age;}
5.生成文件展示
address:enabled: falseaddress_config: address.yaml
name: test01
age: 66
model:framework: tensorflowinput_features:- pclass- plib- pnummodel_config: model.conf

更多推荐

SnakeYAML序列化时去掉!!类路径信息

本文发布于:2023-11-15 19:52:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1605356.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路径   序列化   信息   SnakeYAML

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!