2016-12-12 4 views
0

나는 간단한 YAML 문서가 있습니다. 예 :YAML 사용자 정의 개체

public class CustomObject { 
     private String value; 

     public CustomObject(String value) { 
      .... 
     } 

     getValue ... 
     setValue ... 
    } 

여기서 value는 속성 연결 결과 a, b, c가 마스크 (결과 1 : 2/3)의 결과입니까? 당신은 다음과 같이 사용할 수 있습니다

class CustomObjectConstructor extends Constructor { 
    public CustomObjectConstructor() { 
     this.yamlConstructors.put(new Tag("!customObject"), new ConstructCustomObject()); 
    } 

    private class ConstructCustomObject extends AbstractConstruct { 
     public Object construct(Node node) { 
      final Map<Object, Object> values = constructMapping(node); 
      final String a = (String) values.get("a"); 
      final String b = (String) values.get("b"); 
      final String c = (String) values.get("c"); 
      return new CustomObject(a + ":" + b + "/" + c); 
     } 
    } 
} 

:

답변

1

이 사용자 정의 생성자와 representers 가능합니다 물론

Yaml yaml = new Yaml(new CustomObjectConstructor()); 
CustomObject myObject = 
    (CustomObject) yaml.load("!customObject\na: 1\nb: 2\nc: 3"); 

이 오류 사례를 처리하기위한 정제를 필요로하지만, 일반을 보여줍니다 생각. 객체를 매핑으로 덤프하려면 여기서 코드와 비슷한 표현자를 정의 할 수 있습니다. 자세한 내용은 documentation을 참조하십시오.