DEV Community

C. Daniel Sanchez R.
C. Daniel Sanchez R.

Posted on

A simple GeoJSON serializer for Jackson

When you are working with Geometry types in Postgis, if you are using a backend with Java and others JVM compatible languages, at some point you will need to send it as GeoJSON.

I was using Play Framework, which uses FasterXML/jackson for JSON serialization, but I couldn't find a suitable library for it. I found something but it was a bit outdated. The good news is that when it is Open Source, anything is a fork of distance :D

I made a fork and added support to get the SRID of any Geometry. I am not sure if it support all the use cases, but the good thing about Open Source software is that anyone can do a PR to improve things.

Any new test is welcome!

https://github.com/GeosatCO/postgis-geojson

Usage

After import it, you need to register the library module within the ObjectMapper instance you are going to use:

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new PostGISModule());
Enter fullscreen mode Exit fullscreen mode

Then you can serialize objects:

String json = mapper.writeValueAsString(new Point(125.6, 10.1));
Enter fullscreen mode Exit fullscreen mode

And deserialize them:

Point point = (Point) mapper.readValue(json, Geometry.class);
Enter fullscreen mode Exit fullscreen mode

Peace ✌

Top comments (0)