Skip to content

Commit 7058467

Browse files
committed
Implement deserialization for triple classes [Adds #85]
1 parent b450ac1 commit 7058467

File tree

4 files changed

+134
-1
lines changed

4 files changed

+134
-1
lines changed

eclipse-collections/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
<dependency>
4242
<groupId>org.eclipse.collections</groupId>
4343
<artifactId>eclipse-collections</artifactId>
44-
<version>9.1.0</version>
44+
<version>10.4.0</version>
4545
</dependency>
4646
</dependencies>
4747

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.Module;
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 Module} 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,97 @@
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 org.eclipse.collections.api.tuple.Triple;
10+
import org.eclipse.collections.api.tuple.Triplet;
11+
import org.eclipse.collections.impl.tuple.Tuples;
12+
13+
public class TripleInstantiators extends ValueInstantiators.Base {
14+
{
15+
// fail early if class does not exist
16+
Triple.class.getName();
17+
}
18+
19+
@Override
20+
public ValueInstantiator findValueInstantiator(
21+
DeserializationConfig config,
22+
BeanDescription beanDesc,
23+
ValueInstantiator defaultInstantiator
24+
) {
25+
if (beanDesc.getBeanClass() == Triple.class) {
26+
JavaType beanType = beanDesc.getType();
27+
return new TripleInstantiator(
28+
beanType,
29+
beanType.containedType(0),
30+
beanType.containedType(1),
31+
beanType.containedType(2));
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, defaultInstantiator);
39+
}
40+
41+
private static class TripleInstantiator extends ValueInstantiator.Base {
42+
private final JavaType oneType;
43+
private final JavaType twoType;
44+
private final JavaType threeType;
45+
46+
private TripleInstantiator(JavaType beanType, JavaType oneType, JavaType twoType, JavaType threeType) {
47+
super(beanType);
48+
this.oneType = oneType;
49+
this.twoType = twoType;
50+
this.threeType = threeType;
51+
}
52+
53+
@Override
54+
public boolean canCreateFromObjectWith() {
55+
return true;
56+
}
57+
58+
@Override
59+
public SettableBeanProperty[] getFromObjectArguments(DeserializationConfig config) {
60+
try {
61+
return new SettableBeanProperty[]{
62+
CreatorProperty.construct(
63+
PropertyName.construct("one"),
64+
oneType,
65+
null,
66+
config.findTypeDeserializer(oneType),
67+
AnnotationCollector.emptyAnnotations(),
68+
null, 0, null, PropertyMetadata.STD_REQUIRED
69+
),
70+
CreatorProperty.construct(
71+
PropertyName.construct("two"),
72+
twoType,
73+
null,
74+
config.findTypeDeserializer(twoType),
75+
AnnotationCollector.emptyAnnotations(),
76+
null, 1, null, PropertyMetadata.STD_REQUIRED
77+
),
78+
CreatorProperty.construct(
79+
PropertyName.construct("three"),
80+
threeType,
81+
null,
82+
config.findTypeDeserializer(threeType),
83+
AnnotationCollector.emptyAnnotations(),
84+
null, 2, null, PropertyMetadata.STD_REQUIRED
85+
)
86+
};
87+
} catch (JsonMappingException e) {
88+
throw new RuntimeException(e);
89+
}
90+
}
91+
92+
@Override
93+
public Object createFromObjectWith(DeserializationContext ctxt, Object[] args) {
94+
return Tuples.triple(args[0], args[1], args[2]);
95+
}
96+
}
97+
}

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
@@ -131,6 +131,8 @@
131131
import org.eclipse.collections.api.set.primitive.MutableShortSet;
132132
import org.eclipse.collections.api.set.primitive.ShortSet;
133133
import org.eclipse.collections.api.tuple.Pair;
134+
import org.eclipse.collections.api.tuple.Triple;
135+
import org.eclipse.collections.api.tuple.Triplet;
134136
import org.eclipse.collections.api.tuple.Twin;
135137
import org.eclipse.collections.api.tuple.primitive.ObjectIntPair;
136138
import org.eclipse.collections.impl.factory.Bags;
@@ -694,4 +696,32 @@ public void nestedMap() throws Exception {
694696
Assert.assertEquals(json, mapperWithModule().writerFor(type).writeValueAsString(pair));
695697
Assert.assertEquals(pair, mapperWithModule().readValue(json, type));
696698
}
699+
700+
@Test
701+
public void triple() throws Exception {
702+
final ObjectMapper mapper = mapperWithModule();
703+
Triple<String, Integer, Boolean> triple = Tuples.triple("a", 2, false);
704+
String actJson = mapper.writerFor(new TypeReference<Triple<String, Integer, Boolean>>() {})
705+
.writeValueAsString(triple);
706+
String expJson = "{\"one\":\"a\",\"two\":2,\"three\":false}";
707+
Assert.assertEquals(mapper.readTree(expJson), mapper.readTree(actJson));
708+
Assert.assertEquals(
709+
triple,
710+
mapper.readValue(actJson, new TypeReference<Triple<String, Integer, Boolean>>() {})
711+
);
712+
}
713+
714+
@Test
715+
public void triplet() throws Exception {
716+
final ObjectMapper mapper = mapperWithModule();
717+
Triplet<String> triple = Tuples.triplet("a", "b", "c");
718+
String actJson = mapper.writerFor(new TypeReference<Triplet<String>>() {})
719+
.writeValueAsString(triple);
720+
String expJson = "{\"one\":\"a\",\"two\":\"b\",\"three\":\"c\"}";
721+
Assert.assertEquals(mapper.readTree(expJson), mapper.readTree(actJson));
722+
Assert.assertEquals(
723+
triple,
724+
mapper.readValue(actJson, new TypeReference<Triplet<String>>() {})
725+
);
726+
}
697727
}

0 commit comments

Comments
 (0)