diff --git a/jOOQ/src/main/java/org/jooq/JSON.java b/jOOQ/src/main/java/org/jooq/JSON.java new file mode 100644 index 0000000000..c31589ed06 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/JSON.java @@ -0,0 +1,85 @@ +/* + * 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; + +/** + * A JSON wrapper type for JSON data obtained from the database. + */ +public final class JSON { + + private final String data; + + private JSON(String data) { + this.data = data; + } + + public static final JSON valueOf(String data) { + return new JSON(data); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((data == null) ? 0 : data.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + JSON other = (JSON) obj; + if (data == null) { + if (other.data != null) + return false; + } + else if (!data.equals(other.data)) + return false; + return true; + } + + @Override + public String toString() { + return data; + } +} diff --git a/jOOQ/src/main/java/org/jooq/tools/Convert.java b/jOOQ/src/main/java/org/jooq/tools/Convert.java index 4cd659da8e..3fedeee1b3 100644 --- a/jOOQ/src/main/java/org/jooq/tools/Convert.java +++ b/jOOQ/src/main/java/org/jooq/tools/Convert.java @@ -83,6 +83,7 @@ import java.util.regex.Pattern; import org.jooq.Converter; import org.jooq.EnumType; import org.jooq.Field; +import org.jooq.JSON; import org.jooq.Record; import org.jooq.SQLDialect; import org.jooq.UDTRecord; @@ -420,9 +421,8 @@ public final class Convert { ConvertAll all = new ConvertAll<>(converter.fromType()); List result = new ArrayList<>(collection.size()); - for (Object o : collection) { + for (Object o : collection) result.add(convert(all.from(o), converter)); - } return result; } @@ -1008,6 +1008,11 @@ public final class Convert { } } + // [#8943] JSON data types can be read from Strings + else if (fromClass == String.class && toClass == JSON.class) { + return (U) JSON.valueOf((String) from); + } + // [#3023] Record types can be converted using the supplied Configuration's // RecordMapperProvider else if (Record.class.isAssignableFrom(fromClass)) {