Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Jackson 3.x GeoJson (de-)serializers.
Adds a GeoJsonJackson3Module. Closes #5100
  • Loading branch information
schauder committed Nov 27, 2025
commit dea6701553a80cc1310fef1b39d19cc4e0340f31
6 changes: 6 additions & 0 deletions spring-data-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,12 @@
<optional>true</optional>
</dependency>

<dependency>
<groupId>tools.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>nl.jqno.equalsverifier</groupId>
<artifactId>equalsverifier</artifactId>
Expand Down
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 {
Copy link
Member

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 GeoJson Jackson3 support to follow what is outlined in spring-projects/spring-data-redis#3219

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GeoJsonJacksonConfiguration is Jackson 2, so is GeoJacksonModule

Should those be renamed to ...Jackson2...

To be honest I find that awfully confusing. Especially, but not only, since commons also goes with Jackson3

Copy link
Member

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.

Copy link
Contributor Author

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 JacksonGeoJsonConfiguration and GeoJsonJacksonConfiguration?

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).

Copy link
Member

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


@Bean
public GeoJsonJackson3Module geoJsonModule() {
return new GeoJsonJackson3Module();
}
}
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(3, 2, 0, null, "org.springframework.data",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

version should be updated

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Darn, I wanted to ask about that.

How exactly ist this version used?

Should we also update the version of the Jackson 2 Module?

Should we we keep it in sync with the module version?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC the version is just some info that can be obtained for info/logging but not used otherwise. I'd sync it with the version we initially release it with or when changes are made to it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

"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();
}

}
}
}
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,30 @@

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.core.geo.GeoJsonJackson3Module;
import org.springframework.data.mongodb.core.geo.GeoJsonModule;
import org.springframework.data.web.config.EnableSpringDataWebSupport;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

/**
* Integration tests for {@link GeoJsonConfiguration}.
* Integration tests for {@link GeoJsonJackson3Configuration}.
*
* @author Oliver Gierke
* @author Bjorn Harvold
* @author Jens Schauder
*/
@RunWith(SpringRunner.class)
@ContextConfiguration
public class GeoJsonConfigurationIntegrationTests {
public class GeoJsonJackson3ConfigurationIntegrationTests {

@Configuration
@EnableSpringDataWebSupport
static class Config {}

@Autowired GeoJsonModule geoJsonModule;
@Autowired
GeoJsonJackson3Module geoJsonModule;

@Test // DATAMONGO-1181
@Test // GH-5100
public void picksUpGeoJsonModuleConfigurationByDefault() {
assertThat(geoJsonModule).isNotNull();
}
Comment on lines 36 to 48
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not remove the the wiring of the old module since it is still present and should be picked up in case Jackson2 is present. SpringDataWebConfigurationImportSelector will pick and choose based on what's present.
It should still be possible to wire the old one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think I removed the wiring for the old version.
I just limited the test to the new version, because testing the old version now requires fiddling with the class path, which I considered too much effort for something that is deprecated.

But I guess I can put it back in.

Expand Down
Loading
Loading