发布时间:2025-12-09 12:02:30 浏览次数:2
import org.yaml.snakeyaml.introspector.PropertyUtils; //导入依赖的package包/类private Object loadYaml(File omapfile) throws IOException { FileReader reader = new FileReader(omapfile); try { Constructor constructor = new Constructor(); PropertyUtils putils = new PropertyUtils(); putils.setSkipMissingProperties(true); constructor.setPropertyUtils(putils); Yaml yaml = new Yaml(constructor); return yaml.load(reader); } catch (Throwable t) { throw new RuntimeException("Error loading yaml from: " + omapfile.getAbsolutePath() + "\n" + t.getMessage(), t); } finally { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } }} import org.yaml.snakeyaml.introspector.PropertyUtils; //导入依赖的package包/类private Object loadYaml(File file) throws FileNotFoundException { FileReader reader = new FileReader(file); try { Constructor constructor = new Constructor(); PropertyUtils putils = new PropertyUtils(); putils.setSkipMissingProperties(true); constructor.setPropertyUtils(putils); Yaml yaml = new Yaml(constructor); return yaml.load(reader); } catch (Throwable t) { throw new RuntimeException("Error loading yaml from: " + file.getAbsolutePath() + "\n" + t.getMessage(), t); } finally { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } }} import org.yaml.snakeyaml.introspector.PropertyUtils; //导入依赖的package包/类public static Yaml newYaml() { PropertyUtils propertyUtils = new AdvancedPropertyUtils(); propertyUtils.setSkipMissingProperties(true); Constructor constructor = new Constructor(Federations.class); TypeDescription federationDescription = new TypeDescription(Federations.class); federationDescription.putListPropertyType("federatedMetaStores", FederatedMetaStore.class); constructor.addTypeDescription(federationDescription); constructor.setPropertyUtils(propertyUtils); Representer representer = new AdvancedRepresenter(); representer.setPropertyUtils(new FieldOrderPropertyUtils()); representer.addClassTag(Federations.class, Tag.MAP); representer.addClassTag(AbstractMetaStore.class, Tag.MAP); representer.addClassTag(WaggleDanceConfiguration.class, Tag.MAP); representer.addClassTag(YamlStorageConfiguration.class, Tag.MAP); representer.addClassTag(GraphiteConfiguration.class, Tag.MAP); DumperOptions dumperOptions = new DumperOptions(); dumperOptions.setIndent(2); dumperOptions.setDefaultFlowStyle(FlowStyle.BLOCK); return new Yaml(constructor, representer, dumperOptions);} import org.yaml.snakeyaml.introspector.PropertyUtils; //导入依赖的package包/类public void testErrorMessage() throws Exception { BeanA1 b = new BeanA1(); b.setId(2l); b.setName("name1"); Constructor c = new Constructor(); Representer r = new Representer(); PropertyUtils pu = new PropertyUtils(); c.setPropertyUtils(pu); r.setPropertyUtils(pu); pu.getProperties(BeanA1.class, BeanAccess.FIELD); Yaml yaml = new Yaml(c, r); // yaml.setBeanAccess(BeanAccess.FIELD); String dump = yaml.dump(b); BeanA1 b2 = (BeanA1) yaml.load(dump); assertEquals(b.getId(), b2.getId()); assertEquals(b.getName(), b2.getName()); }