日期:2014-05-16  浏览次数:20379 次

<二> Google Gson 转换Json字符串和对象(日期,枚举的转化)

例子肯定能运行成功,考虑到面向对象,所以多写了几个类。

?

一、日期的转换需要在Create GsonBuilder时注册日期的格式。

?

二、对于枚举类,如果是默认转换,则不需要做任何操作,Gson会将枚举类转换为字符串。但如果需要自定义规则来进行枚举类和字符串的转换,则需要自定义相关的类实现JsonSerializer和JsonDeserializer类。

?

三、含有枚举类型的Java Object:NucleonEvent

?

/**
 * 
 */
package com.ebay.montage.pm.model;

import java.util.List;
import java.util.Map;

/**
 * @author Josh Wang(Sheng)
 *
 * @email  swang6@ebay.com
 */
public class NucleonEvent {
	
	private String id;
	
	private String host;
	
	private String odbFunction;
	
	private String source;
	
	private String format;
	
	private String classification;
	
	private long detectionTime;
	
	private long reportedTime;
	
	private Severity severity;
	
	private NucleonEventType type;
	
	private NucleonMetadata metadata;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public NucleonEventType getType() {
		return type;
	}

	public void setType(NucleonEventType type) {
		this.type = type;
	}
	
	public enum NucleonEventType {
		
		nwmon_checkMem,nwmon_checkCPU_busyio;

	}
	
	public enum Severity {
		low;
	}
	
	public static class NucleonMetadata {
		private boolean passed;
		
		private String name;
		
		private List<Map<String, String>> msgs;
		
		private Map<String , Object> resultDataMap;

		public boolean isPassed() {
			return passed;
		}

		public void setPassed(boolean passed) {
			this.passed = passed;
		}

		public String getName() {
			return name;
		}

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

		public List<Map<String, String>> getMsgs() {
			return msgs;
		}

		public void setMsgs(List<Map<String, String>> msgs) {
			this.msgs = msgs;
		}

		public Map<String, Object> getResultDataMap() {
			return resultDataMap;
		}

		public void setResultDataMap(Map<String, Object> resultDataMap) {
			this.resultDataMap = resultDataMap;
		}
		
		
	}

	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public String getOdbFunction() {
		return odbFunction;
	}

	public void setOdbFunction(String odbFunction) {
		this.odbFunction = odbFunction;
	}

	public String getSource() {
		return source;
	}

	public void setSource(String source) {
		this.source = source;
	}

	public String getClassification() {
		return classification;
	}

	public void setClassification(String classification) {
		this.classification = classification;
	}

	public long getDetectionTime() {
		return detectionTime;
	}

	public void setDetectionTime(long detectionTime) {
		this.detectionTime = detectionTime;
	}

	public long getReportedTime() {
		return reportedTime;
	}

	public void setReportedTime(long reportedTime) {
		this.reportedTime = reportedTime;
	}

	public Severity getSeverity() {
		return severity;
	}

	public void setSeverity(Severity severity) {
		this.severity = severity;
	}

	public String getFormat() {
		return format;
	}

	public void setFormat(String format) {
		this.format = format;
	}

	public NucleonMetadata getMetadata() {
		return metadata;
	}

	public void setMetadata(NucleonMetadata metadata) {
		this.metadata = metadata;
	}

	@Override
	public String toString() {
		return "NucleonEvent [id=" + id + ", host=" + host + ", odbFunction="
				+ odbFunction + ", source=" + source + ", format=" + format
				+ ", classification=" + classification + ", detectionTime="
				+ detectionTime + ", reportedTime=" + reportedTime
				+ ", severity=" + severity + ", type=" + type + ", metadata="
				+ metadata.getName() + ":" + metadata.isPassed() + metadata.getMsgs() + "]";
	}

	

	
}

?

?

四、抽象的转换类: EventDataparser

?

/**
 * 
 */
package com.ebay.montage.pm.handler;