Jackson

  接上一篇文章Json解析工具Jackson(簡單應用),jackson在實際應用中給咱們提供了一系列註解,提升了開發的靈活性,下面介紹一下最經常使用的一些註解java

  • @JsonIgnoreProperties

         此註解是類註解,做用是json序列化時將java bean中的一些屬性忽略掉,序列化和反序列化都受影響。程序員

  • @JsonIgnore

         此註解用於屬性或者方法上(最好是屬性上),做用和上面的@JsonIgnoreProperties同樣。json

  • @JsonFormat

        此註解用於屬性或者方法上(最好是屬性上),能夠方便的把Date類型直接轉化爲咱們想要的模式,好比@JsonFormat(pattern = "yyyy-MM-dd HH-mm-ss")app

  • @JsonSerialize

        此註解用於屬性或者getter方法上,用於在序列化時嵌入咱們自定義的代碼,好比序列化一個double時在其後面限制兩位小數點。ide

public class CustomDoubleSerialize extends JsonSerializer<Double> {

	private DecimalFormat df = new DecimalFormat("##.00");

	@Override
	public void serialize(Double value, JsonGenerator jgen,
			SerializerProvider provider) throws IOException,
			JsonProcessingException {

		jgen.writeString(df.format(value));
	}
}
  • @JsonDeserialize

         此註解用於屬性或者setter方法上,用於在反序列化時能夠嵌入咱們自定義的代碼,相似於上面的@JsonSerialize工具

public class CustomDateDeserialize extends JsonDeserializer<Date> {

	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

	@Override
	public Date deserialize(JsonParser jp, DeserializationContext ctxt)
			throws IOException, JsonProcessingException {

		Date date = null;
		try {
			date = sdf.parse(jp.getText());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
}
  • 完整例子
//表示序列化時忽略的屬性
@JsonIgnoreProperties(value = { "word" })
public class Person {
	private String name;
	private int age;
	private boolean sex;
	private Date birthday;
	private String word;
	private double salary;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public boolean isSex() {
		return sex;
	}

	public void setSex(boolean sex) {
		this.sex = sex;
	}

	public Date getBirthday() {
		return birthday;
	}

	// 反序列化一個固定格式的Date
	@JsonDeserialize(using = CustomDateDeserialize.class)
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public String getWord() {
		return word;
	}

	public void setWord(String word) {
		this.word = word;
	}

	// 序列化指定格式的double格式
	@JsonSerialize(using = CustomDoubleSerialize.class)
	public double getSalary() {
		return salary;
	}

	public void setSalary(double salary) {
		this.salary = salary;
	}

	public Person(String name, int age) {
		this.name = name;
		this.age = age;
	}

	public Person(String name, int age, boolean sex, Date birthday,
			String word, double salary) {
		super();
		this.name = name;
		this.age = age;
		this.sex = sex;
		this.birthday = birthday;
		this.word = word;
		this.salary = salary;
	}

	public Person() {
	}

	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + ", sex=" + sex
				+ ", birthday=" + birthday + ", word=" + word + ", salary="
				+ salary + "]";
	}

}
public class Demo {
	public static void main(String[] args) {

		writeJsonObject();

		// readJsonObject();
	}

	// 直接寫入一個對象(所謂序列化)
	public static void writeJsonObject() {
		ObjectMapper mapper = new ObjectMapper();
		Person person = new Person("nomouse", 25, true, new Date(), "程序員",
				2500.0);
		try {
			mapper.writeValue(new File("c:/person.json"), person);
		} catch (JsonGenerationException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// 直接將一個json轉化爲對象(所謂反序列化)
	public static void readJsonObject() {
		ObjectMapper mapper = new ObjectMapper();

		try {
			Person person = mapper.readValue(new File("c:/person.json"),
					Person.class);
			System.out.println(person.toString());
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}


}
相關文章
相關標籤/搜索