|
| 1 | +package com.fasterxml.jackson.databind.deser.impl; |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +import com.fasterxml.jackson.databind.DeserializationContext; |
| 6 | +import com.fasterxml.jackson.databind.JavaType; |
| 7 | +import com.fasterxml.jackson.databind.JsonDeserializer; |
| 8 | +import com.fasterxml.jackson.databind.JsonMappingException; |
| 9 | +import com.fasterxml.jackson.databind.deser.std.StdDelegatingDeserializer; |
| 10 | +import com.fasterxml.jackson.databind.type.TypeFactory; |
| 11 | +import com.fasterxml.jackson.databind.util.Converter; |
| 12 | + |
| 13 | +/** |
| 14 | + * Helper class used to contain logic for deserializing "special" containers |
| 15 | + * from {@code java.util.Collections} and {@code java.util.Arrays}. This is needed |
| 16 | + * because they do not have usable no-arguments constructor: however, are easy enough |
| 17 | + * to deserialize using delegating deserializer. |
| 18 | + * |
| 19 | + * @since 2.9.4 |
| 20 | + */ |
| 21 | +public abstract class JavaUtilCollectionsDeserializers |
| 22 | +{ |
| 23 | + private final static int TYPE_SINGLETON_SET = 1; |
| 24 | + private final static int TYPE_SINGLETON_LIST = 2; |
| 25 | + private final static int TYPE_SINGLETON_MAP = 3; |
| 26 | + |
| 27 | + private final static int TYPE_UNMODIFIABLE_SET = 4; |
| 28 | + private final static int TYPE_UNMODIFIABLE_LIST = 5; |
| 29 | + private final static int TYPE_UNMODIFIABLE_MAP = 6; |
| 30 | + |
| 31 | + public final static int TYPE_AS_LIST = 7; |
| 32 | + |
| 33 | + // 10-Jan-2018, tatu: There are a few "well-known" special containers in JDK too: |
| 34 | + |
| 35 | + private final static Class<?> CLASS_AS_ARRAYS_LIST = Arrays.asList(null, null).getClass(); |
| 36 | + |
| 37 | + private final static Class<?> CLASS_SINGLETON_SET = Collections.singleton(Boolean.TRUE).getClass(); |
| 38 | + private final static Class<?> CLASS_SINGLETON_LIST = Collections.singletonList(Boolean.TRUE).getClass(); |
| 39 | + private final static Class<?> CLASS_SINGLETON_MAP = Collections.singletonMap("a", "b").getClass(); |
| 40 | + |
| 41 | + public static JsonDeserializer<?> findForCollection(DeserializationContext ctxt, |
| 42 | + JavaType type) |
| 43 | + throws JsonMappingException |
| 44 | + { |
| 45 | + JavaUtilCollectionsConverter conv; |
| 46 | + |
| 47 | + // 10-Jan-2017, tatu: Some types from `java.util.Collections`/`java.util.Arrays` need bit of help... |
| 48 | + if (type.hasRawClass(CLASS_AS_ARRAYS_LIST)) { |
| 49 | + conv = converter(TYPE_AS_LIST, type); |
| 50 | + } else if (type.hasRawClass(CLASS_SINGLETON_LIST)) { |
| 51 | + conv = converter(TYPE_SINGLETON_LIST, type); |
| 52 | + } else if (type.hasRawClass(CLASS_SINGLETON_SET)) { |
| 53 | + conv = converter(TYPE_SINGLETON_SET, type); |
| 54 | + } else { |
| 55 | + return null; |
| 56 | + } |
| 57 | + return new StdDelegatingDeserializer<Object>(conv); |
| 58 | + } |
| 59 | + |
| 60 | + public static JsonDeserializer<?> findForMap(DeserializationContext ctxt, |
| 61 | + JavaType type) |
| 62 | + throws JsonMappingException |
| 63 | + { |
| 64 | + JavaUtilCollectionsConverter conv; |
| 65 | + |
| 66 | + // 10-Jan-2017, tatu: Some types from `java.util.Collections`/`java.util.Arrays` need bit of help... |
| 67 | + if (type.hasRawClass(CLASS_SINGLETON_MAP)) { |
| 68 | + conv = converter(TYPE_SINGLETON_MAP, type); |
| 69 | + } else { |
| 70 | + return null; |
| 71 | + } |
| 72 | + return new StdDelegatingDeserializer<Object>(conv); |
| 73 | + } |
| 74 | + |
| 75 | + static JavaUtilCollectionsConverter converter(int kind, JavaType concreteType) |
| 76 | + { |
| 77 | + JavaType inputType; |
| 78 | + |
| 79 | + switch (kind) { |
| 80 | + case TYPE_SINGLETON_SET: |
| 81 | + case TYPE_UNMODIFIABLE_SET: |
| 82 | + inputType = concreteType.findSuperType(Set.class); |
| 83 | + break; |
| 84 | + |
| 85 | + case TYPE_SINGLETON_MAP: |
| 86 | + case TYPE_UNMODIFIABLE_MAP: |
| 87 | + inputType = concreteType.findSuperType(Map.class); |
| 88 | + break; |
| 89 | + |
| 90 | + case TYPE_SINGLETON_LIST: |
| 91 | + case TYPE_UNMODIFIABLE_LIST: |
| 92 | + case TYPE_AS_LIST: |
| 93 | + default: |
| 94 | + inputType = concreteType.findSuperType(List.class); |
| 95 | + break; |
| 96 | + } |
| 97 | + return new JavaUtilCollectionsConverter(kind, inputType); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Implementation used for converting from various generic container |
| 102 | + * types ({@link java.util.Set}, {@link java.util.List}, {@link java.util.Map}) |
| 103 | + * into more specific implementations accessible via {@code java.util.Collections}. |
| 104 | + */ |
| 105 | + private static class JavaUtilCollectionsConverter implements Converter<Object,Object> |
| 106 | + { |
| 107 | + private final JavaType _inputType; |
| 108 | + |
| 109 | + private final int _kind; |
| 110 | + |
| 111 | + private JavaUtilCollectionsConverter(int kind, JavaType inputType) { |
| 112 | + _inputType = inputType; |
| 113 | + _kind = kind; |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public Object convert(Object value) { |
| 118 | + if (value == null) { // is this legal to get? |
| 119 | + return null; |
| 120 | + } |
| 121 | + |
| 122 | + switch (_kind) { |
| 123 | + case TYPE_SINGLETON_SET: |
| 124 | + { |
| 125 | + Set<?> set = (Set<?>) value; |
| 126 | + _checkSingleton(set.size()); |
| 127 | + return Collections.singleton(set.iterator().next()); |
| 128 | + } |
| 129 | + case TYPE_SINGLETON_LIST: |
| 130 | + { |
| 131 | + List<?> list = (List<?>) value; |
| 132 | + _checkSingleton(list.size()); |
| 133 | + return Collections.singletonList(list.get(0)); |
| 134 | + } |
| 135 | + case TYPE_SINGLETON_MAP: |
| 136 | + { |
| 137 | + Map<?,?> map = (Map<?,?>) value; |
| 138 | + _checkSingleton(map.size()); |
| 139 | + Map.Entry<?,?> entry = map.entrySet().iterator().next(); |
| 140 | + return Collections.singletonMap(entry.getKey(), entry.getValue()); |
| 141 | + } |
| 142 | + |
| 143 | + case TYPE_UNMODIFIABLE_SET: |
| 144 | + return Collections.unmodifiableSet((Set<?>) value); |
| 145 | + case TYPE_UNMODIFIABLE_LIST: |
| 146 | + return Collections.unmodifiableList((List<?>) value); |
| 147 | + case TYPE_UNMODIFIABLE_MAP: |
| 148 | + return Collections.unmodifiableMap((Map<?,?>) value); |
| 149 | + |
| 150 | + case TYPE_AS_LIST: |
| 151 | + default: |
| 152 | + // Here we do not actually care about impl type, just return List as-is: |
| 153 | + return value; |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + @Override |
| 158 | + public JavaType getInputType(TypeFactory typeFactory) { |
| 159 | + return _inputType; |
| 160 | + } |
| 161 | + |
| 162 | + @Override |
| 163 | + public JavaType getOutputType(TypeFactory typeFactory) { |
| 164 | + // we don't actually care, so: |
| 165 | + return _inputType; |
| 166 | + } |
| 167 | + |
| 168 | + private void _checkSingleton(int size) { |
| 169 | + if (size != 1) { |
| 170 | + // not the best error ever but... has to do |
| 171 | + throw new IllegalArgumentException("Can not deserialize Singleton container from "+size+" entries"); |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | +} |
0 commit comments