-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add support for serializing java.sql.Blob
#2925
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
src/main/java/com/fasterxml/jackson/databind/deser/std/SqlBlobDeserializer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.fasterxml.jackson.databind.deser.std; | ||
|
||
import java.io.IOException; | ||
import java.sql.Blob; | ||
import java.util.Arrays; | ||
import java.util.UUID; | ||
|
||
import javax.sql.rowset.serial.SerialBlob; | ||
|
||
import com.fasterxml.jackson.core.Base64Variants; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.JsonMappingException; | ||
import com.fasterxml.jackson.databind.exc.InvalidFormatException; | ||
|
||
|
||
/* | ||
* Deserializer from base64 string to {@link java.sql.Blob} | ||
*/ | ||
|
||
public class SqlBlobDeserializer extends FromStringDeserializer<Blob> | ||
{ | ||
private static final long serialVersionUID = 1L; | ||
|
||
|
||
public SqlBlobDeserializer() { super(Blob.class); } | ||
|
||
@Override | ||
public Object getEmptyValue(DeserializationContext ctxt) { | ||
|
||
return null; | ||
} | ||
|
||
@Override | ||
protected Blob _deserialize(String data, DeserializationContext ctxt) throws IOException | ||
{ | ||
try { | ||
return new SerialBlob(ctxt.getBase64Variant().decode(data)); | ||
} | ||
catch(Exception e) { | ||
throw new JsonMappingException("Failed to Decode the Base64 String into Blob"); | ||
} | ||
|
||
} | ||
|
||
// @Override | ||
// protected Blob _deserializeEmbedded(Object ob, DeserializationContext ctxt) throws IOException | ||
// { | ||
// if (ob instanceof byte[]) { | ||
// return _fromBytes((byte[]) ob, ctxt); | ||
// } | ||
// return super._deserializeEmbedded(ob, ctxt); | ||
// } | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/main/java/com/fasterxml/jackson/databind/ser/std/SqlBlobSerializer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.fasterxml.jackson.databind.ser.std; | ||
|
||
import java.io.IOException; | ||
import java.sql.Blob; | ||
import java.util.Base64; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.databind.JsonMappingException; | ||
import com.fasterxml.jackson.databind.JsonSerializer; | ||
import com.fasterxml.jackson.databind.SerializerProvider; | ||
import com.fasterxml.jackson.databind.annotation.JacksonStdImpl; | ||
|
||
/** | ||
* This is serializer for {@link java.sql.Blob} into base64 String | ||
*/ | ||
|
||
@JacksonStdImpl | ||
@SuppressWarnings("serial") | ||
public class SqlBlobSerializer | ||
extends StdScalarSerializer<Blob> | ||
{ | ||
|
||
public SqlBlobSerializer() { | ||
super(Blob.class); | ||
} | ||
|
||
|
||
|
||
@Override | ||
public void serialize(Blob value, JsonGenerator gen, | ||
SerializerProvider serializers) throws IOException { | ||
// TODO Auto-generated method stub | ||
|
||
try { | ||
int bLength = (int) value.length(); | ||
byte[] blob1 = value.getBytes(1, bLength); | ||
gen.writeBinary(blob1); | ||
// gen.writeString(Base64.getEncoder().encodeToString(blob1)); | ||
|
||
|
||
} | ||
catch(Exception e) { | ||
|
||
throw new JsonMappingException("Failed to serialize Blob into Base64 String"); | ||
} | ||
|
||
|
||
} | ||
|
||
@Override | ||
public boolean isEmpty(SerializerProvider provider, Blob value) { | ||
return value==null; | ||
} | ||
|
||
// @Override | ||
// public Class<Blob> handledType() { | ||
// | ||
// return Blob.class; | ||
// } | ||
|
||
|
||
} |
66 changes: 66 additions & 0 deletions
66
src/test/java/com/fasterxml/jackson/databind/ext/SqlBlobDeserializationTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.fasterxml.jackson.databind.ext; | ||
|
||
import java.sql.Blob; | ||
|
||
import javax.sql.rowset.serial.SerialBlob; | ||
|
||
import com.fasterxml.jackson.core.Base64Variants; | ||
import com.fasterxml.jackson.databind.BaseMapTest; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
// Tests for `java.sql.Date`, `java.sql.Time` and `java.sql.Timestamp` | ||
public class SqlBlobDeserializationTest extends BaseMapTest | ||
{ | ||
static class BlobObject { | ||
Blob sqlBlob1; | ||
|
||
public Blob getSqlBlob1() { | ||
return sqlBlob1; | ||
} | ||
|
||
public void setSqlBlob1(Blob sqlBlob1) { | ||
this.sqlBlob1 = sqlBlob1; | ||
} | ||
|
||
|
||
} | ||
|
||
/* | ||
/********************************************************** | ||
/* Test methods | ||
/********************************************************** | ||
*/ | ||
|
||
private final ObjectMapper m = new ObjectMapper(); | ||
public void testSqlBlobDeserializer() throws Exception { | ||
|
||
String testWord="TestObject1"; | ||
String base64Blob=Base64Variants.getDefaultVariant().encode(testWord.getBytes()); | ||
|
||
String json=m.writeValueAsString(base64Blob); | ||
Blob obj2=m.readValue(json, Blob.class); | ||
String result=new String( | ||
obj2.getBytes(1L, (int)obj2.length())); | ||
assertEquals(result, testWord); | ||
|
||
|
||
} | ||
public void testSqlBlobDeserializer2() throws Exception { | ||
|
||
String testWord="TestObject1"; | ||
SerialBlob blob1=new SerialBlob(testWord.getBytes()); | ||
|
||
BlobObject obj1=new BlobObject(); | ||
obj1.sqlBlob1=blob1; | ||
|
||
String json=m.writeValueAsString(obj1); | ||
BlobObject obj2=m.readValue(json, BlobObject.class); | ||
String result=new String( | ||
obj2.getSqlBlob1().getBytes(1L, (int)obj2.getSqlBlob1().length())); | ||
assertEquals(result, testWord); | ||
|
||
|
||
} | ||
|
||
|
||
} |
51 changes: 51 additions & 0 deletions
51
src/test/java/com/fasterxml/jackson/databind/ext/SqlBlobSerializationTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.fasterxml.jackson.databind.ext; | ||
|
||
import java.sql.Blob; | ||
import java.util.Base64; | ||
|
||
import javax.sql.rowset.serial.SerialBlob; | ||
|
||
import com.fasterxml.jackson.core.Base64Variant; | ||
import com.fasterxml.jackson.core.Base64Variants; | ||
import com.fasterxml.jackson.databind.BaseMapTest; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
// Tests for `java.sql.Date`, `java.sql.Time` and `java.sql.Timestamp` | ||
public class SqlBlobSerializationTest extends BaseMapTest | ||
{ | ||
static class BlobObject { | ||
// @JsonSerialize(using=SqlBlobSerializer.class) | ||
Blob sqlBlob1; | ||
|
||
public Blob getSqlBlob1() { | ||
return sqlBlob1; | ||
} | ||
|
||
public void setSqlBlob1(Blob sqlBlob1) { | ||
this.sqlBlob1 = sqlBlob1; | ||
} | ||
|
||
|
||
} | ||
|
||
/* | ||
/********************************************************** | ||
/* Test methods | ||
/********************************************************** | ||
*/ | ||
public void testSqlBlobSerializer() throws Exception { | ||
ObjectMapper m = new ObjectMapper(); | ||
String testWord="TestObject1"; | ||
SerialBlob blob1=new SerialBlob(testWord.getBytes()); | ||
String base64Blob=Base64Variants.getDefaultVariant().encode(testWord.getBytes()); | ||
|
||
|
||
BlobObject obj1=new BlobObject(); | ||
obj1.sqlBlob1=blob1; | ||
|
||
String json=m.writeValueAsString(obj1); | ||
assertEquals("{\"sqlBlob1\":\""+base64Blob+"\"}", json); | ||
|
||
|
||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.