-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Jackson 3.x GeoJson (de-)serializers #5102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
schauder
wants to merge
4
commits into
main
Choose a base branch
from
issue/5100-geojson
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+469
−22
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
...b/src/main/java/org/springframework/data/mongodb/config/GeoJsonJackson3Configuration.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| /* | ||
| * Copyright 2015-2025 the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.springframework.data.mongodb.config; | ||
|
|
||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.data.mongodb.core.geo.GeoJsonJackson3Module; | ||
| import org.springframework.data.web.config.SpringDataJackson3Modules; | ||
|
|
||
| /** | ||
| * Configuration class to expose {@link GeoJsonJackson3Module} as a Spring bean. | ||
| * | ||
| * @author Jens Schauder | ||
| */ | ||
| public class GeoJsonJackson3Configuration implements SpringDataJackson3Modules { | ||
|
|
||
| @Bean | ||
| public GeoJsonJackson3Module geoJsonModule() { | ||
| return new GeoJsonJackson3Module(); | ||
| } | ||
| } | ||
296 changes: 296 additions & 0 deletions
296
...ongodb/src/main/java/org/springframework/data/mongodb/core/geo/GeoJsonJackson3Module.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,296 @@ | ||
| package org.springframework.data.mongodb.core.geo; | ||
|
|
||
| import tools.jackson.core.JacksonException; | ||
| import tools.jackson.core.JsonGenerator; | ||
| import tools.jackson.core.JsonParser; | ||
| import tools.jackson.core.Version; | ||
| import tools.jackson.databind.DeserializationContext; | ||
| import tools.jackson.databind.JacksonModule; | ||
| import tools.jackson.databind.JsonNode; | ||
| import tools.jackson.databind.SerializationContext; | ||
| import tools.jackson.databind.ValueDeserializer; | ||
| import tools.jackson.databind.ValueSerializer; | ||
| import tools.jackson.databind.module.SimpleDeserializers; | ||
| import tools.jackson.databind.module.SimpleSerializers; | ||
| import tools.jackson.databind.node.ArrayNode; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.Iterator; | ||
| import java.util.List; | ||
|
|
||
| import org.jspecify.annotations.Nullable; | ||
| import org.springframework.data.geo.Point; | ||
| import org.springframework.util.Assert; | ||
|
|
||
| public class GeoJsonJackson3Module { | ||
|
|
||
| private static Version version = new Version(4, 1, 0, null, "org.springframework.data", | ||
| "spring-data-mongodb-geojson"); | ||
|
|
||
| public static class Serializers extends JacksonModule { | ||
|
|
||
| @Override | ||
| public String getModuleName() { | ||
| return "Spring Data MongoDB GeoJson - Serializers"; | ||
| } | ||
|
|
||
| @Override | ||
| public Version version() { | ||
|
|
||
| return version; | ||
| } | ||
|
|
||
| @Override | ||
| public void setupModule(SetupContext ctx) { | ||
|
|
||
| final SimpleSerializers serializers = new SimpleSerializers(); | ||
|
|
||
| serializers.addSerializer(GeoJsonPoint.class, new GeoJsonPointSerializer()); | ||
| serializers.addSerializer(GeoJsonMultiPoint.class, new GeoJsonMultiPointSerializer()); | ||
| serializers.addSerializer(GeoJsonLineString.class, new GeoJsonLineStringSerializer()); | ||
| serializers.addSerializer(GeoJsonMultiLineString.class, new GeoJsonMultiLineStringSerializer()); | ||
| serializers.addSerializer(GeoJsonPolygon.class, new GeoJsonPolygonSerializer()); | ||
| serializers.addSerializer(GeoJsonMultiPolygon.class, new GeoJsonMultiPolygonSerializer()); | ||
|
|
||
| ctx.addSerializers(serializers); | ||
| } | ||
| } | ||
|
|
||
| public static class Deserializers extends JacksonModule { | ||
|
|
||
| @Override | ||
| public String getModuleName() { | ||
| return "Spring Data MongoDB GeoJson - Deserializers"; | ||
| } | ||
|
|
||
| @Override | ||
| public Version version() { | ||
| return version; | ||
| } | ||
|
|
||
| @Override | ||
| public void setupModule(SetupContext ctx) { | ||
|
|
||
| final SimpleDeserializers deserializers = new SimpleDeserializers(); | ||
|
|
||
| deserializers.addDeserializer(GeoJsonPoint.class, new GeoJsonPointDeserializer()); | ||
| deserializers.addDeserializer(GeoJsonMultiPoint.class, new GeoJsonMultiPointDeserializer()); | ||
| deserializers.addDeserializer(GeoJsonLineString.class, new GeoJsonLineStringDeserializer()); | ||
| deserializers.addDeserializer(GeoJsonMultiLineString.class, new GeoJsonMultiLineStringDeserializer()); | ||
| deserializers.addDeserializer(GeoJsonPolygon.class, new GeoJsonPolygonDeserializer()); | ||
| deserializers.addDeserializer(GeoJsonMultiPolygon.class, new GeoJsonMultiPolygonDeserializer()); | ||
|
|
||
| ctx.addDeserializers(deserializers); | ||
| } | ||
| } | ||
|
|
||
| private abstract static class GeoJsonDeserializer<T extends GeoJson<?>> extends ValueDeserializer<T> { | ||
|
|
||
| public @Nullable T deserialize(JsonParser jp, @Nullable DeserializationContext context) throws JacksonException { | ||
|
|
||
| JsonNode node = jp.readValueAsTree(); | ||
| JsonNode coordinates = node.get("coordinates"); | ||
| return coordinates != null && coordinates.isArray() ? this.doDeserialize((ArrayNode) coordinates) : null; | ||
| } | ||
|
|
||
| protected abstract @Nullable T doDeserialize(ArrayNode coordinates); | ||
|
|
||
| protected @Nullable GeoJsonPoint toGeoJsonPoint(@Nullable ArrayNode node) { | ||
| return node == null ? null : new GeoJsonPoint(node.get(0).asDouble(), node.get(1).asDouble()); | ||
| } | ||
|
|
||
| protected @Nullable Point toPoint(@Nullable ArrayNode node) { | ||
| return node == null ? null : new Point(node.get(0).asDouble(), node.get(1).asDouble()); | ||
| } | ||
|
|
||
| protected List<Point> toPoints(@Nullable ArrayNode node) { | ||
|
|
||
| if (node == null) { | ||
| return Collections.emptyList(); | ||
| } else { | ||
| List<Point> points = new ArrayList<>(node.size()); | ||
|
|
||
| for (JsonNode coordinatePair : node) { | ||
|
|
||
| if (coordinatePair.isArray()) { | ||
|
|
||
| Point point = this.toPoint((ArrayNode) coordinatePair); | ||
|
|
||
| Assert.notNull(point, "Point must not be null!"); | ||
|
|
||
| points.add(point); | ||
| } | ||
| } | ||
|
|
||
| return points; | ||
| } | ||
| } | ||
|
|
||
| protected GeoJsonLineString toLineString(ArrayNode node) { | ||
| return new GeoJsonLineString(this.toPoints(node)); | ||
| } | ||
| } | ||
|
|
||
| private static class GeoJsonPointDeserializer extends GeoJsonDeserializer<GeoJsonPoint> { | ||
|
|
||
| protected @Nullable GeoJsonPoint doDeserialize(ArrayNode coordinates) { | ||
| return this.toGeoJsonPoint(coordinates); | ||
| } | ||
| } | ||
|
|
||
| private static class GeoJsonLineStringDeserializer extends GeoJsonDeserializer<GeoJsonLineString> { | ||
|
|
||
| protected GeoJsonLineString doDeserialize(ArrayNode coordinates) { | ||
| return new GeoJsonLineString(this.toPoints(coordinates)); | ||
| } | ||
| } | ||
|
|
||
| private static class GeoJsonMultiPointDeserializer extends GeoJsonDeserializer<GeoJsonMultiPoint> { | ||
|
|
||
| protected GeoJsonMultiPoint doDeserialize(ArrayNode coordinates) { | ||
| return new GeoJsonMultiPoint(this.toPoints(coordinates)); | ||
| } | ||
| } | ||
|
|
||
| private static class GeoJsonMultiLineStringDeserializer extends GeoJsonDeserializer<GeoJsonMultiLineString> { | ||
|
|
||
| protected GeoJsonMultiLineString doDeserialize(ArrayNode coordinates) { | ||
| List<GeoJsonLineString> lines = new ArrayList<>(coordinates.size()); | ||
|
|
||
| for (JsonNode lineString : coordinates) { | ||
| if (lineString.isArray()) { | ||
| lines.add(this.toLineString((ArrayNode) lineString)); | ||
| } | ||
| } | ||
|
|
||
| return new GeoJsonMultiLineString(lines); | ||
| } | ||
| } | ||
|
|
||
| private static class GeoJsonPolygonDeserializer extends GeoJsonDeserializer<GeoJsonPolygon> { | ||
|
|
||
| protected @Nullable GeoJsonPolygon doDeserialize(ArrayNode coordinates) { | ||
|
|
||
| Iterator<JsonNode> coordinateIterator = coordinates.iterator(); | ||
| if (coordinateIterator.hasNext()) { | ||
|
|
||
| JsonNode ring = coordinateIterator.next(); | ||
| return new GeoJsonPolygon(this.toPoints((ArrayNode) ring)); | ||
|
|
||
| } else { | ||
| return null; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static class GeoJsonMultiPolygonDeserializer extends GeoJsonDeserializer<GeoJsonMultiPolygon> { | ||
|
|
||
| protected GeoJsonMultiPolygon doDeserialize(ArrayNode coordinates) { | ||
| List<GeoJsonPolygon> polygons = new ArrayList<>(coordinates.size()); | ||
|
|
||
| for (JsonNode polygon : coordinates) { | ||
|
|
||
| for (JsonNode ring : polygon) { | ||
| polygons.add(new GeoJsonPolygon(this.toPoints((ArrayNode) ring))); | ||
| } | ||
| } | ||
|
|
||
| return new GeoJsonMultiPolygon(polygons); | ||
| } | ||
| } | ||
|
|
||
| private abstract static class GeoJsonSerializer<T extends GeoJson<? extends Iterable<?>>> extends ValueSerializer<T> { | ||
|
|
||
| @Override | ||
| public void serialize(T shape, JsonGenerator jsonGenerator, SerializationContext context) { | ||
|
|
||
| jsonGenerator.writeStartObject(); | ||
| jsonGenerator.writeStringProperty("type", shape.getType()); | ||
| jsonGenerator.writeArrayPropertyStart("coordinates"); | ||
| this.doSerialize(shape, jsonGenerator); | ||
| jsonGenerator.writeEndArray(); | ||
| jsonGenerator.writeEndObject(); | ||
| } | ||
|
|
||
| protected abstract void doSerialize(T shape, JsonGenerator jsonGenerator); | ||
|
|
||
| protected void writePoint(Point point, JsonGenerator jsonGenerator) { | ||
|
|
||
| jsonGenerator.writeStartArray(); | ||
| this.writeRawCoordinates(point, jsonGenerator); | ||
| jsonGenerator.writeEndArray(); | ||
| } | ||
|
|
||
| protected void writeRawCoordinates(Point point, JsonGenerator jsonGenerator) { | ||
|
|
||
| jsonGenerator.writeNumber(point.getX()); | ||
| jsonGenerator.writeNumber(point.getY()); | ||
| } | ||
|
|
||
| protected void writeLine(Iterable<Point> points, JsonGenerator jsonGenerator) { | ||
|
|
||
| jsonGenerator.writeStartArray(); | ||
| this.writeRawLine(points, jsonGenerator); | ||
| jsonGenerator.writeEndArray(); | ||
| } | ||
|
|
||
| protected void writeRawLine(Iterable<Point> points, JsonGenerator jsonGenerator) { | ||
| for (Point point : points) { | ||
| this.writePoint(point, jsonGenerator); | ||
| } | ||
|
|
||
| } | ||
| } | ||
|
|
||
| static class GeoJsonPointSerializer extends GeoJsonSerializer<GeoJsonPoint> { | ||
| protected void doSerialize(GeoJsonPoint value, JsonGenerator jsonGenerator) { | ||
| this.writeRawCoordinates(value, jsonGenerator); | ||
| } | ||
| } | ||
|
|
||
| static class GeoJsonLineStringSerializer extends GeoJsonSerializer<GeoJsonLineString> { | ||
| protected void doSerialize(GeoJsonLineString value, JsonGenerator jsonGenerator) { | ||
| this.writeRawLine(value.getCoordinates(), jsonGenerator); | ||
| } | ||
| } | ||
|
|
||
| static class GeoJsonMultiPointSerializer extends GeoJsonSerializer<GeoJsonMultiPoint> { | ||
| protected void doSerialize(GeoJsonMultiPoint value, JsonGenerator jsonGenerator) { | ||
| this.writeRawLine(value.getCoordinates(), jsonGenerator); | ||
| } | ||
| } | ||
|
|
||
| static class GeoJsonMultiLineStringSerializer extends GeoJsonSerializer<GeoJsonMultiLineString> { | ||
| protected void doSerialize(GeoJsonMultiLineString value, JsonGenerator jsonGenerator) { | ||
| for (GeoJsonLineString lineString : value.getCoordinates()) { | ||
| this.writeLine(lineString.getCoordinates(), jsonGenerator); | ||
| } | ||
|
|
||
| } | ||
| } | ||
|
|
||
| static class GeoJsonPolygonSerializer extends GeoJsonSerializer<GeoJsonPolygon> { | ||
| protected void doSerialize(GeoJsonPolygon value, JsonGenerator jsonGenerator) throws JacksonException { | ||
| for (GeoJsonLineString lineString : value.getCoordinates()) { | ||
| this.writeLine(lineString.getCoordinates(), jsonGenerator); | ||
| } | ||
|
|
||
| } | ||
| } | ||
|
|
||
| static class GeoJsonMultiPolygonSerializer extends GeoJsonSerializer<GeoJsonMultiPolygon> { | ||
| protected void doSerialize(GeoJsonMultiPolygon value, JsonGenerator jsonGenerator) throws JacksonException { | ||
| for (GeoJsonPolygon polygon : value.getCoordinates()) { | ||
| jsonGenerator.writeStartArray(); | ||
|
|
||
| for (GeoJsonLineString lineString : polygon.getCoordinates()) { | ||
| this.writeLine(lineString.getCoordinates(), jsonGenerator); | ||
| } | ||
|
|
||
| jsonGenerator.writeEndArray(); | ||
| } | ||
|
|
||
| } | ||
| } | ||
| } |
1 change: 1 addition & 0 deletions
1
spring-data-mongodb/src/main/resources/META-INF/spring.factories
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| org.springframework.data.web.config.SpringDataJacksonModules=org.springframework.data.mongodb.config.GeoJsonConfiguration | ||
| org.springframework.data.web.config.SpringDataJackson3Modules=org.springframework.data.mongodb.config.GeoJsonJackson3Configuration | ||
| org.springframework.data.repository.core.support.RepositoryFactorySupport=org.springframework.data.mongodb.repository.support.MongoRepositoryFactory |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given the opportunity I think we should align the naming our newly added
GeoJsonJackson3 support to follow what is outlined in spring-projects/spring-data-redis#3219There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GeoJsonJacksonConfigurationis Jackson 2, so isGeoJacksonModuleShould those be renamed to
...Jackson2...To be honest I find that awfully confusing. Especially, but not only, since commons also goes with
Jackson3There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd leave the ones we have as is and name the others something like
JacksonGeoJson...maybe.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really don't like that. If anyone looks at this in a year (or myself in two weeks), how are they supposed to tell what the reason is behind having
JacksonGeoJsonConfigurationandGeoJsonJacksonConfiguration?How about this: We go with Jackson3 for now and once we remove the Jackson 2 support, we rename everything to just Jackson? This way the distinction is clear as long we have both and once we have only one we get clean names (until Jackson 4 of course).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
jackson does not contain the 3 in any of it, still it's the new baseline and 2 the old one, so we also could have ripped com.fasterxmll out and go with tools.jackson without changing. the leading jackson is more of an alignment with other projects