JYaml是Java語言使用的Yaml庫,能夠用於讀寫Yaml文檔,並能夠實現Java對象和Yaml代碼間的轉換。java
Yaml技術主頁:http://yaml.org/
app
JYaml下載地址: http://sourceforge.net/projects/jyaml
函數
下載下來是一個jar包(jyaml-1.3.jar) ,引入工程後便可使用,個人JDK版本爲1.8ui
設咱們有一個名爲Hero的類,將Hero類轉變爲對應yaml代碼的寫法爲:this
String yaml = Yaml.dump(hero);
再將這段代碼轉換爲Hero類的寫法爲:
.net
Hero result = (Hero) Yaml.load(yaml);
另外一種寫法也能夠:
code
Hero result = Yaml.loadType(yaml, Hero.class);
下面咱們舉個例子,來講明這兩段代碼的用法。orm
類Hero的實現以下:xml
import java.util.Date; import java.util.LinkedList; public class Hero { // 編號 private long id; // 名稱 private String name; // 等級 private int level; // 性別 private Boolean gender; // 出生日期 private Date birthDate; // 所獲榮譽 private String[] honors; // 改動記錄 private LinkedList<String> logs = new LinkedList<String>(); // 配偶 private Hero spouse; // 子女 private Hero[] children; // Getter & Setter public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getLevel() { return level; } public void setLevel(int level) { this.level = level; } public Boolean getGender() { return gender; } public void setGender(Boolean gender) { this.gender = gender; } public Date getBirthDate() { return birthDate; } public void setBirthDate(Date birthDate) { this.birthDate = birthDate; } public String[] getHonors() { return honors; } public void setHonors(String[] honors) { this.honors = honors; } public LinkedList<String> getLog() { return logs; } public void setLog(LinkedList<String> logs) { this.logs = logs; } public Hero getSpouse() { return spouse; } public void setSpouse(Hero spouse) { this.spouse = spouse; } public Hero[] getChildren() { return children; } public void setChildren(Hero[] children) { this.children = children; } public String toString() { StringBuilder sb = new StringBuilder(); sb.append("id:" + id + "\n"); sb.append("name:" + name + "\n"); sb.append("level:" + level + "\n"); sb.append("gender:" + gender + "\n"); sb.append("birthDate:" + birthDate.toString() + "\n"); sb.append("honors:" + String.join(",", honors) + "\n"); sb.append("logs:" + String.join(",", logs) + "\n"); sb.append("spouse:" + spouse.name + "\n"); sb.append("children:"); for (int i = 0; i < children.length; i++) { if (i < children.length - 1) { sb.append(children[i].name + ","); } else { sb.append(children[i].name); } } sb.append("\n"); return sb.toString(); } }
在main函數中新建一Hero類,代碼以下:對象
Hero hero = new Hero(); hero.setId(1L); hero.setName("Tsybius2014"); hero.setLevel(5); hero.setGender(true); try { hero.setBirthDate((new SimpleDateFormat("yyyy/MM/dd")).parse("1991/09/20")); } catch (ParseException e) { e.printStackTrace(); } hero.setHonors(new String[] {"abcdefg", "hijklmn", "opqrst", "uvwxyz"}); hero.getLog().addLast("log1"); hero.getLog().addLast("log2"); hero.getLog().addLast("log3"); Hero spouse = new Hero(); spouse.setId(2L); spouse.setName("Galatea"); spouse.setGender(false); spouse.setHonors(new String[] {"Honor1", "Honor2"}); hero.setSpouse(spouse); Hero child1 = new Hero(); child1.setId(3L); child1.setName("Quintus"); Hero child2 = new Hero(); child2.setId(4L); child2.setName("Gaius"); hero.setChildren(new Hero[] {child1, child2}); String yaml = Yaml.dump(hero); System.out.println(yaml);
最後輸出的結果爲:
--- !Hero birthDate: !java.util.Date "685296000000" children: !Hero[] - !Hero id: !java.lang.Long 3 name: Quintus - !Hero id: !java.lang.Long 4 name: Gaius gender: true honors: !java.lang.String[] - abcdefg - hijklmn - opqrst - uvwxyz id: !java.lang.Long 1 level: 5 log: !java.util.LinkedList - log1 - log2 - log3 name: Tsybius2014 spouse: !Hero gender: false honors: !java.lang.String[] - Honor1 - Honor2 id: !java.lang.Long 2 name: Galatea
現將這段yaml代碼再反序列化到對象中,代碼以下:
Hero result = (Hero) Yaml.load(yaml); //Hero result = Yaml.loadType(yaml, Hero.class); //另外一種方法 System.out.println(result);
控制檯輸出的結果爲:
id:1 name:Tsybius2014 level:5 gender:true birthDate:Fri Sep 20 00:00:00 CST 1991 honors:abcdefg,hijklmn,opqrst,uvwxyz logs:log1,log2,log3 spouse:Galatea children:Quintus,Gaius
最後順便再提一下,Java對象與XML間的序列化與反序列化,能夠經過下面兩個函數實現
import java.beans.XMLDecoder; import java.beans.XMLEncoder; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; public class XmlParser { /** * 將對象序列化爲XML * @param entity * @return */ public static <T> String parseToXml(T entity) { ByteArrayOutputStream out = new ByteArrayOutputStream(); XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(out)); encoder.writeObject(entity); encoder.close(); return out.toString(); } /** * 將XML反序列化到指定對象 * @param xml * @return */ @SuppressWarnings("unchecked") public static <T> T loadFromXml(String xml) { ByteArrayInputStream in = new ByteArrayInputStream(xml.getBytes()); XMLDecoder decoder = new XMLDecoder(new BufferedInputStream(in)); decoder.close(); return (T) decoder.readObject(); } }
END