[jOOQ/jOOQ#13605] Add JSONtoJacksonConverter, JSONBtoJacksonConverter, XMLtoJAXBConverter implementations

This commit is contained in:
Lukas Eder 2022-05-26 15:56:49 +02:00
parent e7454b3e2a
commit 11467e91c2
12 changed files with 441 additions and 0 deletions

6
jOOQ-jackson-extensions/.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
/target
/.cache
/.idea
/*.iml

View File

@ -0,0 +1,19 @@
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
http://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.
Other licenses:
-----------------------------------------------------------------------------
Commercial licenses for this work are available. These replace the above
ASL 2.0 and offer limited warranties, support, maintenance, and commercial
database integrations.
For more information, please visit: http://www.jooq.org/licenses

View File

@ -0,0 +1,10 @@
Third party NOTICE.txt contents
===============================
Contents of https://github.com/apache/commons-lang/blob/master/NOTICE.txt
-------------------------------------------------------------------------
Apache Commons Lang
Copyright 2001-2019 The Apache Software Foundation
This product includes software developed at
The Apache Software Foundation (http://www.apache.org/).

View File

@ -0,0 +1,58 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.jooq</groupId>
<artifactId>jooq-parent</artifactId>
<version>3.17.0-SNAPSHOT</version>
</parent>
<artifactId>jooq-jackson-extensions</artifactId>
<name>jOOQ Jackson Extensions</name>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<Automatic-Module-Name>org.jooq.jackson.extensions</Automatic-Module-Name>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<scope>provided</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,14 @@
/**
* The jOOQ Jackson extensions module.
*/
module org.jooq.jackson.extensions {
// Other jOOQ modules
requires transitive org.jooq;
requires com.fasterxml.jackson.databind;
// Nullability annotations for better Kotlin interop
requires static org.jetbrains.annotations;
exports org.jooq.jackson.extensions.converters;
}

View File

@ -0,0 +1,92 @@
/*
* 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
*
* http://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.
*
* Other licenses:
* -----------------------------------------------------------------------------
* Commercial licenses for this work are available. These replace the above
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
* database integrations.
*
* For more information, please visit: http://www.jooq.org/licenses
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq.jackson.extensions.converters;
import org.jooq.JSON;
import org.jooq.JSONB;
import org.jooq.exception.DataTypeException;
import org.jooq.impl.AbstractConverter;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* A base class for {@link JSON} or {@link JSONB} to Jackson POJO conversion.
*
* @author Lukas Eder
*/
abstract class AbstractToJacksonConverter<J, U> extends AbstractConverter<J, U> {
final ObjectMapper mapper;
public AbstractToJacksonConverter(Class<J> fromType, Class<U> toType) {
super(fromType, toType);
mapper = new ObjectMapper();
}
abstract String data(J json);
abstract J json(String string);
@Override
public U from(J databaseObject) {
if (databaseObject == null)
return null;
try {
return mapper.readValue(data(databaseObject), toType());
}
catch (JsonProcessingException e) {
throw new DataTypeException("Error when converting JSON to " + toType(), e);
}
}
@Override
public J to(U userObject) {
if (userObject == null)
return null;
try {
return json(mapper.writeValueAsString(userObject));
}
catch (JsonProcessingException e) {
throw new DataTypeException("Error when converting object of type " + toType() + " to JSON", e);
}
}
}

View File

@ -0,0 +1,63 @@
/*
* 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
*
* http://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.
*
* Other licenses:
* -----------------------------------------------------------------------------
* Commercial licenses for this work are available. These replace the above
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
* database integrations.
*
* For more information, please visit: http://www.jooq.org/licenses
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq.jackson.extensions.converters;
import org.jooq.JSONB;
/**
* A base class for {@link JSONB} to Jackson POJO conversion.
*
* @author Lukas Eder
*/
public class JSONBtoJacksonConverter<U> extends AbstractToJacksonConverter<JSONB, U> {
public JSONBtoJacksonConverter(Class<U> toType) {
super(JSONB.class, toType);
}
@Override
final String data(JSONB json) {
return json.data();
}
@Override
final JSONB json(String string) {
return JSONB.jsonb(string);
}
}

View File

@ -0,0 +1,63 @@
/*
* 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
*
* http://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.
*
* Other licenses:
* -----------------------------------------------------------------------------
* Commercial licenses for this work are available. These replace the above
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
* database integrations.
*
* For more information, please visit: http://www.jooq.org/licenses
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq.jackson.extensions.converters;
import org.jooq.JSON;
/**
* A base class for {@link JSON} to Jackson POJO conversion.
*
* @author Lukas Eder
*/
public class JSONtoJacksonConverter<U> extends AbstractToJacksonConverter<JSON, U> {
public JSONtoJacksonConverter(Class<U> toType) {
super(JSON.class, toType);
}
@Override
final String data(JSON json) {
return json.data();
}
@Override
final JSON json(String string) {
return JSON.json(string);
}
}

View File

@ -0,0 +1,19 @@
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
http://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.
Other licenses:
-----------------------------------------------------------------------------
Commercial licenses for this work are available. These replace the above
ASL 2.0 and offer limited warranties, support, maintenance, and commercial
database integrations.
For more information, please visit: http://www.jooq.org/licenses

View File

@ -0,0 +1,2 @@
Thanks for downloading jOOQ.
Please visit http://www.jooq.org for more information.

View File

@ -0,0 +1,77 @@
/*
* 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
*
* http://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.
*
* Other licenses:
* -----------------------------------------------------------------------------
* Commercial licenses for this work are available. These replace the above
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
* database integrations.
*
* For more information, please visit: http://www.jooq.org/licenses
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq.impl;
import java.io.StringReader;
import java.io.StringWriter;
import org.jooq.XML;
import jakarta.xml.bind.JAXB;
/**
* A base class for {@link XML} to JAXB POJO conversion.
*
* @author Lukas Eder
*/
public class XMLtoJAXBConverter<U> extends AbstractConverter<XML, U> {
public XMLtoJAXBConverter(Class<U> toType) {
super(XML.class, toType);
}
@Override
public U from(XML databaseObject) {
if (databaseObject == null)
return null;
else
return JAXB.unmarshal(new StringReader(databaseObject.data()), toType());
}
@Override
public XML to(U userObject) {
if (userObject == null) {
return null;
}
else {
StringWriter w = new StringWriter();
JAXB.marshal(userObject, w);
return XML.xml(w.toString());
}
}
}

18
pom.xml
View File

@ -107,6 +107,11 @@
<artifactId>jooq-postgres-extensions</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq-jackson-extensions</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq-codegen</artifactId>
@ -156,6 +161,18 @@
<version>3.4.7</version>
</dependency>
<!-- Data binding -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-kotlin</artifactId>
<version>2.12.3</version>
</dependency>
<!-- At the point of release 3.16, it seems not all drivers have been upgraded to 0.9.0.RELEASE yet -->
<dependency>
<groupId>io.r2dbc</groupId>
@ -830,6 +847,7 @@
<module>jOOQ-checker</module>
<module>jOOQ-jackson-extensions</module>
<module>jOOQ-postgres-extensions</module>
<module>jOOQ-meta</module>