📄 defaulttypeadapters.java
字号:
@Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append(DefaultDateTypeAdapter.class.getSimpleName()); sb.append('(').append(format.getClass().getSimpleName()).append(')'); return sb.toString(); } } private static class GregorianCalendarTypeAdapter implements JsonSerializer<GregorianCalendar>, JsonDeserializer<GregorianCalendar> { private static final String YEAR = "year"; private static final String MONTH = "month"; private static final String DAY_OF_MONTH = "dayOfMonth"; private static final String HOUR_OF_DAY = "hourOfDay"; private static final String MINUTE = "minute"; private static final String SECOND = "second"; public JsonElement serialize(GregorianCalendar src, Type typeOfSrc, JsonSerializationContext context) { JsonObject obj = new JsonObject(); obj.addProperty(YEAR, src.get(Calendar.YEAR)); obj.addProperty(MONTH, src.get(Calendar.MONTH)); obj.addProperty(DAY_OF_MONTH, src.get(Calendar.DAY_OF_MONTH)); obj.addProperty(HOUR_OF_DAY, src.get(Calendar.HOUR_OF_DAY)); obj.addProperty(MINUTE, src.get(Calendar.MINUTE)); obj.addProperty(SECOND, src.get(Calendar.SECOND)); return obj; } public GregorianCalendar deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonObject obj = json.getAsJsonObject(); int year = obj.get(YEAR).getAsInt(); int month = obj.get(MONTH).getAsInt(); int dayOfMonth = obj.get(DAY_OF_MONTH).getAsInt(); int hourOfDay = obj.get(HOUR_OF_DAY).getAsInt(); int minute = obj.get(MINUTE).getAsInt(); int second = obj.get(SECOND).getAsInt(); return new GregorianCalendar(year, month, dayOfMonth, hourOfDay, minute, second); } @Override public String toString() { return GregorianCalendarTypeAdapter.class.getSimpleName(); } } @SuppressWarnings("unchecked") private static class EnumTypeAdapter<T extends Enum<T>> implements JsonSerializer<T>, JsonDeserializer<T> { public JsonElement serialize(T src, Type typeOfSrc, JsonSerializationContext context) { return new JsonPrimitive(src.name()); } @SuppressWarnings("cast") public T deserialize(JsonElement json, Type classOfT, JsonDeserializationContext context) throws JsonParseException { return (T) Enum.valueOf((Class<T>) classOfT, json.getAsString()); } @Override public String toString() { return EnumTypeAdapter.class.getSimpleName(); } } private static class UrlTypeAdapter implements JsonSerializer<URL>, JsonDeserializer<URL> { public JsonElement serialize(URL src, Type typeOfSrc, JsonSerializationContext context) { return new JsonPrimitive(src.toExternalForm()); } public URL deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { try { return new URL(json.getAsString()); } catch (MalformedURLException e) { throw new JsonParseException(e); } } @Override public String toString() { return UrlTypeAdapter.class.getSimpleName(); } } private static class UriTypeAdapter implements JsonSerializer<URI>, JsonDeserializer<URI> { public JsonElement serialize(URI src, Type typeOfSrc, JsonSerializationContext context) { return new JsonPrimitive(src.toASCIIString()); } public URI deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { try { return new URI(json.getAsString()); } catch (URISyntaxException e) { throw new JsonParseException(e); } } @Override public String toString() { return UriTypeAdapter.class.getSimpleName(); } } private static class UuidTypeAdapter implements JsonSerializer<UUID>, JsonDeserializer<UUID> { public JsonElement serialize(UUID src, Type typeOfSrc, JsonSerializationContext context) { return new JsonPrimitive(src.toString()); } public UUID deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { return UUID.fromString(json.getAsString()); } @Override public String toString() { return UuidTypeAdapter.class.getSimpleName(); } } private static class LocaleTypeAdapter implements JsonSerializer<Locale>, JsonDeserializer<Locale> { public JsonElement serialize(Locale src, Type typeOfSrc, JsonSerializationContext context) { return new JsonPrimitive(src.toString()); } public Locale deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { String locale = json.getAsString(); StringTokenizer tokenizer = new StringTokenizer(locale, "_"); String language = null; String country = null; String variant = null; if (tokenizer.hasMoreElements()) { language = tokenizer.nextToken(); } if (tokenizer.hasMoreElements()) { country = tokenizer.nextToken(); } if (tokenizer.hasMoreElements()) { variant = tokenizer.nextToken(); } if (country == null && variant == null) { return new Locale(language); } else if (variant == null) { return new Locale(language, country); } else { return new Locale(language, country, variant); } } @Override public String toString() { return LocaleTypeAdapter.class.getSimpleName(); } } @SuppressWarnings({ "unchecked" }) private static class CollectionTypeAdapter implements JsonSerializer<Collection>, JsonDeserializer<Collection>, InstanceCreator<Collection> { public JsonElement serialize(Collection src, Type typeOfSrc, JsonSerializationContext context) { if (src == null) { return JsonNull.createJsonNull(); } JsonArray array = new JsonArray(); Type childGenericType = null; if (typeOfSrc instanceof ParameterizedType) { childGenericType = new TypeInfoCollection(typeOfSrc).getElementType(); } for (Object child : src) { Type childType = (childGenericType == null || childGenericType == Object.class) ? child.getClass() : childGenericType; JsonElement element = context.serialize(child, childType); array.add(element); } return array; } public Collection deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (json.isJsonNull()) { return null; } // Use ObjectConstructor to create instance instead of hard-coding a specific type. // This handles cases where users are using their own subclass of Collection. Collection collection = constructCollectionType(typeOfT, context); Type childType = new TypeInfoCollection(typeOfT).getElementType(); for (JsonElement childElement : json.getAsJsonArray()) { if (childElement == null || childElement.isJsonNull()) { collection.add(null); } else { Object value = context.deserialize(childElement, childType); collection.add(value); } } return collection; } private Collection constructCollectionType(Type collectionType, JsonDeserializationContext context) { JsonDeserializationContextDefault contextImpl = (JsonDeserializationContextDefault) context; ObjectConstructor objectConstructor = contextImpl.getObjectConstructor(); return (Collection) objectConstructor.construct(collectionType); } public Collection createInstance(Type type) { return new LinkedList(); } } private static class PropertiesCreator implements InstanceCreator<Properties> { public Properties createInstance(Type type) { return new Properties(); } } @SuppressWarnings("unchecked") static class MapTypeAdapter implements JsonSerializer<Map>, JsonDeserializer<Map>, InstanceCreator<Map> { public JsonElement serialize(Map src, Type typeOfSrc, JsonSerializationContext context) { JsonObject map = new JsonObject(); Type childGenericType = null; if (typeOfSrc instanceof ParameterizedType) { childGenericType = new TypeInfoMap(typeOfSrc).getValueType(); } for (Map.Entry entry : (Set<Map.Entry>) src.entrySet()) { Object value = entry.getValue(); JsonElement valueElement; if (value == null) { valueElement = JsonNull.createJsonNull(); } else { Type childType = (childGenericType == null) ? value.getClass() : childGenericType; valueElement = context.serialize(value, childType); } map.add(String.valueOf(entry.getKey()), valueElement); } return map; } public Map deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { // Use ObjectConstructor to create instance instead of hard-coding a specific type. // This handles cases where users are using their own subclass of Map. Map<Object, Object> map = constructMapType(typeOfT, context); TypeInfoMap mapTypeInfo = new TypeInfoMap(typeOfT); for (Map.Entry<String, JsonElement> entry : json.getAsJsonObject().entrySet()) { Object key = context.deserialize(new JsonPrimitive(entry.getKey()), mapTypeInfo.getKeyType()); Object value = context.deserialize(entry.getValue(), mapTypeInfo.getValueType()); map.put(key, value); } return map; } private Map constructMapType(Type mapType, JsonDeserializationContext context) { JsonDeserializationContextDefault contextImpl = (JsonDeserializationContextDefault) context; ObjectConstructor objectConstructor = contextImpl.getObjectConstructor(); return (Map) objectConstructor.construct(mapType); } public Map createInstance(Type type) { return new LinkedHashMap(); } @Override
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -