Skip to content

Commit e70ac62

Browse files
committed
Merge branch '2.13'
# Conflicts: # eclipse-collections/pom.xml
2 parents dfe1868 + 7058467 commit e70ac62

File tree

4 files changed

+161
-1
lines changed

4 files changed

+161
-1
lines changed

eclipse-collections/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
</contributors>
3030

3131
<properties>
32-
<version.eclipse-collections>9.1.0</version.eclipse-collections>
32+
<version.eclipse-collections>10.4.0</version.eclipse-collections>
3333

3434
<!-- Generate PackageVersion.java into this directory. -->
3535
<packageVersion.dir>com/fasterxml/jackson/datatype/eclipsecollections</packageVersion.dir>

eclipse-collections/src/main/java/com/fasterxml/jackson/datatype/eclipsecollections/EclipseCollectionsModule.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.fasterxml.jackson.core.Version;
44
import com.fasterxml.jackson.databind.JacksonModule;
55
import com.fasterxml.jackson.datatype.eclipsecollections.deser.pair.PairInstantiators;
6+
import com.fasterxml.jackson.datatype.eclipsecollections.deser.pair.TripleInstantiators;
67

78
/**
89
* Basic Jackson {@link JacksonModule} that adds support for eclipse-collections types.
@@ -30,6 +31,11 @@ public void setupModule(SetupContext context) {
3031
context.addSerializers(new EclipseCollectionsSerializers());
3132

3233
context.addValueInstantiators(new PairInstantiators());
34+
try {
35+
context.addValueInstantiators(new TripleInstantiators());
36+
} catch (NoClassDefFoundError ignored) {
37+
// triples were added in EC 10
38+
}
3339

3440
context.addTypeModifier(new EclipseCollectionsTypeModifier());
3541
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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+
}

eclipse-collections/src/test/java/com/fasterxml/jackson/datatype/eclipsecollections/DeserializerTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import org.eclipse.collections.api.set.MutableSet;
3030
import org.eclipse.collections.api.set.primitive.*;
3131
import org.eclipse.collections.api.tuple.Pair;
32+
import org.eclipse.collections.api.tuple.Triple;
33+
import org.eclipse.collections.api.tuple.Triplet;
3234
import org.eclipse.collections.api.tuple.Twin;
3335
import org.eclipse.collections.api.tuple.primitive.ObjectIntPair;
3436
import org.eclipse.collections.impl.factory.*;
@@ -558,4 +560,32 @@ public void nestedMap() throws Exception {
558560
Assert.assertEquals(json, mapperWithModule().writerFor(type).writeValueAsString(pair));
559561
Assert.assertEquals(pair, mapperWithModule().readValue(json, type));
560562
}
563+
564+
@Test
565+
public void triple() throws Exception {
566+
final ObjectMapper mapper = mapperWithModule();
567+
Triple<String, Integer, Boolean> triple = Tuples.triple("a", 2, false);
568+
String actJson = mapper.writerFor(new TypeReference<Triple<String, Integer, Boolean>>() {})
569+
.writeValueAsString(triple);
570+
String expJson = "{\"one\":\"a\",\"two\":2,\"three\":false}";
571+
Assert.assertEquals(mapper.readTree(expJson), mapper.readTree(actJson));
572+
Assert.assertEquals(
573+
triple,
574+
mapper.readValue(actJson, new TypeReference<Triple<String, Integer, Boolean>>() {})
575+
);
576+
}
577+
578+
@Test
579+
public void triplet() throws Exception {
580+
final ObjectMapper mapper = mapperWithModule();
581+
Triplet<String> triple = Tuples.triplet("a", "b", "c");
582+
String actJson = mapper.writerFor(new TypeReference<Triplet<String>>() {})
583+
.writeValueAsString(triple);
584+
String expJson = "{\"one\":\"a\",\"two\":\"b\",\"three\":\"c\"}";
585+
Assert.assertEquals(mapper.readTree(expJson), mapper.readTree(actJson));
586+
Assert.assertEquals(
587+
triple,
588+
mapper.readValue(actJson, new TypeReference<Triplet<String>>() {})
589+
);
590+
}
561591
}

0 commit comments

Comments
 (0)