DEV Community

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

Posted on

1

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 ✌

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more