Java Code Examples for org.yaml.snakeyaml.constructor.Constructor#setPropertyUtils()

The following examples show how to use org.yaml.snakeyaml.constructor.Constructor#setPropertyUtils() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StarterConfigure.java    From ClashForMagisk with GNU General Public License v3.0 6 votes vote down vote up
static StarterConfigure loadFromFile(File file) throws IOException {
    FileInputStream inputStream = new FileInputStream(file);
    Constructor constructor = new Constructor(StarterConfigure.class);

    constructor.setPropertyUtils(new PropertyUtils() {
        {
            setSkipMissingProperties(true);
        }
    });

    StarterConfigure result = new Yaml(constructor).loadAs(inputStream, StarterConfigure.class);

    inputStream.close();

    return result;
}
 
Example 2
Source File: ObjectMapItem.java    From marathonv5 with Apache License 2.0 6 votes vote down vote up
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();
        }
    }
}
 
Example 3
Source File: OMapContainer.java    From marathonv5 with Apache License 2.0 6 votes vote down vote up
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();
        }
    }
}
 
Example 4
Source File: YamlFactory.java    From waggle-dance with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example 5
Source File: AbstractBeanTest.java    From snake-yaml with Apache License 2.0 6 votes vote down vote up
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());
    }
 
Example 6
Source File: YamlConfigTestUtils.java    From submarine with Apache License 2.0 5 votes vote down vote up
public static YamlConfigFile readYamlConfigFile(String filename) {
  Constructor constructor = new Constructor(YamlConfigFile.class);
  constructor.setPropertyUtils(new RunJobParameters.UnderscoreConverterPropertyUtils());
  Yaml yaml = new Yaml(constructor);
  InputStream inputStream = YamlConfigTestUtils.class
      .getClassLoader()
      .getResourceAsStream(filename);
  return yaml.loadAs(inputStream, YamlConfigFile.class);
}
 
Example 7
Source File: ClashConfigure.java    From ClashForMagisk with GNU General Public License v3.0 5 votes vote down vote up
static ClashConfigure loadFromFile(File file) throws IOException {
    FileInputStream inputStream = new FileInputStream(file);
    Constructor constructor = new Constructor(ClashConfigure.class);

    constructor.setPropertyUtils(new PropertyUtils() {
        {
            setSkipMissingProperties(true);
        }

        @Override
        public Property getProperty(Class<?> type, String name) {
            switch (name) {
                case "port":
                    name = "portHttp";
                    break;
                case "socks-port":
                    name = "portSocks";
                    break;
                case "redir-port":
                    name = "portRedirect";
                    break;
            }
            return super.getProperty(type, name);
        }
    });

    ClashConfigure result = new Yaml(constructor).loadAs(inputStream, ClashConfigure.class);

    inputStream.close();

    return result;
}
 
Example 8
Source File: YamlEnvironmentPostProcessor.java    From syncer with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static <T> Yaml getYaml(Class<T> tClass) {
  Constructor c = new Constructor(tClass);
  c.setPropertyUtils(new PropertyUtils() {
    @Override
    public Property getProperty(Class<?> type, String name) {
      if (name.indexOf('-') > -1) {
        name = CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, name);
      }
      return super.getProperty(type, name);
    }
  });
  return new Yaml(c);
}
 
Example 9
Source File: PropertyUtilsSharingTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
public void testYamlConstructorWithPropertyUtils() {
    Constructor constructor1 = new Constructor();
    PropertyUtils pu = new PropertyUtils();
    constructor1.setPropertyUtils(pu);
    Yaml yaml = new Yaml(constructor1);
    assertSame(pu, yaml.constructor.getPropertyUtils());
    assertSame(pu, yaml.representer.getPropertyUtils());
}
 
Example 10
Source File: PropertyUtilsSharingTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
@Test
public void testYamlConstructorANDRepresenterWithPropertyUtils() {
    Constructor constructor = new Constructor();
    PropertyUtils pu_c = new PropertyUtils();
    constructor.setPropertyUtils(pu_c);
    Representer representer = new Representer();
    PropertyUtils pu_r = new PropertyUtils();
    representer.setPropertyUtils(pu_r);
    Yaml yaml = new Yaml(constructor, representer);
    assertSame(pu_c, yaml.constructor.getPropertyUtils());
    assertSame(pu_r, yaml.representer.getPropertyUtils());
}