2017-09-19 39 views

回答

0

對於我正在開發的項目之一。我被要求將特徵文件解析成Java對象以作進一步處理和使用。我創建了一個映射類(以上詳述),並用下面的代碼:

import com.fasterxml.jackson.databind.ObjectMapper; 
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; 
import gherkin.formatter.JSONFormatter; 
import gherkin.parser.Parser; 
import gherkin.util.FixJava; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.InputStreamReader; 

public Feature[] getFeatures(String filepath) throws Exception { 

     //Step One: Parse feature into JSON using Gherkin 
     String featureText = FixJava.readReader(new InputStreamReader(new FileInputStream(filepath), "UTF-8")); 
     StringBuilder json = new StringBuilder(); 
     JSONFormatter formatter = new JSONFormatter(json); 
     Parser parser = new Parser(formatter); 
     parser.parse(featureText, filepath, 0); 
     formatter.done(); 
     formatter.close(); 

     //Step Two: Convert the Features to plain old java object, and return an array of features 
     ObjectMapper mapper = new ObjectMapper(); 
     return mapper.readValue(json.toString(), Feature[].class); 
} 

這裏是要點=>https://gist.github.com/jatin-sethi/7c6f6a95a2e7dd0ea12fce105070aa40