Genson :一個快速、可擴展的JSON數據轉換Java類庫
Genson是一個開源的Java類庫,用于實現Java到Json和 Json到Java的轉換。Genson具備可擴展,并且還可配置,快速和易于使用。
示例代碼:
class Person {
String fullName;
// will be converted even if it is private
@JsonProperty private Date birthDate;
Adress adress;
@JsonIgnore public int ignoredField;
private int privateNotDetected;
private Person() {}
@Creator public static Person create() {
return new Person();
}
public String getFullName(){
// will be used instead of direct field access
}
}
class Adress {
final int building;
final String street;
// only a constructor with arguments genson will use it during deserialization
public Adress(@JsonProperty("building") int building, @JsonProperty("street") String street) {
}
}
Person someone = new Person("eugen", new GregorianCalendar(1986, 1, 16).getTime(), new Adress(157, "paris"));
// we obtain the following json string
//{"adress":{"building":157,"street":"paris"},"birthDate":"16 févr. 1986","happy":true,"fullName":"eugen"}
String json = genson.serialize(someone);
// now we deserialize it back
someone = genson.deserialize(json, Person.class);</pre>