Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>5.1.0-SNAPSHOT</version>
<version>5.1.0-5100-geojson-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>5.1.0-SNAPSHOT</version>
<version>5.1.0-5100-geojson-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
8 changes: 7 additions & 1 deletion spring-data-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>5.1.0-SNAPSHOT</version>
<version>5.1.0-5100-geojson-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

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

}
}
}
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
Loading
Loading