001package com.thed.util.gson; 002import com.google.gson.JsonDeserializationContext; 003 import com.google.gson.JsonDeserializer; 004 import com.google.gson.JsonElement; 005 import com.google.gson.JsonPrimitive; 006 import com.google.gson.JsonSerializationContext; 007 import com.google.gson.JsonSerializer; 008 009 import java.lang.reflect.Type; 010 011 import javax.xml.datatype.DatatypeFactory; 012 import javax.xml.datatype.XMLGregorianCalendar; 013 014 public class XGCalConverter 015 { 016 public static class Serializer implements JsonSerializer 017 { 018 public Serializer() 019 { 020 super(); 021 } 022 023 public JsonElement serialize(Object t, Type type, 024 JsonSerializationContext jsonSerializationContext) 025 { 026 XMLGregorianCalendar xgcal=(XMLGregorianCalendar)t; 027 return new JsonPrimitive(xgcal.toXMLFormat()); 028 } 029 } 030 public static class Deserializer implements JsonDeserializer 031 { 032 033 public Object deserialize(JsonElement jsonElement, Type type, 034 JsonDeserializationContext jsonDeserializationContext) 035 { 036 try 037 { 038 return DatatypeFactory.newInstance().newXMLGregorianCalendar(jsonElement.getAsString()); 039 } 040 catch(Exception ex) 041 { 042 ex.printStackTrace(); 043 return null; 044 } 045 } 046 } 047 }