├── .gitignore ├── LICENSE ├── README.md ├── build.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── src ├── main └── java │ └── com │ ├── github │ └── jneat │ │ └── mybatis │ │ └── JsonElementTypeHandler.java │ └── google │ └── gson │ └── JsonElementLazyWrapper.java └── test ├── java └── com │ └── github │ └── jneat │ └── mybatis │ ├── JsonEntity.java │ ├── JsonHandlersTestApi.java │ ├── JsonMapper.java │ └── PostgresqlTest.java └── resources ├── com └── github │ └── jneat │ └── mybatis │ └── JsonMapper.xml └── postgresql.sql /.gitignore: -------------------------------------------------------------------------------- 1 | build 2 | 3 | .gradle 4 | .nb-gradle-properties 5 | .nb-gradle 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2018 rumatoest at github 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # JSON support for Mybatis 3.x using Gson 2.x 2 | 3 | Provide support for JSON like field types in any Database. 4 | I'm developed this handler with PostgreSql in mind, 5 | but it looks like it can be used with any other database even without JSON support. 6 | 7 | Artifact does not include direct gson dependencies - it is up to you to add them into your project. 8 | Looks like you can use any Gson version compatible with API version >= 2.3 9 | 10 | [![Release](https://jitpack.io/v/jneat/mybatis-gson.svg)](https://jitpack.io/#jneat/mybatis-gson) 11 | [API javadoc](https://jitpack.io/com/github/jneat/mybatis-gson/-SNAPSHOT/javadoc/) 12 | 13 | ##How does it work 14 | Because JDBC does not support JSON types, it transfer JSON to/from database as a string. 15 | It serialize JSON to string on save and deserialize from string on read. 16 | This feature means that we are really do not care if our DB can support JSON or not. 17 | 18 | ###Lazy reading 19 | Type handler returns TreeNode wrapper that actually does not parse JSON from string. 20 | It is waiting for you to call any of its methods - only then it will read JSON into structure. 21 | But this approach may lead to **unexpected runtime exception** in a case if your database will return 22 | invalid JSON string. 23 | 24 | ##Add to your project 25 | 26 | You can add this artifact to your project using [JitPack](https://jitpack.io/#jneat/mybatis-gson). 27 | All versions list, instructions for gradle, maven, ivy etc. can be found by link above. 28 | 29 | To get latest commit use -SNAPSHOT instead version number. 30 | 31 | ## Configure 32 | In result map configuration you should use ```javaType=com.google.gson.JsonElement"``` 33 | 34 | You should not configure anything if you want to use JsonElement types as arguments in your mapper 35 | functions, but keep in mind that handler only expect objects of type JsonArray or JsonObject. 36 | 37 | ### Mybatis config 38 | ``` 39 | 40 | 41 | 42 | 43 | ``` 44 | 45 | Or you can use package search 46 | 47 | ``` 48 | 49 | 50 | 51 | 52 | ``` 53 | 54 | ### Mybatis via Spring 55 | ``` 56 | 57 | 58 | 59 | 60 | ``` -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'java' 2 | apply plugin: 'maven-publish' 3 | 4 | group = 'com.github.jneat' 5 | version = '0.3' 6 | 7 | sourceCompatibility = 1.8 8 | targetCompatibility = 1.8 9 | 10 | repositories { 11 | mavenCentral() 12 | maven { 13 | url "https://jitpack.io" 14 | } 15 | } 16 | 17 | dependencies { 18 | runtime 'com.google.code.gson:gson:2.3' 19 | runtime 'org.mybatis:mybatis:3.3.0' 20 | compile 'com.github.jneat:mybatis-types:0.5.1' 21 | 22 | testCompile 'com.google.code.gson:gson:2.3' 23 | testCompile 'org.mybatis:mybatis:3.3.0' 24 | 25 | testCompile 'junit:junit:4.12' 26 | testCompile 'org.assertj:assertj-core:3.9.0' 27 | 28 | testCompile 'org.postgresql:postgresql:42.2.1' 29 | } 30 | 31 | //Include runtime for compilation 32 | sourceSets.main.compileClasspath += configurations.runtime 33 | javadoc.classpath += configurations.runtime 34 | 35 | buildscript { 36 | repositories { 37 | mavenCentral() 38 | } 39 | dependencies { 40 | classpath 'org.mybatis:mybatis:3.3.0' 41 | } 42 | } 43 | 44 | task sourcesJar(type: Jar, dependsOn: classes) { 45 | classifier = 'sources' 46 | from sourceSets.main.allSource 47 | } 48 | 49 | task javadocJar(type: Jar, dependsOn: javadoc) { 50 | classifier = 'javadoc' 51 | from javadoc.destinationDir 52 | } 53 | 54 | artifacts { 55 | archives sourcesJar 56 | archives javadocJar 57 | } 58 | 59 | publishing { 60 | publications { 61 | mavenJar(MavenPublication) { 62 | from components.java 63 | artifact sourcesJar { 64 | classifier "sources" 65 | } 66 | artifact javadocJar { 67 | classifier "javadoc" 68 | } 69 | } 70 | } 71 | } 72 | 73 | // Tasks 74 | [compileJava, compileTestJava]*.options*.encoding = 'UTF-8' 75 | 76 | gradle.projectsEvaluated { 77 | tasks.withType(JavaCompile) { 78 | options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation" 79 | } 80 | } -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jneat/mybatis-gson/e25570dd985678b486281ca1adbaba19b8c4a323/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Thu Feb 15 22:00:09 MSK 2018 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.2.1-bin.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'mybatis-gson' 2 | -------------------------------------------------------------------------------- /src/main/java/com/github/jneat/mybatis/JsonElementTypeHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2018 rumatoest at github 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package com.github.jneat.mybatis; 25 | 26 | import com.google.gson.JsonArray; 27 | import com.google.gson.JsonElement; 28 | import com.google.gson.JsonElementLazyWrapper; 29 | import com.google.gson.JsonNull; 30 | import com.google.gson.JsonObject; 31 | import com.google.gson.internal.Streams; 32 | import com.google.gson.stream.JsonWriter; 33 | import org.apache.ibatis.type.JdbcType; 34 | import org.apache.ibatis.type.MappedTypes; 35 | 36 | import java.io.IOException; 37 | import java.io.StringWriter; 38 | import java.sql.CallableStatement; 39 | import java.sql.PreparedStatement; 40 | import java.sql.ResultSet; 41 | import java.sql.SQLException; 42 | 43 | /** 44 | * Map PostgreSQL JSON type to Gson JsonElement does not return null (empty node instead). 45 | * Just use JSON string representation as intermediate data format. 46 | * 47 | * @see JsonElement 48 | */ 49 | @MappedTypes({JsonElement.class, JsonArray.class, JsonObject.class}) 50 | public class JsonElementTypeHandler extends NotNullResultTypeHandler { 51 | 52 | @Override 53 | public void setNonNullParameter(PreparedStatement ps, int i, JsonElement parameter, JdbcType jdbcType) throws SQLException { 54 | try { 55 | StringWriter sw = new StringWriter(); 56 | JsonWriter jsonWriter = new JsonWriter(sw); 57 | jsonWriter.setLenient(false); 58 | Streams.write(parameter, jsonWriter); 59 | ps.setString(i, sw.toString()); 60 | } catch (IOException ex) { 61 | throw new RuntimeException(ex.getMessage(), ex); 62 | } 63 | } 64 | 65 | @Override 66 | public JsonElement getNullableResult(ResultSet rs, String columnName) throws SQLException { 67 | String jsonSource = rs.getString(columnName); 68 | if (jsonSource != null) { 69 | return fromString(jsonSource); 70 | } 71 | return JsonNull.INSTANCE; 72 | } 73 | 74 | @Override 75 | public JsonElement getNullableResult(ResultSet rs, int columnIndex) throws SQLException { 76 | String jsonSource = rs.getString(columnIndex); 77 | if (jsonSource != null) { 78 | return fromString(jsonSource); 79 | } 80 | return JsonNull.INSTANCE; 81 | } 82 | 83 | @Override 84 | public JsonElement getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { 85 | String jsonSource = cs.getString(columnIndex); 86 | if (jsonSource != null) { 87 | return fromString(jsonSource); 88 | } 89 | return JsonNull.INSTANCE; 90 | } 91 | 92 | private JsonElement fromString(String source) { 93 | return source == null ? null : new JsonElementLazyWrapper(source); 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /src/main/java/com/google/gson/JsonElementLazyWrapper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2018 rumatoest at github 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package com.google.gson; 25 | 26 | import java.io.Serializable; 27 | import java.math.BigDecimal; 28 | import java.math.BigInteger; 29 | 30 | /** 31 | * Lazy JSON node wrapper, that will create generate real JsonElement after first call to it's methods. 32 | * Note, that in a case if input JSON string is invalid it may throw runtime exception from any method. 33 | * This is really awkward, but I can extend JsonElement only from com.google.gson package. 34 | */ 35 | public class JsonElementLazyWrapper extends JsonElement implements Serializable { 36 | 37 | private static final JsonParser PARSER = new JsonParser(); 38 | 39 | private static final long serialVersionUID = -4882634013737740905L; 40 | 41 | private final String json; 42 | 43 | private JsonElement node; 44 | 45 | public JsonElementLazyWrapper(String json) { 46 | this.json = json; 47 | } 48 | 49 | /** 50 | * This will return source JSON string passed as argument into constructor. 51 | */ 52 | public String getJsonSource() { 53 | return this.json; 54 | } 55 | 56 | private JsonElement element() { 57 | if (this.node == null) { 58 | synchronized (this) { 59 | if (this.node == null) { 60 | node = PARSER.parse(json); 61 | } 62 | } 63 | } 64 | return this.node; 65 | } 66 | 67 | @Override 68 | JsonElement deepCopy() { 69 | return element().deepCopy(); 70 | } 71 | 72 | @Override 73 | public boolean isJsonArray() { 74 | return element().isJsonArray(); 75 | } 76 | 77 | @Override 78 | public boolean isJsonObject() { 79 | return element().isJsonObject(); 80 | } 81 | 82 | @Override 83 | public boolean isJsonPrimitive() { 84 | return element().isJsonPrimitive(); 85 | } 86 | 87 | @Override 88 | public boolean isJsonNull() { 89 | return element().isJsonNull(); 90 | } 91 | 92 | @Override 93 | public JsonObject getAsJsonObject() { 94 | return element().getAsJsonObject(); 95 | } 96 | 97 | @Override 98 | public JsonArray getAsJsonArray() { 99 | return element().getAsJsonArray(); 100 | } 101 | 102 | @Override 103 | public JsonPrimitive getAsJsonPrimitive() { 104 | return element().getAsJsonPrimitive(); 105 | } 106 | 107 | @Override 108 | public JsonNull getAsJsonNull() { 109 | return element().getAsJsonNull(); 110 | } 111 | 112 | @Override 113 | public boolean getAsBoolean() { 114 | return element().getAsBoolean(); 115 | } 116 | 117 | @Override 118 | public Number getAsNumber() { 119 | return element().getAsNumber(); 120 | } 121 | 122 | @Override 123 | public String getAsString() { 124 | return element().getAsString(); 125 | } 126 | 127 | @Override 128 | public double getAsDouble() { 129 | return element().getAsDouble(); 130 | } 131 | 132 | @Override 133 | public float getAsFloat() { 134 | return element().getAsFloat(); 135 | } 136 | 137 | @Override 138 | public long getAsLong() { 139 | return element().getAsLong(); 140 | } 141 | 142 | @Override 143 | public int getAsInt() { 144 | return element().getAsInt(); 145 | } 146 | 147 | @Override 148 | public byte getAsByte() { 149 | return element().getAsByte(); 150 | } 151 | 152 | @Override 153 | public char getAsCharacter() { 154 | return element().getAsCharacter(); 155 | } 156 | 157 | @Override 158 | public BigDecimal getAsBigDecimal() { 159 | return element().getAsBigDecimal(); 160 | } 161 | 162 | @Override 163 | public BigInteger getAsBigInteger() { 164 | return element().getAsBigInteger(); 165 | } 166 | 167 | @Override 168 | public short getAsShort() { 169 | return element().getAsShort(); 170 | } 171 | 172 | @Override 173 | public String toString() { 174 | return element().toString(); 175 | } 176 | 177 | @Override 178 | public int hashCode() { 179 | return element().hashCode(); 180 | } 181 | 182 | @Override 183 | public boolean equals(Object o) { 184 | return element().equals(o); 185 | } 186 | } 187 | -------------------------------------------------------------------------------- /src/test/java/com/github/jneat/mybatis/JsonEntity.java: -------------------------------------------------------------------------------- 1 | package com.github.jneat.mybatis; 2 | 3 | import com.google.gson.JsonElement; 4 | 5 | import java.io.Serializable; 6 | 7 | public class JsonEntity implements Serializable { 8 | 9 | private static final long serialVersionUID = 2361613838967425855L; 10 | 11 | private long id; 12 | 13 | private JsonElement jsonArray; 14 | 15 | private JsonElement jsonObject; 16 | 17 | public JsonEntity() { 18 | } 19 | 20 | public JsonEntity(long id, JsonElement jsonArray, JsonElement jsonObject) { 21 | this.id = id; 22 | this.jsonArray = jsonArray; 23 | this.jsonObject = jsonObject; 24 | } 25 | 26 | public long getId() { 27 | return id; 28 | } 29 | 30 | public void setId(long id) { 31 | this.id = id; 32 | } 33 | 34 | public JsonElement getJsonArray() { 35 | return jsonArray; 36 | } 37 | 38 | public void setJsonArray(JsonElement jsonArray) { 39 | this.jsonArray = jsonArray; 40 | } 41 | 42 | public JsonElement getJsonObject() { 43 | return jsonObject; 44 | } 45 | 46 | public void setJsonObject(JsonElement jsonObject) { 47 | this.jsonObject = jsonObject; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/test/java/com/github/jneat/mybatis/JsonHandlersTestApi.java: -------------------------------------------------------------------------------- 1 | package com.github.jneat.mybatis; 2 | 3 | import org.apache.ibatis.mapping.Environment; 4 | import org.apache.ibatis.session.Configuration; 5 | import org.apache.ibatis.session.SqlSessionFactory; 6 | import org.apache.ibatis.session.SqlSessionFactoryBuilder; 7 | import org.apache.ibatis.transaction.TransactionFactory; 8 | import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory; 9 | import org.assertj.core.api.Assertions; 10 | 11 | import java.io.IOException; 12 | import java.io.InputStream; 13 | import java.io.StringWriter; 14 | import java.sql.Connection; 15 | import java.sql.SQLException; 16 | import java.sql.Statement; 17 | import javax.sql.DataSource; 18 | 19 | public abstract class JsonHandlersTestApi { 20 | 21 | protected static String getResourceAsString(String resource) throws IOException { 22 | String query; 23 | try (final InputStream is = JsonHandlersTestApi.class.getResourceAsStream(resource)) { 24 | 25 | Assertions.assertThat(is).isNotNull(); 26 | 27 | StringWriter stringWriter = new StringWriter(); 28 | int b; 29 | while ((b = is.read()) != -1) { 30 | stringWriter.write(b); 31 | } 32 | query = stringWriter.toString(); 33 | } 34 | return query; 35 | } 36 | 37 | protected static SqlSessionFactory setUpDb(DataSource ds, String initSql) throws SQLException, IOException { 38 | try (final Connection cnx = ds.getConnection(); final Statement st = cnx.createStatement()) { 39 | st.execute(getResourceAsString(initSql)); 40 | } 41 | 42 | // Init mybatis 43 | TransactionFactory transactionFactory = new JdbcTransactionFactory(); 44 | Environment environment = new Environment("jneat", transactionFactory, ds); 45 | Configuration configuration = new Configuration(environment); 46 | configuration.getTypeHandlerRegistry().register("com.github.jneat.mybatis"); 47 | configuration.addMapper(JsonMapper.class); 48 | 49 | return new SqlSessionFactoryBuilder().build(configuration); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/test/java/com/github/jneat/mybatis/JsonMapper.java: -------------------------------------------------------------------------------- 1 | package com.github.jneat.mybatis; 2 | 3 | import com.google.gson.JsonArray; 4 | import com.google.gson.JsonObject; 5 | import org.apache.ibatis.annotations.Param; 6 | 7 | public interface JsonMapper { 8 | 9 | JsonEntity get(@Param("id") long id); 10 | 11 | int insert(JsonEntity entity); 12 | 13 | int insertValues( 14 | @Param("id") long id, 15 | @Param("jsonArray") JsonArray jArray, 16 | @Param("jsonObject") JsonObject jObj 17 | ); 18 | } 19 | -------------------------------------------------------------------------------- /src/test/java/com/github/jneat/mybatis/PostgresqlTest.java: -------------------------------------------------------------------------------- 1 | package com.github.jneat.mybatis; 2 | 3 | import static org.assertj.core.api.Assertions.*; 4 | 5 | import com.google.gson.JsonArray; 6 | import com.google.gson.JsonElement; 7 | import com.google.gson.JsonObject; 8 | import com.google.gson.JsonParser; 9 | import org.apache.ibatis.session.SqlSession; 10 | import org.apache.ibatis.session.SqlSessionFactory; 11 | import org.junit.BeforeClass; 12 | import org.junit.FixMethodOrder; 13 | import org.junit.Test; 14 | import org.junit.runners.MethodSorters; 15 | import org.postgresql.ds.PGSimpleDataSource; 16 | 17 | import java.io.IOException; 18 | import java.sql.SQLException; 19 | import java.util.Map; 20 | 21 | @FixMethodOrder(MethodSorters.NAME_ASCENDING) 22 | public class PostgresqlTest extends JsonHandlersTestApi { 23 | 24 | private static final String PG_URL = "jdbc:postgresql://jneat/jneat?user=jneat&password=jneat"; 25 | 26 | private static final String PG_SQL = "/postgresql.sql"; 27 | 28 | private static final JsonParser PARSER = new JsonParser(); 29 | 30 | private static SqlSessionFactory sessionFactory; 31 | 32 | private static JsonArray aNode; 33 | 34 | private static JsonObject oNode; 35 | 36 | @BeforeClass 37 | public static void init() throws SQLException, IOException { 38 | PGSimpleDataSource pgDs = new PGSimpleDataSource(); 39 | pgDs.setUrl(PG_URL); 40 | sessionFactory = JsonHandlersTestApi.setUpDb(pgDs, PG_SQL); 41 | } 42 | 43 | JsonElement readJson(String json) throws IOException { 44 | return PARSER.parse(json); 45 | } 46 | 47 | @Test 48 | public void test1InsertNulls() { 49 | try (SqlSession sess = sessionFactory.openSession()) { 50 | JsonMapper mapper = sess.getMapper(JsonMapper.class); 51 | mapper.insert(new JsonEntity(1, null, null)); 52 | mapper.insertValues(2, null, null); 53 | sess.commit(); 54 | } 55 | } 56 | 57 | @Test 58 | public void test2InsertValues() throws IOException { 59 | aNode = (JsonArray)readJson("[1,2,3,7,8]"); 60 | oNode = (JsonObject)readJson("{a:12, b:12.12, c: \"some name\"}"); 61 | try (SqlSession sess = sessionFactory.openSession()) { 62 | JsonMapper mapper = sess.getMapper(JsonMapper.class); 63 | 64 | mapper.insert(new JsonEntity(3, aNode, oNode)); 65 | mapper.insertValues(4, aNode, oNode); 66 | sess.commit(); 67 | } 68 | } 69 | 70 | @Test 71 | public void test3ReadNulls() { 72 | try (SqlSession sess = sessionFactory.openSession()) { 73 | JsonMapper mapper = sess.getMapper(JsonMapper.class); 74 | JsonEntity e1 = mapper.get(1); 75 | assertThat(e1.getJsonArray().isJsonNull()).isTrue(); 76 | assertThat(e1.getJsonObject().isJsonNull()).isTrue(); 77 | 78 | JsonEntity e2 = mapper.get(2); 79 | assertThat(e2.getJsonArray().isJsonNull()).isTrue(); 80 | assertThat(e2.getJsonObject().isJsonNull()).isTrue(); 81 | } 82 | } 83 | 84 | @Test 85 | public void test4ReadValues() { 86 | try (SqlSession sess = sessionFactory.openSession()) { 87 | JsonMapper mapper = sess.getMapper(JsonMapper.class); 88 | JsonEntity e1 = mapper.get(3); 89 | 90 | // assertThat(aNode).isEqualTo(e1.getJsonArray()); 91 | // assertThat(oNode).isEqualTo(e1.getJsonObject()); 92 | compareArrays(aNode, e1.getJsonArray()); 93 | compareObjects(oNode, e1.getJsonObject()); 94 | 95 | JsonEntity e2 = mapper.get(4); 96 | // assertThat(aNode).isEqualTo(e2.getJsonArray()); 97 | // assertThat(oNode).isEqualTo(e2.getJsonObject()); 98 | compareArrays(aNode, e2.getJsonArray()); 99 | compareObjects(oNode, e2.getJsonObject()); 100 | } 101 | } 102 | 103 | protected void compareArrays(JsonElement a, JsonElement b) { 104 | assertThat(a.isJsonArray()).isEqualTo(b.isJsonArray()); 105 | assertThat(a.getAsJsonArray().size()).isEqualTo(b.getAsJsonArray().size()); 106 | for (int i = 0; i < a.getAsJsonArray().size(); i++) { 107 | assertThat(a.getAsJsonArray().get(i)).isEqualTo(b.getAsJsonArray().get(i)); 108 | } 109 | } 110 | 111 | protected void compareObjects(JsonElement a, JsonElement b) { 112 | assertThat(a.isJsonObject()).isEqualTo(b.isJsonObject()); 113 | 114 | for (Map.Entry e : a.getAsJsonObject().entrySet()) { 115 | assertThat(b.getAsJsonObject().get(e.getKey())).isEqualTo(e.getValue()); 116 | } 117 | } 118 | 119 | } 120 | -------------------------------------------------------------------------------- /src/test/resources/com/github/jneat/mybatis/JsonMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 16 | 17 | 18 | INSERT INTO mybatis_jackson 19 | (id, jsonArray, jsonObject) 20 | VALUES ( 21 | #{id}, 22 | null, 23 | (#{jsonArray})::json, 24 | null 25 | (#{jsonObject})::json 26 | ) 27 | 28 | 29 | 30 | INSERT INTO mybatis_jackson 31 | (id, jsonArray, jsonObject) 32 | VALUES ( 33 | #{id}, 34 | null, 35 | (#{jsonArray})::json, 36 | null 37 | (#{jsonObject})::json 38 | ) 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /src/test/resources/postgresql.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE IF EXISTS mybatis_jackson; 2 | 3 | CREATE TABLE mybatis_jackson ( 4 | id INT8, 5 | jsonArray JSON, 6 | jsonObject JSON 7 | ); --------------------------------------------------------------------------------