|
| 1 | +package com.fasterxml.jackson.datatype.eclipsecollections.deser.pair; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.*; |
| 4 | +import com.fasterxml.jackson.databind.deser.CreatorProperty; |
| 5 | +import com.fasterxml.jackson.databind.deser.SettableBeanProperty; |
| 6 | +import com.fasterxml.jackson.databind.deser.ValueInstantiator; |
| 7 | +import com.fasterxml.jackson.databind.deser.ValueInstantiators; |
| 8 | +import com.fasterxml.jackson.databind.introspect.AnnotationCollector; |
| 9 | +import com.fasterxml.jackson.databind.jsontype.TypeDeserializer; |
| 10 | +import org.eclipse.collections.api.tuple.Triple; |
| 11 | +import org.eclipse.collections.api.tuple.Triplet; |
| 12 | +import org.eclipse.collections.impl.tuple.Tuples; |
| 13 | + |
| 14 | +public class TripleInstantiators extends ValueInstantiators.Base { |
| 15 | + { |
| 16 | + // fail early if class does not exist |
| 17 | + Triple.class.getName(); |
| 18 | + } |
| 19 | + |
| 20 | + @Override |
| 21 | + public ValueInstantiator findValueInstantiator(DeserializationConfig config, BeanDescription beanDesc) { |
| 22 | + if (beanDesc.getBeanClass() == Triple.class) { |
| 23 | + JavaType beanType = beanDesc.getType(); |
| 24 | + return new TripleInstantiator( |
| 25 | + beanType, |
| 26 | + beanType.containedType(0), |
| 27 | + beanType.containedType(1), |
| 28 | + beanType.containedType(2), |
| 29 | + // type deserializers are filled in by createContextual |
| 30 | + null, null, null |
| 31 | + ); |
| 32 | + } |
| 33 | + if (beanDesc.getBeanClass() == Triplet.class) { |
| 34 | + JavaType beanType = beanDesc.getType(); |
| 35 | + JavaType singleType = beanType.containedType(0); |
| 36 | + return new TripleInstantiator(beanType, singleType, singleType, singleType); |
| 37 | + } |
| 38 | + return super.findValueInstantiator(config, beanDesc); |
| 39 | + } |
| 40 | + |
| 41 | + private static class TripleInstantiator extends ValueInstantiator.Base { |
| 42 | + private final JavaType beanType; |
| 43 | + private final JavaType oneType; |
| 44 | + private final JavaType twoType; |
| 45 | + private final JavaType threeType; |
| 46 | + private final TypeDeserializer oneTypeDeser; |
| 47 | + private final TypeDeserializer twoTypeDeser; |
| 48 | + private final TypeDeserializer threeTypeDeser; |
| 49 | + |
| 50 | + TripleInstantiator( |
| 51 | + JavaType beanType, |
| 52 | + JavaType oneType, JavaType twoType, JavaType threeType |
| 53 | + ) { |
| 54 | + // type deserializers are filled in by createContextual |
| 55 | + this(beanType, oneType, twoType, threeType, null, null, null); |
| 56 | + } |
| 57 | + |
| 58 | + private TripleInstantiator( |
| 59 | + JavaType beanType, |
| 60 | + JavaType oneType, JavaType twoType, JavaType threeType, |
| 61 | + TypeDeserializer oneTypeDeser, TypeDeserializer twoTypeDeser, TypeDeserializer threeTypeDeser |
| 62 | + ) { |
| 63 | + super(beanType); |
| 64 | + this.beanType = beanType; |
| 65 | + this.oneType = oneType; |
| 66 | + this.twoType = twoType; |
| 67 | + this.threeType = threeType; |
| 68 | + this.oneTypeDeser = oneTypeDeser; |
| 69 | + this.twoTypeDeser = twoTypeDeser; |
| 70 | + this.threeTypeDeser = threeTypeDeser; |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public boolean canCreateFromObjectWith() { |
| 75 | + return true; |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public SettableBeanProperty[] getFromObjectArguments(DeserializationConfig config) { |
| 80 | + return new SettableBeanProperty[]{ |
| 81 | + CreatorProperty.construct( |
| 82 | + PropertyName.construct("one"), |
| 83 | + oneType, |
| 84 | + null, |
| 85 | + oneTypeDeser, |
| 86 | + AnnotationCollector.emptyAnnotations(), |
| 87 | + null, 0, null, PropertyMetadata.STD_REQUIRED |
| 88 | + ), |
| 89 | + CreatorProperty.construct( |
| 90 | + PropertyName.construct("two"), |
| 91 | + twoType, |
| 92 | + null, |
| 93 | + twoTypeDeser, |
| 94 | + AnnotationCollector.emptyAnnotations(), |
| 95 | + null, 1, null, PropertyMetadata.STD_REQUIRED |
| 96 | + ), |
| 97 | + CreatorProperty.construct( |
| 98 | + PropertyName.construct("three"), |
| 99 | + threeType, |
| 100 | + null, |
| 101 | + threeTypeDeser, |
| 102 | + AnnotationCollector.emptyAnnotations(), |
| 103 | + null, 2, null, PropertyMetadata.STD_REQUIRED |
| 104 | + ) |
| 105 | + }; |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public ValueInstantiator createContextual(DeserializationContext ctxt, BeanDescription beanDesc) { |
| 110 | + return new TripleInstantiator( |
| 111 | + beanType, |
| 112 | + oneType, twoType, threeType, |
| 113 | + ctxt.findTypeDeserializer(oneType), |
| 114 | + ctxt.findTypeDeserializer(twoType), |
| 115 | + ctxt.findTypeDeserializer(threeType) |
| 116 | + ); |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public Object createFromObjectWith(DeserializationContext ctxt, Object[] args) { |
| 121 | + return Tuples.triple(args[0], args[1], args[2]); |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments