Converter for PostgreSQL JSON and JSONB

580 views
Skip to first unread message

jo...@zynde.com

unread,
Nov 20, 2017, 8:34:15 PM11/20/17
to SimpleFlatMapper
Hi all,

Can anyone give me pointers on the following.

I am using PostgreSQL and I have some JSON and JSONB columns being returned to a Spring JDBC implementation.

Everything is working fine and I have managed to get SFM in as the RowMapper without any issue so far.

I am currently putting a JSON column in to a string field in my Model which is working so I know all the data is moving through correctly. What I would like to do now is convert the JSON in the RowMapping to an Object from the JSON. I'm confused about how to go about creating a converter both back and forwards to the database.

Thanks

Arnaud Roger

unread,
Nov 21, 2017, 1:51:52 AM11/21/17
to SimpleFlatMapper
The Postgres jdbc driver does not have a json type for json column,
most of the example I see treat the json as s String.

What type would you like to transform into ?a Jackson JsonNode? gson? or any other library?
That should be possible by changing the type in your object and adding a converter - not very complicated but not trivial.
I might add some sample code to do that with jackson.

John Mooney

unread,
Nov 21, 2017, 2:44:14 PM11/21/17
to Arnaud Roger, SimpleFlatMapper
Hi

I’m AEST 🙃 so conversation will be slow

I can probably manage to create the conversions I need but just found the documentation on converters a little had to build upon.

If you can give any pointer or further examples that would be great.

I’m thinking either transforming to JsonNode or my own POJO

thanks for the replies


--
You received this message because you are subscribed to a topic in the Google Groups "SimpleFlatMapper" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/simpleflatmapper/xZI6Bl8RN-c/unsubscribe.
To unsubscribe from this group and all its topics, send an email to simpleflatmapp...@googlegroups.com.
To post to this group, send email to simplefl...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/simpleflatmapper/55f6f2ed-aaa4-4869-be46-15f627007b6e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Arnaud Roger

unread,
Nov 21, 2017, 3:28:58 PM11/21/17
to SimpleFlatMapper

a good start would be to look at https://github.com/arnaudroger/SimpleFlatMapper/blob/master/sfm-converter-protobuf

the converter for some protobuf object

the converters are register using the ServiceLocator

you need to build a AbstractConverterFactoryProducer like https://github.com/arnaudroger/SimpleFlatMapper/blob/master/sfm-converter-protobuf/src/main/java/org/simpleflatmapper/converter/protobuf/ProtobufConverterFactoryProducer.java
it's job is to register ConverterFactory.

then you'll need to tell the ServiceLocator about it by creating a META-INF/services/
org.simpleflatmapper.converter.ConverterFactoryProducer with the name of the impl of 
AbstractConverterFactoryProducer like
https://github.com/arnaudroger/SimpleFlatMapper/blob/master/sfm-converter-protobuf/src/main/resources/META-INF/services/org.simpleflatmapper.converter.ConverterFactoryProducer

so you
ConverterFactoryProducer
will likely look like


public class JsonConverterFactoryProducer extends AbstractConverterFactoryProducer {

@Override
public void produce(Consumer<? super ConverterFactory<?, ?>> consumer) {
constantConverter(consumer, String.class, JsonNode.class, new StringToJsonConverter());
constantConverter(consumer, Node.class, String.class, new JsonToStringConverter());
}

what's left is create the converter classes
just implement implements Converter<String, JsonNode> and Converter<JsonNode, String>


and you should be done, hopefully .


On Tuesday, 21 November 2017 19:44:14 UTC, John Mooney wrote:


Hi

I’m AEST 🙃 so conversation will be slow

I can probably manage to create the conversions I need but just found the documentation on converters a little had to build upon.

If you can give any pointer or further examples that would be great.

I’m thinking either transforming to JsonNode or my own POJO

thanks for the replies


On Tue, 21 Nov 2017 at 4:51 pm, Arnaud Roger <arnaud...@gmail.com> wrote:
The Postgres jdbc driver does not have a json type for json column,
most of the example I see treat the json as s String.

What type would you like to transform into ?a Jackson JsonNode? gson? or any other library?
That should be possible by changing the type in your object and adding a converter - not very complicated but not trivial.
I might add some sample code to do that with jackson.

 
 
On Tuesday, 21 November 2017 01:34:15 UTC, jo...@zynde.com wrote:
Hi all,

Can anyone give me pointers on the following.

I am using PostgreSQL and I have some JSON and JSONB columns being returned to a Spring JDBC implementation.

Everything is working fine and I have managed to get SFM in as the RowMapper without any issue so far.

I am currently putting a JSON column in to a string field in my Model which is working so I know all the data is moving through correctly. What I would like to do now is convert the JSON in the RowMapping to an Object from the JSON. I'm confused about how to go about creating a converter both back and forwards to the database.

Thanks

--
You received this message because you are subscribed to a topic in the Google Groups "SimpleFlatMapper" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/simpleflatmapper/xZI6Bl8RN-c/unsubscribe.
To unsubscribe from this group and all its topics, send an email to simpleflatmapper+unsubscribe@googlegroups.com.
To post to this group, send email to simpleflatmapper@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages