Skip to content

Commit 18a13c3

Browse files
Merge pull request #6671 from ibi-group/geocoder-id
Fix serialization of FeedScopedId in geocoder sandbox
2 parents a088d45 + d6bc473 commit 18a13c3

File tree

2 files changed

+105
-2
lines changed

2 files changed

+105
-2
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package org.opentripplanner.ext.geocoder;
2+
3+
import static org.opentripplanner.ext.geocoder.StopCluster.LocationType.STOP;
4+
import static org.opentripplanner.test.support.JsonAssertions.assertEqualJson;
5+
import static org.opentripplanner.transit.model._data.TimetableRepositoryForTest.id;
6+
7+
import java.util.List;
8+
import java.util.Set;
9+
import org.junit.jupiter.api.Test;
10+
import org.opentripplanner.framework.json.ObjectMappers;
11+
12+
class SerializationTest {
13+
14+
private static final String STOP_CLUSTER_JSON =
15+
"""
16+
{
17+
"primary" : {
18+
"id" : "F:123",
19+
"code" : "aaa",
20+
"type" : "STOP",
21+
"name" : "A stop",
22+
"coordinate" : {
23+
"lat" : 1.0,
24+
"lon" : 2.0
25+
},
26+
"modes" : [
27+
"RAIL"
28+
],
29+
"agencies" : [
30+
{
31+
"id" : "F:a1",
32+
"name" : "Agency"
33+
}
34+
],
35+
"feedPublisher" : {
36+
"name" : "Publisher"
37+
}
38+
},
39+
"secondaries" : [
40+
{
41+
"id" : "F:123",
42+
"code" : "aaa",
43+
"type" : "STOP",
44+
"name" : "A stop",
45+
"coordinate" : {
46+
"lat" : 1.0,
47+
"lon" : 2.0
48+
},
49+
"modes" : [
50+
"RAIL"
51+
],
52+
"agencies" : [
53+
{
54+
"id" : "F:a1",
55+
"name" : "Agency"
56+
}
57+
],
58+
"feedPublisher" : {
59+
"name" : "Publisher"
60+
}
61+
}
62+
]
63+
}
64+
""";
65+
66+
@Test
67+
void serialize() {
68+
var mapper = ObjectMappers.ignoringExtraFields();
69+
70+
var loc = new StopCluster.Location(
71+
id("123"),
72+
"aaa",
73+
STOP,
74+
"A stop",
75+
new StopCluster.Coordinate(1.0d, 2.0d),
76+
Set.of("RAIL"),
77+
List.of(new StopCluster.Agency(id("a1"), "Agency")),
78+
new StopCluster.FeedPublisher("Publisher")
79+
);
80+
var cluster = new StopCluster(loc, List.of(loc));
81+
82+
var json = mapper.valueToTree(cluster);
83+
84+
assertEqualJson(STOP_CLUSTER_JSON, json);
85+
}
86+
}

application/src/ext/java/org/opentripplanner/ext/geocoder/StopCluster.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package org.opentripplanner.ext.geocoder;
22

3+
import com.fasterxml.jackson.core.JsonGenerator;
4+
import com.fasterxml.jackson.databind.JsonSerializer;
5+
import com.fasterxml.jackson.databind.SerializerProvider;
6+
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
7+
import java.io.IOException;
38
import java.util.Collection;
49
import java.util.List;
510
import java.util.Objects;
@@ -24,7 +29,10 @@ public record Coordinate(double lat, double lon) {}
2429
/**
2530
* Easily serializable version of an agency
2631
*/
27-
public record Agency(FeedScopedId id, String name) {}
32+
public record Agency(
33+
@JsonSerialize(using = FeedScopedIdSerializer.class) FeedScopedId id,
34+
String name
35+
) {}
2836

2937
/**
3038
* Easily serializable version of a feed publisher
@@ -37,7 +45,7 @@ public enum LocationType {
3745
}
3846

3947
public record Location(
40-
FeedScopedId id,
48+
@JsonSerialize(using = FeedScopedIdSerializer.class) FeedScopedId id,
4149
@Nullable String code,
4250
LocationType type,
4351
String name,
@@ -55,4 +63,13 @@ public record Location(
5563
Objects.requireNonNull(agencies);
5664
}
5765
}
66+
67+
private static class FeedScopedIdSerializer extends JsonSerializer<FeedScopedId> {
68+
69+
@Override
70+
public void serialize(FeedScopedId value, JsonGenerator gen, SerializerProvider provider)
71+
throws IOException {
72+
gen.writeString(value.toString());
73+
}
74+
}
5875
}

0 commit comments

Comments
 (0)