diff --git a/jOOQ-jackson-extensions/.gitignore b/jOOQ-jackson-extensions/.gitignore new file mode 100644 index 0000000000..41b88d42d8 --- /dev/null +++ b/jOOQ-jackson-extensions/.gitignore @@ -0,0 +1,6 @@ +/target + +/.cache + +/.idea +/*.iml \ No newline at end of file diff --git a/jOOQ-jackson-extensions/LICENSE.txt b/jOOQ-jackson-extensions/LICENSE.txt new file mode 100644 index 0000000000..d090694f44 --- /dev/null +++ b/jOOQ-jackson-extensions/LICENSE.txt @@ -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 \ No newline at end of file diff --git a/jOOQ-jackson-extensions/NOTICE.txt b/jOOQ-jackson-extensions/NOTICE.txt new file mode 100644 index 0000000000..698edf47ee --- /dev/null +++ b/jOOQ-jackson-extensions/NOTICE.txt @@ -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/). \ No newline at end of file diff --git a/jOOQ-jackson-extensions/pom.xml b/jOOQ-jackson-extensions/pom.xml new file mode 100644 index 0000000000..85d4d58e80 --- /dev/null +++ b/jOOQ-jackson-extensions/pom.xml @@ -0,0 +1,58 @@ + + + + 4.0.0 + + + org.jooq + jooq-parent + 3.17.0-SNAPSHOT + + + jooq-jackson-extensions + jOOQ Jackson Extensions + + + + + org.apache.maven.plugins + maven-jar-plugin + + + + org.jooq.jackson.extensions + + + + + + + + + + org.jooq + jooq + + + + org.jetbrains + annotations + provided + true + + + + com.fasterxml.jackson.core + jackson-databind + + + + + + + + + + + \ No newline at end of file diff --git a/jOOQ-jackson-extensions/src/main/java/module-info.java b/jOOQ-jackson-extensions/src/main/java/module-info.java new file mode 100644 index 0000000000..bacc0a2637 --- /dev/null +++ b/jOOQ-jackson-extensions/src/main/java/module-info.java @@ -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; +} diff --git a/jOOQ-jackson-extensions/src/main/java/org/jooq/jackson/extensions/converters/AbstractToJacksonConverter.java b/jOOQ-jackson-extensions/src/main/java/org/jooq/jackson/extensions/converters/AbstractToJacksonConverter.java new file mode 100644 index 0000000000..4269908087 --- /dev/null +++ b/jOOQ-jackson-extensions/src/main/java/org/jooq/jackson/extensions/converters/AbstractToJacksonConverter.java @@ -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 extends AbstractConverter { + + final ObjectMapper mapper; + + public AbstractToJacksonConverter(Class fromType, Class 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); + } + } +} diff --git a/jOOQ-jackson-extensions/src/main/java/org/jooq/jackson/extensions/converters/JSONBtoJacksonConverter.java b/jOOQ-jackson-extensions/src/main/java/org/jooq/jackson/extensions/converters/JSONBtoJacksonConverter.java new file mode 100644 index 0000000000..7ebe029175 --- /dev/null +++ b/jOOQ-jackson-extensions/src/main/java/org/jooq/jackson/extensions/converters/JSONBtoJacksonConverter.java @@ -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 extends AbstractToJacksonConverter { + + public JSONBtoJacksonConverter(Class toType) { + super(JSONB.class, toType); + } + + @Override + final String data(JSONB json) { + return json.data(); + } + + @Override + final JSONB json(String string) { + return JSONB.jsonb(string); + } +} diff --git a/jOOQ-jackson-extensions/src/main/java/org/jooq/jackson/extensions/converters/JSONtoJacksonConverter.java b/jOOQ-jackson-extensions/src/main/java/org/jooq/jackson/extensions/converters/JSONtoJacksonConverter.java new file mode 100644 index 0000000000..dffe058a88 --- /dev/null +++ b/jOOQ-jackson-extensions/src/main/java/org/jooq/jackson/extensions/converters/JSONtoJacksonConverter.java @@ -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 extends AbstractToJacksonConverter { + + public JSONtoJacksonConverter(Class toType) { + super(JSON.class, toType); + } + + @Override + final String data(JSON json) { + return json.data(); + } + + @Override + final JSON json(String string) { + return JSON.json(string); + } +} diff --git a/jOOQ-jackson-extensions/src/main/resources/META-INF/LICENSE.txt b/jOOQ-jackson-extensions/src/main/resources/META-INF/LICENSE.txt new file mode 100644 index 0000000000..d090694f44 --- /dev/null +++ b/jOOQ-jackson-extensions/src/main/resources/META-INF/LICENSE.txt @@ -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 \ No newline at end of file diff --git a/jOOQ-jackson-extensions/src/main/resources/META-INF/README.txt b/jOOQ-jackson-extensions/src/main/resources/META-INF/README.txt new file mode 100644 index 0000000000..3e6e227e05 --- /dev/null +++ b/jOOQ-jackson-extensions/src/main/resources/META-INF/README.txt @@ -0,0 +1,2 @@ +Thanks for downloading jOOQ. +Please visit http://www.jooq.org for more information. \ No newline at end of file diff --git a/jOOQ/src/main/java/org/jooq/impl/XMLtoJAXBConverter.java b/jOOQ/src/main/java/org/jooq/impl/XMLtoJAXBConverter.java new file mode 100644 index 0000000000..3c394e3c33 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/impl/XMLtoJAXBConverter.java @@ -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 extends AbstractConverter { + + public XMLtoJAXBConverter(Class 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()); + } + } +} diff --git a/pom.xml b/pom.xml index 115724bcc8..ab2ea90814 100644 --- a/pom.xml +++ b/pom.xml @@ -107,6 +107,11 @@ jooq-postgres-extensions ${project.version} + + org.jooq + jooq-jackson-extensions + ${project.version} + org.jooq jooq-codegen @@ -156,6 +161,18 @@ 3.4.7 + + + com.fasterxml.jackson.core + jackson-databind + 2.13.3 + + + com.fasterxml.jackson.module + jackson-module-kotlin + 2.12.3 + + io.r2dbc @@ -830,6 +847,7 @@ jOOQ-checker + jOOQ-jackson-extensions jOOQ-postgres-extensions jOOQ-meta